tcasestmt.nim 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. discard """
  2. output:
  3. '''
  4. Not found!
  5. Found!
  6. 1
  7. compiles for 1
  8. i am always two
  9. default for 3
  10. set is 4 not 5
  11. array is 6 not 7
  12. default for 8
  13. an identifier
  14. OK
  15. OK
  16. OK
  17. ayyydd
  18. '''
  19. """
  20. block arrayconstr:
  21. const md_extension = [".md", ".markdown"]
  22. proc test(ext: string) =
  23. case ext
  24. of ".txt", md_extension:
  25. echo "Found!"
  26. else:
  27. echo "Not found!"
  28. test(".something")
  29. # ensure it's not evaluated at compile-time:
  30. var foo = ".markdown"
  31. test(foo)
  32. converter toInt(x: char): int =
  33. x.int
  34. block t8333:
  35. case 0
  36. of 'a': echo 0
  37. else: echo 1
  38. block emptyset_when:
  39. proc whenCase(a: int) =
  40. case a
  41. of (when compiles(whenCase(1)): 1 else: {}): echo "compiles for 1"
  42. of {}: echo "me not fail"
  43. of 2: echo "i am always two"
  44. of []: echo "me neither"
  45. of {4,5}: echo "set is 4 not 5"
  46. of [6,7]: echo "array is 6 not 7"
  47. of (when compiles(neverCompilesIBet()): 3 else: {}): echo "compiles for 3"
  48. #of {},[]: echo "me neither"
  49. else: echo "default for ", a
  50. whenCase(1)
  51. whenCase(2)
  52. whenCase(3)
  53. whenCase(4)
  54. whenCase(6)
  55. whenCase(8)
  56. block setconstr:
  57. const
  58. SymChars: set[char] = {'a'..'z', 'A'..'Z', '\x80'..'\xFF'}
  59. proc classify(s: string) =
  60. case s[0]
  61. of SymChars, '_': echo "an identifier"
  62. of {'0'..'9'}: echo "a number"
  63. else: echo "other"
  64. classify("Hurra")
  65. block tduplicates:
  66. type Kind = enum A, B
  67. var k = A
  68. template reject(b) =
  69. static: doAssert(not compiles(b))
  70. reject:
  71. var i = 2
  72. case i
  73. of [1, 1]: discard
  74. else: discard
  75. reject:
  76. var i = 2
  77. case i
  78. of 1, { 1..2 }: discard
  79. else: discard
  80. reject:
  81. var i = 2
  82. case i
  83. of { 1, 1 }: discard
  84. of { 1, 1 }: discard
  85. else: discard
  86. reject:
  87. case k
  88. of [A, A]: discard
  89. var i = 2
  90. case i
  91. of { 1, 1 }: discard
  92. of { 2, 2 }: echo "OK"
  93. else: discard
  94. case i
  95. of { 10..30, 15..25, 5..15, 25..35 }: discard
  96. else: echo "OK"
  97. case k
  98. of {A, A..A}: echo "OK"
  99. of B: discard
  100. block tcasestm:
  101. type
  102. Tenum = enum eA, eB, eC
  103. var
  104. x: string = "yyy"
  105. y: Tenum = eA
  106. i: int
  107. case y
  108. of eA: write(stdout, "a")
  109. of eB, eC: write(stdout, "b or c")
  110. case x
  111. of "Andreas", "Rumpf": write(stdout, "Hallo Meister!")
  112. of "aa", "bb": write(stdout, "Du bist nicht mein Meister")
  113. of "cc", "hash", "when": discard
  114. of "will", "it", "finally", "be", "generated": discard
  115. var z = case i
  116. of 1..5, 8, 9: "aa"
  117. of 6, 7: "bb"
  118. elif x == "Ha":
  119. "cc"
  120. elif x == "yyy":
  121. write(stdout, x)
  122. "dd"
  123. else:
  124. "zz"
  125. echo z
  126. #OUT ayyy
  127. let str1 = "Y"
  128. let str2 = "NN"
  129. let a = case str1:
  130. of "Y": true
  131. of "N": false
  132. else:
  133. echo "no good"
  134. quit("quitting")
  135. proc toBool(s: string): bool =
  136. case s:
  137. of "": raise newException(ValueError, "Invalid boolean")
  138. elif s[0] == 'Y': true
  139. elif s[0] == 'N': false
  140. else: "error".quit(2)
  141. let b = "NN".toBool()
  142. doAssert(a == true)
  143. doAssert(b == false)
  144. static:
  145. #bug #7407
  146. let bstatic = "N".toBool()
  147. doAssert(bstatic == false)
  148. var bb: bool
  149. doAssert(not compiles(
  150. bb = case str2:
  151. of "": raise newException(ValueError, "Invalid boolean")
  152. elif str.startsWith("Y"): true
  153. elif str.startsWith("N"): false
  154. ))
  155. doAssert(not compiles(
  156. bb = case str2:
  157. of "Y": true
  158. of "N": false
  159. ))
  160. doAssert(not compiles(
  161. bb = case str2:
  162. of "Y": true
  163. of "N": raise newException(ValueError, "N not allowed")
  164. ))
  165. doAssert(not compiles(
  166. bb = case str2:
  167. of "Y": raise newException(ValueError, "Invalid Y")
  168. else: raise newException(ValueError, "Invalid N")
  169. ))
  170. doAssert(not compiles(
  171. bb = case str2:
  172. of "Y":
  173. raise newException(ValueError, "Invalid Y")
  174. true
  175. else: raise newException(ValueError, "Invalid")
  176. ))
  177. doAssert(not compiles(
  178. bb = case str2:
  179. of "Y":
  180. "invalid Y".quit(3)
  181. true
  182. else: raise newException(ValueError, "Invalid")
  183. ))
  184. #issue #11552
  185. proc positiveOrNegative(num: int): string =
  186. result = case num
  187. of (low(int)+2) .. -1:
  188. "negative"
  189. of 0:
  190. "zero"
  191. else:
  192. "impossible"
  193. #issue #11551
  194. proc negativeOrNot(num: int): string =
  195. result = case num
  196. of low(int) .. -1:
  197. "negative"
  198. else:
  199. "zero or positive"
  200. doAssert negativeOrNot(-1) == "negative"
  201. doAssert negativeOrNot(10000000) == "zero or positive"
  202. doAssert negativeOrNot(0) == "zero or positive"
  203. ########################################################
  204. # issue #13490
  205. import strutils
  206. func foo(input: string): int =
  207. try:
  208. parseInt(input)
  209. except:
  210. return
  211. func foo2(b, input: string): int =
  212. case b:
  213. of "Y":
  214. for c in input:
  215. result = if c in '0'..'9': parseInt($c)
  216. else: break
  217. of "N":
  218. for c in input:
  219. result = if c in '0'..'9': parseInt($c)
  220. else: continue
  221. else: return
  222. static:
  223. doAssert(foo("3") == 3)
  224. doAssert(foo("a") == 0)
  225. doAssert(foo2("Y", "a2") == 0)
  226. doAssert(foo2("Y", "2a") == 2)
  227. doAssert(foo2("N", "a3") == 3)
  228. doAssert(foo2("z", "2") == 0)
  229. doAssert(foo("3") == 3)
  230. doAssert(foo("a") == 0)
  231. doAssert(foo2("Y", "a2") == 0)
  232. doAssert(foo2("Y", "2a") == 2)
  233. doAssert(foo2("N", "a3") == 3)
  234. doAssert(foo2("z", "2") == 0)
  235. # bug #20031
  236. proc main(a: uint64) =
  237. case a
  238. else:
  239. discard
  240. static:
  241. main(10)
  242. main(10)
  243. block:
  244. # Just needs to compile
  245. proc bar(): int {.discardable.} = discard
  246. proc foo() {.noreturn.} = discard
  247. case "*"
  248. of "*":
  249. bar()
  250. else:
  251. # Make sure this noreturn doesn't
  252. # cause the discardable to not discard
  253. foo()