2020-02-21 13:47:29 +01:00
|
|
|
{ depot, pkgs, ... }:
|
2019-12-20 22:52:59 +01:00
|
|
|
|
2020-02-21 13:47:29 +01:00
|
|
|
with depot.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.
|
|
|
|
|
2021-02-01 10:14:48 +01:00
|
|
|
let
|
|
|
|
|
|
|
|
inherit (depot.nix.runTestsuite)
|
|
|
|
runTestsuite
|
|
|
|
it
|
|
|
|
assertEq
|
|
|
|
assertThrows
|
|
|
|
assertDoesNotThrow
|
|
|
|
;
|
|
|
|
|
|
|
|
testPrimitives = it "checks that all primitive types match" [
|
|
|
|
(assertDoesNotThrow "unit type" (unit {}))
|
|
|
|
(assertDoesNotThrow "int type" (int 15))
|
|
|
|
(assertDoesNotThrow "bool type" (bool false))
|
|
|
|
(assertDoesNotThrow "float type" (float 13.37))
|
|
|
|
(assertDoesNotThrow "string type" (string "Hello!"))
|
|
|
|
(assertDoesNotThrow "function type" (function (x: x * 2)))
|
|
|
|
(assertDoesNotThrow "path type" (path /nix))
|
2019-08-08 23:20:00 +02:00
|
|
|
];
|
|
|
|
|
2021-02-01 10:14:48 +01:00
|
|
|
testPoly = it "checks that polymorphic types work as intended" [
|
|
|
|
(assertDoesNotThrow "option type" (option int null))
|
|
|
|
(assertDoesNotThrow "list type" (list string [ "foo" "bar" ]))
|
|
|
|
(assertDoesNotThrow "either type" (either int float 42))
|
2019-08-08 23:20:00 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
# Test that structures work as planned.
|
|
|
|
person = struct "person" {
|
|
|
|
name = string;
|
|
|
|
age = int;
|
|
|
|
|
|
|
|
contact = option (struct {
|
|
|
|
email = string;
|
|
|
|
phone = option string;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2021-02-01 10:14:48 +01:00
|
|
|
testStruct = it "checks that structures work as intended" [
|
|
|
|
(assertDoesNotThrow "person struct" (person {
|
|
|
|
name = "Brynhjulf";
|
|
|
|
age = 42;
|
|
|
|
contact.email = "brynhjulf@yants.nix";
|
|
|
|
}))
|
|
|
|
];
|
2019-08-08 23:20:00 +02:00
|
|
|
|
|
|
|
# Test enum definitions & matching
|
|
|
|
colour = enum "colour" [ "red" "blue" "green" ];
|
2021-02-01 10:14:48 +01:00
|
|
|
colourMatcher = {
|
2019-08-08 23:20:00 +02:00
|
|
|
red = "It is in fact red!";
|
2021-02-01 10:14:48 +01:00
|
|
|
blue = "It should not be blue!";
|
|
|
|
green = "It should not be green!";
|
2019-08-08 23:20:00 +02:00
|
|
|
};
|
2019-08-09 00:32:43 +02:00
|
|
|
|
2021-02-01 10:14:48 +01:00
|
|
|
testEnum = it "checks enum definitions and matching" [
|
|
|
|
(assertEq "enum is matched correctly"
|
|
|
|
"It is in fact red!" (colour.match "red" colourMatcher))
|
|
|
|
(assertThrows "out of bounds enum fails"
|
|
|
|
(colour.match "alpha" (colourMatcher // {
|
|
|
|
alpha = "This should never happen";
|
|
|
|
}))
|
|
|
|
)
|
|
|
|
];
|
|
|
|
|
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" ];
|
|
|
|
};
|
2021-02-01 10:14:48 +01:00
|
|
|
some-human = creature {
|
2019-08-24 15:10:41 +02:00
|
|
|
human = {
|
|
|
|
name = "Brynhjulf";
|
|
|
|
age = 42;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2021-02-01 10:14:48 +01:00
|
|
|
testSum = it "checks sum types definitions and matching" [
|
|
|
|
(assertDoesNotThrow "creature sum type" some-human)
|
|
|
|
(assertEq "sum type is matched correctly"
|
|
|
|
"It's a human named Brynhjulf" (creature.match some-human {
|
|
|
|
human = v: "It's a human named ${v.name}";
|
|
|
|
pet = v: "It's not supposed to be a pet!";
|
|
|
|
})
|
|
|
|
)
|
|
|
|
];
|
2019-08-28 15:33:41 +02:00
|
|
|
|
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");
|
|
|
|
|
2021-02-01 10:14:48 +01:00
|
|
|
testFunctions = it "checks function definitions" [
|
|
|
|
(assertDoesNotThrow "function application" (func "Brynhjulf" 42))
|
|
|
|
];
|
2019-08-28 15:31:45 +02:00
|
|
|
|
|
|
|
# Test that all types are types.
|
2021-02-01 10:14:48 +01:00
|
|
|
assertIsType = name: t:
|
|
|
|
assertDoesNotThrow "${name} is a type" (type t);
|
|
|
|
testTypes = it "checks that all types are types" [
|
|
|
|
(assertIsType "any" any)
|
|
|
|
(assertIsType "bool" bool)
|
|
|
|
(assertIsType "drv" drv)
|
|
|
|
(assertIsType "float" float)
|
|
|
|
(assertIsType "int" int)
|
|
|
|
(assertIsType "string" string)
|
|
|
|
(assertIsType "path" path)
|
|
|
|
|
|
|
|
(assertIsType "attrs int" (attrs int))
|
|
|
|
(assertIsType "eitherN [ ... ]" (eitherN [ int string bool ]))
|
|
|
|
(assertIsType "either int string" (either int string))
|
|
|
|
(assertIsType "enum [ ... ]" (enum [ "foo" "bar" ]))
|
|
|
|
(assertIsType "list string" (list string))
|
|
|
|
(assertIsType "option int" (option int))
|
|
|
|
(assertIsType "option (list string)" (option (list string)))
|
|
|
|
(assertIsType "struct { ... }" (struct { a = int; b = option string; }))
|
|
|
|
(assertIsType "sum { ... }" (sum { a = int; b = option string; }))
|
2019-08-28 15:31:45 +02:00
|
|
|
];
|
2021-01-01 21:36:42 +01:00
|
|
|
|
2021-02-01 10:14:48 +01:00
|
|
|
testRestrict = it "checks restrict types" [
|
|
|
|
(assertDoesNotThrow "< 42" ((restrict "< 42" (i: i < 42) int) 25))
|
|
|
|
(assertDoesNotThrow "list length < 3"
|
|
|
|
((restrict "not too long" (l: builtins.length l < 3) (list int)) [ 1 2 ]))
|
|
|
|
(assertDoesNotThrow "list eq 5"
|
|
|
|
(list (restrict "eq 5" (v: v == 5) any) [ 5 5 5 ]))
|
2021-01-01 21:36:42 +01:00
|
|
|
];
|
|
|
|
|
2021-02-01 10:14:48 +01:00
|
|
|
in
|
|
|
|
runTestsuite "yants" [
|
|
|
|
testPrimitives
|
|
|
|
testPoly
|
|
|
|
testStruct
|
|
|
|
testEnum
|
|
|
|
testSum
|
|
|
|
testFunctions
|
|
|
|
testTypes
|
|
|
|
testRestrict
|
|
|
|
]
|