Skip to content

Commit

Permalink
make FullNmae & Affiliation comparable
Browse files Browse the repository at this point in the history
  • Loading branch information
andrew2net committed Jul 11, 2024
1 parent a4ecc16 commit 1ee6c62
Show file tree
Hide file tree
Showing 12 changed files with 308 additions and 2 deletions.
18 changes: 18 additions & 0 deletions lib/relaton_bib/contributor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ def initialize(**args) # rubocop:disable Metrics/CyclomaticComplexity
@formatted_address = args[:formatted_address] unless args[:city] && args[:country]
end

def ==(other)
street == other.street && city == other.city && state == other.state &&
country == other.country && postcode == other.postcode &&
formatted_address == other.formatted_address
end

# @param doc [Nokogiri::XML::Document]
def to_xml(doc) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
doc.address do
Expand Down Expand Up @@ -100,6 +106,10 @@ def initialize(type:, value:, subtype: nil)
@value = value
end

def ==(other)
type == other.type && subtype == other.subtype && value == other.value
end

# @param builder [Nokogiri::XML::Document]
def to_xml(builder)
node = builder.send type, value
Expand Down Expand Up @@ -144,6 +154,10 @@ def initialize(organization: nil, name: nil, description: [])
@description = description
end

def ==(other)
name == other.name && organization == other.organization && description == other.description
end

# @param opts [Hash]
# @option opts [Nokogiri::XML::Builder] :builder XML builder
# @option opts [String] :lang language
Expand Down Expand Up @@ -224,6 +238,10 @@ def initialize(url: nil, contact: [])
@contact = contact
end

def ==(other)
uri == other.uri && contact == other.contact
end

# Returns url.
# @return [String]
def url
Expand Down
4 changes: 4 additions & 0 deletions lib/relaton_bib/forename.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ def initialize(content: nil, language: [], script: [], initial: nil)
super content, language, script
end

def ==(other)
super && initial == other.initial
end

def to_s
content.nil? ? initial : super
end
Expand Down
4 changes: 4 additions & 0 deletions lib/relaton_bib/formatted_string.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ def initialize(content: "", language: nil, script: nil, format: "text/plain")
super(content, language, script)
end

def ==(other)
super && format == other.format
end

# @param builder [Nokogiri::XML::Builder]
def to_xml(builder)
builder.parent["format"] = format if format
Expand Down
5 changes: 5 additions & 0 deletions lib/relaton_bib/full_name.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ def initialize(**args) # rubocop:disable Metrics/AbcSize
@completename = args[:completename]
end

def ==(other)
surname == other.surname && abbreviation == other.abbreviation && completename == other.completename &&
forename == other.forename && initials == other.initials && addition == other.addition && prefix == other.prefix
end

# @param opts [Hash]
# @option opts [Nokogiri::XML::Builder] :builder XML builder
# @option opts [String] :lang language
Expand Down
6 changes: 6 additions & 0 deletions lib/relaton_bib/image.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ def initialize(id:, src:, mimetype:, **args)
@longdesc = args[:longdesc]
end

def ==(other)
other.is_a?(Image) && id == other.id && src == other.src && mimetype == other.mimetype &&
filename == other.filename && width == other.width && height == other.height &&
alt == other.alt && title == other.title && longdesc == other.longdesc
end

#
# Converts the image object to XML format.
#
Expand Down
6 changes: 6 additions & 0 deletions lib/relaton_bib/localized_string.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ def initialize(content, language = nil, script = nil) # rubocop:disable Metrics/
end
end

def ==(other)
return false unless other.is_a? LocalizedString

content == other.content && language == other.language && script == other.script
end

#
# String representation.
#
Expand Down
10 changes: 10 additions & 0 deletions lib/relaton_bib/organization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ def initialize(type, value)
@value = value
end

def ==(other)
type == other.type && value == other.value
end

