-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest_enum.rb
122 lines (88 loc) · 3.17 KB
/
test_enum.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
# encoding: utf-8
##
# to run use
# ruby -I ./lib -I ./test test/test_enum.rb
require 'helper'
module Safe
## Enum.new( :State, :fundraising, :expired_refund, :successful )
enum :State, :fundraising, :expired_refund, :successful
pp State
pp State(0)
puts "Safe.constants:"
pp Safe.constants #=> [:SafeHelper, :Enum, :State, :Color]
puts "Enum.constants:"
pp Enum.constants #=> []
end
class TestEnum < MiniTest::Test
include Safe ## make all enums (and "convenience" converters) global
## Enum.new( 'Color', :red, :green, :blue )
enum :Color, :red, :green, :blue
pp Color
pp Color(0)
enum :Roman, {i: 1, iv: 4, v: 5, ix: 9, x: 10 }
pp Roman
pp Roman(0) ## first member by index
def test_state
assert_equal [:FUNDRAISING, :EXPIRED_REFUND, :SUCCESSFUL], State.constants
assert_equal [0, 1, 2], State.values
assert_equal [:fundraising, :expired_refund, :successful], State.keys
assert_equal 3, State.size
assert_equal 3, State.length
assert_equal State.fundraising, State::FUNDRAISING
assert_equal State.expired_refund, State::EXPIRED_REFUND
assert_equal State.successful, State::SUCCESSFUL
pp State.members
assert_equal :fundraising, State.members[0].key
assert_equal 0, State.members[0].value
assert_equal :expired_refund, State.members[1].key
assert_equal 1, State.members[1].value
assert_equal 0, State.fundraising.value
assert_equal :fundraising, State.fundraising.key
assert_equal 0, State::FUNDRAISING.value
assert_equal :fundraising, State::FUNDRAISING.key
pp State
state = State.fundraising
pp state
assert_equal true, state.fundraising?
assert_equal 0, state.value
assert_equal true, state.is_a?( Enum )
assert_equal true, state.is_a?( State )
assert_equal State.fundraising, State(0)
assert_equal State.expired_refund, State(1)
assert_equal State.successful, State(2)
pp State.zero
assert_equal true, State(0) == State.zero
assert_equal false, State(1) == State.zero
assert_equal true, State(0).zero?
assert_equal true, State.zero.zero?
assert_equal false, State(1).zero?
assert_equal false, State(2).zero?
assert_equal State.fundraising, State.value(0)
assert_equal State.fundraising, State.key(:fundraising)
assert_equal State.fundraising, State[:fundraising]
end
def test_color
assert_equal [0, 1, 2], Color.values
assert_equal [:red, :green, :blue], Color.keys
assert_equal [:RED, :GREEN, :BLUE], Color.constants
assert_equal 3, Color.size
assert_equal 3, Color.length
pp Color.zero
pp Color.red
assert_equal Color.red, Color(0)
assert_equal Color.green, Color(1)
assert_equal Color.blue, Color(2)
pp Color.members
assert_equal Color.green, Color.value(1)
assert_equal Color.red, Color.key(:red)
assert_equal Color.red, Color[:red]
color = Color.red
assert_equal true, color.red?
assert_equal true, color == Color.red
assert_equal false, color.blue?
assert_equal false, color == Color.blue
assert_equal Color.red, Color::RED
assert_equal Color.green, Color::GREEN
assert_equal Color.blue, Color::BLUE
end
end # class TestEnum