Debian Codegraph @

vendor/mruby-source/test/t/methods.rb

raw
1 ##
2 # Chapter 13.3 "Methods" ISO Test
3
4 assert('The alias statement', '13.3.6 a) 4)') do
5 # check aliasing in all possible ways
6
7 def alias_test_method_original; true; end
8
9 alias alias_test_method_a alias_test_method_original
10 alias :alias_test_method_b :alias_test_method_original
11
12 assert_true(alias_test_method_original)
13 assert_true(alias_test_method_a)
14 assert_true(alias_test_method_b)
15 end
16
17 assert('The alias statement (overwrite original)', '13.3.6 a) 4)') do
18 # check that an aliased method can be overwritten
19 # without side effect
20
21 def alias_test_method_original; true; end
22
23 alias alias_test_method_a alias_test_method_original
24 alias :alias_test_method_b :alias_test_method_original
25
26 assert_true(alias_test_method_original)
27
28 def alias_test_method_original; false; end
29
30 assert_false(alias_test_method_original)
31 assert_true(alias_test_method_a)
32 assert_true(alias_test_method_b)
33 end
34
35 assert('The alias statement', '13.3.6 a) 5)') do
36 # check that alias is raising NameError if
37 # non-existing method should be undefined
38
39 assert_raise(NameError) do
40 alias new_name_a non_existing_method
41 end
42
43 assert_raise(NameError) do
44 alias :new_name_b :non_existing_method
45 end
46 end
47
48 assert('The undef statement', '13.3.7 a) 4)') do
49 # check that undef is undefining method
50 # based on the method name
51
52 def existing_method_a; true; end
53 def existing_method_b; true; end
54 def existing_method_c; true; end
55 def existing_method_d; true; end
56 def existing_method_e; true; end
57 def existing_method_f; true; end
58
59 # check that methods are defined
60
61 assert_true(existing_method_a, 'Method should be defined')
62 assert_true(existing_method_b, 'Method should be defined')
63 assert_true(existing_method_c, 'Method should be defined')
64 assert_true(existing_method_d, 'Method should be defined')
65 assert_true(existing_method_e, 'Method should be defined')
66 assert_true(existing_method_f, 'Method should be defined')
67
68 # undefine in all possible ways and check that method
69 # is undefined
70
71 undef existing_method_a
72 assert_raise(NoMethodError) do
73 existing_method_a
74 end
75
76 undef :existing_method_b
77 assert_raise(NoMethodError) do
78 existing_method_b
79 end
80
81 undef existing_method_c, existing_method_d
82 assert_raise(NoMethodError) do
83 existing_method_c
84 end
85 assert_raise(NoMethodError) do
86 existing_method_d
87 end
88
89 undef :existing_method_e, :existing_method_f
90 assert_raise(NoMethodError) do
91 existing_method_e
92 end
93 assert_raise(NoMethodError) do
94 existing_method_f
95 end
96 end
97
98 assert('The undef statement (method undefined)', '13.3.7 a) 5)') do
99 # check that undef is raising NameError if
100 # non-existing method should be undefined
101
102 assert_raise(NameError) do
103 undef non_existing_method
104 end
105
106 assert_raise(NameError) do
107 undef :non_existing_method
108 end
109 end
110
111 assert('method_added hook') do
112 c = Class.new do
113 # method to retrieve @name
114 def self.name; @name; end
115 # hook method on method definition
116 def self.method_added(name) @name = name; end
117 # method definition
118 def foo; end
119 end
120 assert_equal(:foo, c.name)
121 c.define_method(:bar){}
122 assert_equal(:bar, c.name)
123 end
124
125 assert('singleton_method_added hook') do
126 a = Object.new
127 # method to retrieve @name
128 def a.name; @name; end
129 # hook method on singleton method definition
130 def a.singleton_method_added(name) @name = name; end
131 # singleton method definition
132 def a.foo; end
133 assert_equal(:foo, a.name)
134 class <<a
135 def bar; end
136 end
137 assert_equal(:bar, a.name)
138 end