From 91e72ae8b4720422b63f8e81451785d9c3736644 Mon Sep 17 00:00:00 2001 From: Bruce Weirdan Date: Wed, 13 Aug 2025 21:50:49 +0200 Subject: [PATCH] Validate `std/random dice` args (#16430) Fixes #16429 --- crates/nu-std/std/random/mod.nu | 5 ++++ crates/nu-std/tests/test_std_random.nu | 38 ++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 crates/nu-std/tests/test_std_random.nu diff --git a/crates/nu-std/std/random/mod.nu b/crates/nu-std/std/random/mod.nu index 1f40cc5b64..409081c08d 100644 --- a/crates/nu-std/std/random/mod.nu +++ b/crates/nu-std/std/random/mod.nu @@ -1,3 +1,5 @@ +use std/assert greater + # for examples alias "random dice" = dice @@ -11,6 +13,9 @@ export def dice [ --dice = 1 # The amount of dice being rolled --sides = 6 # The amount of sides a die has ]: nothing -> list { + greater $dice 0 "The amount of dice must be greater than 0" + greater $sides 1 "The amount of sides must be greater than 1" + mut out = [] for _ in 1..$dice { $out ++= [(random int 1..$sides)] diff --git a/crates/nu-std/tests/test_std_random.nu b/crates/nu-std/tests/test_std_random.nu new file mode 100644 index 0000000000..b0f2b7c324 --- /dev/null +++ b/crates/nu-std/tests/test_std_random.nu @@ -0,0 +1,38 @@ +use std/assert +use std/testing * +use std/random + +@test +def "random dice rejects negative sides" [] { + assert error { + random dice --sides (-2) + } "--sides (-2) should not have been accepted" +} + +@test +def "random dice rejects zero sides" [] { + assert error { + random dice --sides 0 + } "--sides 0 should not have been accepted" +} + +@test +def "random dice rejects negative dice" [] { + assert error { + random dice --dice (-2) + } "--dice (-2) should not have been accepted" +} + +@test +def "random dice rejects zero dice" [] { + assert error { + random dice --dice 0 + } "--dice 0 should not have been accepted" +} + +@test +def "random dice rejects one-sided dice" [] { + assert error { + random dice --sides 1 + } "--sides 1 should not have been accepted" +}