Skip to content

Commit

Permalink
Fix failing specs
Browse files Browse the repository at this point in the history
  • Loading branch information
Novtopro committed Nov 4, 2024
1 parent cd7dada commit 5be3f52
Show file tree
Hide file tree
Showing 16 changed files with 122 additions and 61 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@

# rspec failure tracking
.rspec_status
Gemfile.lock
input.xml
output.xml
13 changes: 13 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,21 @@ require:
- rubocop-rake
- rubocop-rspec

Metrics/BlockLength:
Enabled: false

Metrics/MethodLength:
Enabled: false

Metrics/AbcSize:
Enabled: false

Layout/LineLength:
Max: 160

AllCops:
TargetRubyVersion: 3.0
SuggestExtensions: false
NewCops: enable
Exclude:
- 'vendor/**/*'
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ gem "rubocop-performance"
gem "rubocop-rake"
gem "rubocop-rspec"
gem "xml-c14n"
gem "pry"
17 changes: 17 additions & 0 deletions bin/util
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/usr/bin/env ruby

require "pathname"

Pathname.glob("spec/fixtures/**/*.xml").each do |it|
path = it.sub("spec/fixtures/", "")

code = <<~RUBY
context "with #{path}" do
let(:fixture) { file_fixture("#{path}") }
it_behaves_like "lossless round-trip converter"
end
RUBY

puts code
end
1 change: 1 addition & 0 deletions lib/oasis/etm/colspec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class Colspec < Lutaml::Model::Serializable

xml do
root "colspec"
namespace "http://docs.oasis-open.org/ns/oasis-exchange/table", "oasis"

map_attribute "colnum", to: :colnum
map_attribute "colname", to: :colname
Expand Down
1 change: 1 addition & 0 deletions lib/oasis/etm/entry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class Entry < Lutaml::Model::Serializable

xml do
root "entry"
namespace "http://docs.oasis-open.org/ns/oasis-exchange/table", "oasis"

# Attribute mappings
map_attribute "colname", to: :colname
Expand Down
1 change: 1 addition & 0 deletions lib/oasis/etm/row.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class Row < Lutaml::Model::Serializable

xml do
root "row"
namespace "http://docs.oasis-open.org/ns/oasis-exchange/table", "oasis"

map_attribute "rowsep", to: :rowsep
map_attribute "valign", to: :valign
Expand Down
1 change: 1 addition & 0 deletions lib/oasis/etm/table.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class Table < Lutaml::Model::Serializable

xml do
root "table", ordered: true
namespace "http://docs.oasis-open.org/ns/oasis-exchange/table", "oasis"

# Frame mappings
map_attribute "frame", to: :frame
Expand Down
1 change: 1 addition & 0 deletions lib/oasis/etm/tbody.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class Tbody < Lutaml::Model::Serializable

xml do
root "tbody"
namespace "http://docs.oasis-open.org/ns/oasis-exchange/table", "oasis"

map_attribute "valign", to: :valign
map_element "row", to: :rows
Expand Down
1 change: 1 addition & 0 deletions lib/oasis/etm/tcol.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class Colspec < Lutaml::Model::Serializable

xml do
root "colspec"
namespace "http://docs.oasis-open.org/ns/oasis-exchange/table", "oasis"

map_attribute "colnum", to: :colnum
map_attribute "colname", to: :colname
Expand Down
1 change: 1 addition & 0 deletions lib/oasis/etm/tgroup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class Tgroup < Lutaml::Model::Serializable

xml do
root "tgroup", ordered: true
namespace "http://docs.oasis-open.org/ns/oasis-exchange/table", "oasis"

# Attribute mappings
map_attribute "cols", to: :cols
Expand Down
1 change: 1 addition & 0 deletions lib/oasis/etm/thead.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class Thead < Lutaml::Model::Serializable

xml do
root "thead"
namespace "http://docs.oasis-open.org/ns/oasis-exchange/table", "oasis"

map_attribute "valign", to: :valign
map_element "row", to: :rows
Expand Down
116 changes: 55 additions & 61 deletions spec/oasis/etm_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,79 +3,73 @@
require "pathname"

RSpec.describe Oasis::Etm do
fixtures_dir = Pathname.new(__dir__).join("../fixtures")

describe "XML round-trip conversion" do
describe "native XML" do
xml_files = Dir[fixtures_dir.join("native", "*.xml")]

xml_files.each do |file_path|
context "with file #{Pathname.new(file_path).relative_path_from(fixtures_dir)}" do
let(:xml_string) { File.read(file_path) }
context "with isosts/isosts_tables.cals.01.xml" do
let(:fixture) { file_fixture("isosts/isosts_tables.cals.01.xml") }
it_behaves_like "lossless round-trip converter"
end

it "performs lossless round-trip conversion" do
parsed = Oasis::Etm::Table.from_xml(xml_string)
generated = parsed.to_xml(
pretty: true,
declaration: true,
encoding: "utf-8",
)
context "with isosts/isosts_tables.cals.02.xml" do
let(:fixture) { file_fixture("isosts/isosts_tables.cals.02.xml") }
it_behaves_like "lossless round-trip converter"
end

