test/structure_test.rb in structure-0.21.0 vs test/structure_test.rb in structure-0.22.0
- old
+ new
@@ -1,106 +1,98 @@
-require 'bundler/setup'
-require 'test/unit'
+require 'minitest/autorun'
+
begin
require 'pry'
rescue LoadError
end
-require File.expand_path('../../lib/structure', __FILE__)
+require 'structure'
+require 'structure/json'
+
class Person < Structure
- key :name
- key :location, Location
- many :friends
+ key :name
+ key :friends, Array, []
+ key :city, City
end
-class Location < Structure
- key :lon, Float
- key :lat, Float
+class City < Structure
+ key :name
end
-class TestStructure < Test::Unit::TestCase
- def test_double
- double = Structure::Double.new(:Foo)
- assert_equal 'Foo', "#{double}"
- assert_raise(NameError) { double.class }
- ::Kernel.const_set(:Foo, 1)
- assert_kind_of Fixnum, double
- end
+class TestStructure < MiniTest::Unit::TestCase
+ def test_lazy_evaluation
+ wrapper = Structure::Wrapper.new(:Foo)
+ assert_raises(NameError) { wrapper.bar }
+ assert_raises(NameError) { wrapper.unwrap.bar }
- def test_anonymous
- hsh = { 'FirstName' => 'John', 'LastName' => 'Doe' }
- str = Structure.new(hsh)
- assert_equal hsh['FirstName'], str.first_name
- assert_equal hsh['LastName'], str.last_name
- assert_raise(NoMethodError) { str.FirstName }
+ klass = Class.new { def self.bar; end }
+ ::Kernel.const_set(:Foo, klass)
+ assert_respond_to wrapper, :bar
+ assert_equal Foo, wrapper.unwrap
end
- def test_anonymous_recursion
- hsh = { 'Name' => 'John',
- 'Address' => { 'City' => 'Oslo' },
- 'Friends' => [{ 'Name' => 'Jane' }]
- }
- str = Structure.new(hsh)
- assert_equal hsh['Address']['City'], str.address.city
- assert_equal hsh['Friends'].first['Name'], str.friends.first.name
- end
-
- def test_enumeration
- assert_respond_to Person.new, :map
- end
-
def test_accessors
assert_respond_to Person.new, :name
assert_respond_to Person.new, :name=
end
def test_key_errors
- assert_raise(NameError) { Person.key :class }
- assert_raise(TypeError) { Person.key :foo, String, 1 }
+ assert_raises(NameError) { Person.key :class }
+ assert_raises(TypeError) { Person.key :foo, Hash, 1 }
end
def test_key_defaults
assert_equal [], Person.new.friends
end
- def test_typechecking
- loc = Location.new
- loc.lon = "1"
- assert_kind_of Float, loc.lon
+ def test_typecasting
+ person = Person.new
+ person.name = 123
+ assert_kind_of String, person.name
- loc.lon = nil
- assert_nil loc.lon
+ person.name = nil
+ assert_nil person.name
end
- def test_array_type
+ def test_many_relationship
person = Person.new
assert_equal [], person.friends
person.friends << Person.new
assert_equal 1, person.friends.size
assert_equal 0, person.friends.first.friends.size
end
- # def test_to_hash
- # person = Person.new(:name => 'John')
- # person.friends << Person.new(:name => 'Jane')
- # assert_equal 'John', person.to_hash[:name]
- # assert_equal 'Jane', person.to_hash[:friends].first[:name]
- # end
+ def test_new
+ person = Person.new(:name => 'John')
+ assert_equal 'John', person.name
+ other = Person.new(:name => 'Jane', :friends => [person])
+ assert_equal 'John', other.friends.first.name
+ end
+
+ def test_to_hash
+ person = Person.new(:name => 'John')
+ person.friends << Person.new(:name => 'Jane')
+ hash = person.to_hash
+
+ assert_equal 'John', hash[:name]
+ assert_equal 'Jane', hash[:friends].first[:name]
+ end
+
def test_json
- person = Person.new
- person.friends << Person.new
+ Person.send :include, Structure::JSON
+
+ person = Person.new(:name => 'John')
+ person.friends << Person.new(:name => 'Jane')
json = person.to_json
assert_kind_of Person, JSON.parse(json)
assert_kind_of Person, JSON.parse(json).friends.first
-
assert_equal false, person.respond_to?(:as_json)
require 'active_support/ordered_hash'
require 'active_support/json'
- require 'structure/ext/active_support'
-
+ load 'structure/json.rb'
assert_equal true, person.as_json(:only => :name).has_key?(:name)
assert_equal false, person.as_json(:except => :name).has_key?(:name)
end
end