test/structure_test.rb in structure-0.24.0 vs test/structure_test.rb in structure-0.25.0

- old
+ new

@@ -1,161 +1,51 @@ -require File.expand_path('../helper.rb', __FILE__) +require 'minitest/autorun' +require 'structure' -# Most tests below are borrowed from RubySpec. -class TestStructure < MiniTest::Unit::TestCase - def setup - @person = Structure.new(:name => 'John') - end +class Location + include Structure - 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 + attr :res - def test_element_reference - assert_raises(NoMethodError) { @person[1] } + def initialize(res) + @res = res end - def test_element_set - assert_raises(NoMethodError) { @person[1] = 2 } + value :latitude do + res.fetch(:lat) end - 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) + value :longitude do + res.fetch(:lng) end +end - 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 +class StructureTest < MiniTest::Test + def setup + @location = Location.new(lat: 10, lng: 100) end - 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 + def test_has_value + assert_equal 10, @location.latitude + assert_equal 100, @location.longitude end - 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) + def test_class_returns_value_names + assert_equal [:latitude, :longitude], Location.value_names end - 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 + def test_returns_values + assert_equal({ latitude: 10, longitude: 100 }, @location.values) + assert_equal @location.to_h, @location.values end - 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.send(:table)[:age] = 20 - assert_equal 20, @person.age - - assert_raises(NoMethodError) { @person.gender(1) } - assert_nil @person.gender - - @person.freeze - assert_raises(RuntimeError) { @person.gender = 'male' } + def test_compares + @other = Location.new(lng: 100, lat: 10) + assert @location == @other + assert @location.eql?(@other) end - def test_new - person = Structure.new(:name => 'John', :age => 70) - assert_equal 'John', person.name - assert_equal 70, person.age - assert_equal({}, Structure.new.send(:table)) - end - - def test_new_field - @person.send(:table)[:age] = 20 - @person.send(:new_field, :age) - - assert_equal 20, @person.age - - @person.age = 30 - assert_equal 30, @person.age - - @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_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 - - 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 - - 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)) + def test_pretty_inspects + assert_equal '#<Location latitude=10, longitude=100>', @location.inspect + assert_equal @location.to_s, @location.inspect end end