test/structure_test.rb in structure-0.13.1 vs test/structure_test.rb in structure-0.14.1

- old
+ new

@@ -1,134 +1,129 @@ +$:.push File.expand_path('../../lib', __FILE__) + require 'rubygems' require 'bundler/setup' -require 'test/unit' -require File.expand_path("../../lib/structure", __FILE__) +begin + require 'ruby-debug' +rescue LoadError +end -class Person - include Structure +require 'structure' +require 'test/unit' - attribute :name - attribute :age, Integer - attribute :married, Boolean, :default => false - embeds_one :spouse - embeds_many :children +class Book < Structure + key :title + key :published, Boolean, :default => true + key :pages, Integer end +class Person < Structure + key :name + one :partner + many :friends +end + class TestStructure < Test::Unit::TestCase def test_enumeration - person = Person.new - assert_respond_to(person, :map) + assert_respond_to Book.new, :map end - def test_method_generation - person = Person.new - assert_respond_to(person, :name) - assert_respond_to(person, :name=) - assert_respond_to(person, :name?) + def test_accessors + book = Book.new + assert_respond_to book, :title + assert_respond_to book, :title= end - def test_attribute_errors - assert_raise(NameError) { Person.attribute :class } - assert_raise(TypeError) { Person.attribute :foo, Object } - assert_raise(TypeError) { Person.attribute :foo, :default => 1 } + def test_key_errors + assert_raise(NameError) { Book.key :class } + assert_raise(TypeError) { Book.key :foo, Object } + assert_raise(TypeError) { Book.key :foo, :default => 1 } end def test_default_attributes - assert_equal({ :name => nil, :age => nil, :married => false, :spouse => nil, :children => [] }, Person.default_attributes) + exp = { :title => nil, + :published => true, + :pages => nil } + assert_equal exp, Book.default_attributes end def test_initialization - person = Person.new(:name => 'John', :age => 28) - assert_equal('John', person.name) - assert_equal(28, person.age) + book = Book.new(:title => 'Foo', :pages => 100) + assert_equal 'Foo', book.title + assert_equal 100, book.pages end def test_typecasting - person = Person.new + book = Book.new - person.age = "28" - assert_equal(28, person.age) + book.pages = "100" + assert_equal 100, book.pages - person.age = nil - assert_nil(person.age) - end + book.pages = nil + assert_nil book.pages - def test_presence - person = Person.new - - person.married = nil - assert(!person.married?) - - person.married = false - assert(!person.married?) - - person.married = true - assert(person.married?) + book.title = 1 + book.title = '1' end - def test_default_type - person = Person.new - person.name = 1 - assert(person.name.is_a? String) - end - def test_boolean_typecasting - person = Person.new + book = Book.new - person.married = 'false' - assert(person.married == false) + book.published = 'false' + assert book.published == false - person.married = 'FALSE' - assert(person.married == false) + book.published = 'FALSE' + assert book.published == false - person.married = '0' - assert(person.married == false) + book.published = '0' + assert book.published == false - person.married = 'foo' - assert(person.married == true) + book.published = 'foo' + assert book.published == true - person.married = 0 - assert(person.married == false) + book.published = 0 + assert book.published == false - person.married = 10 - assert(person.married == true) + book.published = 10 + assert book.published == true end def test_defaults - person = Person.new - assert_equal(false, person.married) - assert_equal(nil, person.name) - assert_equal(nil, person.spouse) - assert_equal([], person.children) + assert_equal nil, Book.new.title + assert_equal true, Book.new.published + assert_equal nil, Person.new.partner + assert_equal [], Person.new.friends end def test_array person = Person.new - child = Person.new - person.children << child - assert_equal(1, person.children.count) - assert_equal(0, child.children.count) + friend = Person.new + person.friends << person + assert_equal 1, person.friends.count + assert_equal 0, friend.friends.count end def test_json - person = Person.new(:name => 'Joe') - json = person.to_json - assert_equal(person, JSON.parse(json)) + book = Book.new(:title => 'Foo') + json = book.to_json + assert_equal book, JSON.parse(json) end def test_json_with_nested_structures person = Person.new - person.children << Person.new + person.friends << Person.new + person.partner = Person.new json = person.to_json - assert(JSON.parse(json).children.first.is_a? Person) + assert JSON.parse(json).friends.first.is_a? Person + assert JSON.parse(json).partner.is_a? Person end def test_json_with_active_support require 'active_support/ordered_hash' require 'active_support/json' - person = Person.new - assert(person.as_json(:only => :name).has_key?(:name)) - assert(!person.as_json(:except => :name).has_key?(:name)) + book = Book.new + assert book.as_json(:only => :title).has_key?(:title) + assert !book.as_json(:except => :title).has_key?(:title) end end