feat(nix/yants/tests): port to runTestsuite

Port existing tests to runTestsuite and add some obvious additional
tests that wouldn't be possible before (using assertThrows and
assertEq).

Change-Id: Ibe950a7a0cda3e23ebb226bdff35f52cdfec5ddf
Reviewed-on: https://cl.tvl.fyi/c/depot/+/2479
Tested-by: BuildkiteCI
Reviewed-by: Profpatsch <mail@profpatsch.de>
Reviewed-by: tazjin <mail@tazj.in>
This commit is contained in:
sterni 2021-02-01 10:14:48 +01:00
parent fde23c5d0a
commit 58020730cd

View file

@ -1,28 +1,34 @@
{ depot, pkgs, ... }: { depot, pkgs, ... }:
with builtins;
with depot.nix.yants; with depot.nix.yants;
# Note: Derivations are not included in the tests below as they cause # Note: Derivations are not included in the tests below as they cause
# issues with deepSeq. # issues with deepSeq.
deepSeq rec { let
# Test that all primitive types match
primitives = [ inherit (depot.nix.runTestsuite)
(unit {}) runTestsuite
(int 15) it
(bool false) assertEq
(float 13.37) assertThrows
(string "Hello!") assertDoesNotThrow
(function (x: x * 2)) ;
(path /nix)
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))
]; ];
# Test that polymorphic types work as intended testPoly = it "checks that polymorphic types work as intended" [
poly = [ (assertDoesNotThrow "option type" (option int null))
(option int null) (assertDoesNotThrow "list type" (list string [ "foo" "bar" ]))
(list string [ "foo" "bar" ]) (assertDoesNotThrow "either type" (either int float 42))
(either int float 42)
]; ];
# Test that structures work as planned. # Test that structures work as planned.
@ -36,20 +42,32 @@ deepSeq rec {
}); });
}; };
testPerson = person { testStruct = it "checks that structures work as intended" [
(assertDoesNotThrow "person struct" (person {
name = "Brynhjulf"; name = "Brynhjulf";
age = 42; age = 42;
contact.email = "brynhjulf@yants.nix"; contact.email = "brynhjulf@yants.nix";
}; }))
];
# Test enum definitions & matching # Test enum definitions & matching
colour = enum "colour" [ "red" "blue" "green" ]; colour = enum "colour" [ "red" "blue" "green" ];
testMatch = colour.match "red" { colourMatcher = {
red = "It is in fact red!"; red = "It is in fact red!";
blue = throw "It should not be blue!"; blue = "It should not be blue!";
green = throw "It should not be green!"; green = "It should not be green!";
}; };
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";
}))
)
];
# Test sum type definitions # Test sum type definitions
creature = sum "creature" { creature = sum "creature" {
human = struct { human = struct {
@ -59,44 +77,70 @@ deepSeq rec {
pet = enum "pet" [ "dog" "lizard" "cat" ]; pet = enum "pet" [ "dog" "lizard" "cat" ];
}; };
some-human = creature {
testSum = creature {
human = { human = {
name = "Brynhjulf"; name = "Brynhjulf";
age = 42; age = 42;
}; };
}; };
testSumMatch = creature.match testSum { 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}"; human = v: "It's a human named ${v.name}";
pet = v: throw "It's not supposed to be a pet!"; pet = v: "It's not supposed to be a pet!";
}; })
)
];
# Test curried function definitions # Test curried function definitions
func = defun [ string int string ] func = defun [ string int string ]
(name: age: "${name} is ${toString age} years old"); (name: age: "${name} is ${toString age} years old");
testFunc = func "Brynhjulf" 42; testFunctions = it "checks function definitions" [
(assertDoesNotThrow "function application" (func "Brynhjulf" 42))
];
# Test that all types are types. # Test that all types are types.
testTypes = map type [ assertIsType = name: t:
any bool drv float int string path 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)
(attrs int) (assertIsType "attrs int" (attrs int))
(eitherN [ int string bool ]) (assertIsType "eitherN [ ... ]" (eitherN [ int string bool ]))
(either int string) (assertIsType "either int string" (either int string))
(enum [ "foo" "bar" ]) (assertIsType "enum [ ... ]" (enum [ "foo" "bar" ]))
(list string) (assertIsType "list string" (list string))
(option int) (assertIsType "option int" (option int))
(option (list string)) (assertIsType "option (list string)" (option (list string)))
(struct { a = int; b = option string; }) (assertIsType "struct { ... }" (struct { a = int; b = option string; }))
(sum { a = int; b = option string; }) (assertIsType "sum { ... }" (sum { a = int; b = option string; }))
]; ];
testRestrict = [ testRestrict = it "checks restrict types" [
((restrict "< 42" (i: i < 42) int) 25) (assertDoesNotThrow "< 42" ((restrict "< 42" (i: i < 42) int) 25))
((restrict "not too long" (l: builtins.length l < 3) (list int)) [ 1 2 ]) (assertDoesNotThrow "list length < 3"
(list (restrict "eq 5" (v: v == 5) any) [ 5 5 5 ]) ((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 ]))
]; ];
} (pkgs.writeText "yants-tests" "All tests passed!") in
runTestsuite "yants" [
testPrimitives
testPoly
testStruct
testEnum
testSum
testFunctions
testTypes
testRestrict
]