tcasestmt.nim 5.6 KB

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