matthieu / witty

Go figure

This URL has Read+Write access

witty / test / control.wy
100644 98 lines (93 sloc) 2.681 kb
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
describe "If control structure" (
  a = (b = 2)
  if true null (a = 4)
  if false (b = 4) null
 
  it "should return the then branch if predicate is true" (
    if (3 == 3) 5 10 == 5
  )
  it "should return the else branch if predicate is false" (
    if false 5 10 == 10
  )
  it "shouldn't evaluate the else branch if predicate is true" (a == 2)
  it "shouldn't evaluate the then branch if predicate is false" (b == 2)
)
/* TODO
describe "Basic for loops" (
  iter = ""
  for (m1 = 0) (m1 < 5) (m1=m1+1) (
    iter = iter + m1
  )
  it "should have iterated through all indexes" (iter == "01234")
  it "should have a correct final index value equal" (m1 == 5)
 
  str = ""
  for (m = 0; n = 3) (n < 11) (m += 1; n += 2) (
    str = str + m
    str = str + n
  )
  it "should iterate with two index variables" (str == "03152739")
)
*/
describe "Iterated for loops" (
  str = ""
  for [2,3,4] (lambda m (str = str + m))
  
  it "should iterate on each element of the list" (str == "234")
/* TODO
  str2 = ""
  for [2,3,4] (
    lambda m count (
      str2 = str2 + m
      str2 = str2 + count
    )
  )
  it "should iterate on each element while setting the counter appropriately" (str2 == "203142")
*/
)
 
describe "Try/catch blocks" (
  it "should return try last body statement when no error occurs" (
    try (2+3) (catch "Foo" "caught") == 5
  )
  it "should allow try with no catch" (
    try (2+3) == 5
  )
  it "should catch standard errors" (
    try (10/0) (catch ArgumentError ae "caught") == "caught"
  )
  it "should catch directly thrown errors" (
    try (raise "Foo") (catch "Foo" f "caught") == "caught"
  )
  it "shouldn't catch other custom errors" (
    try (
      try (raise "Bar") (catch "Foo" f "caught")
    ) (catch "Bar" b "through") == "through"
  )
  it "shouldn't catch other standard errors" (
    try (
      try (raise UserError) (catch ArgumentError ae "caught")
    ) (catch UserError ue "through") == "through"
  )
  it "should allow catch predefinition" (
    c = catch "Foo" f "caught"
    try (raise "Foo") c == "caught"
  )
  it "should realize a closure on a predefined catch body" (
    val = "before"
    c = catch "Foo" x val
    val = "caught"
    try (raise "Foo") c == "caught"
  )
  it "should catch errors from levels down" (
    errLambda = lambda (raise "Foo")
    try (errLambda()) (catch "Foo" f "caught") == "caught"
  )
  it "should catch all when anonymous" (
    try (10/0) (catchAll "caught") == "caught"
  )
  it "should allow empty catch to swallow" (
    try (10/0) (catchAll()) == null
  )
  it "should bind error variables" (
    val = null
    try (raise "caught") (catch "caught" e (val = e))
    val == "caught"
  )
)