vmsim 0.2.4 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,94 +0,0 @@
1
- require File.join(File.dirname(__FILE__),'..','test_helper')
2
- require 'mocha'
3
- require 'hardware'
4
- require 'hardware_adapter'
5
-
6
- module Hardware
7
- class DisplayTest < Test::Unit::TestCase
8
- attr_reader :display
9
-
10
- def setup
11
- @display = Display.new(Adapter::ActuatorCollection.null)
12
- end
13
-
14
- def test_can_display_text_on_first_line
15
- display.show(0, "line 1")
16
- assert_equal "line 1", display.line_one
17
- assert_equal "", display.line_two
18
- end
19
-
20
- def test_can_display_text_on_second_line
21
- display.show(1, "line 2")
22
- assert_equal "", display.line_one
23
- assert_equal "line 2", display.line_two
24
- end
25
-
26
- def test_displaying_on_higher_line_is_ignored
27
- display.show(3, "blah")
28
- assert_equal "", display.line_one
29
- assert_equal "", display.line_two
30
- end
31
-
32
- def test_displaying_on_negative_line_is_ignored
33
- display.show(-1, "blah")
34
- assert_equal "", display.line_one
35
- assert_equal "", display.line_two
36
- end
37
-
38
- def test_displaying_with_string_as_index_is_ignored
39
- display.show("0", "blah")
40
- assert_equal "", display.line_one
41
- assert_equal "", display.line_two
42
- end
43
-
44
- def test_line_will_be_limited_to_16
45
- display.show(0, "much longer than sixteen chars")
46
- assert_equal "much longer than", display.line_one
47
- display.show(1, "much longer than sixteen chars")
48
- assert_equal "much longer than", display.line_two
49
- end
50
-
51
-
52
- def test_display_updates_ui_when_show_is_called
53
- ui_contains = 'nothing'
54
- display.monitor_changes do
55
- ui_contains = [display.line_one, display.line_two].join('-')
56
- end
57
- display.show(1, 'blah')
58
- assert_equal '-blah', ui_contains
59
- end
60
-
61
- def test_display_updates_ui_when_nothing_shown
62
- ui_contains = 'nothing'
63
- display.monitor_changes do
64
- ui_contains = [display.line_one, display.line_two].join('-')
65
- end
66
- display.show(-1, 'blah') # error - nothing shown
67
- assert_equal 'nothing', ui_contains
68
- end
69
- end
70
-
71
- class DisplayConfigureTest < Test::Unit::TestCase
72
- attr_reader :display, :actuator_collection
73
-
74
- def setup
75
- @actuator_collection = mock('actuator_collection')
76
- @display = Display.new(actuator_collection)
77
- end
78
-
79
- def test_configure_configures_actuator_for_display
80
- actuator_collection.expects(:create_actuator_for).with(:display_show)
81
- display.configure
82
- end
83
-
84
- def test_firing_actuator_will_call_show
85
- actuator_collection.expects(:create_actuator_for).
86
- with(:display_show).
87
- yields(0, 'line one')
88
-
89
- display.configure
90
- assert_equal "line one", display.line_one
91
- assert_equal "", display.line_two
92
- end
93
- end
94
- end
@@ -1,100 +0,0 @@
1
- require File.expand_path File.join(File.dirname(__FILE__),'..','test_helper')
2
- require 'mocha'
3
- require 'hardware'
4
-
5
- module Hardware
6
- RIGHT_AWAY = 0
7
- class DrawerDroppingTest < Test::Unit::TestCase
8
- attr_reader :drawer, :bin
9
-
10
- def setup
11
- @bin = mock('bin')
12
- @drawer = Drawer.new(bin)
13
- @drawer.fill(Can.cola, 1, RIGHT_AWAY)
14
- end
15
-
16
- def test_drops_item_to_bin
17
- bin.expects(:receive).with(Can.cola)
18
- drawer.drop(RIGHT_AWAY)
19
- bin.expects(:receive).with(Can.fanta)
20
- Drawer.new(bin).fill(Can.fanta, 1, RIGHT_AWAY).drop(RIGHT_AWAY)
21
- end
22
-
23
- def test_dropping_reduces_the_stock
24
- bin.stubs(:receive)
25
- drawer.drop(RIGHT_AWAY)
26
- assert_equal [], drawer.stock
27
- end
28
-
29
- def test_dropping_more_than_you_have_has_no_effect
30
- bin.expects(:receive).with(Can.cola).once
31
- drawer.drop(RIGHT_AWAY)
32
- drawer.drop(RIGHT_AWAY)
33
- end
34
-
35
- def test_filling_adds_a_number_of_cans_in_stock
36
- drawer.fill(Can.cola, 2, RIGHT_AWAY)
37
- assert_equal([Can.cola, Can.cola, Can.cola], drawer.stock)
38
- end
39
- end
40
-
41
- class DrawerSensorTest < Test::Unit::TestCase
42
- attr_reader :drawer, :bin, :sensor_collection
43
-
44
- def setup
45
- @bin = stub('bin', :receive)
46
- @sensor_collection = mock('sensor_collection')
47
- @drawer = Drawer.new(bin, sensor_collection)
48
- end
49
-
50
- def test_dropping_a_can_fires_a_sensor_event
51
- @sensor_collection.stubs(:fire).with(:drawer_fill_can_0)
52
- @drawer.fill(Can.cola, 1, RIGHT_AWAY)
53
- @sensor_collection.expects(:fire).with(:drawer_drop_can_0)
54
- @drawer.drop(RIGHT_AWAY)
55
- end
56
-
57
- def test_filling_fires_fill_can_event_for_each_can
58
- @sensor_collection.expects(:fire).with(:drawer_fill_can_0).times(3)
59
- @drawer.fill(Can.cola, 3, RIGHT_AWAY)
60
- end
61
-
62
- end
63
-
64
- class ConfiguringDrawerTest < Test::Unit::TestCase
65
- attr_reader :drawer, :bin, :sensor_collection, :actuator_collection
66
-
67
- def setup
68
- @sensor_collection = mock('sensor_collection')
69
- @actuator_collection = mock('mock_collection')
70
- @bin = mock('bin')
71
-
72
- @drawer = Drawer.new(bin, sensor_collection, actuator_collection, 1)
73
- end
74
-
75
- def test_configure_sensors_configures_drop_and_fill_can
76
- sensor_collection.expects(:create_sensor_for).with(:drawer_drop_can_1)
77
- sensor_collection.expects(:create_sensor_for).with(:drawer_fill_can_1)
78
- actuator_collection.expects(:create_actuator_for).with(:drawer_drop_can_1)
79
-
80
- drawer.configure
81
- end
82
-
83
- def test_fired_actuator_drops_can
84
- sensor_collection.stubs(:create_sensor_for)
85
- # YIELD is what happens when the actuator is fired
86
- actuator_collection.expects(:create_actuator_for).with(:drawer_drop_can_1).yields
87
-
88
- def drawer.drop
89
- @drop_called = true
90
- end
91
- def drawer.drop_called
92
- @drop_called
93
- end
94
-
95
- drawer.configure
96
-
97
- assert drawer.drop_called, "drop should be called as result of yield of actuator"
98
- end
99
- end
100
- end
@@ -1,33 +0,0 @@
1
- require File.join(File.dirname(__FILE__), '..','test_helper')
2
- require 'hardware/enum'
3
- module Hardware
4
- class EnumTest < Test::Unit::TestCase
5
- class MyEnum < Enum
6
- values :first, :second
7
- end
8
-
9
- def test_enum_value_is_accessible_with_class_method
10
- assert_nothing_raised do
11
- MyEnum.first
12
- end
13
- end
14
-
15
- def test_non_existing_value_raises_exception
16
- assert_raises RuntimeError do
17
- MyEnum.blah
18
- end
19
- end
20
-
21
- def test_enum_values_are_exactly_same
22
- assert_same MyEnum.first, MyEnum.first
23
- end
24
-
25
- def test_different_enum_values_are_exactly_same
26
- assert_not_same MyEnum.second, MyEnum.first
27
- end
28
-
29
- def test_inspecting_a_value_returns_a_readable_string
30
- assert_equal 'Hardware::EnumTest::MyEnum.first', MyEnum.first.inspect
31
- end
32
- end
33
- end
@@ -1,189 +0,0 @@
1
- require File.join(File.dirname(__FILE__),'..','test_helper')
2
- require 'mocha'
3
- require 'hardware'
4
-
5
- module Hardware
6
- class SimulatorAssembleABinTest < Test::Unit::TestCase
7
- def test_attaches_a_sensor_to_receiving_an_item
8
- sensors = Adapter::SensorCollection.new
9
- simulator = Simulator.new
10
- bin = Bin.new(sensors)
11
- simulator.assemble_hardware_component(:bin, bin)
12
- sensor_called = false
13
- sensors.on(:bin_entry) do
14
- sensor_called = true
15
- end
16
- bin.receive("some_object")
17
- assert sensor_called, "bin_entry sensor should be fired"
18
- end
19
- end
20
-
21
- class SimulatorAssembleHardwareComponents < Test::Unit::TestCase
22
- attr_reader :sensors, :simulator, :component
23
- def setup
24
- @sensors = Adapter::SensorCollection.new
25
- @simulator = Simulator.new
26
- @component = mock("hardware_component")
27
- end
28
-
29
- def test_configuring_a_hardware_component_configures_the_component
30
- component.expects(:configure).with().once
31
- simulator.assemble_hardware_component(:bin, component)
32
- end
33
-
34
- def test_configuring_a_list_of_similar_hardware_components_configures_them_all
35
- component.expects(:configure).with().twice
36
- simulator.assemble_hardware_components(:drawer, [component, component])
37
- end
38
-
39
- def test_configured_hardware_component_is_available_by_name
40
- bin = stub('bin', :configure)
41
- simulator.assemble_hardware_component(:bin, bin)
42
- assert_same bin, simulator.component(:bin)
43
- end
44
-
45
- def test_configured_hardware_components_are_available_by_name
46
- drawer0 = stub('drawer0', :configure)
47
- drawer1 = stub('drawer1', :configure)
48
-
49
- simulator.assemble_hardware_components(:drawer, [drawer0, drawer1])
50
-
51
- assert_same drawer0, simulator.component(:drawer_0)
52
- assert_same drawer1, simulator.component(:drawer_1)
53
- end
54
-
55
- def test_monitor_changes_delegates_to_the_appropriate_hardware_component
56
- component.stubs(:configure)
57
- simulator.assemble_hardware_component(:my_comp, component)
58
-
59
- component.expects(:monitor_changes).yields()
60
- block_given = 'block not given'
61
- simulator.monitor_changes(:my_comp) do
62
- block_given = 'block_given'
63
- end
64
-
65
- assert_equal 'block_given', block_given
66
- end
67
- end
68
-
69
- class SimulatorAssembleHardware < Test::Unit::TestCase
70
- attr_reader :actuators, :sensors, :simulator
71
- def setup
72
- @actuators = Adapter::ActuatorCollection.new
73
- @sensors = Adapter::SensorCollection.new
74
- @simulator = Simulator.new(actuators, sensors)
75
- end
76
-
77
- def test_assembles_one_bin
78
- simulator.assemble_hardware
79
- assert_equal(Bin, simulator.bin.class)
80
- assert sensors.list.include?(:bin_entry)
81
- assert sensors.list.include?(:bin_fetch_all)
82
- end
83
-
84
- def test_assembles_one_display
85
- simulator.assemble_hardware
86
- assert_equal(Display, simulator.display.class)
87
- assert actuators.list.include?(:display_show)
88
- end
89
-
90
- def test_assembles_4_drawers
91
- simulator.assemble_hardware
92
- assert_equal(Drawer, simulator.drawer(0).class)
93
- assert_equal(Drawer, simulator.drawer(1).class)
94
- assert_equal(Drawer, simulator.drawer(2).class)
95
- assert_equal(Drawer, simulator.drawer(3).class)
96
- assert actuators.list.include?(:drawer_drop_can_0)
97
- assert actuators.list.include?(:drawer_drop_can_1)
98
- assert actuators.list.include?(:drawer_drop_can_2)
99
- assert actuators.list.include?(:drawer_drop_can_3)
100
- assert sensors.list.include?(:drawer_drop_can_0)
101
- assert sensors.list.include?(:drawer_drop_can_1)
102
- assert sensors.list.include?(:drawer_drop_can_2)
103
- assert sensors.list.include?(:drawer_drop_can_3)
104
- assert sensors.list.include?(:drawer_fill_can_0)
105
- assert sensors.list.include?(:drawer_fill_can_1)
106
- assert sensors.list.include?(:drawer_fill_can_2)
107
- assert sensors.list.include?(:drawer_fill_can_3)
108
- end
109
- def test_assembles_4_buttons
110
- simulator.assemble_hardware
111
- assert_equal(Button, simulator.button(0).class)
112
- assert_equal(Button, simulator.button(1).class)
113
- assert_equal(Button, simulator.button(2).class)
114
- assert_equal(Button, simulator.button(3).class)
115
- assert sensors.list.include?(:button_press_0)
116
- assert sensors.list.include?(:button_press_1)
117
- assert sensors.list.include?(:button_press_2)
118
- assert sensors.list.include?(:button_press_3)
119
- end
120
-
121
- def test_assembles_cash_register
122
- simulator.assemble_hardware
123
- assert_equal(CashRegister, simulator.component(:cash_register).class)
124
- assert actuators.list.include?(:cash_drop_100)
125
- assert actuators.list.include?(:cash_drop_200)
126
- assert actuators.list.include?(:cash_drop_50)
127
- assert sensors.list.include?(:cash_fill_100)
128
- assert sensors.list.include?(:cash_fill_200)
129
- assert sensors.list.include?(:cash_fill_50)
130
- assert sensors.list.include?(:cash_insert_100)
131
- assert sensors.list.include?(:cash_insert_200)
132
- assert sensors.list.include?(:cash_insert_50)
133
- end
134
- end
135
-
136
- class SimulatorReset < Test::Unit::TestCase
137
- attr_reader :actuators, :sensors, :simulator
138
- def setup
139
- @actuators = Adapter::ActuatorCollection.new
140
- @sensors = Adapter::SensorCollection.new
141
- @simulator = Simulator.new(actuators, sensors)
142
- simulator.assemble_hardware
143
- end
144
-
145
- def test_empties_drawers
146
- simulator.drawer(0).fill(Hardware::Can.cola, 2, 0)
147
- simulator.reset
148
- assert simulator.drawer(0).empty?
149
- end
150
-
151
- def test_empties_cash_register
152
- simulator.cash_register.fill(Hardware::Coin.one_euro, 2, 0)
153
- simulator.reset
154
- assert simulator.cash_register.empty?
155
- end
156
-
157
- def test_removes_sensor_listeners
158
- bin_entry_called = "was not called"
159
- sensors.on(:bin_entry) { bin_entry_called = "was called" }
160
- simulator.reset
161
- sensors.fire(:bin_entry)
162
-
163
- assert_equal "was not called", bin_entry_called
164
- end
165
-
166
- def test_fires_start_boot_block
167
- boot_block = "was not called"
168
- simulator.on_boot { boot_block = "was called" }
169
- simulator.reset
170
- assert_equal "was called", boot_block
171
- end
172
- end
173
-
174
- class SimulatorBoot < Test::Unit::TestCase
175
- attr_reader :simulator
176
- def setup
177
- @simulator = Simulator.new(Adapter::ActuatorCollection.null, Adapter::SensorCollection.null)
178
- simulator.assemble_hardware
179
- end
180
-
181
- def test_fires_start_boot_block
182
- boot_block = "was not called"
183
- simulator.on_boot { boot_block = "was called" }
184
- simulator.boot
185
- assert_equal "was called", boot_block
186
- end
187
- end
188
-
189
- end
data/test/test_helper.rb DELETED
@@ -1,5 +0,0 @@
1
- require 'test/unit'
2
- require 'rubygems'
3
- module Hardware
4
- end
5
- $: << 'lib'