test/structure_test.rb in structure-0.22.1 vs test/structure_test.rb in structure-0.23.0
- old
+ new
@@ -1,109 +1,161 @@
-require 'minitest/autorun'
+require File.expand_path('../helper.rb', __FILE__)
-begin
- require 'pry'
-rescue LoadError
-end
+# Most tests below are borrowed from RubySpec.
+class TestStructure < MiniTest::Unit::TestCase
+ def setup
+ @person = Structure.new(:name => 'John')
+ end
-require 'structure'
-require 'structure/json'
+ def test_delete_field
+ @person.delete_field(:name)
+ assert_nil @person.send(:table)[:name]
+ refute_respond_to(@person, :name)
+ refute_respond_to(@person, :name=)
+ end
-class Person < Structure
- key :name
- key :friends, Array, []
- key :city, City
-end
+ def test_element_reference
+ assert_raises(NoMethodError) { @person[1] }
+ end
-class City < Structure
- key :name
-end
+ def test_element_set
+ assert_raises(NoMethodError) { @person[1] = 2 }
+ 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_equal_value
+ refute @person == 'foo'
+ assert @person == @person
+ assert @person == Structure.new(:name => 'John')
+ assert @person == Class.new(Structure).new(:name => 'John')
+ refute @person == Structure.new(:name => 'Johnny')
+ refute @person == Structure.new(:name => 'John', :age => 20)
+ end
- klass = Class.new { def self.bar; end }
- ::Kernel.const_set(:Foo, klass)
- assert_respond_to wrapper, :bar
- assert_equal Foo, wrapper.unwrap
+ def test_frozen
+ @person.freeze
+
+ assert_equal 'John', @person.name
+ assert_raises(RuntimeError) { @person.age = 42 }
+ assert_raises(RuntimeError) { @person.state = :new }
+
+ c = @person.clone
+ assert_equal 'John', c.name
+ assert_raises(RuntimeError) { c.age = 42 }
+ assert_raises(RuntimeError) { c.state = :new }
+
+ d = @person.dup
+ assert_equal 'John', d.name
+ d.age = 42
+ assert_equal 42, d.age
end
- def test_accessors
- assert_respond_to Person.new, :name
- assert_respond_to Person.new, :name=
+ def test_initialize_copy
+ d = @person.dup
+ d.name = 'Jane'
+ assert_equal 'Jane', d.name
+ assert_equal 'John', @person.name
+
+ @person.friends = ['Joe']
+ d = @person.dup
+ d.friends = ['Jim']
+ assert_equal ['Jim'], d.friends
+ assert_equal ['Joe'], @person.friends
end
- def test_key_errors
- assert_raises(NameError) { Person.key :class }
- assert_raises(TypeError) { Person.key :foo, Hash, 1 }
+ def test_json
+ friend = Structure.new(:name => 'Jane')
+ @person.friend = friend
+ @person.cities = ['Zurich']
+ json = '{"json_class":"Structure",
+ "name":"John",
+ "friend":{"name":"Jane"},
+ "cities":["Zurich"]}'.gsub(/\s+/, '')
+ assert_equal @person, JSON.parse(json)
+ assert_equal friend, JSON.parse(json).friend
+ assert_equal 'Zurich', JSON.parse(json).cities.first
+
+ refute_respond_to @person, :as_json
+ require 'active_support/ordered_hash'
+ require 'active_support/json'
+ load 'structure.rb'
+ assert @person.as_json(:only => :name).has_key?(:name)
+ refute @person.as_json(:except => :name).has_key?(:name)
end
- def test_key_defaults
- assert_equal [], Person.new.friends
+ def test_marshaling
+ assert_equal({ :name => 'John' }, @person.marshal_dump)
+ @person.marshal_load(:age => 20, :name => 'Jane')
+ assert_equal 20, @person.age
+ assert_equal 'Jane', @person.name
end
- def test_typecasting
- person = Person.new
- person.name = 123
- assert_kind_of String, person.name
+ def test_method_missing
+ @person.test = 'test'
+ assert_respond_to @person, :test
+ assert_respond_to @person, :test=
+ assert_equal 'test', @person.test
+ assert_equal 'test', @person.send(:table)[:test]
+ @person.test = 'changed'
+ assert_equal 'changed', @person.test
- person.name = nil
- assert_nil person.name
- end
- def test_many_relationship
- person = Person.new
- assert_equal [], person.friends
+ @person.send(:table)[:age] = 20
+ assert_equal 20, @person.age
- person.friends << Person.new
- assert_equal 1, person.friends.size
- assert_equal 0, person.friends.first.friends.size
+ assert_raises(NoMethodError) { @person.gender(1) }
+ assert_nil @person.gender
+
+ @person.freeze
+ assert_raises(RuntimeError) { @person.gender = 'male' }
end
def test_new
- person = Person.new(:name => 'John')
+ person = Structure.new(:name => 'John', :age => 70)
assert_equal 'John', person.name
-
- other = Person.new(:name => 'Jane', :friends => [person])
- assert_equal 'John', other.friends.first.name
+ assert_equal 70, person.age
+ assert_equal({}, Structure.new.send(:table))
end
- def test_cant_change_sex_when_frozen
- person = Person.new(:name => 'John')
- person.freeze
- assert_raises(TypeError) { person.name = 'Jane' }
- end
+ def test_new_field
+ @person.send(:table)[:age] = 20
+ @person.send(:new_field, :age)
- def test_to_hash
- person = Person.new(:name => 'John')
- friend = Person.new(:name => 'Jane')
- person.friends << friend
- hsh = person.to_hash
+ assert_equal 20, @person.age
- assert_equal person.name, hsh[:name]
- assert_equal friend.name, hsh[:friends].first[:name]
+ @person.age = 30
+ assert_equal 30, @person.age
- person.friends = [[friend]]
- hsh = person.to_hash
- assert_equal friend.name, hsh[:friends].first.first[:name]
+ @person.instance_eval { def gender; 'male'; end }
+ @person.send(:new_field, :gender)
+ assert_equal 'male', @person.gender
+ refute_respond_to @person, :gender=
end
- def test_json
- Person.send :include, Structure::JSON
+ def test_recursive_assignment
+ friend = { :name => 'Jane' }
+ @person.friend = friend
+ @person.friends = [friend]
+ assert_equal 'Jane', @person.friend.name
+ assert_equal 'Jane', @person.friends.first.name
+ end
- 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)
+ def test_recursive_marshaling
+ hsh = {
+ :name => 'John',
+ :friend => { :name => 'Jane' },
+ :friends => [{ :name => 'Jane' }]
+ }
+ friend = Structure.new(:name => 'Jane')
+ @person.friend = friend
+ @person.friends = [friend]
+ assert_equal hsh, @person.marshal_dump
- require 'active_support/ordered_hash'
- require 'active_support/json'
- 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)
+ person = Structure.new
+ person.marshal_load(hsh)
+ assert_equal friend, person.friend
+ assert_equal friend, person.friends.first
+ end
+
+ def test_table
+ assert_equal({ :name => 'John' }, @person.send(:table))
end
end