forked from jnunemaker/happymapper
-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #236 from mvz/reorganize-spec
Reorganize some specs
- Loading branch information
Showing
13 changed files
with
703 additions
and
573 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.