From 0dd646775e8f3458040e5d76a4f0bfdfe3eefabb Mon Sep 17 00:00:00 2001 From: tristan Date: Tue, 2 Jan 2024 13:15:50 +0000 Subject: [PATCH] add describe function --- flake.nix | 4 +++- test/describe.test.nix | 21 +++++++++++++++++++++ test/it.test.nix | 22 ++++++++++++++-------- test/run.test.nix | 8 +++----- test/test.test.nix | 40 ++++++++++++++++++++++++---------------- tix/describe.nix | 8 ++++++++ tix/test.nix | 14 +++++++------- 7 files changed, 80 insertions(+), 37 deletions(-) create mode 100644 test/describe.test.nix create mode 100644 tix/describe.nix diff --git a/flake.nix b/flake.nix index 1dcc622..cbd5b4e 100644 --- a/flake.nix +++ b/flake.nix @@ -9,10 +9,12 @@ pkgs = import nixpkgs {inherit system;}; tix = import ./tix/. {inherit pkgs;}; in - tix // { + tix + // { packages.x86_64-linux.test = tix.run [ ./test/it.test.nix ./test/test.test.nix + ./test/describe.test.nix ]; packages.x86_64-linux.example = tix.run [ ./mytest.nix diff --git a/test/describe.test.nix b/test/describe.test.nix new file mode 100644 index 0000000..efd6395 --- /dev/null +++ b/test/describe.test.nix @@ -0,0 +1,21 @@ +{ + describe, + it, + ... +}: +describe "the describe function" [ + (it "returns an array with the message changed" { + expected = [ + { + success = true; + msg = "the describe function: works"; + } + ]; + actual = describe "the describe function" [ + { + success = true; + msg = "works"; + } + ]; + }) +] diff --git a/test/it.test.nix b/test/it.test.nix index 82c4e98..6daf1a2 100644 --- a/test/it.test.nix +++ b/test/it.test.nix @@ -1,11 +1,17 @@ -{ it, ... }: -[ +{ + it, + describe, + ... +}: +describe "it" [ (it "test's itself???" rec { - expected = {success = true; msg = "test's itself???";}; - actual = - (it "test's itself???" { - expected = expected; - actual = expected; - }); + expected = { + success = true; + msg = "test's itself???"; + }; + actual = it "test's itself???" { + expected = expected; + actual = expected; + }; }) ] diff --git a/test/run.test.nix b/test/run.test.nix index ea49311..ed03dea 100644 --- a/test/run.test.nix +++ b/test/run.test.nix @@ -1,10 +1,8 @@ -{ it, ... }: -let +{it, ...}: let pkgs = import {}; # impure ¯\_(ツ)_/¯ run = import ../tix/run.nix pkgs; - output = run [ ./it.test.nix ]; -in -[ + output = run [./it.test.nix]; +in [ (it "makes a derivation called test" { expected = "test"; actual = output.name; diff --git a/test/test.test.nix b/test/test.test.nix index f792a7a..a6a6807 100644 --- a/test/test.test.nix +++ b/test/test.test.nix @@ -1,18 +1,26 @@ -{ it, ... }: -let +{ + describe, + it, + ... +}: let test = import ../tix/test.nix; in -[ - (it "get's test results" { - expected = [{"msg" = "test's itself???"; "success"= true;}]; - actual = ( test ./it.test.nix ).results; - }) - (it "has a path" { - actual = builtins.typeOf ( test ./it.test.nix ).path; - expected = "path"; - }) - (it "fails to build non test" { - actual = ( test ./bad/string.nix ); - throws = true; - }) -] + describe "the test function" [ + (it "get's test results" { + expected = [ + { + "msg" = "it: test's itself???"; + "success" = true; + } + ]; + actual = (test ./it.test.nix).results; + }) + (it "has a path" { + actual = builtins.typeOf (test ./it.test.nix).path; + expected = "path"; + }) + (it "fails to build non test" { + actual = test ./bad/string.nix; + throws = true; + }) + ] diff --git a/tix/describe.nix b/tix/describe.nix new file mode 100644 index 0000000..3d855e9 --- /dev/null +++ b/tix/describe.nix @@ -0,0 +1,8 @@ +name: +map ({ + msg, + success, + actual ? null, + expected ? null, + } @ result: + result // {msg = "${name}: ${msg}";}) diff --git a/tix/test.nix b/tix/test.nix index 8a2897b..5b08439 100644 --- a/tix/test.nix +++ b/tix/test.nix @@ -1,14 +1,14 @@ path: let it = import ./it.nix; + describe = import ./describe.nix; suite = import path; in builtins.trace ("testing " + builtins.baseNameOf path) ( - if !builtins.isFunction suite - then throw "A test suite should be in the form { it }: [ ]." - else - { - inherit path; - results = suite {inherit it;}; - } + if !builtins.isFunction suite + then throw "A test suite should be in the form { it }: [ ]." + else { + inherit path; + results = suite {inherit it describe;}; + } )