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 | |
| 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')
| -rw-r--r-- | spec/01-time_spec.lua | 77 | ||||
| -rw-r--r-- | spec/02-random_spec.lua | 47 | ||||
| -rw-r--r-- | spec/03-environment_spec.lua | 81 | ||||
| -rw-r--r-- | spec/time_spec.lua | 31 |
4 files changed, 205 insertions, 31 deletions
diff --git a/spec/01-time_spec.lua b/spec/01-time_spec.lua new file mode 100644 index 0000000..80faa75 --- /dev/null +++ b/spec/01-time_spec.lua | |||
| @@ -0,0 +1,77 @@ | |||
| 1 | local system = require 'system.core' | ||
| 2 | |||
| 3 | describe('Test time functions', function() | ||
| 4 | |||
| 5 | -- returns the new second, on the new second | ||
| 6 | local function wait_for_second_rollover() | ||
| 7 | local start_time = math.floor(os.time()) | ||
| 8 | local end_time = math.floor(os.time()) | ||
| 9 | while end_time == start_time do | ||
| 10 | end_time = math.floor(os.time()) | ||
| 11 | end | ||
| 12 | return end_time | ||
| 13 | end | ||
| 14 | |||
| 15 | |||
| 16 | describe("time()", function() | ||
| 17 | |||
| 18 | it('returns current time', function() | ||
| 19 | wait_for_second_rollover() | ||
| 20 | local expected_time = wait_for_second_rollover() | ||
| 21 | local received_time = system.gettime() | ||
| 22 | assert.is.near(expected_time, received_time, 0.02) | ||
| 23 | |||
| 24 | wait_for_second_rollover() | ||
| 25 | assert.is.near(1, system.gettime() - received_time, 0.02) | ||
| 26 | end) | ||
| 27 | |||
| 28 | end) | ||
| 29 | |||
| 30 | |||
| 31 | |||
| 32 | describe("monotime()", function() | ||
| 33 | |||
| 34 | it('returns monotonically increasing time', function() | ||
| 35 | local starttime = system.monotime() | ||
| 36 | local endtime = system.monotime() | ||
| 37 | local delta = endtime - starttime | ||
| 38 | assert.is_true(starttime > 0) | ||
| 39 | assert.is_true(delta >= 0) | ||
| 40 | assert.is_true(system.monotime() - endtime >= 0) | ||
| 41 | end) | ||
| 42 | |||
| 43 | end) | ||
| 44 | |||
| 45 | |||
| 46 | |||
| 47 | describe("sleep()", function() | ||
| 48 | |||
| 49 | it("should sleep for the specified time", function() | ||
| 50 | local start_time = system.gettime() | ||
| 51 | system.sleep(1, 1) | ||
| 52 | local end_time = system.gettime() | ||
| 53 | local elapsed_time = end_time - start_time | ||
| 54 | assert.is.near(elapsed_time, 1, 0.01) | ||
| 55 | end) | ||
| 56 | |||
| 57 | |||
| 58 | it("should sleep for the specified time; fractional", function() | ||
| 59 | local start_time = system.gettime() | ||
| 60 | system.sleep(0.5, 1) | ||
| 61 | local end_time = system.gettime() | ||
| 62 | local elapsed_time = end_time - start_time | ||
| 63 | assert.is.near(0.5, elapsed_time, 0.01) | ||
| 64 | end) | ||
| 65 | |||
| 66 | |||
| 67 | it("should return immediately for a non-positive sleep time", function() | ||
| 68 | local start_time = system.gettime() | ||
| 69 | system.sleep(-1) | ||
| 70 | local end_time = system.gettime() | ||
| 71 | local elapsed_time = end_time - start_time | ||
| 72 | assert.is.near(elapsed_time, 0, 0.01) | ||
| 73 | end) | ||
| 74 | |||
| 75 | end) | ||
| 76 | |||
| 77 | end) | ||
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) | ||
diff --git a/spec/03-environment_spec.lua b/spec/03-environment_spec.lua new file mode 100644 index 0000000..842ed6f --- /dev/null +++ b/spec/03-environment_spec.lua | |||
| @@ -0,0 +1,81 @@ | |||
| 1 | -- Import the library that contains the environment-related functions | ||
| 2 | local system = require("system") | ||
| 3 | |||
| 4 | describe("Environment Variables:", function() | ||
| 5 | |||
| 6 | describe("setenv()", function() | ||
| 7 | |||
| 8 | it("should set an environment variable", function() | ||
| 9 | assert.is_true(system.setenv("TEST_VAR", "test_value")) | ||
| 10 | assert.is_equal("test_value", system.getenv("TEST_VAR")) | ||
| 11 | end) | ||
| 12 | |||
| 13 | |||
| 14 | local func = system.windows and pending or it --pending on Windows | ||
| 15 | -- Windows will unset a variable if set as an empty string | ||
| 16 | func("should set an empty environment variable value", function() | ||
| 17 | assert.is_true(system.setenv("TEST_VAR", "")) | ||
| 18 | assert.is_equal("", system.getenv("TEST_VAR")) | ||
| 19 | end) | ||
| 20 | |||
| 21 | |||
| 22 | it("should unset an environment variable on nil", function() | ||
| 23 | assert.is_true(system.setenv("TEST_VAR", "test_value")) | ||
| 24 | assert.is_equal("test_value", system.getenv("TEST_VAR")) | ||
| 25 | |||
| 26 | assert.is_true(system.setenv("TEST_VAR", nil)) | ||
| 27 | assert.is_nil(system.getenv("TEST_VAR")) | ||
| 28 | end) | ||
| 29 | |||
| 30 | |||
| 31 | it("should error on input bad type", function() | ||
| 32 | assert.has_error(function() | ||
| 33 | system.setenv("TEST_VAR", {}) | ||
| 34 | end) | ||
| 35 | assert.has_error(function() | ||
| 36 | system.setenv({}, "test_value") | ||
| 37 | end) | ||
| 38 | end) | ||
| 39 | |||
| 40 | |||
| 41 | it("should return success on deleting a variable that doesn't exist", function() | ||
| 42 | if system.getenv("TEST_VAR") ~= nil then | ||
| 43 | -- clear if it was already set | ||
| 44 | assert.is_true(system.setenv("TEST_VAR", nil)) | ||
| 45 | end | ||
| 46 | |||
| 47 | assert.is_true(system.setenv("TEST_VAR", nil)) -- clear again shouldn't fail | ||
| 48 | end) | ||
| 49 | |||
| 50 | end) | ||
| 51 | |||
| 52 | |||
| 53 | |||
| 54 | describe("getenvs()", function() | ||
| 55 | |||
| 56 | it("should list environment variables", function() | ||
| 57 | assert.is_true(system.setenv("TEST_VAR1", nil)) | ||
| 58 | assert.is_true(system.setenv("TEST_VAR2", nil)) | ||
| 59 | assert.is_true(system.setenv("TEST_VAR3", nil)) | ||
| 60 | local envVars1 = system.getenvs() | ||
| 61 | assert.is_true(system.setenv("TEST_VAR1", "test_value1")) | ||
| 62 | assert.is_true(system.setenv("TEST_VAR2", "test_value2")) | ||
| 63 | assert.is_true(system.setenv("TEST_VAR3", "test_value3")) | ||
| 64 | local envVars2 = system.getenvs() | ||
| 65 | assert.is_true(system.setenv("TEST_VAR1", nil)) | ||
| 66 | assert.is_true(system.setenv("TEST_VAR2", nil)) | ||
| 67 | assert.is_true(system.setenv("TEST_VAR3", nil)) | ||
| 68 | |||
| 69 | for k,v in pairs(envVars1) do | ||
| 70 | envVars2[k] = nil | ||
| 71 | end | ||
| 72 | assert.are.same({ | ||
| 73 | TEST_VAR1 = "test_value1", | ||
| 74 | TEST_VAR2 = "test_value2", | ||
| 75 | TEST_VAR3 = "test_value3", | ||
| 76 | }, envVars2) | ||
| 77 | end) | ||
| 78 | |||
| 79 | end) | ||
| 80 | |||
| 81 | end) | ||
diff --git a/spec/time_spec.lua b/spec/time_spec.lua deleted file mode 100644 index a017cfe..0000000 --- a/spec/time_spec.lua +++ /dev/null | |||
| @@ -1,31 +0,0 @@ | |||
| 1 | local system = require 'system.core' | ||
| 2 | |||
| 3 | describe('Test time functions', function() | ||
| 4 | it('gettime returns current time', function() | ||
| 5 | local starttime = system.gettime() | ||
| 6 | local expected = os.time() | ||
| 7 | local endtime = system.gettime() | ||
| 8 | local delta = endtime - starttime | ||
| 9 | local avg = starttime + delta/2 | ||
| 10 | assert.is_true(expected >= math.floor(starttime)) | ||
| 11 | assert.is_true(expected <= math.ceil(endtime)) | ||
| 12 | assert.is_near(expected, avg, 1 + delta) | ||
| 13 | end) | ||
| 14 | |||
| 15 | it('monottime returns monotonically increasing time', function() | ||
| 16 | local starttime = system.monotime() | ||
| 17 | local endtime = system.monotime() | ||
| 18 | local delta = endtime - starttime | ||
| 19 | assert.is_true(starttime > 0) | ||
| 20 | assert.is_true(delta >= 0) | ||
| 21 | assert.is_true(system.monotime() - endtime >= 0) | ||
| 22 | end) | ||
| 23 | |||
| 24 | it('sleep will wait for specified amount of time', function() | ||
| 25 | local starttime = system.gettime() | ||
| 26 | local starttick = system.monotime() | ||
| 27 | system.sleep(0.5) | ||
| 28 | assert.is_near(0.5, system.gettime() - starttime, 0.15) | ||
| 29 | assert.is_near(0.5, system.monotime() - starttick, 0.15) | ||
| 30 | end) | ||
| 31 | end) | ||
