otests.nim 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. # Fields
  2. const x = 5
  3. # Test substring
  4. static:
  5. assert "test".substring(3) == "t"
  6. assert "test".substring(2,1) == "s"
  7. assert "test".substring(3,2) == "t"
  8. assert "test".substring(1,2) == "es"
  9. # Various parsing tests
  10. when true:
  11. block: #no_substitution
  12. proc actual: string = tmpli html"""
  13. <p>Test!</p>
  14. """
  15. const expected = html"""
  16. <p>Test!</p>
  17. """
  18. doAssert actual() == expected
  19. block: #basic
  20. proc actual: string = tmpli html"""
  21. <p>Test $$x</p>
  22. $x
  23. """
  24. const expected = html"""
  25. <p>Test $x</p>
  26. 5
  27. """
  28. doAssert actual() == expected
  29. block: #expression
  30. proc actual: string = tmpli html"""
  31. <p>Test $$(x * 5)</p>
  32. $(x * 5)
  33. """
  34. const expected = html"""
  35. <p>Test $(x * 5)</p>
  36. 25
  37. """
  38. doAssert actual() == expected
  39. block: #escape
  40. proc actual: string = tmpli js"""
  41. [{
  42. "hello world"
  43. }]
  44. """
  45. const expected = js"""
  46. [{
  47. "hello world"
  48. }]
  49. """
  50. doAssert actual() == expected
  51. block: #forIn
  52. proc actual: string = tmpli html"""
  53. <p>Test for</p>
  54. <ul>
  55. $for y in 0..2 {
  56. <li>$y</li>
  57. }
  58. </ul>
  59. """
  60. const expected = html"""
  61. <p>Test for</p>
  62. <ul>
  63. <li>0</li>
  64. <li>1</li>
  65. <li>2</li>
  66. </ul>
  67. """
  68. doAssert actual() == expected
  69. block: #while
  70. proc actual: string = tmpli html"""
  71. <p>Test while/stmt</p>
  72. <ul>
  73. ${ var y = 0 }
  74. $while y < 4 {
  75. <li>$y</li>
  76. ${ inc(y) }
  77. }
  78. </ul>
  79. """
  80. const expected = html"""
  81. <p>Test while/stmt</p>
  82. <ul>
  83. <li>0</li>
  84. <li>1</li>
  85. <li>2</li>
  86. <li>3</li>
  87. </ul>
  88. """
  89. doAssert actual() == expected
  90. block: #ifElifElse
  91. proc actual: string = tmpli html"""
  92. <p>Test if/elif/else</p>
  93. $if x == 8 {
  94. <div>x is 8!</div>
  95. }
  96. $elif x == 7 {
  97. <div>x is 7!</div>
  98. }
  99. $else {
  100. <div>x is neither!</div>
  101. }
  102. """
  103. const expected = html"""
  104. <p>Test if/elif/else</p>
  105. <div>x is neither!</div>
  106. """
  107. doAssert actual() == expected
  108. block: #multiLineStatements
  109. proc actual: string = tmpli html"""
  110. <p>Test multiline statements</p>
  111. ${
  112. var x = 5
  113. var y = 7
  114. }
  115. <span>$x</span><span>$y</span>
  116. """
  117. const expected = html"""
  118. <p>Test multiline statements</p>
  119. <span>5</span><span>7</span>
  120. """
  121. doAssert actual() == expected
  122. block: #caseOfElse
  123. proc actual: string = tmpli html"""
  124. <p>Test case</p>
  125. $case x
  126. $of 5 {
  127. <div>x == 5</div>
  128. }
  129. $of 6 {
  130. <div>x == 6</div>
  131. }
  132. $else {}
  133. """
  134. const expected = html"""
  135. <p>Test case</p>
  136. <div>x == 5</div>
  137. """
  138. doAssert actual() == expected
  139. when true: #embeddingTest
  140. proc no_substitution: string = tmpli html"""
  141. <h1>Template test!</h1>
  142. """
  143. # # Single variable substitution
  144. proc substitution(who = "nobody"): string = tmpli html"""
  145. <div id="greeting">hello $who!</div>
  146. """
  147. # Expression template
  148. proc test_expression(nums: openArray[int] = []): string =
  149. var i = 2
  150. tmpli html"""
  151. $(no_substitution())
  152. $(substitution("Billy"))
  153. <div id="age">Age: $($nums[i] & "!!")</div>
  154. """
  155. proc test_statements(nums: openArray[int] = []): string =
  156. tmpli html"""
  157. $(test_expression(nums))
  158. $if true {
  159. <ul>
  160. $for i in nums {
  161. <li>$(i * 2)</li>
  162. }
  163. </ul>
  164. }
  165. """
  166. var actual = test_statements([0,1,2])
  167. const expected = html"""
  168. <h1>Template test!</h1>
  169. <div id="greeting">hello Billy!</div>
  170. <div id="age">Age: 2!!</div>
  171. <ul>
  172. <li>0</li>
  173. <li>2</li>
  174. <li>4</li>
  175. </ul>
  176. """
  177. doAssert actual == expected
  178. when defined(future):
  179. block: #tryCatch
  180. proc actual: string = tmpli html"""
  181. <p>Test try/catch</p>
  182. <div>
  183. $try {
  184. <div>Lets try this!</div>
  185. }
  186. $except {
  187. <div>Uh oh!</div>
  188. }
  189. </div>
  190. """
  191. const expected = html"""
  192. <p>Test try/catch</p>
  193. <div>
  194. <div>Lets try this!</div>
  195. </div>
  196. """
  197. doAssert actual() == expected