# @param builder [Nokogiri::XML::Builder]
def to_xml(builder)
builder.identifier(value, type: type)
Expand Down Expand Up @@ -90,6 +94,12 @@ def initialize(**args) # rubocop:disable Metrics/AbcSize,Metrics/MethodLength
@logo = args[:logo]
end

def ==(other)
name == other.name && abbreviation == other.abbreviation &&
subdivision == other.subdivision && identifier == other.identifier &&
logo == other.logo && super
end

# @param opts [Hash]
# @option opts [Nokogiri::XML::Builder] :builder XML builder
# @option opts [String] :lang language
Expand Down
2 changes: 1 addition & 1 deletion lib/relaton_bib/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module RelatonBib
VERSION = "1.19.0".freeze
VERSION = "1.19.1".freeze
end
69 changes: 69 additions & 0 deletions spec/relaton_bib/contributor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,20 @@
expect { RelatonBib::Address.new }.to raise_error(ArgumentError)
end

context "==" do
it "same content" do
contrib = RelatonBib::Address.new(formatted_address: "formatted address")
other = RelatonBib::Address.new(formatted_address: "formatted address")
expect(contrib).to eq other
end

it "different content" do
contrib = RelatonBib::Address.new(formatted_address: "formatted address")
other = RelatonBib::Address.new(formatted_address: "other formatted address")
expect(contrib).not_to eq other
end
end

context "render formatted address" do
let(:contrib) { RelatonBib::Address.new(formatted_address: "formatted address") }

Expand All @@ -21,6 +35,48 @@
expect(contrib.to_asciibib).to eq "address.formatted_address:: formatted address\n"
end
end

context "render address" do
let(:contrib) do
RelatonBib::Address.new(
street: ["street1", "street2"],
city: "city",
state: "state",
country: "country",
postcode: "postcode",
)
end

it "as XML" do
xml = Nokogiri::XML::Builder.new { |b| contrib.to_xml(b) }.doc.root
street = xml.xpath("/address/street").map(&:text)
expect(street).to eq %w[street1 street2]
expect(xml.xpath("/address/city").text).to eq "city"
expect(xml.xpath("/address/state").text).to eq "state"
expect(xml.xpath("/address/country").text).to eq "country"
expect(xml.xpath("/address/postcode").text).to eq "postcode"
end

it "as Hash" do
hash = contrib.to_hash
expect(hash["address"]["street"]).to eq %w[street1 street2]
expect(hash["address"]["city"]).to eq "city"
expect(hash["address"]["state"]).to eq "state"
expect(hash["address"]["country"]).to eq "country"
expect(hash["address"]["postcode"]).to eq "postcode"
end

it "as AsciiBib" do
expect(contrib.to_asciibib).to eq <<~ASCIIBIB
address.street:: street1
address.street:: street2
address.city:: city
address.state:: state
address.country:: country
address.postcode:: postcode
ASCIIBIB
end
end
end

describe RelatonBib::Affiliation do
Expand All @@ -32,6 +88,19 @@
described_class.new(organization: org, name: name, description: description)
end

context "==" do
it "same content" do
other = described_class.new(organization: org, name: name, description: [desc])
expect(subject).to eq other
end

it "different content" do
name = RelatonBib::LocalizedString.new("Other", "en")
other = described_class.new(organization: org, name: name, description: [desc])
expect(subject).not_to eq other
end
end

context "render affiliation" do
context "with all fields" do
it "as XML" do
Expand Down
12 changes: 12 additions & 0 deletions spec/relaton_bib/formatted_string_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,18 @@
XML
end

context "==" do
it "same content" do
other = RelatonBib::FormattedString.new content: subject.content, language: "en", script: "Latn", format: "text/html"
expect(subject).to eq other
end

it "different content" do
other = RelatonBib::FormattedString.new content: "other", language: "en", script: "Latn", format: "text/html"
expect(subject).not_to eq other
end
end

