Validate std/random dice args (#16430)

Fixes #16429
This commit is contained in:
Bruce Weirdan
2025-08-13 21:50:49 +02:00
committed by GitHub
parent da54ed8ea1
commit 91e72ae8b4
2 changed files with 43 additions and 0 deletions

View File

@@ -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<int> {
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)]

View File

@@ -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"
}