about summary refs log tree commit diff
path: root/nix/yants/tests.nix
diff options
context:
space:
mode:
authorVincent Ambo <tazjin@google.com>2019-12-20T21·46+0000
committerVincent Ambo <tazjin@google.com>2019-12-20T21·46+0000
commit210893ce090d251df1b75082035deb60a9b06be5 (patch)
treefc86177801d84a19e52e5c1df1a25200118eedbf /nix/yants/tests.nix
parent5f6b51cce4feb91ac5748099811c1a70c30b2937 (diff)
chore(yants): Prepare for depot-merge
Yants is being integrated at //depot/nix/yants
Diffstat (limited to 'nix/yants/tests.nix')
-rw-r--r--nix/yants/tests.nix92
1 files changed, 92 insertions, 0 deletions
diff --git a/nix/yants/tests.nix b/nix/yants/tests.nix
new file mode 100644
index 0000000000..6863ced066
--- /dev/null
+++ b/nix/yants/tests.nix
@@ -0,0 +1,92 @@
+with builtins;
+with (import ./default.nix {});
+
+# 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))
+    (path /nix)
+  ];
+
+  # 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;
+    };
+  };
+
+  testSumMatch = creature.match testSum {
+    human = v: "It's a human named ${v.name}";
+    pet = v: throw "It's not supposed to be a pet!";
+  };
+
+  # Test curried function definitions
+  func = defun [ string int string ]
+  (name: age: "${name} is ${toString age} years old");
+
+  testFunc = func "Brynhjulf" 42;
+
+  # Test that all types are types.
+  testTypes = map type [
+    any bool drv float int string path
+
+    (attrs int)
+    (eitherN [ int string bool ])
+    (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; })
+  ];
+} "All tests passed!\n"