| 1 | # frozen_string_literal: true |
| 2 | |
| 3 | require "rails_helper" |
| 4 | |
| 5 | RSpec.describe SyntaxHighlighter do |
| 6 | it "returns one fragment per source line" do |
| 7 | expect(described_class.lines("def a\n 1\nend\n", filename: "a.rb").length).to eq(3) |
| 8 | end |
| 9 | |
| 10 | it "does not emit a trailing blank line when the file ends with a newline" do |
| 11 | expect(described_class.lines("one\ntwo\n", filename: "f.txt").length).to eq(2) |
| 12 | end |
| 13 | |
| 14 | it "counts the final line when there is no trailing newline" do |
| 15 | expect(described_class.lines("one\ntwo", filename: "f.txt").length).to eq(2) |
| 16 | end |
| 17 | |
| 18 | it "highlights a known language" do |
| 19 | expect(described_class.lines("puts 'hi'\n", filename: "x.rb").first).to include("<span") |
| 20 | end |
| 21 | |
| 22 | it "falls back to plain text for an unknown extension" do |
| 23 | lines = described_class.lines("just text\n", filename: "x.unknownext") |
| 24 | expect(lines.length).to eq(1) |
| 25 | expect(lines.first).to include("just text") |
| 26 | end |
| 27 | |
| 28 | it "escapes HTML in the content" do |
| 29 | line = described_class.lines("<script>\n", filename: "x.unknownext").first |
| 30 | expect(line).not_to include("<script>") |
| 31 | expect(line).to include("<script>") |
| 32 | end |
| 33 | |
| 34 | it "handles empty content" do |
| 35 | expect(described_class.lines("", filename: "x.txt")).to eq([]) |
| 36 | end |
| 37 | end |