From 0fce7f61a7fd5b6530c56cc750f7ec71384f8842 Mon Sep 17 00:00:00 2001 From: River He Date: Tue, 29 Oct 2024 16:17:14 +0800 Subject: [PATCH] Fix failing specs --- .rubocop.yml | 5 +++- Gemfile | 2 +- lib/niso-jats.rb | 2 +- lib/niso/jats.rb | 10 ------- lib/niso/jats/alt_title.rb | 2 ++ lib/niso/jats/article_title.rb | 2 ++ lib/niso/jats/attrib.rb | 2 ++ lib/niso/jats/bold.rb | 2 ++ lib/niso/jats/comment.rb | 2 ++ lib/niso/jats/def_head.rb | 2 ++ lib/niso/jats/fixed_case.rb | 2 ++ lib/niso/jats/italic.rb | 2 ++ lib/niso/jats/meta_value.rb | 2 ++ lib/niso/jats/monospace.rb | 2 ++ lib/niso/jats/overline.rb | 2 ++ lib/niso/jats/roman.rb | 2 ++ lib/niso/jats/sc.rb | 2 ++ lib/niso/jats/see.rb | 2 ++ lib/niso/jats/see_also.rb | 2 ++ lib/niso/jats/strike.rb | 2 ++ lib/niso/jats/sub.rb | 2 ++ lib/niso/jats/subtitle.rb | 2 ++ lib/niso/jats/sup.rb | 2 ++ lib/niso/jats/term.rb | 2 ++ lib/niso/jats/term_head.rb | 2 ++ lib/niso/jats/title.rb | 2 ++ lib/niso/jats/trans_subtitle.rb | 2 ++ lib/niso/jats/trans_title.rb | 2 ++ lib/niso/jats/underline.rb | 2 ++ lib/niso_jats.rb | 5 ++++ niso-jats.gemspec | 1 + spec/fixtures/bmj_sample.xml | 10 +++---- spec/fixtures/element_citation.xml | 7 +++++ spec/niso/jats/article_spec.rb | 36 +++++-------------------- spec/niso/jats/element_citation_spec.rb | 9 +++++++ spec/spec_helper.rb | 13 +++------ spec/support/helper.rb | 9 +++++++ spec/support/shared_examples.rb | 19 +++++++++++++ 38 files changed, 121 insertions(+), 57 deletions(-) create mode 100644 lib/niso_jats.rb create mode 100644 spec/fixtures/element_citation.xml create mode 100644 spec/niso/jats/element_citation_spec.rb create mode 100644 spec/support/helper.rb create mode 100644 spec/support/shared_examples.rb diff --git a/.rubocop.yml b/.rubocop.yml index f5c7423..7973f8a 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -12,7 +12,10 @@ Metrics/MethodLength: Metrics/AbcSize: Enabled: false +Layout/LineLength: + Max: 120 + AllCops: - TargetRubyVersion: 2.7 + TargetRubyVersion: 3.0 SuggestExtensions: false NewCops: enable diff --git a/Gemfile b/Gemfile index 09d9cff..7016771 100644 --- a/Gemfile +++ b/Gemfile @@ -6,7 +6,7 @@ source "https://rubygems.org" gemspec gem "equivalent-xml" -gem "pry", "~> 0.12" +gem "pry" gem "rake", "~> 13.0" gem "rspec", "~> 3.11" gem "rubocop", "~> 1.58" diff --git a/lib/niso-jats.rb b/lib/niso-jats.rb index b20f7a7..db6d3e9 100644 --- a/lib/niso-jats.rb +++ b/lib/niso-jats.rb @@ -1 +1 @@ -require_relative "niso/jats" +require_relative "niso_jats" diff --git a/lib/niso/jats.rb b/lib/niso/jats.rb index 1b317ca..f3f24fc 100644 --- a/lib/niso/jats.rb +++ b/lib/niso/jats.rb @@ -12,13 +12,3 @@ module Jats class Error < StandardError; end end end - -require "zeitwerk" - -loader = Zeitwerk::Loader.for_gem -loader.push_dir(__dir__, namespace: Niso) -loader.ignore("#{__dir__}/jats/module_includer.rb") -loader.setup -loader.eager_load - -require_relative "jats/module_includer" diff --git a/lib/niso/jats/alt_title.rb b/lib/niso/jats/alt_title.rb index b922299..fd0ae13 100644 --- a/lib/niso/jats/alt_title.rb +++ b/lib/niso/jats/alt_title.rb @@ -3,6 +3,8 @@ module Niso module Jats class AltTitle < Lutaml::Model::Serializable + include BasicText + attribute :content, :string attribute :alt_title_type, :string attribute :id, :string diff --git a/lib/niso/jats/article_title.rb b/lib/niso/jats/article_title.rb index a0ea3ca..d567faf 100644 --- a/lib/niso/jats/article_title.rb +++ b/lib/niso/jats/article_title.rb @@ -3,6 +3,8 @@ module Niso module Jats class ArticleTitle < Lutaml::Model::Serializable + include BasicText + attribute :content, :string attribute :id, :string attribute :lang, :string diff --git a/lib/niso/jats/attrib.rb b/lib/niso/jats/attrib.rb index 1711bfe..4a85ecc 100644 --- a/lib/niso/jats/attrib.rb +++ b/lib/niso/jats/attrib.rb @@ -3,6 +3,8 @@ module Niso module Jats class Attrib < Lutaml::Model::Serializable + include BasicText + attribute :content, :string attribute :id, :string attribute :specific_use, :string diff --git a/lib/niso/jats/bold.rb b/lib/niso/jats/bold.rb index 6136062..268e90e 100644 --- a/lib/niso/jats/bold.rb +++ b/lib/niso/jats/bold.rb @@ -3,6 +3,8 @@ module Niso module Jats class Bold < Lutaml::Model::Serializable + include BasicText + attribute :content, :string attribute :id, :string attribute :specific_use, :string diff --git a/lib/niso/jats/comment.rb b/lib/niso/jats/comment.rb index 53f6144..d5dfa44 100644 --- a/lib/niso/jats/comment.rb +++ b/lib/niso/jats/comment.rb @@ -3,6 +3,8 @@ module Niso module Jats class Comment < Lutaml::Model::Serializable + include BasicText + attribute :content, :string attribute :content_type, :string attribute :id, :string diff --git a/lib/niso/jats/def_head.rb b/lib/niso/jats/def_head.rb index c595784..e014b7b 100644 --- a/lib/niso/jats/def_head.rb +++ b/lib/niso/jats/def_head.rb @@ -3,6 +3,8 @@ module Niso module Jats class DefHead < Lutaml::Model::Serializable + include BasicText + attribute :content, :string attribute :id, :string diff --git a/lib/niso/jats/fixed_case.rb b/lib/niso/jats/fixed_case.rb index c055ad5..bb8a972 100644 --- a/lib/niso/jats/fixed_case.rb +++ b/lib/niso/jats/fixed_case.rb @@ -3,6 +3,8 @@ module Niso module Jats class FixedCase < Lutaml::Model::Serializable + include BasicText + attribute :content, :string attribute :content_type, :string attribute :id, :string diff --git a/lib/niso/jats/italic.rb b/lib/niso/jats/italic.rb index 9070260..28c7f84 100644 --- a/lib/niso/jats/italic.rb +++ b/lib/niso/jats/italic.rb @@ -3,6 +3,8 @@ module Niso module Jats class Italic < Lutaml::Model::Serializable + include BasicText + attribute :content, :string attribute :id, :string attribute :specific_use, :string diff --git a/lib/niso/jats/meta_value.rb b/lib/niso/jats/meta_value.rb index 02d8145..142d05e 100644 --- a/lib/niso/jats/meta_value.rb +++ b/lib/niso/jats/meta_value.rb @@ -3,6 +3,8 @@ module Niso module Jats class MetaValue < Lutaml::Model::Serializable + include BasicText + attribute :content, :string attribute :id, :string diff --git a/lib/niso/jats/monospace.rb b/lib/niso/jats/monospace.rb index f9e449b..fa9eec3 100644 --- a/lib/niso/jats/monospace.rb +++ b/lib/niso/jats/monospace.rb @@ -3,6 +3,8 @@ module Niso module Jats class Monospace < Lutaml::Model::Serializable + include BasicText + attribute :content, :string attribute :id, :string attribute :specific_use, :string diff --git a/lib/niso/jats/overline.rb b/lib/niso/jats/overline.rb index 7a18c99..1f0457a 100644 --- a/lib/niso/jats/overline.rb +++ b/lib/niso/jats/overline.rb @@ -3,6 +3,8 @@ module Niso module Jats class Overline < Lutaml::Model::Serializable + include BasicText + attribute :content, :string attribute :id, :string attribute :specific_use, :string diff --git a/lib/niso/jats/roman.rb b/lib/niso/jats/roman.rb index 7469832..d92cc06 100644 --- a/lib/niso/jats/roman.rb +++ b/lib/niso/jats/roman.rb @@ -3,6 +3,8 @@ module Niso module Jats class Roman < Lutaml::Model::Serializable + include BasicText + attribute :content, :string attribute :id, :string attribute :specific_use, :string diff --git a/lib/niso/jats/sc.rb b/lib/niso/jats/sc.rb index ec0c3e2..91f9699 100644 --- a/lib/niso/jats/sc.rb +++ b/lib/niso/jats/sc.rb @@ -3,6 +3,8 @@ module Niso module Jats class Sc < Lutaml::Model::Serializable + include BasicText + attribute :content, :string attribute :id, :string attribute :specific_use, :string diff --git a/lib/niso/jats/see.rb b/lib/niso/jats/see.rb index a256726..0d304f3 100644 --- a/lib/niso/jats/see.rb +++ b/lib/niso/jats/see.rb @@ -3,6 +3,8 @@ module Niso module Jats class See < Lutaml::Model::Serializable + include BasicText + attribute :content, :string attribute :content_type, :string attribute :id, :string diff --git a/lib/niso/jats/see_also.rb b/lib/niso/jats/see_also.rb index 8e6b7b2..c52fb3c 100644 --- a/lib/niso/jats/see_also.rb +++ b/lib/niso/jats/see_also.rb @@ -3,6 +3,8 @@ module Niso module Jats class SeeAlso < Lutaml::Model::Serializable + include BasicText + attribute :content, :string attribute :content_type, :string attribute :id, :string diff --git a/lib/niso/jats/strike.rb b/lib/niso/jats/strike.rb index 29adda2..20d8e43 100644 --- a/lib/niso/jats/strike.rb +++ b/lib/niso/jats/strike.rb @@ -3,6 +3,8 @@ module Niso module Jats class Strike < Lutaml::Model::Serializable + include BasicText + attribute :content, :string attribute :id, :string attribute :specific_use, :string diff --git a/lib/niso/jats/sub.rb b/lib/niso/jats/sub.rb index 25f16d1..eac68d0 100644 --- a/lib/niso/jats/sub.rb +++ b/lib/niso/jats/sub.rb @@ -3,6 +3,8 @@ module Niso module Jats class Sub < Lutaml::Model::Serializable + include BasicText + attribute :content, :string attribute :arrange, :string attribute :id, :string diff --git a/lib/niso/jats/subtitle.rb b/lib/niso/jats/subtitle.rb index 930f2d1..8d9f7e7 100644 --- a/lib/niso/jats/subtitle.rb +++ b/lib/niso/jats/subtitle.rb @@ -3,6 +3,8 @@ module Niso module Jats class Subtitle < Lutaml::Model::Serializable + include BasicText + attribute :content, :string attribute :content_type, :string attribute :id, :string diff --git a/lib/niso/jats/sup.rb b/lib/niso/jats/sup.rb index d56a020..4e21449 100644 --- a/lib/niso/jats/sup.rb +++ b/lib/niso/jats/sup.rb @@ -3,6 +3,8 @@ module Niso module Jats class Sup < Lutaml::Model::Serializable + include BasicText + attribute :content, :string attribute :arrange, :string attribute :id, :string diff --git a/lib/niso/jats/term.rb b/lib/niso/jats/term.rb index abea72f..26fd8ea 100644 --- a/lib/niso/jats/term.rb +++ b/lib/niso/jats/term.rb @@ -3,6 +3,8 @@ module Niso module Jats class Term < Lutaml::Model::Serializable + include BasicText + attribute :content, :string attribute :id, :string attribute :rid, :string diff --git a/lib/niso/jats/term_head.rb b/lib/niso/jats/term_head.rb index b220fce..d737a3d 100644 --- a/lib/niso/jats/term_head.rb +++ b/lib/niso/jats/term_head.rb @@ -3,6 +3,8 @@ module Niso module Jats class TermHead < Lutaml::Model::Serializable + include BasicText + attribute :content, :string attribute :id, :string diff --git a/lib/niso/jats/title.rb b/lib/niso/jats/title.rb index 8a789e7..d3310b9 100644 --- a/lib/niso/jats/title.rb +++ b/lib/niso/jats/title.rb @@ -3,6 +3,8 @@ module Niso module Jats class Title < Lutaml::Model::Serializable + include BasicText + attribute :content, :string attribute :content_type, :string attribute :id, :string diff --git a/lib/niso/jats/trans_subtitle.rb b/lib/niso/jats/trans_subtitle.rb index d363961..dde83ea 100644 --- a/lib/niso/jats/trans_subtitle.rb +++ b/lib/niso/jats/trans_subtitle.rb @@ -3,6 +3,8 @@ module Niso module Jats class TransSubtitle < Lutaml::Model::Serializable + include BasicText + attribute :content, :string attribute :id, :string attribute :specific_use, :string diff --git a/lib/niso/jats/trans_title.rb b/lib/niso/jats/trans_title.rb index cd79ed6..8cf0b64 100644 --- a/lib/niso/jats/trans_title.rb +++ b/lib/niso/jats/trans_title.rb @@ -3,6 +3,8 @@ module Niso module Jats class TransTitle < Lutaml::Model::Serializable + include BasicText + attribute :content, :string attribute :content_type, :string attribute :id, :string diff --git a/lib/niso/jats/underline.rb b/lib/niso/jats/underline.rb index a0614e2..a19e996 100644 --- a/lib/niso/jats/underline.rb +++ b/lib/niso/jats/underline.rb @@ -3,6 +3,8 @@ module Niso module Jats class Underline < Lutaml::Model::Serializable + include BasicText + attribute :content, :string attribute :id, :string attribute :specific_use, :string diff --git a/lib/niso_jats.rb b/lib/niso_jats.rb new file mode 100644 index 0000000..6988deb --- /dev/null +++ b/lib/niso_jats.rb @@ -0,0 +1,5 @@ +require "zeitwerk" + +loader = Zeitwerk::Loader.for_gem(warn_on_extra_files: false) +loader.ignore("#{__dir__}/niso-jats.rb") +loader.setup diff --git a/niso-jats.gemspec b/niso-jats.gemspec index 62953d0..7b7f64e 100644 --- a/niso-jats.gemspec +++ b/niso-jats.gemspec @@ -35,4 +35,5 @@ Gem::Specification.new do |spec| spec.add_dependency "lutaml-model" spec.add_dependency "nokogiri" spec.add_dependency "zeitwerk", "~> 2.6.18" + spec.metadata["rubygems_mfa_required"] = "true" end diff --git a/spec/fixtures/bmj_sample.xml b/spec/fixtures/bmj_sample.xml index 3045e19..e7e0921 100644 --- a/spec/fixtures/bmj_sample.xml +++ b/spec/fixtures/bmj_sample.xml @@ -906,16 +906,16 @@ P + + www.sdo.lshtm.ac.uk/continuityofcare.htm (accessed 2 Jan 2002) Continuity of care: report of a scoping exercise for the national co-ordinating centre for NHS Service Delivery and Organisation R&D (NCCSDO), Summer 2000 2001 London NCCSDO - - www.sdo.lshtm.ac.uk/continuityofcare.htm (accessed 2 Jan 2002) @@ -1065,4 +1065,4 @@ - \ No newline at end of file + diff --git a/spec/fixtures/element_citation.xml b/spec/fixtures/element_citation.xml new file mode 100644 index 0000000..0b31311 --- /dev/null +++ b/spec/fixtures/element_citation.xml @@ -0,0 +1,7 @@ + + + + www.sdo.lshtm.ac.uk/continuityofcare.htm + (accessed 2 Jan 2002) + diff --git a/spec/niso/jats/article_spec.rb b/spec/niso/jats/article_spec.rb index dc7ea02..2c457cf 100644 --- a/spec/niso/jats/article_spec.rb +++ b/spec/niso/jats/article_spec.rb @@ -1,37 +1,13 @@ # frozen_string_literal: true RSpec.describe Niso::Jats::Article do - def file_contents(filename) - File.read(filename) + context "with bmj_sample.xml" do + let(:fixture) { file_fixture("bmj_sample.xml") } + it_behaves_like "a serializer" end - glob_path = Pathname.new(__dir__) - .join("../../fixtures/*.xml") - - Dir.glob(glob_path)[0..1].each do |filename| - fn = Pathname.new(filename).basename - xit "round-trips #{fn} with equivalent-xml" do - input = file_contents(Pathname.new(filename)) - parsed = described_class.from_xml(input) - generated = parsed.to_xml( - pretty: true, - declaration: true, - encoding: "utf-8", - ) - - expect(generated).to be_equivalent_to(input) - end - - it "round-trips #{fn} with xml-c14" do - input = file_contents(Pathname.new(filename)) - parsed = described_class.from_xml(input) - generated = parsed.to_xml( - pretty: true, - declaration: true, - encoding: "utf-8", - ) - - expect(generated).to be_analogous_with(input) - end + context "with pnas_sample.xml" do + let(:fixture) { file_fixture("pnas_sample.xml") } + it_behaves_like "a serializer" end end diff --git a/spec/niso/jats/element_citation_spec.rb b/spec/niso/jats/element_citation_spec.rb new file mode 100644 index 0000000..0733311 --- /dev/null +++ b/spec/niso/jats/element_citation_spec.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +RSpec.describe Niso::Jats::ElementCitation do + context "with a element_citation.xml" do + let(:fixture) { file_fixture("element_citation.xml") } + + it_behaves_like "a serializer" + end +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 363b5e8..c6ab127 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,5 +1,6 @@ # frozen_string_literal: true +require "pry" require "niso-jats" require "xml-c14n" require "equivalent-xml" @@ -7,18 +8,12 @@ require "lutaml/model/xml_adapter/nokogiri_adapter" require "lutaml/model/json_adapter/standard_json_adapter" -module TestHelper - def fixture(name) - Pathname.new(fixtures_path(name)) - end - - def fixtures_path(name) - File.join(__dir__, "fixtures", name) - end +Dir["spec/support/**/*.rb"].each do |it| + require File.expand_path(it) end RSpec.configure do |config| - config.include TestHelper + config.include Helper # Enable flags like --only-failures and --next-failure config.example_status_persistence_file_path = ".rspec_status" diff --git a/spec/support/helper.rb b/spec/support/helper.rb new file mode 100644 index 0000000..0db1096 --- /dev/null +++ b/spec/support/helper.rb @@ -0,0 +1,9 @@ +module Helper + def file_fixture(name) + Pathname.new(fixtures_path(name)) + end + + def fixtures_path(name) + File.join("spec/fixtures", name) + end +end diff --git a/spec/support/shared_examples.rb b/spec/support/shared_examples.rb new file mode 100644 index 0000000..6211262 --- /dev/null +++ b/spec/support/shared_examples.rb @@ -0,0 +1,19 @@ +RSpec.shared_examples "a serializer" do + xit "round-trips with equivalent-xml" do + input = fixture.read + + serialized = described_class.from_xml(input) + output = serialized.to_xml(declaration: true, encoding: "utf-8") + + expect(equivalent_xml?(output, input)).to be_truthy + end + + it "round-trips with xml-c14" do + input = Xml::C14n.format(fixture.read) + + serialized = described_class.from_xml(input) + output = Xml::C14n.format(serialized.to_xml) + + expect(output).to be_analogous_with(input) + end +end