Skip to content

Commit

Permalink
Merge pull request #236 from mvz/reorganize-spec
Browse files Browse the repository at this point in the history
Reorganize some specs
  • Loading branch information
mvz committed May 12, 2024
2 parents a1f9831 + 80655c0 commit af2dd53
Show file tree
Hide file tree
Showing 13 changed files with 703 additions and 573 deletions.
5 changes: 5 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ Naming/PredicateName:
- 'has_many'
- 'has_xml_content'

# regression specs do not describe classes
RSpec/DescribeClass:
Exclude:
- 'spec/regressions/*.rb'

# Disabled because RSpec/SpecFilePathFormat is automatically enabled as a new cop
RSpec/FilePath:
Enabled: false
Expand Down
62 changes: 62 additions & 0 deletions spec/features/inline_element_attributes_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# frozen_string_literal: true

require "spec_helper"

RSpec.describe "specifying element attributes inline" do
let(:currentweather_klass) do
Class.new do
include HappyMapper

tag "ob"
namespace "aws"
element :temperature, Integer, tag: "temp"
element :feels_like, Integer, tag: "feels-like"
element :current_condition, String, tag: "current-condition",
attributes: { icon: String }
end
end

let(:atomfeed_klass) do
Class.new do
include HappyMapper
tag "feed"

attribute :xmlns, String, single: true
element :id, String, single: true
element :title, String, single: true
element :updated, DateTime, single: true
element :link, String, single: false, attributes: {
rel: String,
type: String,
href: String
}
end
end

it "adds the values of the attributes to the element" do
items = currentweather_klass.parse(fixture_file("current_weather.xml"))
first = items[0]

aggregate_failures do
expect(first.temperature).to eq(51)
expect(first.feels_like).to eq(51)
expect(first.current_condition).to eq("Sunny")
expect(first.current_condition.icon).to eq("http://deskwx.weatherbug.com/images/Forecast/icons/cond007.gif")
end
end

it "parses xml when the element with embedded attributes is not present in the xml" do
expect do
currentweather_klass.parse(fixture_file("current_weather_missing_elements.xml"))
end.not_to raise_error
end

it "parses xml with attributes of elements that aren't :single => true" do
feed = atomfeed_klass.parse(fixture_file("atom.xml"))

aggregate_failures do
expect(feed.link.first.href).to eq("http://www.example.com")
expect(feed.link.last.href).to eq("http://www.example.com/tv_shows.atom")
end
end
end
27 changes: 27 additions & 0 deletions spec/features/optional_attributes_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# frozen_string_literal: true

RSpec.describe "Parsing optional attributes" do
before do
klass = Class.new do
include HappyMapper
tag "address"

attribute :street, String
end
stub_const "OptionalAttribute", klass
end

let(:parsed_result) { OptionalAttribute.parse(fixture_file("optional_attributes.xml")) }

it "parses an empty String as empty" do
expect(parsed_result[0].street).to eq("")
end

it "parses a String with value" do
expect(parsed_result[1].street).to eq("Milchstrasse")
end

it "parses an element with no value for the attribute" do
expect(parsed_result[2].street).to be_nil
end
end
Loading

0 comments on commit af2dd53

Please sign in to comment.