From 4212bb72bdbc65d935dc8e0e9c93751ff7ff2e37 Mon Sep 17 00:00:00 2001
From: STEVAN Antoine <antoine.stevan@isae-supaero.fr>
Date: Wed, 29 May 2024 08:40:17 +0000
Subject: [PATCH] add tests for the `color.nu` module (dragoon/komodo!121)

this also bumps Nushell to 0.93.0 to include the "extra" command `fmt`, see [Nushell 0.92.0](https://www.nushell.sh/blog/2024-04-02-nushell_0_92_0.html#incorporating-the-extra-feature-by-default-toc)
---
 .gitlab-ci.yml    |  2 +-
 .nushell/color.nu | 12 ++++++---
 Makefile          |  1 +
 README.md         |  2 +-
 tests/color.nu    | 64 +++++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 76 insertions(+), 5 deletions(-)
 create mode 100644 tests/color.nu

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index 423b4b04..25b7762d 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -6,7 +6,7 @@ stages:
 
 variables:
   NUSHELL_ARCH: "x86_64-unknown-linux-musl"
-  NUSHELL_VERSION: "0.91.0"
+  NUSHELL_VERSION: "0.93.0"
 
 workflow:
   rules:
diff --git a/.nushell/color.nu b/.nushell/color.nu
index 54baf954..af279b69 100644
--- a/.nushell/color.nu
+++ b/.nushell/color.nu
@@ -67,10 +67,16 @@ export def "color from-ints" [
 }
 
 def try-string-to-int []: string -> int {
+    let s = $in
     try {
-        $"0x($in)" | into int
-    } catch {
-        get debug | parse --regex 'CantConvert { to_type: "(?<to>.*)", from_type: "(?<from>.*)", span: Span { (?<span>.*) }, help: Some\("(?<help>.*)"\) }' | into record | error make --unspanned { msg: ($in.help | str replace --all '\"' '"') }
+        $"0x($s)" | into int
+    } catch { |e|
+        let err = $e.debug
+            | parse --regex 'CantConvert { to_type: "(?<to>.*)", from_type: "(?<from>.*)", span: Span { (?<span>.*) }, help: Some\("(?<help>.*)"\) }'
+            | into record
+
+        let msg = $err.help | str replace --all '\"' '"'
+        error make --unspanned { msg: $"($msg), found ($s)" }
     }
 }
 
diff --git a/Makefile b/Makefile
index 23d35c34..bb44f39c 100644
--- a/Makefile
+++ b/Makefile
@@ -19,6 +19,7 @@ test:
 	cargo test --examples --verbose
 	nu tests/cli.nu
 	nu tests/binary.nu
+	nu tests/color.nu
 
 example:
 	nu examples/cli.nu
diff --git a/README.md b/README.md
index 3c0dadab..ae38c5eb 100644
--- a/README.md
+++ b/README.md
@@ -13,7 +13,7 @@ below is an example of how to use the binary application with Nushell:
 ```
 
 > **Note**  
-> requires Nushell 0.89.0 or later
+> requires Nushell 0.93.0 or later
 
 tests for the binary application can also be run with
 ```bash
diff --git a/tests/color.nu b/tests/color.nu
new file mode 100644
index 00000000..0374a9ea
--- /dev/null
+++ b/tests/color.nu
@@ -0,0 +1,64 @@
+use ../.nushell/color.nu [
+    "color from-floats",
+    "color from-ints",
+    "color from-string",
+    "color mix",
+    "color to-hex",
+    RED,
+    GREEN,
+]
+
+use std assert
+
+def "assert error" [code: closure, --msg: string, --body: string] {
+    try {
+        do $code
+    } catch { |e|
+        if $msg != null and ($e.msg | ansi strip) != $msg {
+            error make {
+                msg: $"(ansi red_bold)assertion: bad error message(ansi reset)",
+                label: {
+                    text: $"error should have message '($msg)'",
+                    span: (metadata $code).span,
+                },
+                help: $"actual: ($e.msg | ansi strip)",
+            }
+        }
+        if $body != null and not ($e.debug | ansi strip | str contains $body) {
+            let actual = $e.debug | ansi strip | parse "{foo}text: {text}, span: {bar}" | into record | get text
+            error make {
+                msg: $"(ansi red_bold)assertion: bad error body(ansi reset)",
+                label: {
+                    text: $"error should contain '($body)'",
+                    span: (metadata $code).span,
+                },
+                help: $"actual: ($actual)",
+            }
+        }
+        return
+    }
+
+    error make --unspanned { msg: "should error" }
+}
+
+assert error { || color from-floats 2 1 1 } --msg "invalid RGB channel" --body "should be between 0 and 1, found 2"
+assert error { || color from-floats 1 (-2 | into float) 1 } --msg "invalid RGB channel" --body "should be between 0 and 1, found -2"
+assert error { || color from-floats 1 1 3.4 } --msg "invalid RGB channel" --body "should be between 0 and 1, found 3.4"
+
+assert error { || color from-ints 256 0 0 } --msg "invalid RGB channel" --body "should be between 0 and 255, found 256"
+assert error { || color from-ints 0 256 0 } --msg "invalid RGB channel" --body "should be between 0 and 255, found 256"
+assert error { || color from-ints 0 0 256 } --msg "invalid RGB channel" --body "should be between 0 and 255, found 256"
+
+assert error { || color from-string "foo" } --msg "invalid HEX color format" --body "format should be '#RRGGBB', found foo"
+assert error { || color from-string "#foo" } --msg "invalid HEX color format" --body "format should be '#RRGGBB', found #foo"
+
+assert error { || color from-string "#xxxxxx" } --msg "hexadecimal digits following \"0x\" should be in 0-9, a-f, or A-F, found xx"
+assert error { || color from-string "#0123yy" } --msg "hexadecimal digits following \"0x\" should be in 0-9, a-f, or A-F, found yy"
+
+assert equal (color from-floats 0.1 0.2 0.3) { r: 0.1, g: 0.2, b: 0.3 }
+assert equal (color from-ints 1 2 3) { r: (1 / 255), g: (2 / 255), b: (3 / 255) }
+assert equal (color from-string "#010203") { r: (1 / 255), g: (2 / 255), b: (3 / 255) }
+
+assert equal (color from-string "#010203" | color to-hex) "#010203"
+
+assert equal (color mix $RED $GREEN 0.5) (color from-floats 0.5 0.5 0.0)
-- 
GitLab