/lib/gemcache/ruby/1.9.1/gems/state_machine-1.1.2/test/unit/branch_test.rb

https://bitbucket.org/technopunk2099/metasploit-framework · Ruby · 888 lines · 699 code · 188 blank · 1 comment · 6 complexity · e05f6e480a2d0bcc005b6d9bcc54aeab 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 BranchWithToRequirementTest < Test::Unit::TestCase
  100. def setup
  101. @object = Object.new
  102. @branch = StateMachine::Branch.new(:to => :idling)
  103. end
  104. def test_should_use_a_whitelist_matcher
  105. assert_instance_of StateMachine::WhitelistMatcher, @branch.state_requirements.first[:to]
  106. end
  107. def test_should_match_if_not_specified
  108. assert @branch.matches?(@object, :from => :parked)
  109. end
  110. def test_should_match_if_included
  111. assert @branch.matches?(@object, :to => :idling)
  112. end
  113. def test_should_not_match_if_not_included
  114. assert !@branch.matches?(@object, :to => :parked)
  115. end
  116. def test_should_not_match_if_nil
  117. assert !@branch.matches?(@object, :to => nil)
  118. end
  119. def test_should_ignore_from
  120. assert @branch.matches?(@object, :to => :idling, :from => :parked)
  121. end
  122. def test_should_ignore_on
  123. assert @branch.matches?(@object, :to => :idling, :on => :ignite)
  124. end
  125. def test_should_be_included_in_known_states
  126. assert_equal [:idling], @branch.known_states
  127. end
  128. def test_should_include_requirement_in_match
  129. match = @branch.match(@object, :to => :idling)
  130. assert_equal @branch.state_requirements.first[:to], match[:to]
  131. end
  132. end
  133. class BranchWithMultipleToRequirementsTest < Test::Unit::TestCase
  134. def setup
  135. @object = Object.new
  136. @branch = StateMachine::Branch.new(:to => [:idling, :parked])
  137. end
  138. def test_should_match_if_included
  139. assert @branch.matches?(@object, :to => :idling)
  140. end
  141. def test_should_not_match_if_not_included
  142. assert !@branch.matches?(@object, :to => :first_gear)
  143. end
  144. def test_should_be_included_in_known_states
  145. assert_equal [:idling, :parked], @branch.known_states
  146. end
  147. end
  148. class BranchWithOnRequirementTest < Test::Unit::TestCase
  149. def setup
  150. @object = Object.new
  151. @branch = StateMachine::Branch.new(:on => :ignite)
  152. end
  153. def test_should_use_a_whitelist_matcher
  154. assert_instance_of StateMachine::WhitelistMatcher, @branch.event_requirement
  155. end
  156. def test_should_match_if_not_specified
  157. assert @branch.matches?(@object, :from => :parked)
  158. end
  159. def test_should_match_if_included
  160. assert @branch.matches?(@object, :on => :ignite)
  161. end
  162. def test_should_not_match_if_not_included
  163. assert !@branch.matches?(@object, :on => :park)
  164. end
  165. def test_should_not_match_if_nil
  166. assert !@branch.matches?(@object, :on => nil)
  167. end
  168. def test_should_ignore_to
  169. assert @branch.matches?(@object, :on => :ignite, :to => :parked)
  170. end
  171. def test_should_ignore_from
  172. assert @branch.matches?(@object, :on => :ignite, :from => :parked)
  173. end
  174. def test_should_not_be_included_in_known_states
  175. assert_equal [], @branch.known_states
  176. end
  177. def test_should_include_requirement_in_match
  178. match = @branch.match(@object, :on => :ignite)
  179. assert_equal @branch.event_requirement, match[:on]
  180. end
  181. end
  182. class BranchWithMultipleOnRequirementsTest < Test::Unit::TestCase
  183. def setup
  184. @object = Object.new
  185. @branch = StateMachine::Branch.new(:on => [:ignite, :park])
  186. end
  187. def test_should_match_if_included
  188. assert @branch.matches?(@object, :on => :ignite)
  189. end
  190. def test_should_not_match_if_not_included
  191. assert !@branch.matches?(@object, :on => :shift_up)
  192. end
  193. end
  194. class BranchWithExceptFromRequirementTest < Test::Unit::TestCase
  195. def setup
  196. @object = Object.new
  197. @branch = StateMachine::Branch.new(:except_from => :parked)
  198. end
  199. def test_should_use_a_blacklist_matcher
  200. assert_instance_of StateMachine::BlacklistMatcher, @branch.state_requirements.first[:from]
  201. end
  202. def test_should_match_if_not_included
  203. assert @branch.matches?(@object, :from => :idling)
  204. end
  205. def test_should_not_match_if_included
  206. assert !@branch.matches?(@object, :from => :parked)
  207. end
  208. def test_should_match_if_nil
  209. assert @branch.matches?(@object, :from => nil)
  210. end
  211. def test_should_ignore_to
  212. assert @branch.matches?(@object, :from => :idling, :to => :parked)
  213. end
  214. def test_should_ignore_on
  215. assert @branch.matches?(@object, :from => :idling, :on => :ignite)
  216. end
  217. def test_should_be_included_in_known_states
  218. assert_equal [:parked], @branch.known_states
  219. end
  220. end
  221. class BranchWithMultipleExceptFromRequirementsTest < Test::Unit::TestCase
  222. def setup
  223. @object = Object.new
  224. @branch = StateMachine::Branch.new(:except_from => [:idling, :parked])
  225. end
  226. def test_should_match_if_not_included
  227. assert @branch.matches?(@object, :from => :first_gear)
  228. end
  229. def test_should_not_match_if_included
  230. assert !@branch.matches?(@object, :from => :idling)
  231. end
  232. def test_should_be_included_in_known_states
  233. assert_equal [:idling, :parked], @branch.known_states
  234. end
  235. end
  236. class BranchWithExceptToRequirementTest < Test::Unit::TestCase
  237. def setup
  238. @object = Object.new
  239. @branch = StateMachine::Branch.new(:except_to => :idling)
  240. end
  241. def test_should_use_a_blacklist_matcher
  242. assert_instance_of StateMachine::BlacklistMatcher, @branch.state_requirements.first[:to]
  243. end
  244. def test_should_match_if_not_included
  245. assert @branch.matches?(@object, :to => :parked)
  246. end
  247. def test_should_not_match_if_included
  248. assert !@branch.matches?(@object, :to => :idling)
  249. end
  250. def test_should_match_if_nil
  251. assert @branch.matches?(@object, :to => nil)
  252. end
  253. def test_should_ignore_from
  254. assert @branch.matches?(@object, :to => :parked, :from => :idling)
  255. end
  256. def test_should_ignore_on
  257. assert @branch.matches?(@object, :to => :parked, :on => :ignite)
  258. end
  259. def test_should_be_included_in_known_states
  260. assert_equal [:idling], @branch.known_states
  261. end
  262. end
  263. class BranchWithMultipleExceptToRequirementsTest < Test::Unit::TestCase
  264. def setup
  265. @object = Object.new
  266. @branch = StateMachine::Branch.new(:except_to => [:idling, :parked])
  267. end
  268. def test_should_match_if_not_included
  269. assert @branch.matches?(@object, :to => :first_gear)
  270. end
  271. def test_should_not_match_if_included
  272. assert !@branch.matches?(@object, :to => :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 BranchWithExceptOnRequirementTest < Test::Unit::TestCase
  279. def setup
  280. @object = Object.new
  281. @branch = StateMachine::Branch.new(:except_on => :ignite)
  282. end
  283. def test_should_use_a_blacklist_matcher
  284. assert_instance_of StateMachine::BlacklistMatcher, @branch.event_requirement
  285. end
  286. def test_should_match_if_not_included
  287. assert @branch.matches?(@object, :on => :park)
  288. end
  289. def test_should_not_match_if_included
  290. assert !@branch.matches?(@object, :on => :ignite)
  291. end
  292. def test_should_match_if_nil
  293. assert @branch.matches?(@object, :on => nil)
  294. end
  295. def test_should_ignore_to
  296. assert @branch.matches?(@object, :on => :park, :to => :idling)
  297. end
  298. def test_should_ignore_from
  299. assert @branch.matches?(@object, :on => :park, :from => :parked)
  300. end
  301. def test_should_not_be_included_in_known_states
  302. assert_equal [], @branch.known_states
  303. end
  304. end
  305. class BranchWithMultipleExceptOnRequirementsTest < Test::Unit::TestCase
  306. def setup
  307. @object = Object.new
  308. @branch = StateMachine::Branch.new(:except_on => [:ignite, :park])
  309. end
  310. def test_should_match_if_not_included
  311. assert @branch.matches?(@object, :on => :shift_up)
  312. end
  313. def test_should_not_match_if_included
  314. assert !@branch.matches?(@object, :on => :ignite)
  315. end
  316. end
  317. class BranchWithConflictingFromRequirementsTest < Test::Unit::TestCase
  318. def test_should_raise_an_exception
  319. exception = assert_raise(ArgumentError) { StateMachine::Branch.new(:from => :parked, :except_from => :parked) }
  320. assert_equal 'Conflicting keys: from, except_from', exception.message
  321. end
  322. end
  323. class BranchWithConflictingToRequirementsTest < Test::Unit::TestCase
  324. def test_should_raise_an_exception
  325. exception = assert_raise(ArgumentError) { StateMachine::Branch.new(:to => :idling, :except_to => :idling) }
  326. assert_equal 'Conflicting keys: to, except_to', exception.message
  327. end
  328. end
  329. class BranchWithConflictingOnRequirementsTest < Test::Unit::TestCase
  330. def test_should_raise_an_exception
  331. exception = assert_raise(ArgumentError) { StateMachine::Branch.new(:on => :ignite, :except_on => :ignite) }
  332. assert_equal 'Conflicting keys: on, except_on', exception.message
  333. end
  334. end
  335. class BranchWithDifferentRequirementsTest < Test::Unit::TestCase
  336. def setup
  337. @object = Object.new
  338. @branch = StateMachine::Branch.new(:from => :parked, :to => :idling, :on => :ignite)
  339. end
  340. def test_should_match_empty_query
  341. assert @branch.matches?(@object)
  342. end
  343. def test_should_match_if_all_requirements_match
  344. assert @branch.matches?(@object, :from => :parked, :to => :idling, :on => :ignite)
  345. end
  346. def test_should_not_match_if_from_not_included
  347. assert !@branch.matches?(@object, :from => :idling)
  348. end
  349. def test_should_not_match_if_to_not_included
  350. assert !@branch.matches?(@object, :to => :parked)
  351. end
  352. def test_should_not_match_if_on_not_included
  353. assert !@branch.matches?(@object, :on => :park)
  354. end
  355. def test_should_be_nil_if_unmatched
  356. assert_nil @branch.match(@object, :from => :parked, :to => :idling, :on => :park)
  357. end
  358. def test_should_include_all_known_states
  359. assert_equal [:parked, :idling], @branch.known_states
  360. end
  361. def test_should_not_duplicate_known_statse
  362. branch = StateMachine::Branch.new(:except_from => :idling, :to => :idling, :on => :ignite)
  363. assert_equal [:idling], branch.known_states
  364. end
  365. end
  366. class BranchWithNilRequirementsTest < Test::Unit::TestCase
  367. def setup
  368. @object = Object.new
  369. @branch = StateMachine::Branch.new(:from => nil, :to => nil)
  370. end
  371. def test_should_match_empty_query
  372. assert @branch.matches?(@object)
  373. end
  374. def test_should_match_if_all_requirements_match
  375. assert @branch.matches?(@object, :from => nil, :to => nil)
  376. end
  377. def test_should_not_match_if_from_not_included
  378. assert !@branch.matches?(@object, :from => :parked)
  379. end
  380. def test_should_not_match_if_to_not_included
  381. assert !@branch.matches?(@object, :to => :idling)
  382. end
  383. def test_should_include_all_known_states
  384. assert_equal [nil], @branch.known_states
  385. end
  386. end
  387. class BranchWithImplicitRequirementTest < Test::Unit::TestCase
  388. def setup
  389. @branch = StateMachine::Branch.new(:parked => :idling, :on => :ignite)
  390. end
  391. def test_should_create_an_event_requirement
  392. assert_instance_of StateMachine::WhitelistMatcher, @branch.event_requirement
  393. assert_equal [:ignite], @branch.event_requirement.values
  394. end
  395. def test_should_use_a_whitelist_from_matcher
  396. assert_instance_of StateMachine::WhitelistMatcher, @branch.state_requirements.first[:from]
  397. end
  398. def test_should_use_a_whitelist_to_matcher
  399. assert_instance_of StateMachine::WhitelistMatcher, @branch.state_requirements.first[:to]
  400. end
  401. end
  402. class BranchWithMultipleImplicitRequirementsTest < Test::Unit::TestCase
  403. def setup
  404. @object = Object.new
  405. @branch = StateMachine::Branch.new(:parked => :idling, :idling => :first_gear, :on => :ignite)
  406. end
  407. def test_should_create_multiple_state_requirements
  408. assert_equal 2, @branch.state_requirements.length
  409. end
  410. def test_should_not_match_event_as_state_requirement
  411. assert !@branch.matches?(@object, :from => :on, :to => :ignite)
  412. end
  413. def test_should_match_if_from_included_in_any
  414. assert @branch.matches?(@object, :from => :parked)
  415. assert @branch.matches?(@object, :from => :idling)
  416. end
  417. def test_should_not_match_if_from_not_included_in_any
  418. assert !@branch.matches?(@object, :from => :first_gear)
  419. end
  420. def test_should_match_if_to_included_in_any
  421. assert @branch.matches?(@object, :to => :idling)
  422. assert @branch.matches?(@object, :to => :first_gear)
  423. end
  424. def test_should_not_match_if_to_not_included_in_any
  425. assert !@branch.matches?(@object, :to => :parked)
  426. end
  427. def test_should_match_if_all_options_match
  428. assert @branch.matches?(@object, :from => :parked, :to => :idling, :on => :ignite)
  429. assert @branch.matches?(@object, :from => :idling, :to => :first_gear, :on => :ignite)
  430. end
  431. def test_should_not_match_if_any_options_do_not_match
  432. assert !@branch.matches?(@object, :from => :parked, :to => :idling, :on => :park)
  433. assert !@branch.matches?(@object, :from => :parked, :to => :first_gear, :on => :park)
  434. end
  435. def test_should_include_all_known_states
  436. assert_equal [:first_gear, :idling, :parked], @branch.known_states.sort_by {|state| state.to_s}
  437. end
  438. def test_should_not_duplicate_known_statse
  439. branch = StateMachine::Branch.new(:parked => :idling, :first_gear => :idling)
  440. assert_equal [:first_gear, :idling, :parked], branch.known_states.sort_by {|state| state.to_s}
  441. end
  442. end
  443. class BranchWithImplicitFromRequirementMatcherTest < Test::Unit::TestCase
  444. def setup
  445. @matcher = StateMachine::BlacklistMatcher.new(:parked)
  446. @branch = StateMachine::Branch.new(@matcher => :idling)
  447. end
  448. def test_should_not_convert_from_to_whitelist_matcher
  449. assert_equal @matcher, @branch.state_requirements.first[:from]
  450. end
  451. def test_should_convert_to_to_whitelist_matcher
  452. assert_instance_of StateMachine::WhitelistMatcher, @branch.state_requirements.first[:to]
  453. end
  454. end
  455. class BranchWithImplicitToRequirementMatcherTest < Test::Unit::TestCase
  456. def setup
  457. @matcher = StateMachine::BlacklistMatcher.new(:idling)
  458. @branch = StateMachine::Branch.new(:parked => @matcher)
  459. end
  460. def test_should_convert_from_to_whitelist_matcher
  461. assert_instance_of StateMachine::WhitelistMatcher, @branch.state_requirements.first[:from]
  462. end
  463. def test_should_not_convert_to_to_whitelist_matcher
  464. assert_equal @matcher, @branch.state_requirements.first[:to]
  465. end
  466. end
  467. class BranchWithImplicitAndExplicitRequirementsTest < Test::Unit::TestCase
  468. def setup
  469. @branch = StateMachine::Branch.new(:parked => :idling, :from => :parked)
  470. end
  471. def test_should_create_multiple_requirements
  472. assert_equal 2, @branch.state_requirements.length
  473. end
  474. def test_should_create_implicit_requirements_for_implicit_options
  475. assert(@branch.state_requirements.any? do |state_requirement|
  476. state_requirement[:from].values == [:parked] && state_requirement[:to].values == [:idling]
  477. end)
  478. end
  479. def test_should_create_implicit_requirements_for_explicit_options
  480. assert(@branch.state_requirements.any? do |state_requirement|
  481. state_requirement[:from].values == [:from] && state_requirement[:to].values == [:parked]
  482. end)
  483. end
  484. end
  485. class BranchWithIfConditionalTest < Test::Unit::TestCase
  486. def setup
  487. @object = Object.new
  488. end
  489. def test_should_have_an_if_condition
  490. branch = StateMachine::Branch.new(:if => lambda {true})
  491. assert_not_nil branch.if_condition
  492. end
  493. def test_should_match_if_true
  494. branch = StateMachine::Branch.new(:if => lambda {true})
  495. assert branch.matches?(@object)
  496. end
  497. def test_should_not_match_if_false
  498. branch = StateMachine::Branch.new(:if => lambda {false})
  499. assert !branch.matches?(@object)
  500. end
  501. def test_should_be_nil_if_unmatched
  502. branch = StateMachine::Branch.new(:if => lambda {false})
  503. assert_nil branch.match(@object)
  504. end
  505. end
  506. class BranchWithMultipleIfConditionalsTest < Test::Unit::TestCase
  507. def setup
  508. @object = Object.new
  509. end
  510. def test_should_match_if_all_are_true
  511. branch = StateMachine::Branch.new(:if => [lambda {true}, lambda {true}])
  512. assert branch.match(@object)
  513. end
  514. def test_should_not_match_if_any_are_false
  515. branch = StateMachine::Branch.new(:if => [lambda {true}, lambda {false}])
  516. assert !branch.match(@object)
  517. branch = StateMachine::Branch.new(:if => [lambda {false}, lambda {true}])
  518. assert !branch.match(@object)
  519. end
  520. end
  521. class BranchWithUnlessConditionalTest < Test::Unit::TestCase
  522. def setup
  523. @object = Object.new
  524. end
  525. def test_should_have_an_unless_condition
  526. branch = StateMachine::Branch.new(:unless => lambda {true})
  527. assert_not_nil branch.unless_condition
  528. end
  529. def test_should_match_if_false
  530. branch = StateMachine::Branch.new(:unless => lambda {false})
  531. assert branch.matches?(@object)
  532. end
  533. def test_should_not_match_if_true
  534. branch = StateMachine::Branch.new(:unless => lambda {true})
  535. assert !branch.matches?(@object)
  536. end
  537. def test_should_be_nil_if_unmatched
  538. branch = StateMachine::Branch.new(:unless => lambda {true})
  539. assert_nil branch.match(@object)
  540. end
  541. end
  542. class BranchWithMultipleUnlessConditionalsTest < Test::Unit::TestCase
  543. def setup
  544. @object = Object.new
  545. end
  546. def test_should_match_if_all_are_false
  547. branch = StateMachine::Branch.new(:unless => [lambda {false}, lambda {false}])
  548. assert branch.match(@object)
  549. end
  550. def test_should_not_match_if_any_are_true
  551. branch = StateMachine::Branch.new(:unless => [lambda {true}, lambda {false}])
  552. assert !branch.match(@object)
  553. branch = StateMachine::Branch.new(:unless => [lambda {false}, lambda {true}])
  554. assert !branch.match(@object)
  555. end
  556. end
  557. class BranchWithConflictingConditionalsTest < Test::Unit::TestCase
  558. def test_should_match_if_if_is_true_and_unless_is_false
  559. branch = StateMachine::Branch.new(:if => lambda {true}, :unless => lambda {false})
  560. assert branch.match(@object)
  561. end
  562. def test_should_not_match_if_if_is_false_and_unless_is_true
  563. branch = StateMachine::Branch.new(:if => lambda {false}, :unless => lambda {true})
  564. assert !branch.match(@object)
  565. end
  566. def test_should_not_match_if_if_is_false_and_unless_is_false
  567. branch = StateMachine::Branch.new(:if => lambda {false}, :unless => lambda {false})
  568. assert !branch.match(@object)
  569. end
  570. def test_should_not_match_if_if_is_true_and_unless_is_true
  571. branch = StateMachine::Branch.new(:if => lambda {true}, :unless => lambda {true})
  572. assert !branch.match(@object)
  573. end
  574. end
  575. class BranchWithoutGuardsTest < Test::Unit::TestCase
  576. def setup
  577. @object = Object.new
  578. end
  579. def test_should_match_if_if_is_false
  580. branch = StateMachine::Branch.new(:if => lambda {false})
  581. assert branch.matches?(@object, :guard => false)
  582. end
  583. def test_should_match_if_if_is_true
  584. branch = StateMachine::Branch.new(:if => lambda {true})
  585. assert branch.matches?(@object, :guard => false)
  586. end
  587. def test_should_match_if_unless_is_false
  588. branch = StateMachine::Branch.new(:unless => lambda {false})
  589. assert branch.matches?(@object, :guard => false)
  590. end
  591. def test_should_match_if_unless_is_true
  592. branch = StateMachine::Branch.new(:unless => lambda {true})
  593. assert branch.matches?(@object, :guard => false)
  594. end
  595. end
  596. begin
  597. # Load library
  598. require 'graphviz'
  599. class BranchDrawingTest < Test::Unit::TestCase
  600. def setup
  601. @machine = StateMachine::Machine.new(Class.new)
  602. states = [:parked, :idling]
  603. graph = GraphViz.new('G')
  604. states.each {|state| graph.add_node(state.to_s)}
  605. @branch = StateMachine::Branch.new(:from => :idling, :to => :parked)
  606. @edges = @branch.draw(graph, :park, states)
  607. end
  608. def test_should_create_edges
  609. assert_equal 1, @edges.size
  610. end
  611. def test_should_use_from_state_from_start_node
  612. assert_equal 'idling', @edges.first.instance_variable_get('@xNodeOne')
  613. end
  614. def test_should_use_to_state_for_end_node
  615. assert_equal 'parked', @edges.first.instance_variable_get('@xNodeTwo')
  616. end
  617. def test_should_use_event_name_as_label
  618. assert_equal 'park', @edges.first['label'].to_s.gsub('"', '')
  619. end
  620. end
  621. class BranchDrawingWithFromRequirementTest < Test::Unit::TestCase
  622. def setup
  623. @machine = StateMachine::Machine.new(Class.new)
  624. states = [:parked, :idling, :first_gear]
  625. graph = GraphViz.new('G')
  626. states.each {|state| graph.add_node(state.to_s)}
  627. @branch = StateMachine::Branch.new(:from => [:idling, :first_gear], :to => :parked)
  628. @edges = @branch.draw(graph, :park, states)
  629. end
  630. def test_should_generate_edges_for_each_valid_from_state
  631. [:idling, :first_gear].each_with_index do |from_state, index|
  632. edge = @edges[index]
  633. assert_equal from_state.to_s, edge.instance_variable_get('@xNodeOne')
  634. assert_equal 'parked', edge.instance_variable_get('@xNodeTwo')
  635. end
  636. end
  637. end
  638. class BranchDrawingWithExceptFromRequirementTest < Test::Unit::TestCase
  639. def setup
  640. @machine = StateMachine::Machine.new(Class.new)
  641. states = [:parked, :idling, :first_gear]
  642. graph = GraphViz.new('G')
  643. states.each {|state| graph.add_node(state.to_s)}
  644. @branch = StateMachine::Branch.new(:except_from => :parked, :to => :parked)
  645. @edges = @branch.draw(graph, :park, states)
  646. end
  647. def test_should_generate_edges_for_each_valid_from_state
  648. %w(idling first_gear).each_with_index do |from_state, index|
  649. edge = @edges[index]
  650. assert_equal from_state, edge.instance_variable_get('@xNodeOne')
  651. assert_equal 'parked', edge.instance_variable_get('@xNodeTwo')
  652. end
  653. end
  654. end
  655. class BranchDrawingWithoutFromRequirementTest < Test::Unit::TestCase
  656. def setup
  657. @machine = StateMachine::Machine.new(Class.new)
  658. states = [:parked, :idling, :first_gear]
  659. graph = GraphViz.new('G')
  660. states.each {|state| graph.add_node(state.to_s)}
  661. @branch = StateMachine::Branch.new(:to => :parked)
  662. @edges = @branch.draw(graph, :park, states)
  663. end
  664. def test_should_generate_edges_for_each_valid_from_state
  665. %w(parked idling first_gear).each_with_index do |from_state, index|
  666. edge = @edges[index]
  667. assert_equal from_state, edge.instance_variable_get('@xNodeOne')
  668. assert_equal 'parked', edge.instance_variable_get('@xNodeTwo')
  669. end
  670. end
  671. end
  672. class BranchDrawingWithoutToRequirementTest < Test::Unit::TestCase
  673. def setup
  674. @machine = StateMachine::Machine.new(Class.new)
  675. graph = GraphViz.new('G')
  676. graph.add_node('parked')
  677. @branch = StateMachine::Branch.new(:from => :parked)
  678. @edges = @branch.draw(graph, :park, [:parked])
  679. end
  680. def test_should_create_loopback_edge
  681. assert_equal 'parked', @edges.first.instance_variable_get('@xNodeOne')
  682. assert_equal 'parked', @edges.first.instance_variable_get('@xNodeTwo')
  683. end
  684. end
  685. class BranchDrawingWithNilStateTest < Test::Unit::TestCase
  686. def setup
  687. @machine = StateMachine::Machine.new(Class.new)
  688. graph = GraphViz.new('G')
  689. graph.add_node('parked')
  690. @branch = StateMachine::Branch.new(:from => :idling, :to => nil)
  691. @edges = @branch.draw(graph, :park, [nil, :idling])
  692. end
  693. def test_should_generate_edges_for_each_valid_from_state
  694. assert_equal 'idling', @edges.first.instance_variable_get('@xNodeOne')
  695. assert_equal 'nil', @edges.first.instance_variable_get('@xNodeTwo')
  696. end
  697. end
  698. rescue LoadError
  699. $stderr.puts 'Skipping GraphViz StateMachine::Branch tests. `gem install ruby-graphviz` >= v0.9.0 and try again.'
  700. end unless ENV['TRAVIS']