/test/unit/atom_test.rb

https://github.com/GunioRobot/method_trails · Ruby · 431 lines · 380 code · 46 blank · 5 comment · 0 complexity · 68be34bc3d9fc181b238839a6f88e1a2 MD5 · raw file

  1. require 'test/unit'
  2. require 'require_relative'
  3. require_relative '/../../lib/classes/atom'
  4. module ShouldBeAnAtom
  5. def test_should_be_of_atom_class
  6. assert_equal(MethodTrails::Atom, @atom.class)
  7. end
  8. def test_should_respond_to_atom?
  9. assert(@atom.respond_to?(:atom?))
  10. end
  11. def test_should_be_an_atom
  12. assert(@atom.atom?)
  13. end
  14. def test_should_be_valid
  15. assert(@atom.valid?)
  16. end
  17. def test_should_respond_to_s_exp?
  18. assert(@atom.respond_to?(:s_exp?))
  19. end
  20. def test_should_not_be_an_s_exp
  21. assert_equal(false, @atom.s_exp?)
  22. end
  23. end
  24. module ShouldBeARegularAtom
  25. def test_should_be_regular_atom
  26. assert(@atom.regular?)
  27. end
  28. include ShouldBeAnAtom
  29. end
  30. module ShouldBeACapturingAtom
  31. def test_should_be_capturing_atom
  32. assert(@atom.capturing?)
  33. end
  34. include ShouldBeAnAtom
  35. end
  36. module ShouldNotBeConvertibleToAtom
  37. def test_should_raise_atom_exception
  38. assert_raise(MethodTrails::AtomException) do
  39. MethodTrails::Atom.new(@attempted_contents)
  40. end
  41. end
  42. end
  43. module ShouldMatchAnyString
  44. def test_should_match_any_string
  45. assert(@atom.match?("government".to_atom))
  46. assert(@atom.match?("for the".to_atom))
  47. assert(@atom.match?("people".to_atom))
  48. end
  49. end
  50. module ShouldNotMatchAnyString
  51. def test_should_not_match_any_string
  52. assert_equal(false, @atom.match?("government".to_atom))
  53. assert_equal(false, @atom.match?("for the".to_atom))
  54. assert_equal(false, @atom.match?("people".to_atom))
  55. end
  56. end
  57. module ShouldMatchAnyInteger
  58. def test_should_match_any_integer
  59. assert(@atom.match?(5.to_atom))
  60. assert(@atom.match?(50.to_atom))
  61. assert(@atom.match?(500.to_atom))
  62. end
  63. end
  64. module ShouldNotMatchAnyInteger
  65. def test_should_not_match_any_integer
  66. assert_equal(false, @atom.match?(5.to_atom))
  67. assert_equal(false, @atom.match?(50.to_atom))
  68. assert_equal(false, @atom.match?(500.to_atom))
  69. end
  70. end
  71. module ShouldMatchAnySymbol
  72. def test_should_match_any_symbol
  73. assert(@atom.match?(:cymbal.to_atom))
  74. assert(@atom.match?(:timpani.to_atom))
  75. assert(@atom.match?(:xylophone.to_atom))
  76. end
  77. end
  78. module ShouldNotMatchAnySymbol
  79. def test_not_should_match_any_symbol
  80. assert_equal(false, @atom.match?(:cymbal.to_atom))
  81. assert_equal(false, @atom.match?(:timpani.to_atom))
  82. assert_equal(false, @atom.match?(:xylophone.to_atom))
  83. end
  84. end
  85. module ShouldRaiseExceptionWhenAttemptingCapture
  86. def test_should_raise_exception_when_attempting_capture
  87. assert_raise(MethodTrails::AtomException) do
  88. @atom.capture(@attempted_capture)
  89. end
  90. end
  91. end
  92. module ShouldReturnNilFromTryCapture
  93. def test_try_capture_integer_should_fail
  94. assert_equal(nil, @atom.try_capture(40.to_atom))
  95. end
  96. def test_try_capture_string_should_fail
  97. assert_equal(nil, @atom.try_capture("cheese".to_atom))
  98. end
  99. def test_try_capture_symbol_should_fail
  100. assert_equal(nil, @atom.try_capture(:symbol.to_atom))
  101. end
  102. end
  103. # ====================
  104. class TestNilAtom < Test::Unit::TestCase
  105. def setup
  106. @atom = MethodTrails::Atom.new(nil)
  107. end
  108. include ShouldBeAnAtom
  109. end
  110. class TestFalseAtom < Test::Unit::TestCase
  111. def setup
  112. @atom = MethodTrails::Atom.new(false)
  113. end
  114. include ShouldBeAnAtom
  115. end
  116. class TestTrueAtom < Test::Unit::TestCase
  117. def setup
  118. @atom = MethodTrails::Atom.new(true)
  119. end
  120. include ShouldBeAnAtom
  121. end
  122. # --------------------
  123. class TestIntegerToRegularAtom < Test::Unit::TestCase
  124. def setup
  125. @atom = 8.to_atom
  126. end
  127. include ShouldBeARegularAtom
  128. end
  129. class TestIntegerToCapturingAtom < Test::Unit::TestCase
  130. def setup
  131. @atom = 8.to_atom(false)
  132. end
  133. include ShouldBeACapturingAtom
  134. end
  135. class TestStringToRegularAtom < Test::Unit::TestCase
  136. def setup
  137. @atom = "a string".to_atom
  138. end
  139. def test_to_s
  140. assert_equal("a string", @atom.to_s)
  141. end
  142. include ShouldBeARegularAtom
  143. end
  144. class TestStringToCapturingAtom < Test::Unit::TestCase
  145. def setup
  146. @atom = "%a capturing string".to_atom
  147. end
  148. def test_to_s
  149. assert_equal("%a capturing string", @atom.to_s)
  150. end
  151. include ShouldBeACapturingAtom
  152. end
  153. class TestSymbolToRegularAtom < Test::Unit::TestCase
  154. def setup
  155. @atom = :a_symbol.to_atom
  156. end
  157. def test_inspect
  158. assert_equal(":a_symbol", @atom.inspect)
  159. end
  160. include ShouldBeARegularAtom
  161. end
  162. class TestSymbolToCapturingAtom < Test::Unit::TestCase
  163. def setup
  164. @atom = :"%a capturing symbol".to_atom
  165. end
  166. def test_inspect
  167. assert_equal('%:"a capturing symbol"', @atom.inspect)
  168. end
  169. include ShouldBeACapturingAtom
  170. end
  171. class TestTrueClassToRegularAtom < Test::Unit::TestCase
  172. def setup
  173. @atom = true.to_atom
  174. end
  175. include ShouldBeARegularAtom
  176. end
  177. class TestTrueClassToCapturingAtom < Test::Unit::TestCase
  178. def setup
  179. @atom = true.to_atom(false)
  180. end
  181. include ShouldBeACapturingAtom
  182. end
  183. class TestFalseClassToRegularAtom < Test::Unit::TestCase
  184. def setup
  185. @atom = false.to_atom
  186. end
  187. include ShouldBeARegularAtom
  188. end
  189. class TestFalseClassToCapturingAtom < Test::Unit::TestCase
  190. def setup
  191. @atom = false.to_atom(false)
  192. end
  193. include ShouldBeACapturingAtom
  194. end
  195. class TestNilClassToRegularAtom < Test::Unit::TestCase
  196. def setup
  197. @atom = nil.to_atom
  198. end
  199. include ShouldBeARegularAtom
  200. end
  201. class TestNilClassToCapturingAtom < Test::Unit::TestCase
  202. def setup
  203. @atom = nil.to_atom(false)
  204. end
  205. include ShouldBeACapturingAtom
  206. end
  207. # --------------------
  208. class TestAttemptBuildingAtomFromArray < Test::Unit::TestCase
  209. def setup
  210. @attempted_contents = [:this, :should, :not, :work]
  211. end
  212. include ShouldNotBeConvertibleToAtom
  213. end
  214. class TestAttemptBuildingAtomFromHash < Test::Unit::TestCase
  215. def setup
  216. @attempted_contents = {:this => "should not work"}
  217. end
  218. include ShouldNotBeConvertibleToAtom
  219. end
  220. # --------------------
  221. class TestRegularIntegerMatch < Test::Unit::TestCase
  222. def setup
  223. @atom = MethodTrails::Atom.new(50)
  224. end
  225. def test_should_match_same_integer
  226. assert(@atom.match?(50.to_atom))
  227. end
  228. def test_should_not_match_different_integer
  229. assert_equal(false, @atom.match?(5.to_atom))
  230. assert_equal(false, @atom.match?(500.to_atom))
  231. end
  232. include ShouldNotMatchAnyString
  233. include ShouldNotMatchAnySymbol
  234. include ShouldBeARegularAtom
  235. end
  236. class TestCapturingIntegerMatch < Test::Unit::TestCase
  237. def setup
  238. @atom = MethodTrails::Atom.new(50, true)
  239. end
  240. include ShouldMatchAnyInteger
  241. include ShouldNotMatchAnyString
  242. include ShouldNotMatchAnySymbol
  243. include ShouldBeACapturingAtom
  244. end
  245. class TestRegularStringMatch < Test::Unit::TestCase
  246. def setup
  247. @atom = MethodTrails::Atom.new("david")
  248. end
  249. def test_should_match_same_string
  250. assert(@atom.match?("david".to_atom))
  251. end
  252. def test_should_not_match_different_string
  253. assert(@atom.match?("david".to_atom))
  254. end
  255. include ShouldBeARegularAtom
  256. include ShouldNotMatchAnyInteger
  257. include ShouldNotMatchAnySymbol
  258. end
  259. class TestCapturingStringMatch < Test::Unit::TestCase
  260. def setup
  261. @atom = MethodTrails::Atom.new("label", true)
  262. end
  263. include ShouldBeACapturingAtom
  264. include ShouldMatchAnyString
  265. include ShouldNotMatchAnyInteger
  266. include ShouldNotMatchAnySymbol
  267. end
  268. class TestRegularSymbolMatch < Test::Unit::TestCase
  269. def setup
  270. @atom = MethodTrails::Atom.new(:cymbal)
  271. end
  272. def test_should_match_same_symbol
  273. assert(@atom.match?(:cymbal.to_atom))
  274. end
  275. def test_should_not_match_different_symbol
  276. assert_equal(false, @atom.match?(:timpani.to_atom))
  277. assert_equal(false, @atom.match?(:xylophone.to_atom))
  278. end
  279. include ShouldBeARegularAtom
  280. include ShouldNotMatchAnyInteger
  281. include ShouldNotMatchAnyString
  282. end
  283. class TestCapturingSymbolMatch < Test::Unit::TestCase
  284. def setup
  285. @atom = MethodTrails::Atom.new(:cymbal, true)
  286. end
  287. include ShouldBeACapturingAtom
  288. include ShouldMatchAnySymbol
  289. include ShouldNotMatchAnyInteger
  290. include ShouldNotMatchAnyString
  291. end
  292. # --------------------
  293. class TestRegularIntegerCapture < Test::Unit::TestCase
  294. def setup
  295. @atom = MethodTrails::Atom.new(23)
  296. @attempted_capture = 30.to_atom
  297. end
  298. include ShouldRaiseExceptionWhenAttemptingCapture
  299. include ShouldReturnNilFromTryCapture
  300. end
  301. class TestRegularStringCapture < Test::Unit::TestCase
  302. def setup
  303. @atom = MethodTrails::Atom.new("string")
  304. @attempted_capture = "string".to_atom
  305. end
  306. include ShouldRaiseExceptionWhenAttemptingCapture
  307. include ShouldReturnNilFromTryCapture
  308. end
  309. class TestRegularSymbolCapture < Test::Unit::TestCase
  310. def setup
  311. @atom = MethodTrails::Atom.new(:symbol)
  312. @attempted_capture = :symbol.to_atom
  313. end
  314. include ShouldRaiseExceptionWhenAttemptingCapture
  315. include ShouldReturnNilFromTryCapture
  316. end
  317. class TestCapturingIntegerCapture < Test::Unit::TestCase
  318. def setup
  319. @atom = MethodTrails::Atom.new(23, true)
  320. end
  321. def test_capturing_integer_should_succeed
  322. assert_equal(40, @atom.capture(40.to_atom))
  323. end
  324. def test_capturing_string_should_fail
  325. assert_equal(nil, @atom.capture("cheese".to_atom))
  326. end
  327. def test_capturing_symbol_should_fail
  328. assert_equal(nil, @atom.capture(:tejas_club.to_atom))
  329. end
  330. def test_try_capture_integer_should_succeed
  331. assert_equal(40, @atom.try_capture(40.to_atom))
  332. end
  333. def test_try_capture_string_should_fail
  334. assert_equal(nil, @atom.try_capture("string".to_atom))
  335. end
  336. def test_try_capture_symbol_should_fail
  337. assert_equal(nil, @atom.try_capture(:symbol.to_atom))
  338. end
  339. end
  340. class TestCapturingStringCapture < Test::Unit::TestCase
  341. def setup
  342. @atom = MethodTrails::Atom.new("string", true)
  343. end
  344. def test_capturing_integer_should_fail
  345. assert_equal(nil, @atom.capture(40.to_atom))
  346. end
  347. def test_capturing_string_should_succeed
  348. assert_equal("cheese", @atom.capture("cheese".to_atom))
  349. end
  350. def test_capturing_symbol_should_fail
  351. assert_equal(nil, @atom.capture(:tejas_club.to_atom))
  352. end
  353. def test_try_capture_integer_should_fail
  354. assert_equal(nil, @atom.try_capture(40.to_atom))
  355. end
  356. def test_try_capture_string_should_succeed
  357. assert_equal("cheese", @atom.try_capture("cheese".to_atom))
  358. end
  359. def test_try_capture_symbol_should_fail
  360. assert_equal(nil, @atom.try_capture(:symbol.to_atom))
  361. end
  362. end
  363. class TestCapturingSymbolCapture < Test::Unit::TestCase
  364. def setup
  365. @atom = MethodTrails::Atom.new(:symbol, true)
  366. end
  367. def test_capturing_integer_should_fail
  368. assert_equal(nil, @atom.capture(40.to_atom))
  369. end
  370. def test_capturing_string_should_fail
  371. assert_equal(nil, @atom.capture("cheese".to_atom))
  372. end
  373. def test_capturing_symbol_should_succeed
  374. assert_equal(:tejas_club, @atom.capture(:tejas_club.to_atom))
  375. end
  376. def test_try_capture_integer_should_fail
  377. assert_equal(nil, @atom.try_capture(40.to_atom))
  378. end
  379. def test_try_capture_string_should_fail
  380. assert_equal(nil, @atom.try_capture("cheese".to_atom))
  381. end
  382. def test_try_capture_symbol_should_succeed
  383. assert_equal(:symbol, @atom.try_capture(:symbol.to_atom))
  384. end
  385. end