summary refs log tree commit diff stats
path: root/tests/casestmt/tcasestm.nim
blob: 4d32d023fe9637e7a291fd3df8874b86180edde4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
discard """
  file: "tcasestm.nim"
  output: "ayyydd"
"""
# Test the case statement

type
  Tenum = enum eA, eB, eC

var
  x: string = "yyy"
  y: Tenum = eA
  i: int

case y
of eA: write(stdout, "a")
of eB, eC: write(stdout, "b or c")

case x
of "Andreas", "Rumpf": write(stdout, "Hallo Meister!")
of "aa", "bb": write(stdout, "Du bist nicht mein Meister")
of "cc", "hash", "when": discard
of "will", "it", "finally", "be", "generated": discard

var z = case i
  of 1..5, 8, 9: "aa"
  of 6, 7: "bb"
  elif x == "Ha":
    "cc"
  elif x == "yyy":
    write(stdout, x)
    "dd"
  else:
    "zz"

echo z
#OUT ayyy

let str1 = "Y"
let str2 = "NN"
let a = case str1:
  of "Y": true
  of "N": false
  else: 
    echo "no good"
    quit("quiting")

proc toBool(s: string): bool = 
  case s:
    of nil, "": raise newException(ValueError, "Invalid boolean")
    elif s[0] == 'Y': true
    elif s[0] == 'N': false
    else: "error".quit(2)


let b = "NN".toBool()

doAssert(a == true)
doAssert(b == false)

static:
  #bug #7407
  let bstatic = "N".toBool()
  doAssert(bstatic == false)

var bb: bool
doassert(not compiles(
  bb = case str2:
    of nil, "": raise newException(ValueError, "Invalid boolean")
    elif str.startsWith("Y"): true
    elif str.startsWith("N"): false
))

doassert(not compiles(
  bb = case str2:
    of "Y": true
    of "N": false
))

doassert(not compiles(
  bb = case str2:
    of "Y": true
    of "N": raise newException(ValueError, "N not allowed")
))

doassert(not compiles(
  bb = case str2:
    of "Y": raise newException(ValueError, "Invalid Y")
    else: raise newException(ValueError, "Invalid N")
))


doassert(not compiles(
  bb = case str2:
    of "Y":
      raise newException(ValueError, "Invalid Y")
      true    
    else: raise newException(ValueError, "Invalid")
))


doassert(not compiles(
  bb = case str2:
    of "Y":
      "invalid Y".quit(3)
      true    
    else: raise newException(ValueError, "Invalid")
))