diff options
author | Thijs Schreijer <thijs@thijsschreijer.nl> | 2023-11-09 23:03:21 +0100 |
---|---|---|
committer | Thijs Schreijer <thijs@thijsschreijer.nl> | 2023-11-15 19:17:57 +0100 |
commit | d45768de3e6f7b28bfecf4d19b192ccac9ce5dc2 (patch) | |
tree | 2d4f86ec87eb87a77f6663924aaaa9286756ce3e /spec/02-random_spec.lua | |
parent | d4222ce6da2a2d7179fc79f9d0cc65fd6c09a686 (diff) | |
download | luasystem-d45768de3e6f7b28bfecf4d19b192ccac9ce5dc2.tar.gz luasystem-d45768de3e6f7b28bfecf4d19b192ccac9ce5dc2.tar.bz2 luasystem-d45768de3e6f7b28bfecf4d19b192ccac9ce5dc2.zip |
feat(*): add environment variable and random functions
Diffstat (limited to 'spec/02-random_spec.lua')
-rw-r--r-- | spec/02-random_spec.lua | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/spec/02-random_spec.lua b/spec/02-random_spec.lua new file mode 100644 index 0000000..23b6d95 --- /dev/null +++ b/spec/02-random_spec.lua | |||
@@ -0,0 +1,47 @@ | |||
1 | local system = require("system") | ||
2 | |||
3 | describe("Random:", function() | ||
4 | |||
5 | describe("random()", function() | ||
6 | |||
7 | it("should return random bytes for a valid number of bytes", function() | ||
8 | local num_bytes = 1 | ||
9 | local result, err_msg = system.random(num_bytes) | ||
10 | assert.is_nil(err_msg) | ||
11 | assert.is.string(result) | ||
12 | assert.is_equal(num_bytes, #result) | ||
13 | end) | ||
14 | |||
15 | |||
16 | it("should return an empty string for 0 bytes", function() | ||
17 | local num_bytes = 0 | ||
18 | local result, err_msg = system.random(num_bytes) | ||
19 | assert.is_nil(err_msg) | ||
20 | assert.are.equal("", result) | ||
21 | end) | ||
22 | |||
23 | |||
24 | it("should return an error message for an invalid number of bytes", function() | ||
25 | local num_bytes = -1 | ||
26 | local result, err_msg = system.random(num_bytes) | ||
27 | assert.is.falsy(result) | ||
28 | assert.are.equal("invalid number of bytes, must not be less than 0", err_msg) | ||
29 | end) | ||
30 | |||
31 | |||
32 | it("should not return duplicate results", function() | ||
33 | local num_bytes = 1025 | ||
34 | local result1, err_msg = system.random(num_bytes) | ||
35 | assert.is_nil(err_msg) | ||
36 | assert.is.string(result1) | ||
37 | |||
38 | local result2, err_msg = system.random(num_bytes) | ||
39 | assert.is_nil(err_msg) | ||
40 | assert.is.string(result2) | ||
41 | |||
42 | assert.is_not.equal(result1, result2) | ||
43 | end) | ||
44 | |||
45 | end) | ||
46 | |||
47 | end) | ||