test/unit/definition_test.rb in roxml-3.3.1 vs test/unit/definition_test.rb in roxml-4.0.0

- old
+ new

@@ -1,8 +1,9 @@ require_relative './../test_helper' +require 'minitest/autorun' -class TestDefinition < ActiveSupport::TestCase +class TestDefinition < Minitest::Test def assert_hash(opts, kvp) assert opts.hash? assert !opts.array? assert_equal kvp, {opts.hash.key.sought_type => opts.hash.key.name, opts.hash.value.sought_type => opts.hash.value.name} @@ -32,21 +33,19 @@ assert ROXML::Definition.new(:author, :required => true).required? assert !ROXML::Definition.new(:author, :required => false).required? end def test_required_conflicts_with_else - assert_raise ArgumentError do + assert_raises ArgumentError do ROXML::Definition.new(:author, :required => true, :else => 'Johnny') end - assert_nothing_raised do - ROXML::Definition.new(:author, :required => false, :else => 'Johnny') - end + ROXML::Definition.new(:author, :required => false, :else => 'Johnny') end def test_hash_of_attrs opts = ROXML::Definition.new(:attributes, :as => {:key => '@name', :value => '@value'}) - assert_hash(opts, :attr => 'name', :attr => 'value') + assert_hash(opts, :attr => 'value') end def test_hash_with_attr_key_and_text_val opts = ROXML::Definition.new(:attributes, :as => {:key => '@name', :value => :value}) @@ -54,11 +53,11 @@ end def test_hash_with_string_class_for_type opts = ROXML::Definition.new(:attributes, :as => {:key => 'name', :value => 'value'}) - assert_hash(opts, :text => 'name', :text => 'value') + assert_hash(opts, :text => 'value') end def test_hash_with_attr_key_and_content_val opts = ROXML::Definition.new(:attributes, :as => {:key => '@name', :value => :content}) @@ -66,11 +65,11 @@ end def test_hash_with_options opts = ROXML::Definition.new(:definitions, :as => {:key => '@dt', :value => '@dd'}, :in => :definitions, :from => 'definition') - assert_hash(opts, :attr => 'dt', :attr => 'dd') + assert_hash(opts, :attr => 'dd') assert_equal 'definition', opts.hash.wrapper end def test_no_block_shorthand_means_no_block assert ROXML::Definition.new(:count).blocks.empty? @@ -94,18 +93,10 @@ opts = ROXML::Definition.new(:count, :from => "@COUNT") assert_equal "COUNT", opts.name assert_equal :attr, opts.sought_type end - def test_multiple_shorthands_raises - assert_raise ArgumentError do - assert_deprecated do - ROXML::Definition.new(:count, :as => [Float, Integer]) - end - end - end - def test_stacked_blocks assert_equal 2, ROXML::Definition.new(:count, :as => Integer) {|val| val.to_i }.blocks.size assert_equal 2, ROXML::Definition.new(:count, :as => Float) {|val| val.object_id }.blocks.size end @@ -113,39 +104,37 @@ assert_equal true, ROXML::Definition.new(:floatvalue, :as => :bool).blocks.first.call("1") assert_equal [true, false, nil], ROXML::Definition.new(:floatvalue, :as => :bool).blocks.first.call(["TrUe", "0", "328"]) end def test_block_shorthand_supports_integer - assert_equal nil, ROXML::Definition.new(:floatvalue, :as => Integer).blocks.first.call(" ") + assert_nil ROXML::Definition.new(:floatvalue, :as => Integer).blocks.first.call(" ") assert_equal 792, ROXML::Definition.new(:floatvalue, :as => Integer).blocks.first.call("792") - assert_raise ArgumentError do - ROXML::Definition.new(:floatvalue, :as => Integer).blocks.first.call("792.13") - end + assert_equal 792, ROXML::Definition.new(:floatvalue, :as => Integer).blocks.first.call("792.13") assert_equal [792, 12, 328], ROXML::Definition.new(:floatvalue, :as => Integer).blocks.first.call(["792", "12", "328"]) end def test_block_shorthand_supports_float - assert_equal nil, ROXML::Definition.new(:floatvalue, :as => Float).blocks.first.call(" ") + assert_nil ROXML::Definition.new(:floatvalue, :as => Float).blocks.first.call(" ") assert_equal 792.13, ROXML::Definition.new(:floatvalue, :as => Float).blocks.first.call("792.13") assert_equal 240.0, ROXML::Definition.new(:floatvalue, :as => Float).blocks.first.call("240") assert_equal [792.13, 240.0, 3.14], ROXML::Definition.new(:floatvalue, :as => Float).blocks.first.call(["792.13", "240", "3.14"]) end def test_block_shorthand_supports_time - assert_equal nil, ROXML::Definition.new(:floatvalue, :as => Time).blocks.first.call(" ") + assert_nil ROXML::Definition.new(:floatvalue, :as => Time).blocks.first.call(" ") assert_equal 31, ROXML::Definition.new(:datevalue, :as => Time).blocks.first.call("12:31am").min assert_equal [31, 0, 59], ROXML::Definition.new(:datevalue, :as => Time).blocks.first.call(["12:31am", "3:00pm", "11:59pm"]).map(&:min) end def test_block_shorthand_supports_date - assert_equal nil, ROXML::Definition.new(:floatvalue, :as => Date).blocks.first.call(" ") + assert_nil ROXML::Definition.new(:floatvalue, :as => Date).blocks.first.call(" ") assert_equal "1970-09-03", ROXML::Definition.new(:datevalue, :as => Date).blocks.first.call("September 3rd, 1970").to_s assert_equal ["1970-09-03", "1776-07-04"], ROXML::Definition.new(:datevalue, :as => Date).blocks.first.call(["September 3rd, 1970", "1776-07-04"]).map(&:to_s) end def test_block_shorthand_supports_datetime - assert_equal nil, ROXML::Definition.new(:floatvalue, :as => DateTime).blocks.first.call(" ") + assert_nil ROXML::Definition.new(:floatvalue, :as => DateTime).blocks.first.call(" ") assert_equal "1970-09-03T12:05:00+00:00", ROXML::Definition.new(:datevalue, :as => DateTime).blocks.first.call("12:05pm, September 3rd, 1970").to_s assert_equal ["1970-09-03T12:05:00+00:00", "1700-05-22T15:00:00+00:00"], ROXML::Definition.new(:datevalue, :as => DateTime).blocks.first.call(["12:05pm, September 3rd, 1970", "3:00pm, May 22, 1700"]).map(&:to_s) end def test_name_explicit_indicates_whether_from_option_is_present @@ -169,15 +158,25 @@ def test_as_supports_generic_roxml_types_in_arrays assert_equal RoxmlObject, ROXML::Definition.new(:types, :as => [RoxmlObject]).sought_type end - def test_default_works + def test_default_for_missing_element_works opts = ROXML::Definition.new(:missing, :else => true) assert_equal true, opts.to_ref(RoxmlObject.new).value_in(ROXML::XML.parse_string('<xml></xml>')) end + def test_default_for_empty_element_works + opts = ROXML::Definition.new(:age, :else => 25) + assert_equal 25, opts.to_ref(RoxmlObject.new).value_in(ROXML::XML.parse_string('<xml><age /></xml>')) + end + + def test_empty_element_works_without_default + opts = ROXML::Definition.new(:age) + assert_equal '', opts.to_ref(RoxmlObject.new).value_in(ROXML::XML.parse_string('<xml><age /></xml>')) + end + def test_default_works_for_arrays opts = ROXML::Definition.new(:missing, :as => []) assert_equal [], opts.to_ref(RoxmlObject.new).value_in(ROXML::XML.parse_string('<xml></xml>')) end @@ -224,12 +223,12 @@ class RoxmlObject include ROXML end -class HashDefinitionTest < ActiveSupport::TestCase +class HashDefinitionTest < Minitest::Test def test_content_detected_as_from opts = ROXML::Definition.new(:hash, :as => {:key => :content, :value => :name}) assert_equal '.', opts.hash.key.name assert_equal :text, opts.hash.key.sought_type end -end \ No newline at end of file +end