Sha256: 1e7067ecc68c6c9b7877857cd007976edd00c975c786d16f157eb7f18e8919bd

Contents?: true

Size: 1.31 KB

Versions: 14

Compression:

Stored size: 1.31 KB

Contents

$:.unshift File.join(File.dirname(__FILE__),"..","lib")

require 'test/unit'
require 'rgen/array_extensions'

class ArrayExtensionsTest < Test::Unit::TestCase

	def test_element_methods
		c = Struct.new("SomeClass",:name,:age)
		a = []
		a << c.new('MyName',33)
		a << c.new('YourName',22)
		assert_equal ["MyName", "YourName"], a >> :name
		assert_raise NoMethodError do
			a.name
		end
		assert_equal [33, 22], a>>:age
		assert_raise NoMethodError do
			a.age
		end
		# unfortunately, any method can be called on an empty array
		assert_equal [], [].age
	end
	
	class MMBaseClass < RGen::MetamodelBuilder::MMBase
		has_attr 'name'
		has_attr 'age', Integer
	end
	
	def test_with_mmbase
		e1 = MMBaseClass.new
		e1.name = "MyName"
		e1.age = 33
		e2 = MMBaseClass.new
		e2.name = "YourName"
		e2.age = 22
		a = [e1, e2]
		assert_equal ["MyName", "YourName"], a >> :name
		assert_equal ["MyName", "YourName"], a.name
		assert_equal [33, 22], a>>:age
		assert_equal [33, 22], a.age
		# put something into the array that is not an MMBase
		a << "not a MMBase"
		# the dot operator will tell that there is something not a MMBase
		assert_raise StandardError do
			a.age
		end
		# the >> operator will try to call the method anyway
		assert_raise NoMethodError do
			a >> :age
		end
	end
	
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
rgen-0.6.1 test/array_extensions_test.rb
rgen-0.6.0 test/array_extensions_test.rb
rgen-0.5.3 test/array_extensions_test.rb
rgen-0.5.4 test/array_extensions_test.rb
rgen-0.5.1 test/array_extensions_test.rb
rgen-0.5.2 test/array_extensions_test.rb
rgen-0.4.0 test/array_extensions_test.rb
rgen-0.4.2 test/array_extensions_test.rb
rgen-0.4.1 test/array_extensions_test.rb
rgen-0.4.3 test/array_extensions_test.rb
rgen-0.5.0 test/array_extensions_test.rb
rgen-0.4.4 test/array_extensions_test.rb
rgen-0.4.6 test/array_extensions_test.rb
rgen-0.4.5 test/array_extensions_test.rb