test/parser_test.rb in asciidoctor-1.5.5 vs test/parser_test.rb in asciidoctor-1.5.6

- old
+ new

@@ -15,10 +15,89 @@ assert_equal 'foobar', Asciidoctor::Parser.sanitize_attribute_name("Foo Bar") assert_equal 'foo', Asciidoctor::Parser.sanitize_attribute_name("foo") assert_equal 'foo3-bar', Asciidoctor::Parser.sanitize_attribute_name("Foo 3^ # - Bar[") end + test 'store attribute with value' do + attr_name, attr_value = Asciidoctor::Parser.store_attribute 'foo', 'bar' + assert_equal 'foo', attr_name + assert_equal 'bar', attr_value + end + + test 'store attribute with negated value' do + { 'foo!' => nil, '!foo' => nil, 'foo' => nil }.each do |name, value| + attr_name, attr_value = Asciidoctor::Parser.store_attribute name, value + assert_equal name.sub('!', ''), attr_name + assert_nil attr_value + end + end + + test 'store accessible attribute on document with value' do + doc = empty_document + doc.set_attribute 'foo', 'baz' + attrs = {} + attr_name, attr_value = Asciidoctor::Parser.store_attribute 'foo', 'bar', doc, attrs + assert_equal 'foo', attr_name + assert_equal 'bar', attr_value + assert_equal 'bar', (doc.attr 'foo') + assert attrs.key?(:attribute_entries) + assert_equal 1, attrs[:attribute_entries].size + assert_equal 'foo', attrs[:attribute_entries][0].name + assert_equal 'bar', attrs[:attribute_entries][0].value + end + + test 'store accessible attribute on document with value that contains attribute reference' do + doc = empty_document + doc.set_attribute 'foo', 'baz' + doc.set_attribute 'release', 'ultramega' + attrs = {} + attr_name, attr_value = Asciidoctor::Parser.store_attribute 'foo', '{release}', doc, attrs + assert_equal 'foo', attr_name + assert_equal 'ultramega', attr_value + assert_equal 'ultramega', (doc.attr 'foo') + assert attrs.key?(:attribute_entries) + assert_equal 1, attrs[:attribute_entries].size + assert_equal 'foo', attrs[:attribute_entries][0].name + assert_equal 'ultramega', attrs[:attribute_entries][0].value + end + + test 'store inaccessible attribute on document with value' do + doc = empty_document :attributes => { 'foo' => 'baz' } + attrs = {} + attr_name, attr_value = Asciidoctor::Parser.store_attribute 'foo', 'bar', doc, attrs + assert_equal 'foo', attr_name + assert_equal 'bar', attr_value + assert_equal 'baz', (doc.attr 'foo') + refute attrs.key?(:attribute_entries) + end + + test 'store accessible attribute on document with negated value' do + { 'foo!' => nil, '!foo' => nil, 'foo' => nil }.each do |name, value| + doc = empty_document + doc.set_attribute 'foo', 'baz' + attrs = {} + attr_name, attr_value = Asciidoctor::Parser.store_attribute name, value, doc, attrs + assert_equal name.sub('!', ''), attr_name + assert_nil attr_value + assert attrs.key?(:attribute_entries) + assert_equal 1, attrs[:attribute_entries].size + assert_equal 'foo', attrs[:attribute_entries][0].name + assert_nil attrs[:attribute_entries][0].value + end + end + + test 'store inaccessible attribute on document with negated value' do + { 'foo!' => nil, '!foo' => nil, 'foo' => nil }.each do |name, value| + doc = empty_document :attributes => { 'foo' => 'baz' } + attrs = {} + attr_name, attr_value = Asciidoctor::Parser.store_attribute name, value, doc, attrs + assert_equal name.sub('!', ''), attr_name + assert_nil attr_value + refute attrs.key?(:attribute_entries) + end + end + test "collect unnamed attribute" do attributes = {} line = 'quote' expected = {1 => 'quote'} Asciidoctor::AttributeList.new(line).parse_into(attributes) @@ -170,19 +249,19 @@ end test "collect options attribute" do attributes = {} line = "quote, options='opt1,opt2 , opt3'" - expected = {1 => 'quote', 'options' => 'opt1,opt2 , opt3', 'opt1-option' => '', 'opt2-option' => '', 'opt3-option' => ''} + expected = {1 => 'quote', 'options' => 'opt1,opt2,opt3', 'opt1-option' => '', 'opt2-option' => '', 'opt3-option' => ''} Asciidoctor::AttributeList.new(line).parse_into(attributes) assert_equal expected, attributes end test "collect opts attribute as options" do attributes = {} line = "quote, opts='opt1,opt2 , opt3'" - expected = {1 => 'quote', 'options' => 'opt1,opt2 , opt3', 'opt1-option' => '', 'opt2-option' => '', 'opt3-option' => ''} + expected = {1 => 'quote', 'options' => 'opt1,opt2,opt3', 'opt1-option' => '', 'opt2-option' => '', 'opt3-option' => ''} Asciidoctor::AttributeList.new(line).parse_into(attributes) assert_equal expected, attributes end test "collect and rekey unnamed attributes" do @@ -200,88 +279,80 @@ assert_equal expected, attributes end test 'parse style attribute with id and role' do attributes = {1 => 'style#id.role'} - style, original_style = Asciidoctor::Parser.parse_style_attribute(attributes) + style = Asciidoctor::Parser.parse_style_attribute(attributes) assert_equal 'style', style - assert_nil original_style assert_equal 'style', attributes['style'] assert_equal 'id', attributes['id'] assert_equal 'role', attributes['role'] assert_equal 'style#id.role', attributes[1] end test 'parse style attribute with style, role, id and option' do attributes = {1 => 'style.role#id%fragment'} - style, original_style = Asciidoctor::Parser.parse_style_attribute(attributes) + style = Asciidoctor::Parser.parse_style_attribute(attributes) assert_equal 'style', style - assert_nil original_style assert_equal 'style', attributes['style'] assert_equal 'id', attributes['id'] assert_equal 'role', attributes['role'] assert_equal '', attributes['fragment-option'] assert_equal 'fragment', attributes['options'] assert_equal 'style.role#id%fragment', attributes[1] end test 'parse style attribute with style, id and multiple roles' do attributes = {1 => 'style#id.role1.role2'} - style, original_style = Asciidoctor::Parser.parse_style_attribute(attributes) + style = Asciidoctor::Parser.parse_style_attribute(attributes) assert_equal 'style', style - assert_nil original_style assert_equal 'style', attributes['style'] assert_equal 'id', attributes['id'] assert_equal 'role1 role2', attributes['role'] assert_equal 'style#id.role1.role2', attributes[1] end test 'parse style attribute with style, multiple roles and id' do attributes = {1 => 'style.role1.role2#id'} - style, original_style = Asciidoctor::Parser.parse_style_attribute(attributes) + style = Asciidoctor::Parser.parse_style_attribute(attributes) assert_equal 'style', style - assert_nil original_style assert_equal 'style', attributes['style'] assert_equal 'id', attributes['id'] assert_equal 'role1 role2', attributes['role'] assert_equal 'style.role1.role2#id', attributes[1] end test 'parse style attribute with positional and original style' do attributes = {1 => 'new_style', 'style' => 'original_style'} - style, original_style = Asciidoctor::Parser.parse_style_attribute(attributes) + style = Asciidoctor::Parser.parse_style_attribute(attributes) assert_equal 'new_style', style - assert_equal 'original_style', original_style assert_equal 'new_style', attributes['style'] assert_equal 'new_style', attributes[1] end test 'parse style attribute with id and role only' do attributes = {1 => '#id.role'} - style, original_style = Asciidoctor::Parser.parse_style_attribute(attributes) + style = Asciidoctor::Parser.parse_style_attribute(attributes) assert_nil style - assert_nil original_style assert_equal 'id', attributes['id'] assert_equal 'role', attributes['role'] assert_equal '#id.role', attributes[1] end test 'parse empty style attribute' do attributes = {1 => nil} - style, original_style = Asciidoctor::Parser.parse_style_attribute(attributes) + style = Asciidoctor::Parser.parse_style_attribute(attributes) assert_nil style - assert_nil original_style assert_nil attributes['id'] assert_nil attributes['role'] assert_nil attributes[1] end test 'parse style attribute with option should preserve existing options' do attributes = {1 => '%header', 'options' => 'footer', 'footer-option' => ''} - style, original_style = Asciidoctor::Parser.parse_style_attribute(attributes) + style = Asciidoctor::Parser.parse_style_attribute(attributes) assert_nil style - assert_nil original_style assert_equal 'header,footer', attributes['options'] assert_equal '', attributes['header-option'] assert_equal '', attributes['footer-option'] end @@ -453,10 +524,60 @@ assert_equal 'Doc Writer', metadata['author'] assert_equal 'Doc Writer', metadata['author_1'] assert_equal 'John Smith', metadata['author_2'] end + test 'parse name with more than 3 parts in author attribute' do + doc = empty_document + parse_header_metadata ':author: Leroy Harold Scherer, Jr.', doc + assert_equal 'Leroy Harold Scherer, Jr.', doc.attributes['author'] + assert_equal 'Leroy', doc.attributes['firstname'] + assert_equal 'Harold', doc.attributes['middlename'] + assert_equal 'Scherer, Jr.', doc.attributes['lastname'] + end + + test 'does not drop name joiner when using multiple authors' do + input = <<-EOS +Kismet Chameleon; Lazarus het_Draeke + EOS + doc = empty_document + parse_header_metadata input, doc + assert_equal 2, doc.attributes['authorcount'] + assert_equal 'Kismet Chameleon, Lazarus het Draeke', doc.attributes['authors'] + assert_equal 'Kismet Chameleon', doc.attributes['author_1'] + assert_equal 'Lazarus het Draeke', doc.attributes['author_2'] + assert_equal 'het Draeke', doc.attributes['lastname_2'] + end + + test 'allows authors to be overridden using explicit author attributes' do + input = <<-EOS +Kismet Chameleon; Johnny Bravo; Lazarus het_Draeke +:author_2: Danger Mouse + EOS + doc = empty_document + parse_header_metadata input, doc + assert_equal 3, doc.attributes['authorcount'] + assert_equal 'Kismet Chameleon, Danger Mouse, Lazarus het Draeke', doc.attributes['authors'] + assert_equal 'Kismet Chameleon', doc.attributes['author_1'] + assert_equal 'Danger Mouse', doc.attributes['author_2'] + assert_equal 'Lazarus het Draeke', doc.attributes['author_3'] + assert_equal 'het Draeke', doc.attributes['lastname_3'] + end + + test 'removes formatting before partitioning author defined using author attribute' do + input = <<-EOS +:author: pass:n[http://example.org/community/team.html[Ze_**Project** team]] + EOS + + doc = empty_document + parse_header_metadata input, doc + assert_equal 1, doc.attributes['authorcount'] + assert_equal '<a href="http://example.org/community/team.html">Ze <strong>Project</strong> team</a>', doc.attributes['authors'] + assert_equal 'Ze Project', doc.attributes['firstname'] + assert_equal 'team', doc.attributes['lastname'] + end + test "parse rev number date remark" do input = <<-EOS Ryan Waldron v0.0.7, 2013-12-18: The first release you can stand on EOS @@ -589,31 +710,30 @@ assert_equal 'Ryan Waldron', metadata['author'] assert_equal '0.0.7', metadata['revnumber'] assert_equal '2013-12-18', metadata['revdate'] end - test "attribute entry overrides generated author initials" do - blankdoc = Asciidoctor::Document.new - reader = Asciidoctor::Reader.new "Stuart Rackham <founder@asciidoc.org>\n:Author Initials: SJR".lines.entries - metadata = Asciidoctor::Parser.parse_header_metadata(reader, blankdoc) + test 'attribute entry overrides generated author initials' do + doc = empty_document + metadata, _ = parse_header_metadata %(Stuart Rackham <founder@asciidoc.org>\n:Author Initials: SJR), doc assert_equal 'SR', metadata['authorinitials'] - assert_equal 'SJR', blankdoc.attributes['authorinitials'] + assert_equal 'SJR', doc.attributes['authorinitials'] end test 'adjust indentation to 0' do input = <<-EOS.chomp def names - @name.split ' ' + @name.split end EOS expected = <<-EOS.chomp def names - @name.split ' ' + @name.split end EOS lines = input.split("\n") @@ -623,19 +743,19 @@ test 'adjust indentation mixed with tabs and spaces to 0' do input = <<-EOS.chomp def names -\t @name.split ' ' +\t @name.split end EOS expected = <<-EOS.chomp def names - @name.split ' ' + @name.split end EOS lines = input.split("\n") @@ -665,19 +785,19 @@ test 'adjust indentation to non-zero' do input = <<-EOS.chomp def names - @name.split ' ' + @name.split end EOS expected = <<-EOS.chomp def names - @name.split ' ' + @name.split end EOS lines = input.split("\n") @@ -687,10 +807,10 @@ test 'preserve block indent if indent is -1' do input = <<-EOS def names - @name.split ' ' + @name.split end EOS expected = input