Skip to content

Commit

Permalink
Extract feature specs for inline element attribute specification
Browse files Browse the repository at this point in the history
  • Loading branch information
mvz committed May 12, 2024
1 parent 949d1e2 commit 80655c0
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 55 deletions.
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
55 changes: 0 additions & 55 deletions spec/happymapper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,6 @@

require "spec_helper"

module Atom
class Feed
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
}
# has_many :entries, Entry # nothing interesting in the entries
end
end

class Country
include HappyMapper

Expand Down Expand Up @@ -80,16 +62,6 @@ class Radar
has_many :places, Place, tag: :place
end

class CurrentWeather
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

module QuarterTest
class Quarter
include HappyMapper
Expand Down Expand Up @@ -487,33 +459,6 @@ class Thing
expect(address.class).to eq(Address)
end

it "parses xml that has attributes of elements" do
items = CurrentWeather.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 with attributes of elements that aren't :single => true" do
feed = Atom::Feed.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

it "parses xml with optional elements with embedded attributes" do
expect do
CurrentWeather.parse(fixture_file("current_weather_missing_elements.xml"))
end.not_to raise_error
end

it "returns nil rather than empty array for absent values when :single => true" do
address = Address.parse('<?xml version="1.0" encoding="UTF-8"?><foo/>', single: true)
expect(address).to be_nil
Expand Down

0 comments on commit 80655c0

Please sign in to comment.