1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
|
require_relative './spec_helper'
describe ROXML do
describe "::VERSION" do
it "should be equal to the VERSION file contents" do
expect(ROXML::VERSION).to eq(File.read('VERSION'))
end
end
describe "#from_xml" do
shared_examples_for "from_xml call" do
it "should fetch values" do
book = BookWithContributors.from_xml(@path)
expect(book.title).to eq("Programming Ruby - 2nd Edition")
expect(book.contributors.map(&:name)).to eq(["David Thomas","Andrew Hunt","Chad Fowler"])
end
end
context "called with PathName" do
before do
@path = Pathname.new(fixture_path(:book_with_contributors))
end
it_should_behave_like "from_xml call"
end
context "called with File" do
before do
@path = File.new(fixture_path(:book_with_contributors))
end
it_should_behave_like "from_xml call"
end
context "called with URI" do
before do
require 'uri'
@path = URI.parse("file://#{File.expand_path(File.expand_path(fixture_path(:book_with_contributors)))}")
end
it_should_behave_like "from_xml call"
end
end
end
describe ROXML, "#xml" do
class DescriptionReadonly
include ROXML
xml_reader :writable, :from => :content
xml_reader :readonly, :from => :content, :frozen => true
end
class Contributor
include ROXML
xml_reader :role, :from => :attr
xml_reader :name
end
class BookWithContributions
include ROXML
xml_name :book
xml_reader :isbn, :from => :attr
xml_reader :title
xml_reader :description, :as => DescriptionReadonly
xml_reader :contributions, :as => [Contributor], :from => 'contributor', :in => "contributions"
end
class BookWithContributionsReadonly
include ROXML
xml_name :book
xml_reader :isbn, :from => :attr, :frozen => true
xml_reader :title, :frozen => true
xml_reader :description, :as => DescriptionReadonly, :frozen => true
xml_reader :contributions, :as => [Contributor], :from => 'contributor', :in => "contributions", :frozen => true
end
before do
@writable = BookWithContributions.from_xml(fixture(:book_with_contributions))
@readonly = BookWithContributionsReadonly.from_xml(fixture(:book_with_contributions))
end
it "should raise on duplicate accessor name" do
expect do
Class.new do
include ROXML
xml_reader :id
xml_accessor :id
end
end.to raise_error(RuntimeError)
end
class OctalInteger
def self.from_xml(val)
new(Integer(val.content))
end
def initialize(value)
@val = value
end
def ==(other)
@val == other
end
def to_xml
sprintf("%#o", @val)
end
end
describe "overriding output" do
class BookWithOctalPages
include ROXML
xml_accessor :pages_with_as, :as => Integer, :to_xml => proc {|val| sprintf("%#o", val) }, :required => true
xml_accessor :pages_with_type, :as => OctalInteger, :required => true
end
# to_xml_test :book_with_octal_pages
describe "with :to_xml option" do
it "should output with to_xml filtering"
end
describe "with #to_xml on the object" do
it "should output with to_xml filtering"
end
end
describe "overriding input" do
before do
@book_with_octal_pages_xml = %{
<book>
<pages>0357</pages>
</book>
}
@expected_pages = 239
end
describe "with #from_xml defined on the object" do
class BookWithOctalPagesType
include ROXML
xml_accessor :pages, :as => OctalInteger, :required => true
end
it "should apply filtering on input" do
book = BookWithOctalPagesType.from_xml(@book_with_octal_pages_xml)
expect(book.pages).to eq(@expected_pages)
end
end
end
describe "attribute reference" do
before do
@frozen = @readonly.isbn
@unfrozen = @writable.isbn
end
it_should_behave_like "freezable xml reference"
end
describe "text reference" do
before do
@frozen = @readonly.title
@unfrozen = @writable.title
end
it_should_behave_like "freezable xml reference"
end
describe "object reference" do
before do
@frozen = @readonly.description
@unfrozen = @writable.description
end
it_should_behave_like "freezable xml reference"
describe "indirect reference via an object" do
it "does not inherit the frozen status from its parent" do
expect(@frozen.writable.frozen?).to be_falsey
expect(@frozen.readonly.frozen?).to be_truthy
expect(@unfrozen.writable.frozen?).to be_falsey
expect(@unfrozen.readonly.frozen?).to be_truthy
end
end
end
describe "array reference" do
before do
@frozen = @readonly.contributions
@unfrozen = @writable.contributions
end
it_should_behave_like "freezable xml reference"
it "should apply :frozen to the constituent elements" do
expect(@frozen.all?(&:frozen?)).to be_truthy
expect(@unfrozen.any?(&:frozen?)).to be_falsey
end
context "no elements are present in root, no :in is specified" do
class BookWithContributors
include ROXML
xml_name :book
xml_reader :isbn, :from => :attr
xml_reader :title
xml_reader :description
xml_reader :contributors, :as => [Contributor]
end
it "should look for elements :in the plural of name" do
book = BookWithContributors.from_xml(%{
<book isbn="0974514055">
<contributors>
<contributor role="author"><name>David Thomas</name></contributor>
<contributor role="supporting author"><name>Andrew Hunt</name></contributor>
<contributor role="supporting author"><name>Chad Fowler</name></contributor>
</contributors>
</book>
})
expect(book.contributors.map(&:name).sort).to eq(["David Thomas","Andrew Hunt","Chad Fowler"].sort)
end
end
end
describe "hash reference" do
class DictionaryOfGuardedNames
include ROXML
xml_name :dictionary
xml_reader :definitions, :as => {:key => :name,
:value => :content}, :in => :definitions
end
class DictionaryOfGuardedNamesReadonly
include ROXML
xml_name :dictionary
xml_reader :definitions, :as => {:key => :name,
:value => :content}, :in => :definitions, :frozen => true
end
before do
@frozen = DictionaryOfGuardedNamesReadonly.from_xml(fixture(:dictionary_of_guarded_names)).definitions
@unfrozen = DictionaryOfGuardedNames.from_xml(fixture(:dictionary_of_guarded_names)).definitions
end
it_should_behave_like "freezable xml reference"
it "should have frozen keys, as with all hashes" do
expect(@frozen.keys.all?(&:frozen?)).to be_truthy
expect(@unfrozen.keys.all?(&:frozen?)).to be_truthy
end
it "should apply :frozen to the constituent values" do
expect(@frozen.values.all?(&:frozen?)).to be_truthy
expect(@unfrozen.values.any?(&:frozen?)).to be_falsey
end
end
end
describe ROXML, "inheritance" do
class Book
include ROXML
xml_accessor :isbn, :from => '@ISBN'
xml_reader :title
xml_reader :description, :cdata => true
xml_reader :author
xml_accessor :pages, :from => 'pagecount', :as => Integer
end
class Measurement
include ROXML
xml_reader :units, :from => :attr
xml_reader :value, :from => :content, :as => Float
def initialize(value = 0, units = 'pixels')
@value = Float(value)
@units = units.to_s
normalize_hundredths
end
def to_s
"#{value} #{units}"
end
def ==(other)
other.units == @units && other.value == @value
end
private
def after_parse
normalize_hundredths
end
def normalize_hundredths
if @units.starts_with? 'hundredths-'
@value /= 100
@units = @units.split('hundredths-')[1]
end
end
end
class InheritedBookWithDepth < Book
xml_reader :depth, :as => Measurement
end
before do
@book_xml = %{
<book ISBN="0201710897">
<title>The PickAxe</title>
<description><![CDATA[Probably the best Ruby book out there]]></description>
<author>David Thomas, Andrew Hunt, Dave Thomas</author>
<depth units="hundredths-meters">1130</depth>
<publisher>Pragmattic Programmers</publisher>
</book>
}
@parent = Book.from_xml(@book_xml)
@child = InheritedBookWithDepth.from_xml(@book_xml)
end
describe "parent" do
it "should include its attributes" do
expect(@child.isbn).to eq("0201710897")
expect(@child.title).to eq("The PickAxe")
expect(@child.description).to eq("Probably the best Ruby book out there")
expect(@child.author).to eq('David Thomas, Andrew Hunt, Dave Thomas')
expect(@child.pages).to eq(nil)
end
it "should not include its child's attributes" do
expect(@parent).to_not respond_to(:depth)
end
end
describe "child" do
it "should include its parent's attributes" do
expect(@child.isbn).to eq(@parent.isbn)
expect(@child.title).to eq(@parent.title)
expect(@child.description).to eq(@parent.description)
expect(@child.author).to eq(@parent.author)
expect(@child.pages).to eq(@parent.pages)
end
it "should include its attributes" do
expect(@child.depth.to_s).to eq('11.3 meters')
end
it "should include parent's attributes added after declaration" do
Book.class_eval do
xml_reader :publisher, :required => true
end
book = InheritedBookWithDepth.from_xml(@book_xml)
expect(book.publisher).to eq("Pragmattic Programmers")
end
end
end
|