Skip to content

Asciidoctor: Handle snippets in definition lists #680

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Mar 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,13 @@ def process_subs(block)
}.freeze

def process_lang_override(block)
next_block = block.next_adjacent_block
# Check if the next block is a marker for the language
# We don't want block.next_adjacent_block because that'll go "too far"
# and it has trouble with definition lists.
my_index = block.parent.blocks.find_index block
return unless my_index

next_block = block.parent.blocks[my_index + 1]
return unless next_block && next_block.context == :paragraph
return unless next_block.source =~ %r{pass:\[//\s*([^:\]]+)(?::\s*([^\]]+))?\]}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,27 @@
expect(actual).to eq(expected.strip)
end
end

context 'a snippet is inside of a definition list' do
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a good step in the direction of idiomatic Rspec : )

I always favor let instead of subject because it's named. The benefit of this, and of using let in general, is so that you can write embedded tests that reuse the "variables". (I put that in quotes because it's actually a method that creates the memoized variable on the fly, when it's called)

So you could write this as

let(:converted) do
  convert <<~ASCIIDOC
        == Example
        Term::
        Definition
        +
        --
        [source,js]
        ----
        GET /
        ----
        --
      ASCIIDOC
end

let(:regexp) do
   %r{<programlisting language="js" linenumbering="unnumbered">GET /</programlisting>}
end

it "converts the string" do
  expect(converted).to match(regexp)
end

Then, if you wanted to write another test using that converted string, you could easily add something like this below the first it block:

it "doesn't match pizza" do
  expect(converted).not_to match(/pizza/)
end

and you can also nest context blocks that reuse the converted string:

let(:converted) do
  convert <<~ASCIIDOC
        == Example
        Term::
        Definition
        +
        --
        [source,js]
        ----
        GET /
        ----
        --
      ASCIIDOC
end

let(:regexp) do
   %r{<programlisting language="js" linenumbering="unnumbered">GET /</programlisting>}
end

it "converts the string" do
  expect(converted).to match(regexp)
end

context "when it's raining" do

  let(:regexp) do
    # And you can override any "variables" you need to!
    /sun/
  end

  it "converts the string" do
    expect(converted).not_to match(regexp)
  end
end

The converted string will only ever be created once, even when you use it repeatedly in tests.

let(:converted) do
convert <<~ASCIIDOC
== Example
Term::
Definition
+
--
[source,js]
----
GET /
----
--
ASCIIDOC
end
let(:has_original_language) do
%r{<programlisting language="js" linenumbering="unnumbered">GET /</programlisting>}
end
it "doesn't break" do
expect(converted).to match(has_original_language)
end
end
end