Skip to content

Commit

Permalink
Improve structure of tests
Browse files Browse the repository at this point in the history
We don't need to call processor.render in every test, as it's always
called with the same arguments. That means it can be pulled out into a
`let` block.

Moving the code block tests into the top level describe lets us use
described_class in the tests, which cuts down on a bit of syntax noise.

We can get a bit clever by only replacing `processor` and not `output`
in the "with syntax highlighting" case - the test will use the `output`
block from `describe "#render a code block"`, but with the `processor`
from `describe "with syntax highlighting"`
  • Loading branch information
richardTowers committed Mar 12, 2021
1 parent 3271053 commit 8efa6f9
Showing 1 changed file with 52 additions and 71 deletions.
123 changes: 52 additions & 71 deletions spec/govuk_tech_docs/tech_docs_html_renderer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,108 +4,89 @@
let(:app) { double("app") }
let(:context) { double("context") }
let(:processor) {
Redcarpet::Markdown.new(described_class.new(context: context), tables: true, fenced_code_blocks: true)
}

before :each do
allow(context).to receive(:app) { app }
allow(app).to receive(:api)
Redcarpet::Markdown.new(described_class.new(context: context), tables: true)
}
end

describe "#render a table" do
markdown_table = <<~MARKDOWN
| A | B |
|------|---|
|# C | D |
| E | F |
|# *G* | H |
MARKDOWN
let(:output) {
processor.render <<~MARKDOWN
| A | B |
|------|---|
|# C | D |
| E | F |
|# *G* | H |
MARKDOWN
}

it "treats cells in the heading row as headings" do
output = processor.render markdown_table

expect(output).to include("<th>A</th>")
expect(output).to include("<th>B</th>")
end

it "treats cells starting with # as row headings" do
output = processor.render markdown_table
expect(output).to include('<th scope="row">C</th>')
end

it "treats cells starting with # with more complex markup as row headings" do
output = processor.render markdown_table
expect(output).to match(/<th scope="row"><em>G<\/em>\s*<\/th>/)
end

it "treats other cells as ordinary cells" do
output = processor.render markdown_table
expect(output).to include("<td>D</td>")
expect(output).to include("<td>E</td>")
expect(output).to include("<td>F</td>")
expect(output).to include("<td>H</td>")
end
end
end

RSpec.describe "code blocks" do
let(:app) { double("app") }
let(:context) { double("context") }

before :each do
allow(context).to receive(:app) { app }
allow(app).to receive(:api)
end

describe "without syntax highlighting" do
let(:processor) {
Redcarpet::Markdown.new(GovukTechDocs::TechDocsHTMLRenderer.new(context: context), fenced_code_blocks: true)
describe "#render a code block" do
let(:output) {
processor.render <<~MARKDOWN
Hello world:
```ruby
def hello_world
puts "hello world"
end
```
MARKDOWN
}

describe "#render" do
it "sets tab index to 0" do
output = processor.render <<~MARKDOWN
Hello world:
```ruby
def hello_world
puts "hello world"
end
```
MARKDOWN

expect(output).to include('<pre tabindex="0">')
expect(output).to include("def hello_world")
expect(output).to include('puts "hello world"')
expect(output).to include("end")
end
it "sets tab index to 0" do
expect(output).to include('<pre tabindex="0">')
end
end

describe "with syntax highlighting" do
let(:processor) {
renderer_class = GovukTechDocs::TechDocsHTMLRenderer.clone.tap do |c|
c.send :include, Middleman::Syntax::RedcarpetCodeRenderer
end

Redcarpet::Markdown.new(renderer_class.new(context: context), fenced_code_blocks: true)
}

describe "#render" do
it "sets tab index to 0" do
output = processor.render <<~MARKDOWN
Hello world:
```ruby
def hello_world
puts "hello world"
end
```
MARKDOWN
it "renders the code without syntax highlighting" do
expect(output).to include("def hello_world")
expect(output).to include('puts "hello world"')
expect(output).to include("end")
end

expect(output).to include('<pre tabindex="0" class=" ruby">')
expect(output).to include("def</span>")
expect(output).to include("hello_world</span>")
expect(output).to include("puts</span>")
expect(output).to include('"hello world"</span>')
expect(output).to include("end</span>")
describe "with syntax highlighting" do
let(:processor) {
renderer_class = described_class.clone.tap do |c|
c.send :include, Middleman::Syntax::RedcarpetCodeRenderer
end
Redcarpet::Markdown.new(renderer_class.new(context: context), tables: true, fenced_code_blocks: true)
}

describe "#render a code block" do
it "sets tab index to 0" do
expect(output).to include('<pre tabindex="0" class=" ruby">')
end

it "renders the code with syntax highlighting" do
expect(output).to include("def</span>")
expect(output).to include("hello_world</span>")
expect(output).to include("puts</span>")
expect(output).to include('"hello world"</span>')
expect(output).to include("end</span>")
end
end
end
end
Expand Down

0 comments on commit 8efa6f9

Please sign in to comment.