File: matcher_test.rb

package info (click to toggle)
ruby-state-machine 1.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 3,764 kB
  • ctags: 6,623
  • sloc: ruby: 26,008; makefile: 11
file content (155 lines) | stat: -rw-r--r-- 3,949 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
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
require File.expand_path(File.dirname(__FILE__) + '/../test_helper')

class MatcherByDefaultTest < Test::Unit::TestCase
  def setup
    @matcher = StateMachine::Matcher.new
  end
  
  def test_should_have_no_values
    assert_equal [], @matcher.values
  end
  
  def test_should_filter_all_values
    assert_equal [], @matcher.filter([:parked, :idling])
  end
end

class MatcherWithValueTest < Test::Unit::TestCase
  def setup
    @matcher = StateMachine::Matcher.new(nil)
  end
  
  def test_should_have_values
    assert_equal [nil], @matcher.values
  end
  
  def test_should_filter_unknown_values
    assert_equal [nil], @matcher.filter([nil, :parked])
  end
end

class MatcherWithMultipleValuesTest < Test::Unit::TestCase
  def setup
    @matcher = StateMachine::Matcher.new([:parked, :idling])
  end
  
  def test_should_have_values
    assert_equal [:parked, :idling], @matcher.values
  end
  
  def test_should_filter_unknown_values
    assert_equal [:parked], @matcher.filter([:parked, :first_gear])
  end
end

class AllMatcherTest < Test::Unit::TestCase
  def setup
    @matcher = StateMachine::AllMatcher.instance
  end
  
  def test_should_have_no_values
    assert_equal [], @matcher.values
  end
  
  def test_should_always_match
    [nil, :parked, :idling].each {|value| assert @matcher.matches?(value)}
  end
  
  def test_should_not_filter_any_values
    assert_equal [:parked, :idling], @matcher.filter([:parked, :idling])
  end
  
  def test_should_generate_blacklist_matcher_after_subtraction
    matcher = @matcher - [:parked, :idling]
    assert_instance_of StateMachine::BlacklistMatcher, matcher
    assert_equal [:parked, :idling], matcher.values
  end
  
  def test_should_have_a_description
    assert_equal 'all', @matcher.description
  end
end

class WhitelistMatcherTest < Test::Unit::TestCase
  def setup
    @matcher = StateMachine::WhitelistMatcher.new([:parked, :idling])
  end
  
  def test_should_have_values
    assert_equal [:parked, :idling], @matcher.values
  end
  
  def test_should_filter_unknown_values
    assert_equal [:parked, :idling], @matcher.filter([:parked, :idling, :first_gear])
  end
  
  def test_should_match_known_values
    assert @matcher.matches?(:parked)
  end
  
  def test_should_not_match_unknown_values
    assert !@matcher.matches?(:first_gear)
  end
  
  def test_should_have_a_description
    assert_equal '[:parked, :idling]', @matcher.description
    
    matcher = StateMachine::WhitelistMatcher.new([:parked])
    assert_equal ':parked', matcher.description
  end
end

class BlacklistMatcherTest < Test::Unit::TestCase
  def setup
    @matcher = StateMachine::BlacklistMatcher.new([:parked, :idling])
  end
  
  def test_should_have_values
    assert_equal [:parked, :idling], @matcher.values
  end
  
  def test_should_filter_known_values
    assert_equal [:first_gear], @matcher.filter([:parked, :idling, :first_gear])
  end
  
  def test_should_match_unknown_values
    assert @matcher.matches?(:first_gear)
  end
  
  def test_should_not_match_known_values
    assert !@matcher.matches?(:parked)
  end
  
  def test_should_have_a_description
    assert_equal 'all - [:parked, :idling]', @matcher.description
    
    matcher = StateMachine::BlacklistMatcher.new([:parked])
    assert_equal 'all - :parked', matcher.description
  end
end

class LoopbackMatcherTest < Test::Unit::TestCase
  def setup
    @matcher = StateMachine::LoopbackMatcher.instance
  end
  
  def test_should_have_no_values
    assert_equal [], @matcher.values
  end
  
  def test_should_filter_all_values
    assert_equal [], @matcher.filter([:parked, :idling])
  end
  
  def test_should_match_if_from_context_is_same
    assert @matcher.matches?(:parked, :from => :parked)
  end
  
  def test_should_not_match_if_from_context_is_different
    assert !@matcher.matches?(:parked, :from => :idling)
  end
  
  def test_should_have_a_description
    assert_equal 'same', @matcher.description
  end
end