PageRenderTime 45ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/test/unit/branch_test.rb

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