/app/test/models/test_language.py

https://gitlab.com/gupta.d.gaurav/part-ii-individual-project-dev · Python · 424 lines · 310 code · 64 blank · 50 comment · 2 complexity · fc7ae81313800f15fe27602d4b3fb19b MD5 · raw file

  1. """
  2. Unit tests for language models.
  3. """
  4. import unittest
  5. import logging
  6. import doctest
  7. from app.models.language import *
  8. from app.test import motivating_applications
  9. logging.basicConfig(level=logging.DEBUG,format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
  10. class TestDoctests(unittest.TestCase):
  11. def test_doctests(self):
  12. doctest.testmod(app.models.language)
  13. class SmartMusicPlayerTest(unittest.TestCase):
  14. """
  15. Uses Smart Music Player motivating application.
  16. """
  17. def test_create_act(self):
  18. """
  19. Tests creating act to describe motivating application.
  20. """
  21. pass
  22. def test_create_inner_loop_scenes(self):
  23. """
  24. Tests creating scenes to describe inner loop from the example.
  25. """
  26. curr_video = VideoExpression()
  27. scene1 = TextScene(
  28. title = "Show video title",
  29. comment = "",
  30. duration = NumberValue(2),
  31. text = YoutubeVideoGetTitle(curr_video),
  32. pre_commands = CommandSequence([]),
  33. post_commands = CommandSequence([])
  34. )
  35. scene2 = TextScene(
  36. title = "Show video description",
  37. comment = "",
  38. duration = NumberValue(2),
  39. text = YoutubeVideoGetDescription(curr_video),
  40. pre_commands = CommandSequence([]),
  41. post_commands = CommandSequence([])
  42. )
  43. scene3 = VideoScene(
  44. title = "Play video",
  45. comment = "",
  46. duration = NumberGetVariableExpression("clip_duration"),
  47. pre_commands = CommandSequence([
  48. NumberSetVariableStatement("clip_duration", NumberValue(1)),
  49. NumberSetVariableStatement("video_duration", YoutubeVideoGetDuration(curr_video)),
  50. NumberSetVariableStatement("clip_offset",
  51. GetRandomNumberBetweenInterval(
  52. NumberValue(0),
  53. Subtract(
  54. NumberGetVariableExpression("video_duration"),
  55. NumberGetVariableExpression("clip_duration")
  56. )
  57. )
  58. )
  59. ]),
  60. post_commands = CommandSequence([]),
  61. offset = NumberGetVariableExpression("clip_offset"),
  62. source = VideoGetVariableExpression("curr_video")
  63. )
  64. def test_translate_inner_loop_scenes(self):
  65. """
  66. Tests translating scenes describing inner loop from the example.
  67. """
  68. curr_video = VideoValue("http://www.youtube.com/watch?v=9bZkp7q19f0") # PSY - GANGNAM STYLE
  69. act1 = Act("",
  70. [
  71. TextScene(
  72. title = "Show video title",
  73. comment = "",
  74. duration = NumberValue(2),
  75. text = YoutubeVideoGetTitle(curr_video),
  76. pre_commands = CommandSequence([]),
  77. post_commands = CommandSequence([])
  78. ),
  79. TextScene(
  80. title = "Show video description",
  81. comment = "",
  82. duration = NumberValue(2),
  83. text = YoutubeVideoGetDescription(curr_video),
  84. pre_commands = CommandSequence([]),
  85. post_commands = CommandSequence([])
  86. ),
  87. VideoScene(
  88. title = "Play video",
  89. comment = "",
  90. duration = NumberGetVariableExpression("clip_duration"),
  91. pre_commands = CommandSequence([
  92. NumberSetVariableStatement("clip_duration", NumberValue(1)),
  93. NumberSetVariableStatement("video_duration", YoutubeVideoGetDuration(curr_video)),
  94. NumberSetVariableStatement("clip_offset",
  95. GetRandomNumberBetweenInterval(
  96. NumberValue(0),
  97. Subtract(
  98. NumberGetVariableExpression("video_duration"),
  99. NumberGetVariableExpression("clip_duration")
  100. )
  101. )
  102. )
  103. ]),
  104. post_commands = CommandSequence([]),
  105. offset = NumberGetVariableExpression("clip_offset"),
  106. source = VideoGetVariableExpression("curr_video")
  107. )
  108. ])
  109. # print act1.translate()
  110. class Test(unittest.TestCase):
  111. """
  112. General tests.
  113. """
  114. def test_operator_functions(self):
  115. exec translate_operator_2("+",NumberValue(1),NumberValue(1))
  116. exec translate_operator_2("-",NumberValue(1),NumberValue(1))
  117. def test_operators(self):
  118. """
  119. Tests of representation of standard Python operators.
  120. """
  121. exec Add(NumberValue(1),NumberValue(2)).translate()
  122. exec Add(NumberValue(1),NumberValue(2)).translate()
  123. exec Subtract(NumberValue(1),NumberValue(2)).translate()
  124. exec Multiply(NumberValue(1),NumberValue(2)).translate()
  125. def test_operator_evaluation_order(self):
  126. # Example where evaluation order different than with brackets.
  127. # 2 * 3 + 4 -> (2*3) + 4 -> 10
  128. # 2 * (3 + 4) -> 14
  129. e = 2 * (3 + 4)
  130. m = Multiply(
  131. NumberValue(2),
  132. Add(
  133. NumberValue(3),
  134. NumberValue(4)
  135. )
  136. )
  137. # Can't use eval or exec here, would need more sophisticated methods
  138. # to check.
  139. def test_instance_methods(self):
  140. """
  141. Tests of representation of instance methods.
  142. """
  143. YoutubeVideoGetTitle(
  144. VideoValue("http://www.youtube.com/watch?v=9bZkp7q19f0")
  145. ).translate()
  146. YoutubeVideoGetDescription(
  147. VideoValue("http://www.youtube.com/watch?v=9bZkp7q19f0")
  148. ).translate()
  149. def test_language_component_children(self):
  150. """
  151. Tests to get to the bottom and check for unusual _children
  152. behaviour.
  153. Behavior isolated - needed to turn _children from a class variable
  154. to an instance variable.
  155. """
  156. # Check values have no children.
  157. self.assertEqual(
  158. NumberValue(1)._children,
  159. []
  160. )
  161. self.assertEqual(
  162. TextValue("one")._children,
  163. []
  164. )
  165. self.assertEqual(
  166. VideoValue("http://www.youtube.com/watch?v=9bZkp7q19f0")._children,
  167. []
  168. )
  169. self.assertEqual(
  170. VideoCollectionValue(["http://www.youtube.com/watch?v=9bZkp7q19f0"])._children,
  171. []
  172. )
  173. a = NumberValue(2)
  174. b = NumberValue(3)
  175. self.assertEqual(
  176. Add(a,b)._children,
  177. [a,b]
  178. )
  179. def test_get_live_variables(self):
  180. self.assertEqual(
  181. NumberGetVariableExpression("a").get_live_variables(Type.NUMBER),
  182. set(["a"])
  183. )
  184. self.assertEqual(
  185. TextValue("one").get_live_variables(Type.TEXT),
  186. set([])
  187. )
  188. self.assertEqual(
  189. NumberValue(1).get_live_variables(Type.NUMBER),
  190. set([])
  191. )
  192. self.assertEqual(
  193. NumberSetVariableStatement("a",NumberValue(1)).get_live_variables(Type.NUMBER),
  194. set(["a"])
  195. )
  196. cs = CommandSequence([
  197. NumberSetVariableStatement("a", NumberValue(1)),
  198. NumberSetVariableStatement("b", NumberValue(2)),
  199. NumberSetVariableStatement("c",
  200. Add(
  201. NumberGetVariableExpression("a"),
  202. NumberGetVariableExpression("b")
  203. )
  204. ),
  205. TypedSetVariableStatement(Type.TEXT, "d", TextValue("one")),
  206. TypedSetVariableStatement(Type.TEXT, "e",
  207. GetVariableExpression(Type.TEXT, "d")
  208. )
  209. ])
  210. self.assertEqual(
  211. cs.get_live_variables(Type.NUMBER),
  212. set(["a","b", "c"])
  213. )
  214. self.assertEqual(
  215. cs.get_live_variables(Type.TEXT),
  216. set(["d", "e"])
  217. )
  218. cs = VideoScene(
  219. title = "Play video",
  220. comment = "",
  221. duration = NumberGetVariableExpression("clip_duration"),
  222. pre_commands = CommandSequence([
  223. NumberSetVariableStatement("clip_duration", NumberValue(1)),
  224. NumberSetVariableStatement("video_duration", YoutubeVideoGetDuration(
  225. VideoGetVariableExpression("curr_video")
  226. )),
  227. NumberSetVariableStatement("clip_offset",
  228. GetRandomNumberBetweenInterval(
  229. NumberValue(0),
  230. Subtract(
  231. NumberGetVariableExpression("video_duration"),
  232. NumberGetVariableExpression("clip_duration")
  233. )
  234. )
  235. )
  236. ]),
  237. post_commands = CommandSequence([]),
  238. offset = NumberGetVariableExpression("clip_offset"),
  239. source = VideoGetVariableExpression("curr_video")
  240. )
  241. self.assertEqual(
  242. cs.get_live_variables(Type.NUMBER),
  243. set(["clip_duration", "video_duration", "clip_offset"])
  244. )
  245. self.assertEqual(
  246. cs.get_live_variables(Type.VIDEO),
  247. set(["curr_video"])
  248. )
  249. def test_indent(self):
  250. """
  251. Tests handling of multiple lines and empty lines.
  252. """
  253. self.assertEqual(
  254. indent("""command1
  255. command2
  256. command3"""),""" command1
  257. command2
  258. command3"""
  259. )
  260. def test_if_scene(self):
  261. # Simple if scene
  262. act = Act("",
  263. [
  264. IfScene("Simple If Scene", "",
  265. TextValue("question?"),
  266. SceneSequence([
  267. ]),
  268. SceneSequence([
  269. ])
  270. )
  271. ])
  272. # print act.translate()
  273. # If scene with complex question and scene sequence in both branches.
  274. curr_video = VideoValue("http://www.youtube.com/watch?v=9bZkp7q19f0") # PSY - GANGNAM STYLE
  275. act = Act("",
  276. [
  277. IfScene("Complex If Scene", "",
  278. GetVariableExpression(Type.TEXT, "question"),
  279. SceneSequence([
  280. TextScene(
  281. title = "Show video title",
  282. comment = "",
  283. duration = NumberValue(2),
  284. text = YoutubeVideoGetTitle(curr_video),
  285. pre_commands = CommandSequence([]),
  286. post_commands = CommandSequence([])
  287. )
  288. ]),
  289. SceneSequence([
  290. TextScene(
  291. title = "Show video description",
  292. comment = "",
  293. duration = NumberValue(2),
  294. text = YoutubeVideoGetDescription(curr_video),
  295. pre_commands = CommandSequence([]),
  296. post_commands = CommandSequence([])
  297. )
  298. ])
  299. )
  300. ])
  301. # print act.translate()
  302. def test_while_scene(self):
  303. # Simple while scene
  304. act = Act("",
  305. [
  306. WhileScene("Simple While Scene", "",
  307. TextValue("question?"),
  308. SceneSequence([
  309. ])
  310. )
  311. ])
  312. print act.translate()
  313. # If scene with complex question and scene sequence in both branches.
  314. curr_video = VideoValue("http://www.youtube.com/watch?v=9bZkp7q19f0") # PSY - GANGNAM STYLE
  315. act = Act("",
  316. [
  317. WhileScene("Complex While Scene", "",
  318. GetVariableExpression(Type.TEXT, "question"),
  319. SceneSequence([
  320. TextScene(
  321. title = "Show video title",
  322. comment = "",
  323. duration = NumberValue(2),
  324. text = YoutubeVideoGetTitle(curr_video),
  325. pre_commands = CommandSequence([]),
  326. post_commands = CommandSequence([])
  327. )
  328. ])
  329. )
  330. ])
  331. print act.translate()
  332. def test_variable_name_generator(self):
  333. """
  334. Test extreme variable names.
  335. """
  336. g = VariableNameGenerator.get_instance()
  337. g.reset()
  338. self.assertEqual(
  339. g.generate(),
  340. "store_a"
  341. )
  342. for i in range(0,24):
  343. g.generate()
  344. self.assertEqual(
  345. g.generate(),
  346. "store_z"
  347. )
  348. self.assertEqual(
  349. g.generate(),
  350. "store_aa"
  351. )
  352. self.assertEqual(
  353. g.generate(),
  354. "store_ab"
  355. )
  356. if __name__ == "__main__":
  357. unittest.main()