2019-12-20 22:52:59 +01:00
|
|
|
{ pkgs, ... }:
|
|
|
|
|
2019-08-08 23:20:00 +02:00
|
|
|
with builtins;
|
2019-12-20 22:52:59 +01:00
|
|
|
with pkgs.nix.yants;
|
2019-08-08 23:20:00 +02:00
|
|
|
|
|
|
|
# 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))
|
2019-09-16 12:27:54 +02:00
|
|
|
(path /nix)
|
2019-08-08 23:20:00 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
# 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!";
|
|
|
|
};
|
2019-08-09 00:32:43 +02:00
|
|
|
|
2019-08-24 15:10:41 +02:00
|
|
|
# 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;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2019-08-28 15:33:41 +02:00
|
|
|
testSumMatch = creature.match testSum {
|
|
|
|
human = v: "It's a human named ${v.name}";
|
|
|
|
pet = v: throw "It's not supposed to be a pet!";
|
|
|
|
};
|
|
|
|
|
2019-08-09 00:32:43 +02:00
|
|
|
# Test curried function definitions
|
|
|
|
func = defun [ string int string ]
|
|
|
|
(name: age: "${name} is ${toString age} years old");
|
|
|
|
|
|
|
|
testFunc = func "Brynhjulf" 42;
|
2019-08-28 15:31:45 +02:00
|
|
|
|
|
|
|
# Test that all types are types.
|
|
|
|
testTypes = map type [
|
2019-09-16 12:27:54 +02:00
|
|
|
any bool drv float int string path
|
2019-08-28 15:31:45 +02:00
|
|
|
|
|
|
|
(attrs int)
|
2019-09-19 16:35:05 +02:00
|
|
|
(eitherN [ int string bool ])
|
2019-08-28 15:31:45 +02:00
|
|
|
(either int string)
|
|
|
|
(enum [ "foo" "bar" ])
|
|
|
|
(list string)
|
|
|
|
(option int)
|
|
|
|
(option (list string))
|
|
|
|
(struct { a = int; b = option string; })
|
|
|
|
(sum { a = int; b = option string; })
|
|
|
|
];
|
2019-12-20 22:52:59 +01:00
|
|
|
} (pkgs.writeText "yants-tests" "All tests passed!")
|