Sha256: 934a7806d81393482b98a4a2f3aa511d167f238a85af485270f06ca1364a0118

Contents?: true

Size: 1.94 KB

Versions: 11

Compression:

Stored size: 1.94 KB

Contents

require File.join(File.dirname(__FILE__), '..', 'test_helper')

class MeasurementWithXmlConstruct
  include ROXML

  xml_reader :units, :attr
  xml_reader :value, :content

  xml_construct_without_deprecation :value, :units

  def initialize(value, units = 'pixels')
    @value = Float(value)
    @units = units.to_s
    if @units.starts_with? 'hundredths-'
      @value /= 100
      @units = @units.split('hundredths-')[1]
    end
  end

  def ==(other)
    other.units == @units && other.value == @value
  end
end

class BookWithDepthWithXmlConstruct
  include ROXML

  xml_reader :isbn, :attr => 'ISBN'
  xml_reader :title
  xml_reader :description, :as => :cdata
  xml_reader :author
  xml_reader :depth, MeasurementWithXmlConstruct
end

class InheritedBookWithDepthWithXmlConstruct < Book
  xml_reader :depth, MeasurementWithXmlConstruct
end

class TestXMLConstruct < Test::Unit::TestCase
  def test_is_deprecated
    assert_deprecated do
      MeasurementWithXmlConstruct.xml_construction_args
    end
  end

  def test_initialize_is_run
    m = MeasurementWithXmlConstruct.from_xml('<measurement units="hundredths-meters">1130</measurement>')
    assert_equal 11.3, m.value
    assert_equal 'meters', m.units
  end

  def test_initialize_is_run_for_nested_type
    b = BookWithDepthWithXmlConstruct.from_xml(fixture(:book_with_depth))
    assert_equal Measurement.new(11.3, 'meters'), b.depth
  end

  def test_initialize_is_run_for_nested_type_with_inheritance
    b = InheritedBookWithDepthWithXmlConstruct.from_xml(fixture(:book_with_depth))
    assert_equal Measurement.new(11.3, 'meters'), b.depth
  end

  def test_xml_name_uses_accessor_not_name
    assert_nothing_raised do
      Class.new do
        include ROXML

        xml_reader :bar, :attr => 'Foo'
        xml_reader :foo, :text => 'Foo'
        xml_reader :baz, :attr => 'Bar'

        xml_construct_without_deprecation :baz, :bar, :foo
        def initialize(baz, bar, foo)
        end
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 2 rubygems

Version Path
Empact-roxml-2.3.0 test/unit/xml_construct_test.rb
Empact-roxml-2.3.1 test/unit/xml_construct_test.rb
Empact-roxml-2.4.0 test/unit/xml_construct_test.rb
Empact-roxml-2.4.1 test/unit/xml_construct_test.rb
Empact-roxml-2.4.2 test/unit/xml_construct_test.rb
Empact-roxml-2.4.3 test/unit/xml_construct_test.rb
roxml-2.4.0 test/unit/xml_construct_test.rb
roxml-2.3.2 test/unit/xml_construct_test.rb
roxml-2.4.3 test/unit/xml_construct_test.rb
roxml-2.4.1 test/unit/xml_construct_test.rb
roxml-2.4.2 test/unit/xml_construct_test.rb