-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_easycmp.rb
executable file
·130 lines (112 loc) · 3.7 KB
/
test_easycmp.rb
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/usr/bin/env ruby
require 'test/unit'
require_relative 'easycmp'
class TestEasyCmp < Test::Unit::TestCase
def setup
@klass=
Class.new do
attr_reader :meth
easy_cmp :@foo,:meth
def initialize foo=0,bar=0,meth=0
@foo=foo
@bar=bar
@meth=meth
end
end
end
def test_standard_case
#If all variables are equal, we should get 0.
assert_equal 0, @klass.new <=>@klass.new
#If the right-hand operand has our second var larger, we should get -1.
assert_equal -1, @klass.new <=>@klass.new(0,0,1)
end
def test_order
#If the left-hand one has our first var larger, though, we get 1 regardless
#of what the right-hand operand has for the second var.
assert_equal 1, @klass.new(1,0,0)<=>@klass.new(0,0,1)
end
def test_no_extras
#Since @foo and meth are the only ones being tested, changing @bar should
#affect nothing.
assert_equal 1, @klass.new(1,0,0)<=>@klass.new(0,1,1)
assert_equal 1, @klass.new(1,1,0)<=>@klass.new(0,0,1)
end
#Let's add @bar to our comparison chain.
def test_field_append
@klass.class_exec{easy_cmp :@bar}
#We'll run the standards to make sure they still work.
test_standard_case
test_order
#Now, to test @bar...
assert_equal 1, @klass.new(0,1,0)<=>@klass.new
assert_equal -1, @klass.new <=>@klass.new(0,1,0)
assert_equal 1, @klass.new(1,0,0)<=>@klass.new(0,1,0)
assert_equal 1, @klass.new(0,0,1)<=>@klass.new(0,1,0)
end
#This is to test that when a :sym is given, that obj.sym is called rather than
#accessing obj's @sym variable.
def test_methods_are_called
mod_meth=@klass.new
assert_equal 0, mod_meth<=>@klass.new
class << mod_meth
def meth
1
end
end
assert_equal 1, mod_meth<=>@klass.new
end
def test_subclass_differentiation
klass=
Class.new(@klass) do
easy_cmp :@new_var
def initialize new_var=0,foo=0,bar=0,meth=0
super foo,bar,meth
@new_var=new_var
end
end
assert_equal 0, klass.new(0,1,1,1)<=>klass.new(0,0,0,0)
assert_equal 1, klass.new(1,0,0,0)<=>klass.new(0,1,1,1)
assert_equal -1, klass.new(0,1,1,1)<=>klass.new(1,0,0,0)
end
def test_method_is_private
assert Object.private_methods.include?(:easy_cmp)
end
def test_easy_cmp_clear
@klass.class_exec{easy_cmp_clear}
assert_equal 0, @klass.new(0,0,1)<=>@klass.new
assert_equal 0, @klass.new(0,1,0)<=>@klass.new
assert_equal 0, @klass.new(1,0,0)<=>@klass.new
end
def test_easy_cmp_clear_with_block
@klass.class_exec{easy_cmp_clear{|oth| [self,oth]}}
one=@klass.new
two=@klass.new
assert_equal [one,two], one<=>two
end
def test_append
@klass.class_exec{easy_cmp :@foo, append: false}
assert_equal 1, @klass.new(1,0,0)<=>@klass.new
assert_equal -1, @klass.new <=>@klass.new(1,0,0)
assert_equal 0, @klass.new(0,0,1)<=>@klass.new
assert_equal 0, @klass.new(0,1,0)<=>@klass.new
#This brings us back to the original set of @foo and meth, so we can use the
#original two tests to check that append:true is honored.
@klass.class_exec{easy_cmp :meth, append: true}
test_standard_case
test_order
test_no_extras
#The case of append not defaulting to true would be caught by other tests.
end
def test_reverse
@klass.class_exec{easy_cmp :@bar, reverse: true}
test_standard_case
test_order
assert_equal 1, @klass.new <=>@klass.new(0,1,0)
assert_equal -1, @klass.new(0,1,0)<=>@klass.new
end
def test_reorder
@klass.class_exec{easy_cmp :@foo}
test_standard_case
assert_equal 1, @klass.new(0,0,1)<=>@klass.new(1,0,0)
end
end