Prohlížeč zdrojového kódu

spec/helpers/highlight_helper_spec.rb

require "rails_helper"
RSpec.describe HighlightHelper, type: :helper do
let(:base_path) { Rails.root.join("spec/fixtures/examples/test_example").to_s }
let(:data) do
{
"title" => "Test Example",
"description" => "A test example.",
"tags" => ["test"],
"source_file" => "test_example.rb",
"position" => 1,
"scenarios" => [
{
"name" => "Greeting",
"description" => "Say hello",
"boilerplate_file" => "greeting_runner.rb",
"inputs" => [
{ "name" => "name", "label" => "Name", "options" => ["Alice", "Bob"] }
]
}
]
}
end
let(:example_entry) { ExampleEntry.new(slug: "test_example", data: data, base_path: base_path) }
describe "#render_markdown" do
it "highlights fenced code blocks with a known language" do
md = "```ruby\nputs 'hello'\n```"
html = helper.render_markdown(md)
expect(html).to include('<div class="code-block highlight"><pre>')
expect(html).to include('<span class="nb">puts</span>')
end
it "renders fenced code blocks without a language hint without raising" do
md = "```\nputs 'hello'\n```"
expect { helper.render_markdown(md) }.not_to raise_error
end
it "renders fenced code blocks with an unknown language without raising" do
md = "```unknownlang\nputs 'hello'\n```"
expect { helper.render_markdown(md) }.not_to raise_error
end
it "renders a label when the info string contains a language and a path" do
md = "```ruby app/models/log.rb\nputs 'hello'\n```"
html = helper.render_markdown(md)
expect(html).to include('class="code-block__label"')
expect(html).to include("app/models/log.rb")
expect(html).to include('<span class="nb">puts</span>')
end
it "renders a label with spaces (multi-file or arbitrary text)" do
md = "```ruby log_header.rb, log_item.rb\nputs 'hello'\n```"
html = helper.render_markdown(md)
expect(html).to include('class="code-block__label"')
expect(html).to include("log_header.rb, log_item.rb")
end
it "does not render a label when only a language hint is given" do
md = "```ruby\nputs 'hello'\n```"
html = helper.render_markdown(md)
expect(html).not_to include("code-block__label")
end
it "does not HTML-escape the label" do
md = "```ruby <script>alert(1)</script>\nputs 'hello'\n```"
html = helper.render_markdown(md)
expect(html).not_to include("&lt;script&gt;")
expect(html).to include("<script>alert")
end
end
describe "#render_example_content" do
it "renders markdown content to HTML" do
html = helper.render_example_content(example_entry)
expect(html).to include("Toto je popis příkladu.")
end
it "replaces <!-- source --> with syntax-highlighted source code" do
html = helper.render_example_content(example_entry)
expect(html).to include('<span class="k">')
expect(html).not_to include("<!-- source -->")
end
it "replaces <!-- scenarios --> with scenario forms" do
html = helper.render_example_content(example_entry)
expect(html).to include("Greeting")
expect(html).to include("Spustit")
expect(html).not_to include("<!-- scenarios -->")
end
it "replaces <!-- scenario:0 --> with a specific scenario form" do
allow(example_entry).to receive(:content_markdown).and_return("<!-- scenario:0 -->")
html = helper.render_example_content(example_entry)
expect(html).to include("Greeting")
expect(html).to include("Spustit")
end
it "silently removes scenario placeholders with invalid index" do
allow(example_entry).to receive(:content_markdown).and_return("<!-- scenario:99 -->")
html = helper.render_example_content(example_entry)
expect(html).not_to include("<!-- scenario:")
end
it "renders two code blocks with <!-- compare --> side by side" do
md = "```ruby\nbefore\n```\n<!-- compare -->\n```ruby\nafter\n```"
allow(example_entry).to receive(:content_markdown).and_return(md)
html = helper.render_example_content(example_entry)
expect(html).to include('class="code-compare"')
expect(html).to include('data-controller="compare-scroll"')
expect(html).to include('class="code-compare__pane"')
expect(html).not_to include("<!-- compare -->")
end
it "renders labels when <!-- compare:A:B --> is used" do
md = "```ruby\nbefore\n```\n<!-- compare:Před:Po -->\n```ruby\nafter\n```"
allow(example_entry).to receive(:content_markdown).and_return(md)
html = helper.render_example_content(example_entry)
expect(html).to include('class="code-compare__label"')
expect(html).to include("Před")
expect(html).to include("Po")
end
it "raises an error when <!-- source --> is used but example has no source_file" do
allow(example_entry).to receive(:source_code).and_return(nil)
allow(example_entry).to receive(:content_markdown).and_return("<!-- source -->")
expect { helper.render_example_content(example_entry) }.to raise_error(ArgumentError, /source_file/)
end
it "does not affect code blocks without a compare tag" do
md = "```ruby\nputs 'hello'\n```"
allow(example_entry).to receive(:content_markdown).and_return(md)
html = helper.render_example_content(example_entry)
expect(html).not_to include("code-compare")
end
end
end