context "escape" do
it "&" do
xml = Nokogiri::XML::Builder.new do |b|
Expand Down
131 changes: 131 additions & 0 deletions spec/relaton_bib/full_name_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
describe RelatonBib::FullName do
context "using name parts" do
subject do
described_class.new(
surname: RelatonBib::LocalizedString.new("Doe"),
abbreviation: RelatonBib::LocalizedString.new("DJ"),
forename: [RelatonBib::Forename.new(content: "John", initial: "J")],
initials: RelatonBib::LocalizedString.new("J.D."),
addition: [RelatonBib::LocalizedString.new("Jr.")],
prefix: [RelatonBib::LocalizedString.new("Dr.")],
)
end

context "==" do
it "same content" do
other = described_class.new(
surname: RelatonBib::LocalizedString.new("Doe"),
abbreviation: RelatonBib::LocalizedString.new("DJ"),
forename: [RelatonBib::Forename.new(content: "John", initial: "J")],
initials: RelatonBib::LocalizedString.new("J.D."),
addition: [RelatonBib::LocalizedString.new("Jr.")],
prefix: [RelatonBib::LocalizedString.new("Dr.")],
)
expect(subject).to eq other
end

it "different content" do
other = described_class.new(
surname: RelatonBib::LocalizedString.new("Doe"),
abbreviation: RelatonBib::LocalizedString.new("DJ"),
forename: [RelatonBib::Forename.new(content: "John", initial: "J")],
initials: RelatonBib::LocalizedString.new("J.D."),
prefix: [RelatonBib::LocalizedString.new("Dr.")],
)
expect(subject).not_to eq other
end
end

it "to_xml" do
builder = Nokogiri::XML::Builder.new
subject.to_xml(builder: builder)
expect(builder.to_xml).to be_equivalent_to <<~XML
<name>
<abbreviation>DJ</abbreviation>
<prefix>Dr.</prefix>
<forename initial="J">John</forename>
<formatted-initials>J.D.</formatted-initials>
<surname>Doe</surname>
<addition>Jr.</addition>
</name>
XML
end

it "to_hash" do
expect(subject.to_hash).to eq(
"abbreviation" => { "content" => "DJ" },
"given" => {
"forename" => [{ "content" => "John", "initial" => "J" }],
"formatted_initials" => { "content" => "J.D." },
},
"surname" => { "content" => "Doe" },
"addition" => [{ "content" => "Jr." }],
"prefix" => [{ "content" => "Dr." }],
)
end

it "to_asciibib" do
expect(subject.to_asciibib("name")).to eq <<~ASCIIBIB
name.name.abbreviation:: DJ
name.given.forename:: John
name.given.forename.initial:: J
name.given.formatted-initials:: J.D.
name.name.surname:: Doe
name.name.addition:: Jr.
name.name.prefix:: Dr.
ASCIIBIB
end
end

context "using completename" do
subject do
described_class.new(
completename: RelatonBib::LocalizedString.new("John Doe"),
)
end

context "==" do
it "same content" do
other = described_class.new(
completename: RelatonBib::LocalizedString.new("John Doe"),
)
expect(subject).to eq other
end

it "different content" do
other = described_class.new(
completename: RelatonBib::LocalizedString.new("Jane Doe"),
)
expect(subject).not_to eq other
end
end

it "to_xml" do
builder = Nokogiri::XML::Builder.new
subject.to_xml(builder: builder, lang: "en")
expect(builder.to_xml).to be_equivalent_to <<~XML
<name>
<completename>John Doe</completename>
</name>
XML
end

it "to_hash" do
expect(subject.to_hash).to eq(
"completename" => { "content" => "John Doe" },
)
end

it "to_asciibib" do
expect(subject.to_asciibib("name")).to eq <<~ASCIIBIB
name.name.completename:: John Doe
ASCIIBIB
end
end

it "raise ArgumentError" do
expect do
described_class.new
end.to raise_error ArgumentError, "Should be given :surname or :completename"
end
end
Loading

0 comments on commit 1ee6c62

Please sign in to comment.