cleaned_xml_string = xml_string
.gsub(/^<\?xml.*\n/, "")
context "with isosts/isosts_tables.cals.03.xml" do
let(:fixture) { file_fixture("isosts/isosts_tables.cals.03.xml") }
it_behaves_like "lossless round-trip converter"
end

expect(generated).to be_analogous_with(cleaned_xml_string)
end
end
end
end
context "with isosts/isosts_tables.cals.04.xml" do
let(:fixture) { file_fixture("isosts/isosts_tables.cals.04.xml") }
it_behaves_like "lossless round-trip converter"
end

describe "namespaced XML (ISOSTS)" do
xml_files = Dir[fixtures_dir.join("isosts", "*.xml")]
context "with isosts/isosts_tables.cals.05.xml" do
let(:fixture) { file_fixture("isosts/isosts_tables.cals.05.xml") }
it_behaves_like "lossless round-trip converter"
end

xml_files.each do |file_path|
context "with file #{Pathname.new(file_path).relative_path_from(fixtures_dir)}" do
let(:xml_string) { File.read(file_path) }
context "with isosts/isosts_tables.cals.06.xml" do
let(:fixture) { file_fixture("isosts/isosts_tables.cals.06.xml") }
it_behaves_like "lossless round-trip converter"
end

it "performs lossless round-trip conversion" do
parsed = Oasis::Etm::Table.from_xml(xml_string)
generated = parsed.to_xml(
pretty: true,
declaration: true,
encoding: "utf-8",
)
context "with isosts/isosts_tables.cals.07.xml" do
let(:fixture) { file_fixture("isosts/isosts_tables.cals.07.xml") }
it_behaves_like "lossless round-trip converter"
end

cleaned_xml_string = xml_string
.gsub(/^<\?xml.*\n/, "")
context "with isosts/isosts_tables.cals.08.xml" do
let(:fixture) { file_fixture("isosts/isosts_tables.cals.08.xml") }
it_behaves_like "lossless round-trip converter"
end

expect(generated).to be_analogous_with(cleaned_xml_string)
end
end
end
end
context "with isosts/isosts_tables.cals.09.xml" do
let(:fixture) { file_fixture("isosts/isosts_tables.cals.09.xml") }
it_behaves_like "lossless round-trip converter"
end

describe "namespaced XML (NISO JATS)" do
xml_files = Dir[fixtures_dir.join("niso-jats", "*.xml")]
context "with isosts/isosts_tables.cals.10.xml" do
let(:fixture) { file_fixture("isosts/isosts_tables.cals.10.xml") }
it_behaves_like "lossless round-trip converter"
end

xml_files.each do |file_path|
context "with file #{Pathname.new(file_path).relative_path_from(fixtures_dir)}" do
let(:xml_string) { File.read(file_path) }
context "with isosts/isosts_tables.cals.11.xml" do
let(:fixture) { file_fixture("isosts/isosts_tables.cals.11.xml") }
it_behaves_like "lossless round-trip converter"
end

it "performs lossless round-trip conversion" do
parsed = Oasis::Etm::Table.from_xml(xml_string)
generated = parsed.to_xml(
pretty: true,
declaration: true,
encoding: "utf-8",
)
context "with isosts/isosts_tables.cals.12.xml" do
let(:fixture) { file_fixture("isosts/isosts_tables.cals.12.xml") }
it_behaves_like "lossless round-trip converter"
end

cleaned_xml_string = xml_string
.gsub(/^<\?xml.*\n/, "")
context "with native/docbook_example.xml" do
let(:fixture) { file_fixture("native/docbook_example.xml") }
it_behaves_like "lossless round-trip converter"
end

expect(generated).to be_analogous_with(cleaned_xml_string)
end
end
end
end
context "with niso-jats/niso-jats-table-wrap.xml" do
let(:fixture) { file_fixture("niso-jats/niso-jats-table-wrap.xml") }
it_behaves_like "lossless round-trip converter"
end
end
3 changes: 3 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
require "oasis-etm"
require "nokogiri"
require "xml-c14n"
require "pry"

# Require all support files
Dir[File.join(__dir__, "support", "**", "*.rb")].each { |f| require f }

RSpec.configure do |config|
include Helper

# Enable flags like --only-failures and --next-failure
config.example_status_persistence_file_path = ".rspec_status"

Expand Down
9 changes: 9 additions & 0 deletions spec/support/helper.rb
Original file line number Diff line number Diff line change
@@ -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
13 changes: 13 additions & 0 deletions spec/support/shared_examples/lossless_round_trip_examples.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
RSpec.shared_examples "lossless round-trip converter" do
it "performs lossless round-trip conversion" do
input = fixture.read

serialized = Oasis::Etm::Table.from_xml(input)
output = serialized.to_xml(pretty: true, declaration: true, encoding: "utf-8")

File.write("output.xml", output)
File.write("input.xml", input)

expect(output).to be_analogous_with(input)
end
end

0 comments on commit 5be3f52

Please sign in to comment.