File: array_extensions_test.rb

package info (click to toggle)
ruby-rgen 0.10.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,428 kB
  • sloc: ruby: 11,344; xml: 1,368; yacc: 72; makefile: 10
file content (64 lines) | stat: -rw-r--r-- 1,606 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
$:.unshift File.join(File.dirname(__FILE__),"..","lib")

require 'minitest/autorun'
require 'rgen/array_extensions'

class ArrayExtensionsTest < Minitest::Test

  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_raises NoMethodError do
      a.name
    end
    assert_equal [33, 22], a>>:age
    assert_raises 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_raises StandardError do
      a.age
    end
    # the >> operator will try to call the method anyway
    assert_raises NoMethodError do
      a >> :age
    end
  end

  def test_hash_square
    assert_equal({}, Hash[[]])
  end

  def test_to_str_on_empty_array
    assert_raises NoMethodError do
      [].to_str
    end
  end
  
end