From 961bb7ac6dee4e35fbca17d4133d40e7100f66ec Mon Sep 17 00:00:00 2001 From: "Tobias L. Maier" Date: Sun, 15 Nov 2026 00:16:00 +0100 Subject: [PATCH 01/12] Test the RDF conversion of a bibliography --- .../bibtex/test_bibliography_rdf_converter.rb | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 test/bibtex/test_bibliography_rdf_converter.rb diff --git a/test/bibtex/test_bibliography_rdf_converter.rb b/test/bibtex/test_bibliography_rdf_converter.rb new file mode 100644 index 0000000..ee9fb31 --- /dev/null +++ b/test/bibtex/test_bibliography_rdf_converter.rb @@ -0,0 +1,50 @@ +# frozen_string_literal: true + +require 'helper' + +module BibTeX + class BibliographyRDFConverterTest < Minitest::Spec + before do + skip 'RDF support not available' unless defined?(::RDF) + end + + let(:bibliography) do + BibTeX.parse <<~BIB + @book{a, author = {Doe, Jane}, title = {First}, year = {2001}} + @article{b, author = {Doe, Jane and Roe, Rick}, title = {Second}, journal = {J}, year = {2002}} + @string{foo = "bar"} + BIB + end + + describe '.convert' do + it 'returns an RDF graph with one statement set per entry' do + graph = BibTeX::Bibliography::RDFConverter.convert(bibliography) + + assert_kind_of ::RDF::Graph, graph + refute_empty graph + titles = graph.query([nil, ::RDF::Vocab::DC.title, nil]).map { |s| s.object.to_s } + + assert_includes titles, 'First' + assert_includes titles, 'Second' + end + + it 'returns an empty graph for an empty bibliography' do + assert_empty BibTeX::Bibliography::RDFConverter.convert(BibTeX::Bibliography.new) + end + + it 'adds to a supplied graph' do + graph = ::RDF::Graph.new + result = BibTeX::Bibliography::RDFConverter.new(bibliography, graph).convert! + + assert_same graph, result + refute_empty graph + end + end + + describe 'Bibliography#to_rdf' do + it 'returns a graph' do + assert_kind_of ::RDF::Graph, bibliography.to_rdf + end + end + end +end From 5e8adf6a107ac517e26b993cc7c345849b38a3ed Mon Sep 17 00:00:00 2001 From: "Tobias L. Maier" Date: Sun, 15 Nov 2026 00:16:00 +0100 Subject: [PATCH 02/12] Test the unicode transliteration fallback --- test/bibtex/test_compatibility.rb | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 test/bibtex/test_compatibility.rb diff --git a/test/bibtex/test_compatibility.rb b/test/bibtex/test_compatibility.rb new file mode 100644 index 0000000..3ec838b --- /dev/null +++ b/test/bibtex/test_compatibility.rb @@ -0,0 +1,26 @@ +# frozen_string_literal: true + +require 'helper' + +module BibTeX + class TestCompatibility < Minitest::Test + def test_transliterate_ascii_is_unchanged + assert_equal 'Poe, Edgar Allen', BibTeX.transliterate('Poe, Edgar Allen') + end + + def test_transliterate_strips_diacritics + assert_equal 'Muller', BibTeX.transliterate('Müller') + end + + def test_transliterate_drops_characters_without_ascii_form + assert_equal 'ab', BibTeX.transliterate('a中b') + end + + def test_transliterate_falls_back_when_normalization_fails + str = +'café ok' + str.define_singleton_method(:unicode_normalize) { |*| raise ArgumentError, 'boom' } + + assert_equal 'caf ok', BibTeX.transliterate(str) + end + end +end From cb74483347e45aebc51582f07049e0bedbfcbd37 Mon Sep 17 00:00:00 2001 From: "Tobias L. Maier" Date: Sun, 15 Nov 2026 00:16:00 +0100 Subject: [PATCH 03/12] Test how parse errors register with a bibliography --- test/bibtex/test_error.rb | 79 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 test/bibtex/test_error.rb diff --git a/test/bibtex/test_error.rb b/test/bibtex/test_error.rb new file mode 100644 index 0000000..a39c1cb --- /dev/null +++ b/test/bibtex/test_error.rb @@ -0,0 +1,79 @@ +# frozen_string_literal: true + +require 'helper' + +module BibTeX + class ErrorTest < Minitest::Spec + before { BibTeX.log.level = Logger::FATAL } + after { BibTeX.log.level = Logger::WARN } + + describe 'when parsing broken input without strict mode' do + before do + @bib = BibTeX.parse('@book{key, title = "Unterminated}', strict: false) + @error = @bib.find { |e| e.is_a?(Error) } + end + + it 'registers the error with the bibliography' do + refute_nil @error + assert_same @bib, @error.bibliography + assert_includes @bib.errors, @error + assert_predicate @bib, :errors? + end + + it 'builds its content from the token trace' do + assert_kind_of Array, @error.trace + assert_equal @error.trace.map { |t| t[1] }.join, @error.content + end + + it 'is removed from the bibliography errors when deleted' do + @bib.delete(@error) + + refute_includes @bib.errors, @error + refute_predicate @bib, :errors? + assert_nil @error.bibliography + end + end + + describe 'when created directly' do + it 'defaults to an empty trace' do + assert_empty Error.new.trace + assert_equal '', Error.new.content + end + + it 'accepts a trace' do + error = Error.new([[:NAME, 'foo'], [:EQ, '=']]) + + assert_equal 'foo=', error.content + end + + it 'replaces the trace with an array' do + error = Error.new + error.trace = [[:NAME, 'bar']] + + assert_equal 'bar', error.content + end + + it 'rejects a trace which is not an array' do + error = Error.new + + assert_raises(BibTeX::ArgumentError) { error.trace = 'nope' } + assert_empty error.trace + end + end + + describe 'when added to and removed from a bibliography by hand' do + it 'tracks itself in the bibliography errors' do + bib = Bibliography.new + error = Error.new([[:NAME, 'x']]) + + bib << error + + assert_equal [error], bib.errors + + bib.delete(error) + + assert_empty bib.errors + end + end + end +end From 87079ce1cce818064a96e1b1457d3aa678c47f74 Mon Sep 17 00:00:00 2001 From: "Tobias L. Maier" Date: Sun, 15 Nov 2026 00:16:00 +0100 Subject: [PATCH 04/12] Test the replaceable behaviour of Preamble and String --- test/bibtex/test_replaceable.rb | 38 +++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 test/bibtex/test_replaceable.rb diff --git a/test/bibtex/test_replaceable.rb b/test/bibtex/test_replaceable.rb new file mode 100644 index 0000000..7d6369d --- /dev/null +++ b/test/bibtex/test_replaceable.rb @@ -0,0 +1,38 @@ +# frozen_string_literal: true + +require 'helper' + +module BibTeX + class ReplaceableTest < Minitest::Spec + describe 'a Preamble including Replaceable' do + before do + @bib = BibTeX.parse('@string{ foo = "foo" } @preamble{ foo # "bar" }') + @preamble = @bib.q('@preamble').first + end + + it 'wraps assigned values in a Value' do + @preamble.value = 'baz' + + assert_instance_of Value, @preamble.value + assert_equal 'baz', @preamble.value.to_s + end + + it 'appends to the value and returns self' do + assert_same @preamble, (@preamble << ' more') + assert_equal 'foo # "bar" # " more"', @preamble.value.to_s(quotes: '"') + end + + it 'replaces symbols and returns self' do + assert_same @preamble, @preamble.replace(foo: 'foo') + refute_predicate @preamble.value, :symbol? + end + + it 'joins adjacent strings and returns self' do + @preamble.replace(foo: 'foo') + + assert_same @preamble, @preamble.join + assert_equal 'foobar', @preamble.value.to_s + end + end + end +end From a9b2ea93d15fd1f6b1b1f1586e58c001f08fa5c5 Mon Sep 17 00:00:00 2001 From: "Tobias L. Maier" Date: Sun, 15 Nov 2026 00:16:00 +0100 Subject: [PATCH 05/12] Test Element defaults, conversions, conditions and matching --- test/bibtex/test_elements.rb | 238 +++++++++++++++++++++++++++++++++++ 1 file changed, 238 insertions(+) diff --git a/test/bibtex/test_elements.rb b/test/bibtex/test_elements.rb index 285630f..8bab49f 100644 --- a/test/bibtex/test_elements.rb +++ b/test/bibtex/test_elements.rb @@ -20,6 +20,244 @@ class ElementTest < Minitest::Spec it 'accepts an array of hashes' do _(Element.parse([{ bibtex_type: :book }, { bibtex_type: :misc }])[1].type).must_be :==, :misc end + + it 'raises an ArgumentError for unsupported input' do + assert_raises(ArgumentError) { Element.parse(42) } + end + end + + describe 'generic behaviour' do + before { @element = Comment.new('blah') } + + it 'has the default no-op implementations' do + base = Element.new + + assert_equal '', base.content + assert_equal [], base.values_at(:a) + assert_equal [], base.names + assert_same base, base.replace + assert_same base, base.join + assert_equal 'element|', base.digest + end + + it 'derives its type from the class name' do + assert_equal :comment, @element.type + assert_equal :meta_content, MetaContent.new.type + end + + it 'answers type queries' do + assert_predicate @element, :comment? + refute_predicate @element, :string? + assert @element.type?(:comment) + end + + it 'has an id' do + assert_equal @element.id, @element.id + assert_kind_of ::String, @element.id + end + + it 'inspects itself' do + assert_equal '#', @element.inspect + assert_equal '#', Comment.new("a\nb").inspect + end + + it 'compares equal elements as equal' do + other = Comment.new('a') + + assert_equal 0, Comment.new('a').<=>(other) + end + + it 'orders elements by string' do + assert_equal(-1, Comment.new('a') <=> Comment.new('b')) + end + + it 'does not compare with other types' do + assert_nil Comment.new('a') <=> 5 + end + + it 'serialises to hash, yaml, json and xml' do + assert_equal({ comment: 'blah' }, @element.to_hash) + assert_equal({ comment: 'blah' }.to_yaml, @element.to_yaml) + assert_equal '{"comment":"blah"}', @element.to_json + assert_equal '{"comment":"blah"}', @element.to_json(:something) + assert_equal 'blah', @element.to_xml.text + end + + it 'tracks its bibliography' do + bib = Bibliography.new + + assert_same @element, @element.added_to_bibliography(bib) + assert_same bib, @element.bibliography + assert_same @element, @element.removed_from_bibliography(bib) + assert_nil @element.bibliography + end + end + + describe '#matches?' do + before do + @bib = BibTeX.parse('@book{a, title = {Foo}, year = {2001}} @article{b, title = {Bar}}') + @a = @bib['a'] + @b = @bib['b'] + end + + it 'matches empty queries' do + assert @a.matches?(nil) + assert @a.matches?('') + end + + it 'matches by symbol id, element, regexp and /regexp/ string' do + assert @a.matches?(:a) + refute @a.matches?(:b) + assert @a.matches?(@a) + assert @a.matches?(/Foo/) + assert @a.matches?('/Foo/') + refute @a.matches?('/Bar/') + end + + it 'matches by plain id' do + assert @a.matches?('a') + refute @a.matches?('b') + end + + it 'matches by type, wildcard and negation' do + assert @a.matches?('@book') + refute @a.matches?('@article') + assert @a.matches?('@*') + assert @a.matches?('!@article') + refute @a.matches?('!@book') + assert @a.matches?('@book[]') + end + + it 'supports equality and alternatives' do + assert @a.matches?('@book[year=2001]') + assert @a.matches?('@book[year = 2001, title=Foo]') + assert @a.matches?('@book[year=1999 || year=2001]') + refute @a.matches?('@book[year=1999]') + end + + it 'supports property existence' do + assert @a.matches?('@book[title]') + refute @a.matches?('@book[author]') + end + + it 'supports inequality' do + assert @a.matches?('@book[year!=1999]') + assert @a.matches?('@book[year/=1999]') + refute @a.matches?('@book[year!=2001]') + end + + it 'supports prefix conditions' do + assert @a.matches?('@book[title^=F]') + refute @a.matches?('@book[author^=F]') + end + + it 'supports regular expression conditions' do + assert @a.matches?('@book[title~=o+]') + refute @a.matches?('@book[author~=o]') + end + + it 'supports negated regular expression conditions' do + assert @a.matches?('@book[title!~Z]') + assert @a.matches?('@book[author!~Z]') + refute @a.matches?('@book[title!~Foo]') + end + + it 'supports numeric comparisons' do + assert @a.matches?('@book[year>2000]') + assert @a.matches?('@book[year>=2001]') + assert @a.matches?('@book[year<2002]') + assert @a.matches?('@book[year<=2001]') + refute @a.matches?('@book[year>2001]') + end + + it 'supports string comparisons' do + refute @a.matches?('@book[author>2001]') + assert @a.matches?('@book[title>Fa]') + end + + it 'handles the type property' do + refute @a.matches?('@book[type=x]') + assert @a.matches?('@book[type!=x]') + end + + it 'supports meets_all?' do + assert @a.meets_all?('year=2001', 'title=Foo') + refute @a.meets_all?('year=2001', 'title=Bar') + end + + it 'supports meets_any?' do + assert @a.meets_any?('year=1999', 'title=Foo') + refute @a.meets_any?('year=1999', 'title=Bar') + end + + it 'supports the aliases of the meets methods' do + assert @a.meet_all?('year=2001') + assert @a.meet_any?('year=2001') + assert @a.meets?(['year=2001']) + assert @a.meet?(['year=2001']) + end + + it 'treats an empty condition as met' do + assert @a.meets_all?('') + end + + it 'does not meet conditions on non-entries' do + refute Comment.new('x').meets_all?('year') + end + end + end + + class StringTest < Minitest::Spec + describe 'a @string' do + before { @string = String.new(:foo, 'bar') } + + it 'exposes its key and value' do + assert_equal :foo, @string.key + assert_equal :bar, @string[:foo].to_s.to_sym + assert_nil @string[:other] + end + + it 'serialises' do + assert_equal 'foo = "bar"', @string.content + assert_equal %(@string{ foo = "bar" }\n), @string.to_s + assert_equal({ string: { foo: '"bar"' } }, @string.to_hash) + end + + it 'yields itself on creation' do + yielded = nil + string = String.new(:a, 'b') { |s| yielded = s } + + assert_same string, yielded + end + + it 'updates its bibliography when the key changes' do + bib = Bibliography.new + bib << @string + @string.key = :baz + + assert_equal :baz, @string.key + assert_same @string, bib.strings[:baz] + refute bib.strings.key?(:foo) + end + + it 'registers with and unregisters from a bibliography' do + bib = Bibliography.new + bib << @string + + assert_same @string, bib.strings[:foo] + bib.delete(@string) + + assert_nil bib.strings[:foo] + end + end + end + + class MetaContentTest < Minitest::Spec + it 'returns its content verbatim' do + meta = MetaContent.new('hello') + + assert_equal 'hello', meta.to_s + assert_equal 'hello', meta.content end end From 31b8d97c9dcbb5446a1e1853c9f1acf6b5cc7e89 Mon Sep 17 00:00:00 2001 From: "Tobias L. Maier" Date: Sun, 15 Nov 2026 00:16:00 +0100 Subject: [PATCH 06/12] Test Names#add and Name#rename_unless --- test/bibtex/test_names.rb | 80 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) diff --git a/test/bibtex/test_names.rb b/test/bibtex/test_names.rb index 601e9d6..7e3fce7 100644 --- a/test/bibtex/test_names.rb +++ b/test/bibtex/test_names.rb @@ -97,6 +97,86 @@ def apply(value) end end + describe '#add' do + it 'appends a Name and returns the collection' do + names = Names.new + + _(names.add(@poe)).must_be_same_as names + _(names.length).must_equal 1 + _(names[0]).must_be_same_as @poe + end + + it 'is aliased as << and push' do + names = Names.new + names << @poe + names.push(Name.new(last: 'Plato')) + + _(names.map(&:last)).must_equal %w[Poe Plato] + end + + it 'raises an ArgumentError for objects that cannot be converted to a string' do + names = Names.new + thing = BasicObject.new + def thing.is_a?(*) = false + def thing.inspect = '#' + def thing.respond_to?(*) = false + + error = _ { names.add(thing) }.must_raise ArgumentError + _(error.message).must_match(/not a name/) + _(names).must_be :empty? + end + end + + describe '.looks_like?' do + it 'returns false for objects that cannot be converted to a string' do + thing = BasicObject.new + def thing.respond_to?(*) = false + + _(Name.looks_like?(thing)).must_equal false + end + end + + describe '#rename_unless' do + it 'does not rename the name if all conditions match' do + _(@poe.rename_unless({ first: 'E.A.' }, last: @poe.last).first).wont_equal 'E.A.' + _(@poe.rename_unless({ first: 'E.A.' }, last: @poe.last, first: @poe.first).first).wont_equal 'E.A.' + end + + it 'renames the name if at least one condition does not match' do + _(@poe.rename_unless({ first: 'E.A.' }, last: 'foo').first).must_equal 'E.A.' + end + + it 'renames the name if the conditions refer to an unknown attribute' do + _(@poe.rename_unless({ first: 'E.A.' }, foo: 'bar').first).must_equal 'E.A.' + end + + it 'renames the name if the block returns false' do + _(@poe.rename_unless(first: 'E.A.') { |_n| false }.first).must_equal 'E.A.' + end + + it 'does not rename the name if the block returns true' do + _(@poe.rename_unless(first: 'E.A.') { |_n| true }.first).wont_equal 'E.A.' + end + + it 'yields the name to the block' do + yielded = nil + @poe.rename_unless(first: 'E.A.') { |n| yielded = n } + + _(yielded).must_be_same_as @poe + end + + it 'returns the name' do + _(@poe.rename_unless({ first: 'E.A.' }, last: 'foo')).must_be_same_as @poe + end + + it 'applies to all names of a collection and returns the collection' do + names = Names.parse('Edgar Allen Poe and Plato') + + _(names.rename_unless({ first: 'X.' }, last: 'Poe')).must_be_same_as names + _(names.map(&:first)).must_equal ['Edgar Allen', 'X.'] + end + end + describe '#merge!' do it 'does not merge duplicates' do n1 = Names.new(@poe) From 3ebe545a655ab096a0b97ee9f53b81aeba4c31bd Mon Sep 17 00:00:00 2001 From: "Tobias L. Maier" Date: Sun, 15 Nov 2026 00:16:00 +0100 Subject: [PATCH 07/12] Test Value#replace --- test/bibtex/test_value.rb | 71 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git a/test/bibtex/test_value.rb b/test/bibtex/test_value.rb index eec15d7..ef01dad 100644 --- a/test/bibtex/test_value.rb +++ b/test/bibtex/test_value.rb @@ -79,6 +79,77 @@ class ValueTest < Minitest::Spec end end + describe '#replace' do + it 'returns the receiver unchanged when it has no symbols' do + value = Value.new('foo') + + assert_same value, value.replace(String.new(:foo, 'bar')) + assert_equal 'foo', value.to_s + end + + it 'returns the receiver itself when it has symbols' do + value = Value.new(:foo) + + assert_same value, value.replace(String.new(:foo, 'bar')) + end + + it 'replaces all tokens with a plain Ruby string' do + assert_equal 'baz', Value.new(:foo, 'bar').replace('baz').to_s + end + + it 'replaces a matching symbol with the value of a BibTeX string' do + assert_equal 'bar', Value.new(:foo).replace(String.new(:foo, 'bar')).to_s + end + + it 'splices the tokens of a multi-token BibTeX string in place' do + string = String.new(:foo, Value.new('a', :b)) + + assert_equal '"x" # "a" # b # "y"', Value.new('x', :foo, 'y').replace(string).to_s + end + + it 'leaves non-matching symbols and strings alone' do + assert_equal 'other # "bar"', Value.new(:other, 'bar').replace(String.new(:foo, 'x')).to_s + end + + it 'replaces every occurrence of the symbol' do + assert_equal '"x" # "x"', Value.new(:foo, :foo).replace(String.new(:foo, 'x')).to_s + end + + it 'replaces symbols using a hash' do + assert_equal '"1" # "2"', Value.new(:a, :b).replace(a: '1', b: '2').to_s + end + + it 'keeps tokens without a hash entry' do + assert_equal '"1" # b # "lit"', Value.new(:a, :b, 'lit').replace(a: '1').to_s + end + + it 'accepts several arguments and applies them in order' do + value = Value.new(:a, :b).replace(String.new(:a, 'x'), { b: 'y' }) + + assert_equal '"x" # "y"', value.to_s + end + + it 'flattens array arguments' do + value = Value.new(:a, :b).replace([String.new(:a, 'x'), String.new(:b, 'y')]) + + assert_equal '"x" # "y"', value.to_s + end + + it 'ignores unsupported argument types' do + value = Value.new(:a) + + assert_equal 'a', value.replace(42, nil).to_s + end + + it 'does nothing when called without arguments' do + assert_equal 'a', Value.new(:a).replace.to_s + end + + it 'stops replacing once a plain string has removed all symbols' do + assert_equal 'abc', Value.new(:a).replace('abc', String.new(:a, 'x')).to_s + end + end + describe '#merge!' do it 'merges two strings by combining them' do assert_equal '"foo" # "bar"', Value.new('foo').merge!(Value.new('bar')).to_s From 9e52b1aea9405d71dc105101ce547596fd4cb35a Mon Sep 17 00:00:00 2001 From: "Tobias L. Maier" Date: Sun, 15 Nov 2026 00:31:43 +0100 Subject: [PATCH 08/12] Fix Element#type? with classes, symbol xml tags, String#to_xml and =~ conditions --- lib/bibtex/elements.rb | 16 ++++++++-------- test/bibtex/test_elements.rb | 25 +++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 8 deletions(-) diff --git a/lib/bibtex/elements.rb b/lib/bibtex/elements.rb index 5cfc729..a956235 100644 --- a/lib/bibtex/elements.rb +++ b/lib/bibtex/elements.rb @@ -87,7 +87,7 @@ def names end def type?(type) - self.type == type.intern || (defined?(type) == 'constant' && is_a?(type)) + type.is_a?(Module) ? is_a?(type) : self.type == type.intern end alias has_type? type? @@ -169,7 +169,7 @@ def to_json(options = {}) def to_xml(_options = {}) require 'rexml/document' - xml = REXML::Element.new(type) + xml = REXML::Element.new(type.to_s) xml.text = content xml end @@ -201,7 +201,7 @@ def inspect private def meets_condition?(condition) - property, operator, value = condition.split(%r{\s*([!~/^<>]?=|!~|=~|<|>)\s*}) + property, operator, value = condition.split(%r{\s*(=~|[!~/^<>]?=|!~|<|>)\s*}) if property.nil? true @@ -318,15 +318,15 @@ def to_hash(_options = {}) def to_xml(_options = {}) require 'rexml/document' - xml = REXML::Element.new(:string) + xml = REXML::Element.new('string') - k = REXML::Element.new(:key) - v = REXML::Element.new(:value) + k = REXML::Element.new('key') + v = REXML::Element.new('value') k.text = key.to_s v.text = value.to_s(quotes: '"') - xml.add_elements(k) - xml.add_elements(v) + xml.add_element(k) + xml.add_element(v) xml end diff --git a/test/bibtex/test_elements.rb b/test/bibtex/test_elements.rb index 8bab49f..c237f28 100644 --- a/test/bibtex/test_elements.rb +++ b/test/bibtex/test_elements.rb @@ -51,6 +51,12 @@ class ElementTest < Minitest::Spec assert @element.type?(:comment) end + it 'answers type queries for classes' do + assert @element.type?(Comment) + assert @element.type?(Element) + refute @element.type?(String) + end + it 'has an id' do assert_equal @element.id, @element.id assert_kind_of ::String, @element.id @@ -83,6 +89,10 @@ class ElementTest < Minitest::Spec assert_equal 'blah', @element.to_xml.text end + it 'names its xml element after its type' do + assert_equal 'blah', @element.to_xml.to_s + end + it 'tracks its bibliography' do bib = Bibliography.new @@ -156,6 +166,13 @@ class ElementTest < Minitest::Spec refute @a.matches?('@book[author~=o]') end + it 'supports =~ as an alias for ~=' do + assert @a.matches?('@book[title=~o+]') + assert @a.matches?('@book[title =~ ^F]') + refute @a.matches?('@book[title=~Z]') + refute @a.matches?('@book[author=~o]') + end + it 'supports negated regular expression conditions' do assert @a.matches?('@book[title!~Z]') assert @a.matches?('@book[author!~Z]') @@ -223,6 +240,14 @@ class StringTest < Minitest::Spec assert_equal({ string: { foo: '"bar"' } }, @string.to_hash) end + it 'serialises to xml' do + xml = @string.to_xml + + assert_equal 'string', xml.name + assert_equal 'foo', xml.elements['key'].text + assert_equal '"bar"', xml.elements['value'].text + end + it 'yields itself on creation' do yielded = nil string = String.new(:a, 'b') { |s| yielded = s } From 7d2d0661135dc6427a8f20bc698a3e9dff5a7cf1 Mon Sep 17 00:00:00 2001 From: "Tobias L. Maier" Date: Sun, 15 Nov 2026 00:31:43 +0100 Subject: [PATCH 09/12] Fix Names#add with strings and Name.looks_like? --- lib/bibtex/names.rb | 4 ++-- test/bibtex/test_names.rb | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/lib/bibtex/names.rb b/lib/bibtex/names.rb index af9bf39..03f88d0 100644 --- a/lib/bibtex/names.rb +++ b/lib/bibtex/names.rb @@ -84,7 +84,7 @@ def add(name) if name.is_a?(Name) @tokens << name elsif name.respond_to?(:to_s) - @tokens += Names.parse(name.to_s) + @tokens += Names.parse(name.to_s).to_a else raise ArgumentError, "failed to add #{name.inspect}: not a name." end @@ -132,7 +132,7 @@ def parse(string) # Returns true if thing looks like a name. # Actually converts thing to a string and tries to parse it. def looks_like?(thing) - thing.respond_to?(:to_s) && [Name.new.parse(string)].flatten.compact.empty? + thing.respond_to?(:to_s) && !Name.parse(thing.to_s).nil? end end diff --git a/test/bibtex/test_names.rb b/test/bibtex/test_names.rb index 7e3fce7..770569d 100644 --- a/test/bibtex/test_names.rb +++ b/test/bibtex/test_names.rb @@ -114,6 +114,20 @@ def apply(value) _(names.map(&:last)).must_equal %w[Poe Plato] end + it 'parses and appends the names of a string' do + names = Names.new(@poe) + + _(names.add('Plato and Aristotle')).must_be_same_as names + _(names.map(&:last)).must_equal %w[Poe Plato Aristotle] + end + + it 'parses and appends the names of any object that converts to a string' do + thing = Object.new + def thing.to_s = 'Plato' + + _(Names.new.add(thing).map(&:last)).must_equal %w[Plato] + end + it 'raises an ArgumentError for objects that cannot be converted to a string' do names = Names.new thing = BasicObject.new @@ -128,6 +142,21 @@ def thing.respond_to?(*) = false end describe '.looks_like?' do + it 'returns true for strings that parse as a name' do + _(Name.looks_like?('Poe, Edgar Allen')).must_equal true + end + + it 'returns true for objects whose string form parses as a name' do + thing = Object.new + def thing.to_s = 'Edgar Allen Poe' + + _(Name.looks_like?(thing)).must_equal true + end + + it 'returns false for strings that do not parse as a name' do + _(Name.looks_like?('')).must_equal false + end + it 'returns false for objects that cannot be converted to a string' do thing = BasicObject.new def thing.respond_to?(*) = false @@ -158,6 +187,10 @@ def thing.respond_to?(*) = false _(@poe.rename_unless(first: 'E.A.') { |_n| true }.first).wont_equal 'E.A.' end + it 'does not rename the name if no condition is given, as the complement of #rename_if' do + _(@poe.rename_unless(first: 'E.A.').first).must_equal 'Edgar Allen' + end + it 'yields the name to the block' do yielded = nil @poe.rename_unless(first: 'E.A.') { |n| yielded = n } From ee3747861d7aa2bf0fe1cf1c21547f137d31b037 Mon Sep 17 00:00:00 2001 From: "Tobias L. Maier" Date: Sun, 15 Nov 2026 00:31:43 +0100 Subject: [PATCH 10/12] Fix Entry#type? with a class argument --- lib/bibtex/entry.rb | 2 ++ test/bibtex/test_entry.rb | 17 +++++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/lib/bibtex/entry.rb b/lib/bibtex/entry.rb index 40d31a3..fa0dadd 100644 --- a/lib/bibtex/entry.rb +++ b/lib/bibtex/entry.rb @@ -178,6 +178,8 @@ def type=(type) end def type?(type) + return super if type.is_a?(Module) + type.to_s.match(/^(?:entry|\*)$/i) || @type.casecmp?(type.to_sym) || super end diff --git a/test/bibtex/test_entry.rb b/test/bibtex/test_entry.rb index 80a8f12..3eed1a1 100644 --- a/test/bibtex/test_entry.rb +++ b/test/bibtex/test_entry.rb @@ -47,6 +47,23 @@ def apply(value) end end + describe '#type?' do + before { @entry = Entry.new(bibtex_type: :book) } + + it 'matches the entry type by name' do + assert @entry.type?(:book) + refute @entry.type?(:article) + end + + it 'matches the entry class' do + assert @entry.type?(Entry) + end + + it 'does not match an unrelated class' do + refute @entry.type?(Comment) + end + end + describe '#add' do it 'preserves BibTeX::Names (and other subclasses of BibTeX::Value)' do e = Entry.new From ed7967353761f5af1766e6d5e59ae49f77d12feb Mon Sep 17 00:00:00 2001 From: "Tobias L. Maier" Date: Sun, 15 Nov 2026 00:33:47 +0100 Subject: [PATCH 11/12] Tighten the id and transliteration fallback tests --- test/bibtex/test_compatibility.rb | 5 ++--- test/bibtex/test_elements.rb | 2 +- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/test/bibtex/test_compatibility.rb b/test/bibtex/test_compatibility.rb index 3ec838b..c037994 100644 --- a/test/bibtex/test_compatibility.rb +++ b/test/bibtex/test_compatibility.rb @@ -17,10 +17,9 @@ def test_transliterate_drops_characters_without_ascii_form end def test_transliterate_falls_back_when_normalization_fails - str = +'café ok' - str.define_singleton_method(:unicode_normalize) { |*| raise ArgumentError, 'boom' } + binary = 'café ok'.b - assert_equal 'caf ok', BibTeX.transliterate(str) + assert_equal 'caf ok', BibTeX.transliterate(binary) end end end diff --git a/test/bibtex/test_elements.rb b/test/bibtex/test_elements.rb index c237f28..bdf8ef7 100644 --- a/test/bibtex/test_elements.rb +++ b/test/bibtex/test_elements.rb @@ -58,8 +58,8 @@ class ElementTest < Minitest::Spec end it 'has an id' do - assert_equal @element.id, @element.id assert_kind_of ::String, @element.id + refute_empty @element.id end it 'inspects itself' do From 5ce32848efeb187063fd19c91415489b7c3e24ed Mon Sep 17 00:00:00 2001 From: "Tobias L. Maier" Date: Sun, 15 Nov 2026 00:42:38 +0100 Subject: [PATCH 12/12] List the =~ behaviour change and the new fixes in History.txt --- History.txt | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/History.txt b/History.txt index 7c2ec9d..aad2363 100644 --- a/History.txt +++ b/History.txt @@ -20,6 +20,10 @@ Unreleased * Entry.new no longer removes :bibtex_type and :bibtex_key from the hash passed to it, and accepts a frozen hash * Entry#to_s and #to_hash, and so Bibliography#to_s, #to_a and #to_hash, no longer add :quotes to the options hash passed in, and accept a frozen hash * Reimplement Entry's dynamic field writers, Bibliography#save_to and the lexer's identifier and key rules from a written specification, without the code they replace (#154) +* Behaviour change: the query condition "prop=~re" now matches a regular expression, like "prop~=re". It used to be read as the equality "prop=" with the value "~re", which could never have been intended +* Fix Element#type? and Entry#type? raising NoMethodError when given a class, e.g. entry.type?(BibTeX::Entry) +* Fix BibTeX::String#to_xml raising NoMethodError, and Element#to_xml producing an empty tag name for elements whose type is a symbol +* Fix Names#add raising TypeError when given a string, and Name.looks_like? raising NameError and returning the inverse of its documented result 6.2.0 / 2025-07-20 ==================