mirror of
https://github.com/nushell/nushell.git
synced 2025-08-19 08:11:28 +02:00
@@ -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)]
|
||||
|
38
crates/nu-std/tests/test_std_random.nu
Normal file
38
crates/nu-std/tests/test_std_random.nu
Normal 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"
|
||||
}
|
Reference in New Issue
Block a user