PageRenderTime 69ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/vendor/plugins/state_machine/test/unit/guard_test.rb

https://bitbucket.org/systech3/boxyroom
Ruby | 908 lines | 714 code | 193 blank | 1 comment | 6 complexity | 5c74c9d851fe61b2742b9a6d640758be MD5 | raw file
Possible License(s): JSON, MIT
  1. require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
  2. class GuardTest < Test::Unit::TestCase
  3. def setup
  4. @guard = StateMachine::Guard.new(:from => :parked, :to => :idling)
  5. end
  6. def test_should_not_raise_exception_if_implicit_option_specified
  7. assert_nothing_raised { StateMachine::Guard.new(:invalid => :valid) }
  8. end
  9. def test_should_not_have_an_if_condition
  10. assert_nil @guard.if_condition
  11. end
  12. def test_should_not_have_an_unless_condition
  13. assert_nil @guard.unless_condition
  14. end
  15. def test_should_have_a_state_requirement
  16. assert_equal 1, @guard.state_requirements.length
  17. end
  18. end
  19. class GuardWithNoRequirementsTest < Test::Unit::TestCase
  20. def setup
  21. @object = Object.new
  22. @guard = StateMachine::Guard.new
  23. end
  24. def test_should_use_all_matcher_for_event_requirement
  25. assert_equal StateMachine::AllMatcher.instance, @guard.event_requirement
  26. end
  27. def test_should_use_all_matcher_for_from_state_requirement
  28. assert_equal StateMachine::AllMatcher.instance, @guard.state_requirements.first[:from]
  29. end
  30. def test_should_use_all_matcher_for_to_state_requirement
  31. assert_equal StateMachine::AllMatcher.instance, @guard.state_requirements.first[:to]
  32. end
  33. def test_should_match_nil_query
  34. assert @guard.matches?(@object, nil)
  35. end
  36. def test_should_match_empty_query
  37. assert @guard.matches?(@object, {})
  38. end
  39. def test_should_match_non_empty_query
  40. assert @guard.matches?(@object, :to => :idling, :from => :parked, :on => :ignite)
  41. end
  42. def test_should_include_all_requirements_in_match
  43. match = @guard.match(@object, nil)
  44. assert_equal @guard.state_requirements.first[:from], match[:from]
  45. assert_equal @guard.state_requirements.first[:to], match[:to]
  46. assert_equal @guard.event_requirement, match[:on]
  47. end
  48. end
  49. class GuardWithFromRequirementTest < Test::Unit::TestCase
  50. def setup
  51. @object = Object.new
  52. @guard = StateMachine::Guard.new(:from => :parked)
  53. end
  54. def test_should_use_a_whitelist_matcher
  55. assert_instance_of StateMachine::WhitelistMatcher, @guard.state_requirements.first[:from]
  56. end
  57. def test_should_match_if_not_specified
  58. assert @guard.matches?(@object, :to => :idling)
  59. end
  60. def test_should_match_if_included
  61. assert @guard.matches?(@object, :from => :parked)
  62. end
  63. def test_should_not_match_if_not_included
  64. assert !@guard.matches?(@object, :from => :idling)
  65. end
  66. def test_should_not_match_if_nil
  67. assert !@guard.matches?(@object, :from => nil)
  68. end
  69. def test_should_ignore_to
  70. assert @guard.matches?(@object, :from => :parked, :to => :idling)
  71. end
  72. def test_should_ignore_on
  73. assert @guard.matches?(@object, :from => :parked, :on => :ignite)
  74. end
  75. def test_should_be_included_in_known_states
  76. assert_equal [:parked], @guard.known_states
  77. end
  78. def test_should_include_requirement_in_match
  79. match = @guard.match(@object, :from => :parked)
  80. assert_equal @guard.state_requirements.first[:from], match[:from]
  81. end
  82. end
  83. class GuardWithMultipleFromRequirementsTest < Test::Unit::TestCase
  84. def setup
  85. @object = Object.new
  86. @guard = StateMachine::Guard.new(:from => [:idling, :parked])
  87. end
  88. def test_should_match_if_included
  89. assert @guard.matches?(@object, :from => :idling)
  90. end
  91. def test_should_not_match_if_not_included
  92. assert !@guard.matches?(@object, :from => :first_gear)
  93. end
  94. def test_should_be_included_in_known_states
  95. assert_equal [:idling, :parked], @guard.known_states
  96. end
  97. end
  98. class GuardWithToRequirementTest < Test::Unit::TestCase
  99. def setup
  100. @object = Object.new
  101. @guard = StateMachine::Guard.new(:to => :idling)
  102. end
  103. def test_should_use_a_whitelist_matcher
  104. assert_instance_of StateMachine::WhitelistMatcher, @guard.state_requirements.first[:to]
  105. end
  106. def test_should_match_if_not_specified
  107. assert @guard.matches?(@object, :from => :parked)
  108. end
  109. def test_should_match_if_included
  110. assert @guard.matches?(@object, :to => :idling)
  111. end
  112. def test_should_not_match_if_not_included
  113. assert !@guard.matches?(@object, :to => :parked)
  114. end
  115. def test_should_not_match_if_nil
  116. assert !@guard.matches?(@object, :to => nil)
  117. end
  118. def test_should_ignore_from
  119. assert @guard.matches?(@object, :to => :idling, :from => :parked)
  120. end
  121. def test_should_ignore_on
  122. assert @guard.matches?(@object, :to => :idling, :on => :ignite)
  123. end
  124. def test_should_be_included_in_known_states
  125. assert_equal [:idling], @guard.known_states
  126. end
  127. def test_should_include_requirement_in_match
  128. match = @guard.match(@object, :to => :idling)
  129. assert_equal @guard.state_requirements.first[:to], match[:to]
  130. end
  131. end
  132. class GuardWithMultipleToRequirementsTest < Test::Unit::TestCase
  133. def setup
  134. @object = Object.new
  135. @guard = StateMachine::Guard.new(:to => [:idling, :parked])
  136. end
  137. def test_should_match_if_included
  138. assert @guard.matches?(@object, :to => :idling)
  139. end
  140. def test_should_not_match_if_not_included
  141. assert !@guard.matches?(@object, :to => :first_gear)
  142. end
  143. def test_should_be_included_in_known_states
  144. assert_equal [:idling, :parked], @guard.known_states
  145. end
  146. end
  147. class GuardWithOnRequirementTest < Test::Unit::TestCase
  148. def setup
  149. @object = Object.new
  150. @guard = StateMachine::Guard.new(:on => :ignite)
  151. end
  152. def test_should_use_a_whitelist_matcher
  153. assert_instance_of StateMachine::WhitelistMatcher, @guard.event_requirement
  154. end
  155. def test_should_match_if_not_specified
  156. assert @guard.matches?(@object, :from => :parked)
  157. end
  158. def test_should_match_if_included
  159. assert @guard.matches?(@object, :on => :ignite)
  160. end
  161. def test_should_not_match_if_not_included
  162. assert !@guard.matches?(@object, :on => :park)
  163. end
  164. def test_should_not_match_if_nil
  165. assert !@guard.matches?(@object, :on => nil)
  166. end
  167. def test_should_ignore_to
  168. assert @guard.matches?(@object, :on => :ignite, :to => :parked)
  169. end
  170. def test_should_ignore_from
  171. assert @guard.matches?(@object, :on => :ignite, :from => :parked)
  172. end
  173. def test_should_not_be_included_in_known_states
  174. assert_equal [], @guard.known_states
  175. end
  176. def test_should_include_requirement_in_match
  177. match = @guard.match(@object, :on => :ignite)
  178. assert_equal @guard.event_requirement, match[:on]
  179. end
  180. end
  181. class GuardWithMultipleOnRequirementsTest < Test::Unit::TestCase
  182. def setup
  183. @object = Object.new
  184. @guard = StateMachine::Guard.new(:on => [:ignite, :park])
  185. end
  186. def test_should_match_if_included
  187. assert @guard.matches?(@object, :on => :ignite)
  188. end
  189. def test_should_not_match_if_not_included
  190. assert !@guard.matches?(@object, :on => :shift_up)
  191. end
  192. end
  193. class GuardWithExceptFromRequirementTest < Test::Unit::TestCase
  194. def setup
  195. @object = Object.new
  196. @guard = StateMachine::Guard.new(:except_from => :parked)
  197. end
  198. def test_should_use_a_blacklist_matcher
  199. assert_instance_of StateMachine::BlacklistMatcher, @guard.state_requirements.first[:from]
  200. end
  201. def test_should_match_if_not_included
  202. assert @guard.matches?(@object, :from => :idling)
  203. end
  204. def test_should_not_match_if_included
  205. assert !@guard.matches?(@object, :from => :parked)
  206. end
  207. def test_should_match_if_nil
  208. assert @guard.matches?(@object, :from => nil)
  209. end
  210. def test_should_ignore_to
  211. assert @guard.matches?(@object, :from => :idling, :to => :parked)
  212. end
  213. def test_should_ignore_on
  214. assert @guard.matches?(@object, :from => :idling, :on => :ignite)
  215. end
  216. def test_should_be_included_in_known_states
  217. assert_equal [:parked], @guard.known_states
  218. end
  219. end
  220. class GuardWithMultipleExceptFromRequirementsTest < Test::Unit::TestCase
  221. def setup
  222. @object = Object.new
  223. @guard = StateMachine::Guard.new(:except_from => [:idling, :parked])
  224. end
  225. def test_should_match_if_not_included
  226. assert @guard.matches?(@object, :from => :first_gear)
  227. end
  228. def test_should_not_match_if_included
  229. assert !@guard.matches?(@object, :from => :idling)
  230. end
  231. def test_should_be_included_in_known_states
  232. assert_equal [:idling, :parked], @guard.known_states
  233. end
  234. end
  235. class GuardWithExceptToRequirementTest < Test::Unit::TestCase
  236. def setup
  237. @object = Object.new
  238. @guard = StateMachine::Guard.new(:except_to => :idling)
  239. end
  240. def test_should_use_a_blacklist_matcher
  241. assert_instance_of StateMachine::BlacklistMatcher, @guard.state_requirements.first[:to]
  242. end
  243. def test_should_match_if_not_included
  244. assert @guard.matches?(@object, :to => :parked)
  245. end
  246. def test_should_not_match_if_included
  247. assert !@guard.matches?(@object, :to => :idling)
  248. end
  249. def test_should_match_if_nil
  250. assert @guard.matches?(@object, :to => nil)
  251. end
  252. def test_should_ignore_from
  253. assert @guard.matches?(@object, :to => :parked, :from => :idling)
  254. end
  255. def test_should_ignore_on
  256. assert @guard.matches?(@object, :to => :parked, :on => :ignite)
  257. end
  258. def test_should_be_included_in_known_states
  259. assert_equal [:idling], @guard.known_states
  260. end
  261. end
  262. class GuardWithMultipleExceptToRequirementsTest < Test::Unit::TestCase
  263. def setup
  264. @object = Object.new
  265. @guard = StateMachine::Guard.new(:except_to => [:idling, :parked])
  266. end
  267. def test_should_match_if_not_included
  268. assert @guard.matches?(@object, :to => :first_gear)
  269. end
  270. def test_should_not_match_if_included
  271. assert !@guard.matches?(@object, :to => :idling)
  272. end
  273. def test_should_be_included_in_known_states
  274. assert_equal [:idling, :parked], @guard.known_states
  275. end
  276. end
  277. class GuardWithExceptOnRequirementTest < Test::Unit::TestCase
  278. def setup
  279. @object = Object.new
  280. @guard = StateMachine::Guard.new(:except_on => :ignite)
  281. end
  282. def test_should_use_a_blacklist_matcher
  283. assert_instance_of StateMachine::BlacklistMatcher, @guard.event_requirement
  284. end
  285. def test_should_match_if_not_included
  286. assert @guard.matches?(@object, :on => :park)
  287. end
  288. def test_should_not_match_if_included
  289. assert !@guard.matches?(@object, :on => :ignite)
  290. end
  291. def test_should_match_if_nil
  292. assert @guard.matches?(@object, :on => nil)
  293. end
  294. def test_should_ignore_to
  295. assert @guard.matches?(@object, :on => :park, :to => :idling)
  296. end
  297. def test_should_ignore_from
  298. assert @guard.matches?(@object, :on => :park, :from => :parked)
  299. end
  300. def test_should_not_be_included_in_known_states
  301. assert_equal [], @guard.known_states
  302. end
  303. end
  304. class GuardWithMultipleExceptOnRequirementsTest < Test::Unit::TestCase
  305. def setup
  306. @object = Object.new
  307. @guard = StateMachine::Guard.new(:except_on => [:ignite, :park])
  308. end
  309. def test_should_match_if_not_included
  310. assert @guard.matches?(@object, :on => :shift_up)
  311. end
  312. def test_should_not_match_if_included
  313. assert !@guard.matches?(@object, :on => :ignite)
  314. end
  315. end
  316. class GuardWithFailuresExcludedTest < Test::Unit::TestCase
  317. def setup
  318. @object = Object.new
  319. @guard = StateMachine::Guard.new(:include_failures => false)
  320. end
  321. def test_should_use_a_blacklist_matcher
  322. assert_instance_of StateMachine::WhitelistMatcher, @guard.success_requirement
  323. end
  324. def test_should_match_if_not_specified
  325. assert @guard.matches?(@object)
  326. end
  327. def test_should_match_if_true
  328. assert @guard.matches?(@object, :success => true)
  329. end
  330. def test_should_not_match_if_false
  331. assert !@guard.matches?(@object, :success => false)
  332. end
  333. end
  334. class GuardWithFailuresIncludedTest < Test::Unit::TestCase
  335. def setup
  336. @object = Object.new
  337. @guard = StateMachine::Guard.new(:include_failures => true)
  338. end
  339. def test_should_use_all_matcher
  340. assert_equal StateMachine::AllMatcher.instance, @guard.success_requirement
  341. end
  342. def test_should_match_if_not_specified
  343. assert @guard.matches?(@object)
  344. end
  345. def test_should_match_if_true
  346. assert @guard.matches?(@object, :success => true)
  347. end
  348. def test_should_match_if_false
  349. assert @guard.matches?(@object, :success => false)
  350. end
  351. end
  352. class GuardWithConflictingFromRequirementsTest < Test::Unit::TestCase
  353. def test_should_raise_an_exception
  354. exception = assert_raise(ArgumentError) { StateMachine::Guard.new(:from => :parked, :except_from => :parked) }
  355. assert_equal 'Conflicting keys: from, except_from', exception.message
  356. end
  357. end
  358. class GuardWithConflictingToRequirementsTest < Test::Unit::TestCase
  359. def test_should_raise_an_exception
  360. exception = assert_raise(ArgumentError) { StateMachine::Guard.new(:to => :idling, :except_to => :idling) }
  361. assert_equal 'Conflicting keys: to, except_to', exception.message
  362. end
  363. end
  364. class GuardWithConflictingOnRequirementsTest < Test::Unit::TestCase
  365. def test_should_raise_an_exception
  366. exception = assert_raise(ArgumentError) { StateMachine::Guard.new(:on => :ignite, :except_on => :ignite) }
  367. assert_equal 'Conflicting keys: on, except_on', exception.message
  368. end
  369. end
  370. class GuardWithDifferentRequirementsTest < Test::Unit::TestCase
  371. def setup
  372. @object = Object.new
  373. @guard = StateMachine::Guard.new(:from => :parked, :to => :idling, :on => :ignite)
  374. end
  375. def test_should_match_empty_query
  376. assert @guard.matches?(@object)
  377. end
  378. def test_should_match_if_all_requirements_match
  379. assert @guard.matches?(@object, :from => :parked, :to => :idling, :on => :ignite)
  380. end
  381. def test_should_not_match_if_from_not_included
  382. assert !@guard.matches?(@object, :from => :idling)
  383. end
  384. def test_should_not_match_if_to_not_included
  385. assert !@guard.matches?(@object, :to => :parked)
  386. end
  387. def test_should_not_match_if_on_not_included
  388. assert !@guard.matches?(@object, :on => :park)
  389. end
  390. def test_should_be_nil_if_unmatched
  391. assert_nil @guard.match(@object, :from => :parked, :to => :idling, :on => :park)
  392. end
  393. def test_should_include_all_known_states
  394. assert_equal [:parked, :idling], @guard.known_states
  395. end
  396. def test_should_not_duplicate_known_statse
  397. guard = StateMachine::Guard.new(:except_from => :idling, :to => :idling, :on => :ignite)
  398. assert_equal [:idling], guard.known_states
  399. end
  400. end
  401. class GuardWithNilRequirementsTest < Test::Unit::TestCase
  402. def setup
  403. @object = Object.new
  404. @guard = StateMachine::Guard.new(:from => nil, :to => nil)
  405. end
  406. def test_should_match_empty_query
  407. assert @guard.matches?(@object)
  408. end
  409. def test_should_match_if_all_requirements_match
  410. assert @guard.matches?(@object, :from => nil, :to => nil)
  411. end
  412. def test_should_not_match_if_from_not_included
  413. assert !@guard.matches?(@object, :from => :parked)
  414. end
  415. def test_should_not_match_if_to_not_included
  416. assert !@guard.matches?(@object, :to => :idling)
  417. end
  418. def test_should_include_all_known_states
  419. assert_equal [nil], @guard.known_states
  420. end
  421. end
  422. class GuardWithImplicitRequirementTest < Test::Unit::TestCase
  423. def setup
  424. @guard = StateMachine::Guard.new(:parked => :idling, :on => :ignite)
  425. end
  426. def test_should_create_an_event_requirement
  427. assert_instance_of StateMachine::WhitelistMatcher, @guard.event_requirement
  428. assert_equal [:ignite], @guard.event_requirement.values
  429. end
  430. def test_should_use_a_whitelist_from_matcher
  431. assert_instance_of StateMachine::WhitelistMatcher, @guard.state_requirements.first[:from]
  432. end
  433. def test_should_use_a_whitelist_to_matcher
  434. assert_instance_of StateMachine::WhitelistMatcher, @guard.state_requirements.first[:to]
  435. end
  436. end
  437. class GuardWithMultipleImplicitRequirementsTest < Test::Unit::TestCase
  438. def setup
  439. @object = Object.new
  440. @guard = StateMachine::Guard.new(:parked => :idling, :idling => :first_gear, :on => :ignite)
  441. end
  442. def test_should_create_multiple_state_requirements
  443. assert_equal 2, @guard.state_requirements.length
  444. end
  445. def test_should_not_match_event_as_state_requirement
  446. assert !@guard.matches?(@object, :from => :on, :to => :ignite)
  447. end
  448. def test_should_match_if_from_included_in_any
  449. assert @guard.matches?(@object, :from => :parked)
  450. assert @guard.matches?(@object, :from => :idling)
  451. end
  452. def test_should_not_match_if_from_not_included_in_any
  453. assert !@guard.matches?(@object, :from => :first_gear)
  454. end
  455. def test_should_match_if_to_included_in_any
  456. assert @guard.matches?(@object, :to => :idling)
  457. assert @guard.matches?(@object, :to => :first_gear)
  458. end
  459. def test_should_not_match_if_to_not_included_in_any
  460. assert !@guard.matches?(@object, :to => :parked)
  461. end
  462. def test_should_match_if_all_options_match
  463. assert @guard.matches?(@object, :from => :parked, :to => :idling, :on => :ignite)
  464. assert @guard.matches?(@object, :from => :idling, :to => :first_gear, :on => :ignite)
  465. end
  466. def test_should_not_match_if_any_options_do_not_match
  467. assert !@guard.matches?(@object, :from => :parked, :to => :idling, :on => :park)
  468. assert !@guard.matches?(@object, :from => :parked, :to => :first_gear, :on => :park)
  469. end
  470. def test_should_include_all_known_states
  471. assert_equal [:first_gear, :idling, :parked], @guard.known_states.sort_by {|state| state.to_s}
  472. end
  473. def test_should_not_duplicate_known_statse
  474. guard = StateMachine::Guard.new(:parked => :idling, :first_gear => :idling)
  475. assert_equal [:first_gear, :idling, :parked], guard.known_states.sort_by {|state| state.to_s}
  476. end
  477. end
  478. class GuardWithImplicitFromRequirementMatcherTest < Test::Unit::TestCase
  479. def setup
  480. @matcher = StateMachine::BlacklistMatcher.new(:parked)
  481. @guard = StateMachine::Guard.new(@matcher => :idling)
  482. end
  483. def test_should_not_convert_from_to_whitelist_matcher
  484. assert_equal @matcher, @guard.state_requirements.first[:from]
  485. end
  486. def test_should_convert_to_to_whitelist_matcher
  487. assert_instance_of StateMachine::WhitelistMatcher, @guard.state_requirements.first[:to]
  488. end
  489. end
  490. class GuardWithImplicitToRequirementMatcherTest < Test::Unit::TestCase
  491. def setup
  492. @matcher = StateMachine::BlacklistMatcher.new(:idling)
  493. @guard = StateMachine::Guard.new(:parked => @matcher)
  494. end
  495. def test_should_convert_from_to_whitelist_matcher
  496. assert_instance_of StateMachine::WhitelistMatcher, @guard.state_requirements.first[:from]
  497. end
  498. def test_should_not_convert_to_to_whitelist_matcher
  499. assert_equal @matcher, @guard.state_requirements.first[:to]
  500. end
  501. end
  502. class GuardWithImplicitAndExplicitRequirementsTest < Test::Unit::TestCase
  503. def setup
  504. @guard = StateMachine::Guard.new(:parked => :idling, :from => :parked)
  505. end
  506. def test_should_create_multiple_requirements
  507. assert_equal 2, @guard.state_requirements.length
  508. end
  509. def test_should_create_implicit_requirements_for_implicit_options
  510. assert(@guard.state_requirements.any? do |state_requirement|
  511. state_requirement[:from].values == [:parked] && state_requirement[:to].values == [:idling]
  512. end)
  513. end
  514. def test_should_create_implicit_requirements_for_explicit_options
  515. assert(@guard.state_requirements.any? do |state_requirement|
  516. state_requirement[:from].values == [:from] && state_requirement[:to].values == [:parked]
  517. end)
  518. end
  519. end
  520. class GuardWithIfConditionalTest < Test::Unit::TestCase
  521. def setup
  522. @object = Object.new
  523. end
  524. def test_should_have_an_if_condition
  525. guard = StateMachine::Guard.new(:if => lambda {true})
  526. assert_not_nil guard.if_condition
  527. end
  528. def test_should_match_if_true
  529. guard = StateMachine::Guard.new(:if => lambda {true})
  530. assert guard.matches?(@object)
  531. end
  532. def test_should_not_match_if_false
  533. guard = StateMachine::Guard.new(:if => lambda {false})
  534. assert !guard.matches?(@object)
  535. end
  536. def test_should_be_nil_if_unmatched
  537. guard = StateMachine::Guard.new(:if => lambda {false})
  538. assert_nil guard.match(@object)
  539. end
  540. end
  541. class GuardWithMultipleIfConditionalsTest < Test::Unit::TestCase
  542. def setup
  543. @object = Object.new
  544. end
  545. def test_should_match_if_all_are_true
  546. guard = StateMachine::Guard.new(:if => [lambda {true}, lambda {true}])
  547. assert guard.match(@object)
  548. end
  549. def test_should_not_match_if_any_are_false
  550. guard = StateMachine::Guard.new(:if => [lambda {true}, lambda {false}])
  551. assert !guard.match(@object)
  552. guard = StateMachine::Guard.new(:if => [lambda {false}, lambda {true}])
  553. assert !guard.match(@object)
  554. end
  555. end
  556. class GuardWithUnlessConditionalTest < Test::Unit::TestCase
  557. def setup
  558. @object = Object.new
  559. end
  560. def test_should_have_an_unless_condition
  561. guard = StateMachine::Guard.new(:unless => lambda {true})
  562. assert_not_nil guard.unless_condition
  563. end
  564. def test_should_match_if_false
  565. guard = StateMachine::Guard.new(:unless => lambda {false})
  566. assert guard.matches?(@object)
  567. end
  568. def test_should_not_match_if_true
  569. guard = StateMachine::Guard.new(:unless => lambda {true})
  570. assert !guard.matches?(@object)
  571. end
  572. def test_should_be_nil_if_unmatched
  573. guard = StateMachine::Guard.new(:unless => lambda {true})
  574. assert_nil guard.match(@object)
  575. end
  576. end
  577. class GuardWithMultipleUnlessConditionalsTest < Test::Unit::TestCase
  578. def setup
  579. @object = Object.new
  580. end
  581. def test_should_match_if_all_are_false
  582. guard = StateMachine::Guard.new(:unless => [lambda {false}, lambda {false}])
  583. assert guard.match(@object)
  584. end
  585. def test_should_not_match_if_any_are_true
  586. guard = StateMachine::Guard.new(:unless => [lambda {true}, lambda {false}])
  587. assert !guard.match(@object)
  588. guard = StateMachine::Guard.new(:unless => [lambda {false}, lambda {true}])
  589. assert !guard.match(@object)
  590. end
  591. end
  592. class GuardWithConflictingConditionalsTest < Test::Unit::TestCase
  593. def test_should_match_if_if_is_true_and_unless_is_false
  594. guard = StateMachine::Guard.new(:if => lambda {true}, :unless => lambda {false})
  595. assert guard.match(@object)
  596. end
  597. def test_should_not_match_if_if_is_false_and_unless_is_true
  598. guard = StateMachine::Guard.new(:if => lambda {false}, :unless => lambda {true})
  599. assert !guard.match(@object)
  600. end
  601. def test_should_not_match_if_if_is_false_and_unless_is_false
  602. guard = StateMachine::Guard.new(:if => lambda {false}, :unless => lambda {false})
  603. assert !guard.match(@object)
  604. end
  605. def test_should_not_match_if_if_is_true_and_unless_is_true
  606. guard = StateMachine::Guard.new(:if => lambda {true}, :unless => lambda {true})
  607. assert !guard.match(@object)
  608. end
  609. end
  610. begin
  611. # Load library
  612. require 'rubygems'
  613. require 'graphviz'
  614. class GuardDrawingTest < Test::Unit::TestCase
  615. def setup
  616. @machine = StateMachine::Machine.new(Class.new)
  617. states = [:parked, :idling]
  618. graph = GraphViz.new('G')
  619. states.each {|state| graph.add_node(state.to_s)}
  620. @guard = StateMachine::Guard.new(:from => :idling, :to => :parked)
  621. @edges = @guard.draw(graph, :park, states)
  622. end
  623. def test_should_create_edges
  624. assert_equal 1, @edges.size
  625. end
  626. def test_should_use_from_state_from_start_node
  627. assert_equal 'idling', @edges.first.instance_variable_get('@xNodeOne')
  628. end
  629. def test_should_use_to_state_for_end_node
  630. assert_equal 'parked', @edges.first.instance_variable_get('@xNodeTwo')
  631. end
  632. def test_should_use_event_name_as_label
  633. assert_equal 'park', @edges.first['label']
  634. end
  635. end
  636. class GuardDrawingWithFromRequirementTest < Test::Unit::TestCase
  637. def setup
  638. @machine = StateMachine::Machine.new(Class.new)
  639. states = [:parked, :idling, :first_gear]
  640. graph = GraphViz.new('G')
  641. states.each {|state| graph.add_node(state.to_s)}
  642. @guard = StateMachine::Guard.new(:from => [:idling, :first_gear], :to => :parked)
  643. @edges = @guard.draw(graph, :park, states)
  644. end
  645. def test_should_generate_edges_for_each_valid_from_state
  646. [:idling, :first_gear].each_with_index do |from_state, index|
  647. edge = @edges[index]
  648. assert_equal from_state.to_s, edge.instance_variable_get('@xNodeOne')
  649. assert_equal 'parked', edge.instance_variable_get('@xNodeTwo')
  650. end
  651. end
  652. end
  653. class GuardDrawingWithExceptFromRequirementTest < Test::Unit::TestCase
  654. def setup
  655. @machine = StateMachine::Machine.new(Class.new)
  656. states = [:parked, :idling, :first_gear]
  657. graph = GraphViz.new('G')
  658. states.each {|state| graph.add_node(state.to_s)}
  659. @guard = StateMachine::Guard.new(:except_from => :parked, :to => :parked)
  660. @edges = @guard.draw(graph, :park, states)
  661. end
  662. def test_should_generate_edges_for_each_valid_from_state
  663. %w(idling first_gear).each_with_index do |from_state, index|
  664. edge = @edges[index]
  665. assert_equal from_state, edge.instance_variable_get('@xNodeOne')
  666. assert_equal 'parked', edge.instance_variable_get('@xNodeTwo')
  667. end
  668. end
  669. end
  670. class GuardDrawingWithoutFromRequirementTest < Test::Unit::TestCase
  671. def setup
  672. @machine = StateMachine::Machine.new(Class.new)
  673. states = [:parked, :idling, :first_gear]
  674. graph = GraphViz.new('G')
  675. states.each {|state| graph.add_node(state.to_s)}
  676. @guard = StateMachine::Guard.new(:to => :parked)
  677. @edges = @guard.draw(graph, :park, states)
  678. end
  679. def test_should_generate_edges_for_each_valid_from_state
  680. %w(parked idling first_gear).each_with_index do |from_state, index|
  681. edge = @edges[index]
  682. assert_equal from_state, edge.instance_variable_get('@xNodeOne')
  683. assert_equal 'parked', edge.instance_variable_get('@xNodeTwo')
  684. end
  685. end
  686. end
  687. class GuardDrawingWithoutToRequirementTest < Test::Unit::TestCase
  688. def setup
  689. @machine = StateMachine::Machine.new(Class.new)
  690. graph = GraphViz.new('G')
  691. graph.add_node('parked')
  692. @guard = StateMachine::Guard.new(:from => :parked)
  693. @edges = @guard.draw(graph, :park, [:parked])
  694. end
  695. def test_should_create_loopback_edge
  696. assert_equal 'parked', @edges.first.instance_variable_get('@xNodeOne')
  697. assert_equal 'parked', @edges.first.instance_variable_get('@xNodeTwo')
  698. end
  699. end
  700. class GuardDrawingWithNilStateTest < Test::Unit::TestCase
  701. def setup
  702. @machine = StateMachine::Machine.new(Class.new)
  703. graph = GraphViz.new('G')
  704. graph.add_node('parked')
  705. @guard = StateMachine::Guard.new(:from => :idling, :to => nil)
  706. @edges = @guard.draw(graph, :park, [nil, :idling])
  707. end
  708. def test_should_generate_edges_for_each_valid_from_state
  709. assert_equal 'idling', @edges.first.instance_variable_get('@xNodeOne')
  710. assert_equal 'nil', @edges.first.instance_variable_get('@xNodeTwo')
  711. end
  712. end
  713. rescue LoadError
  714. $stderr.puts 'Skipping GraphViz StateMachine::Guard tests. `gem install ruby-graphviz` and try again.'
  715. end