about summary refs log tree commit diff
path: root/z-yants-tests.nix
blob: f468f61322ec0984e74f0219e56839e360bb01cd (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
with (import ./yants.nix {});
with builtins;

# Note: Derivations are not included in the tests below as they cause
# issues with deepSeq.

deepSeq rec {
  # Test that all primitive types match
  primitives = [
    (int 15)
    (bool false)
    (float 13.37)
    (string "Hello!")
    (function (x: x * 2))
  ];

  # Test that polymorphic types work as intended
  poly = [
    (option int null)
    (list string [ "foo" "bar" ])
    (either int float 42)
  ];

  # Test that structures work as planned.
  person = struct "person" {
    name = string;
    age  = int;

    contact = option (struct {
      email = string;
      phone = option string;
    });
  };

  testPerson = person {
    name = "Brynhjulf";
    age  = 42;
    contact.email = "brynhjulf@yants.nix";
  };

  # Test enum definitions & matching
  colour = enum "colour" [ "red" "blue" "green" ];
  testMatch = colour.match "red" {
    red = "It is in fact red!";
    blue = throw "It should not be blue!";
    green = throw "It should not be green!";
  };

  # Test sum type definitions
  creature = sum "creature" {
    human = struct {
      name = string;
      age = option int;
    };

    pet = enum "pet" [ "dog" "lizard" "cat" ];
  };

  testSum = creature {
    human = {
      name = "Brynhjulf";
      age = 42;
    };
  };

  # Test curried function definitions
  func = defun [ string int string ]
  (name: age: "${name} is ${toString age} years old");

  testFunc = func "Brynhjulf" 42;
} "All tests passed!\n"