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 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485
|
require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
class PathByDefaultTest < Test::Unit::TestCase
def setup
@klass = Class.new
@machine = StateMachine::Machine.new(@klass)
@object = @klass.new
@path = StateMachine::Path.new(@object, @machine)
end
def test_should_have_an_object
assert_equal @object, @path.object
end
def test_should_have_a_machine
assert_equal @machine, @path.machine
end
def test_should_not_have_walked_anywhere
assert_equal [], @path
end
def test_should_not_have_a_from_name
assert_nil @path.from_name
end
def test_should_have_no_from_states
assert_equal [], @path.from_states
end
def test_should_not_have_a_to_name
assert_nil @path.to_name
end
def test_should_have_no_to_states
assert_equal [], @path.to_states
end
def test_should_have_no_events
assert_equal [], @path.events
end
def test_should_not_be_able_to_walk_anywhere
walked = false
@path.walk { walked = true }
assert_equal false, walked
end
def test_should_not_be_complete
assert_equal false, @path.complete?
end
end
class PathTest < Test::Unit::TestCase
def setup
@klass = Class.new
@machine = StateMachine::Machine.new(@klass)
@object = @klass.new
end
def test_should_raise_exception_if_invalid_option_specified
exception = assert_raise(ArgumentError) {StateMachine::Path.new(@object, @machine, :invalid => true)}
assert_equal 'Invalid key(s): invalid', exception.message
end
end
class PathWithoutTransitionsTest < Test::Unit::TestCase
def setup
@klass = Class.new
@machine = StateMachine::Machine.new(@klass)
@machine.state :parked, :idling
@machine.event :ignite
@object = @klass.new
@path = StateMachine::Path.new(@object, @machine)
@path.concat([
@ignite_transition = StateMachine::Transition.new(@object, @machine, :ignite, :parked, :idling)
])
end
def test_should_not_be_able_to_walk_anywhere
walked = false
@path.walk { walked = true }
assert_equal false, walked
end
end
class PathWithTransitionsTest < Test::Unit::TestCase
def setup
@klass = Class.new
@machine = StateMachine::Machine.new(@klass)
@machine.state :parked, :idling, :first_gear
@machine.event :ignite, :shift_up
@object = @klass.new
@object.state = 'parked'
@path = StateMachine::Path.new(@object, @machine)
@path.concat([
@ignite_transition = StateMachine::Transition.new(@object, @machine, :ignite, :parked, :idling),
@shift_up_transition = StateMachine::Transition.new(@object, @machine, :shift_up, :idling, :first_gear)
])
end
def test_should_enumerate_transitions
assert_equal [@ignite_transition, @shift_up_transition], @path
end
def test_should_have_a_from_name
assert_equal :parked, @path.from_name
end
def test_should_have_from_states
assert_equal [:parked, :idling], @path.from_states
end
def test_should_have_a_to_name
assert_equal :first_gear, @path.to_name
end
def test_should_have_to_states
assert_equal [:idling, :first_gear], @path.to_states
end
def test_should_have_events
assert_equal [:ignite, :shift_up], @path.events
end
def test_should_not_be_able_to_walk_anywhere
walked = false
@path.walk { walked = true }
assert_equal false, walked
end
def test_should_be_complete
assert_equal true, @path.complete?
end
end
class PathWithDuplicatesTest < Test::Unit::TestCase
def setup
@klass = Class.new
@machine = StateMachine::Machine.new(@klass)
@machine.state :parked, :idling
@machine.event :park, :ignite
@object = @klass.new
@object.state = 'parked'
@path = StateMachine::Path.new(@object, @machine)
@path.concat([
@ignite_transition = StateMachine::Transition.new(@object, @machine, :ignite, :parked, :idling),
@park_transition = StateMachine::Transition.new(@object, @machine, :park, :idling, :parked),
@ignite_again_transition = StateMachine::Transition.new(@object, @machine, :ignite, :parked, :idling)
])
end
def test_should_not_include_duplicates_in_from_states
assert_equal [:parked, :idling], @path.from_states
end
def test_should_not_include_duplicates_in_to_states
assert_equal [:idling, :parked], @path.to_states
end
def test_should_not_include_duplicates_in_events
assert_equal [:ignite, :park], @path.events
end
end
class PathWithAvailableTransitionsTest < Test::Unit::TestCase
def setup
@klass = Class.new
@machine = StateMachine::Machine.new(@klass)
@machine.state :parked, :idling, :first_gear
@machine.event :ignite
@machine.event :shift_up do
transition :idling => :first_gear
end
@machine.event :park do
transition :idling => :parked
end
@object = @klass.new
@object.state = 'parked'
@path = StateMachine::Path.new(@object, @machine)
@path.concat([
@ignite_transition = StateMachine::Transition.new(@object, @machine, :ignite, :parked, :idling)
])
end
def test_should_not_be_complete
assert !@path.complete?
end
def test_should_walk_each_available_transition
paths = []
@path.walk {|path| paths << path}
assert_equal [
[@ignite_transition, StateMachine::Transition.new(@object, @machine, :shift_up, :idling, :first_gear)],
[@ignite_transition, StateMachine::Transition.new(@object, @machine, :park, :idling, :parked)]
], paths
end
def test_should_yield_path_instances_when_walking
@path.walk do |path|
assert_instance_of StateMachine::Path, path
end
end
def test_should_not_modify_current_path_after_walking
@path.walk {}
assert_equal [@ignite_transition], @path
end
def test_should_not_modify_object_after_walking
@path.walk {}
assert_equal 'parked', @object.state
end
end
class PathWithGuardedTransitionsTest < Test::Unit::TestCase
def setup
@klass = Class.new
@machine = StateMachine::Machine.new(@klass)
@machine.state :parked, :idling
@machine.event :ignite
@machine.event :shift_up do
transition :idling => :first_gear, :if => lambda {false}
end
@object = @klass.new
@object.state = 'parked'
end
def test_should_not_walk_transitions_if_guard_enabled
path = StateMachine::Path.new(@object, @machine)
path.concat([
StateMachine::Transition.new(@object, @machine, :ignite, :parked, :idling)
])
paths = []
path.walk {|next_path| paths << next_path}
assert_equal [], paths
end
def test_should_not_walk_transitions_if_guard_disabled
path = StateMachine::Path.new(@object, @machine, :guard => false)
path.concat([
ignite_transition = StateMachine::Transition.new(@object, @machine, :ignite, :parked, :idling)
])
paths = []
path.walk {|next_path| paths << next_path}
assert_equal [
[ignite_transition, StateMachine::Transition.new(@object, @machine, :shift_up, :idling, :first_gear)]
], paths
end
end
class PathWithEncounteredTransitionsTest < Test::Unit::TestCase
def setup
@klass = Class.new
@machine = StateMachine::Machine.new(@klass)
@machine.state :parked, :idling, :first_gear
@machine.event :ignite do
transition :parked => :idling
end
@machine.event :park do
transition :idling => :parked
end
@object = @klass.new
@object.state = 'parked'
@path = StateMachine::Path.new(@object, @machine)
@path.concat([
@ignite_transition = StateMachine::Transition.new(@object, @machine, :ignite, :parked, :idling),
@park_transition = StateMachine::Transition.new(@object, @machine, :park, :idling, :parked)
])
end
def test_should_be_complete
assert_equal true, @path.complete?
end
def test_should_not_be_able_to_walk
walked = false
@path.walk { walked = true }
assert_equal false, walked
end
end
class PathWithUnreachedTargetTest < Test::Unit::TestCase
def setup
@klass = Class.new
@machine = StateMachine::Machine.new(@klass)
@machine.state :parked, :idling
@machine.event :ignite do
transition :parked => :idling
end
@object = @klass.new
@object.state = 'parked'
@path = StateMachine::Path.new(@object, @machine, :target => :parked)
@path.concat([
@ignite_transition = StateMachine::Transition.new(@object, @machine, :ignite, :parked, :idling)
])
end
def test_should_not_be_complete
assert_equal false, @path.complete?
end
def test_should_not_be_able_to_walk
walked = false
@path.walk { walked = true }
assert_equal false, walked
end
end
class PathWithReachedTargetTest < Test::Unit::TestCase
def setup
@klass = Class.new
@machine = StateMachine::Machine.new(@klass)
@machine.state :parked, :idling
@machine.event :ignite do
transition :parked => :idling
end
@machine.event :park do
transition :idling => :parked
end
@object = @klass.new
@object.state = 'parked'
@path = StateMachine::Path.new(@object, @machine, :target => :parked)
@path.concat([
@ignite_transition = StateMachine::Transition.new(@object, @machine, :ignite, :parked, :idling),
@park_transition = StateMachine::Transition.new(@object, @machine, :park, :idling, :parked)
])
end
def test_should_be_complete
assert_equal true, @path.complete?
end
def test_should_not_be_able_to_walk
walked = false
@path.walk { walked = true }
assert_equal false, walked
end
end
class PathWithAvailableTransitionsAfterReachingTargetTest < Test::Unit::TestCase
def setup
@klass = Class.new
@machine = StateMachine::Machine.new(@klass)
@machine.state :parked, :idling
@machine.event :ignite do
transition :parked => :idling
end
@machine.event :shift_up do
transition :parked => :first_gear
end
@machine.event :park do
transition [:idling, :first_gear] => :parked
end
@object = @klass.new
@object.state = 'parked'
@path = StateMachine::Path.new(@object, @machine, :target => :parked)
@path.concat([
@ignite_transition = StateMachine::Transition.new(@object, @machine, :ignite, :parked, :idling),
@park_transition = StateMachine::Transition.new(@object, @machine, :park, :idling, :parked)
])
end
def test_should_be_complete
assert_equal true, @path.complete?
end
def test_should_be_able_to_walk
paths = []
@path.walk {|path| paths << path}
assert_equal [
[@ignite_transition, @park_transition, StateMachine::Transition.new(@object, @machine, :shift_up, :parked, :first_gear)]
], paths
end
end
class PathWithDeepTargetTest < Test::Unit::TestCase
def setup
@klass = Class.new
@machine = StateMachine::Machine.new(@klass)
@machine.state :parked, :idling
@machine.event :ignite do
transition :parked => :idling
end
@machine.event :shift_up do
transition :parked => :first_gear
end
@machine.event :park do
transition [:idling, :first_gear] => :parked
end
@object = @klass.new
@object.state = 'parked'
@path = StateMachine::Path.new(@object, @machine, :target => :parked)
@path.concat([
@ignite_transition = StateMachine::Transition.new(@object, @machine, :ignite, :parked, :idling),
@park_transition = StateMachine::Transition.new(@object, @machine, :park, :idling, :parked),
@shift_up_transition = StateMachine::Transition.new(@object, @machine, :shift_up, :parked, :first_gear)
])
end
def test_should_not_be_complete
assert_equal false, @path.complete?
end
def test_should_be_able_to_walk
paths = []
@path.walk {|path| paths << path}
assert_equal [
[@ignite_transition, @park_transition, @shift_up_transition, StateMachine::Transition.new(@object, @machine, :park, :first_gear, :parked)]
], paths
end
end
class PathWithDeepTargetReachedTest < Test::Unit::TestCase
def setup
@klass = Class.new
@machine = StateMachine::Machine.new(@klass)
@machine.state :parked, :idling
@machine.event :ignite do
transition :parked => :idling
end
@machine.event :shift_up do
transition :parked => :first_gear
end
@machine.event :park do
transition [:idling, :first_gear] => :parked
end
@object = @klass.new
@object.state = 'parked'
@path = StateMachine::Path.new(@object, @machine, :target => :parked)
@path.concat([
@ignite_transition = StateMachine::Transition.new(@object, @machine, :ignite, :parked, :idling),
@park_transition = StateMachine::Transition.new(@object, @machine, :park, :idling, :parked),
@shift_up_transition = StateMachine::Transition.new(@object, @machine, :shift_up, :parked, :first_gear),
@park_transition_2 = StateMachine::Transition.new(@object, @machine, :park, :first_gear, :parked)
])
end
def test_should_be_complete
assert_equal true, @path.complete?
end
def test_should_not_be_able_to_walk
walked = false
@path.walk { walked = true }
assert_equal false, walked
end
def test_should_not_be_able_to_walk_with_available_transitions
@machine.event :park do
transition :parked => same
end
walked = false
@path.walk { walked = true }
assert_equal false, walked
end
end
|