More stuff
This commit is contained in:
parent
a882ccd7b8
commit
e1645dcefa
3 changed files with 390 additions and 24 deletions
76
test.nim
Normal file
76
test.nim
Normal file
|
|
@ -0,0 +1,76 @@
|
|||
import unittest, patty
|
||||
|
||||
|
||||
suite "adt construction":
|
||||
test "basic creation":
|
||||
adt Shape:
|
||||
Circle(r: float, x: float, y: float)
|
||||
Rectangle(w: float, h: float)
|
||||
Square(side: int)
|
||||
|
||||
let c = Shape(kind: Circle, r: 4, x: 2, y: 0)
|
||||
check c.r == 4.0
|
||||
|
||||
test "allowing empty objects":
|
||||
adt Shape:
|
||||
Circle(r: float, x: float, y: float)
|
||||
Rectangle(w: float, h: float)
|
||||
Square(side: int)
|
||||
UnitCircle
|
||||
|
||||
let r = Shape(kind: Rectangle, w: 2, h: 5)
|
||||
check r.h == 5.0
|
||||
|
||||
|
||||
suite "pattern matching":
|
||||
type
|
||||
ShapeKind = enum
|
||||
Circle, Rectangle
|
||||
Shape = object
|
||||
case kind: ShapeKind
|
||||
of Circle:
|
||||
x, y, r: float
|
||||
of Rectangle:
|
||||
w, h: float
|
||||
|
||||
test "basic matching":
|
||||
let c = Shape(kind: Circle, r: 4, x: 2, y: 0)
|
||||
var res: float = 0
|
||||
match c:
|
||||
Circle(x: x, y: y, r: r):
|
||||
res = r
|
||||
Rectangle(w: w, h: h):
|
||||
res = 1
|
||||
check res == 4.0
|
||||
|
||||
test "binding to different variable names":
|
||||
let c = Shape(kind: Circle, r: 4, x: 2, y: 0)
|
||||
var res: float = 0
|
||||
match c:
|
||||
Circle(x: x, y: y, r: someNumber):
|
||||
res = someNumber
|
||||
Rectangle(w: w, h: h):
|
||||
res = 1
|
||||
check res == 4.0
|
||||
|
||||
test "binding a complex expression":
|
||||
proc makeRect(w, h: float): Shape =
|
||||
Shape(kind: Rectangle, w: w, h: h)
|
||||
|
||||
var res: float = 0
|
||||
match makeRect(3, 4):
|
||||
Circle(x: x, y: y, r: r):
|
||||
res = r
|
||||
Rectangle(w: w, h: h):
|
||||
res = w + h
|
||||
check res == 7.0
|
||||
|
||||
test "ignoring _ bindings":
|
||||
let c = Shape(kind: Circle, r: 4, x: 2, y: 0)
|
||||
var res: float = 0
|
||||
match c:
|
||||
Circle(x: _, y: _, r: r):
|
||||
res = r
|
||||
Rectangle(w: w, h: h):
|
||||
res = w + h
|
||||
check res == 4.0
|
||||
Loading…
Add table
Add a link
Reference in a new issue