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.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 @@ +local system = require 'system.core' + +describe('Test time functions', function() + + -- returns the new second, on the new second + local function wait_for_second_rollover() + local start_time = math.floor(os.time()) + local end_time = math.floor(os.time()) + while end_time == start_time do + end_time = math.floor(os.time()) + end + return end_time + end + + + describe("time()", function() + + it('returns current time', function() + wait_for_second_rollover() + local expected_time = wait_for_second_rollover() + local received_time = system.gettime() + assert.is.near(expected_time, received_time, 0.02) + + wait_for_second_rollover() + assert.is.near(1, system.gettime() - received_time, 0.02) + end) + + end) + + + + describe("monotime()", function() + + it('returns monotonically increasing time', function() + local starttime = system.monotime() + local endtime = system.monotime() + local delta = endtime - starttime + assert.is_true(starttime > 0) + assert.is_true(delta >= 0) + assert.is_true(system.monotime() - endtime >= 0) + end) + + end) + + + + describe("sleep()", function() + + it("should sleep for the specified time", function() + local start_time = system.gettime() + system.sleep(1, 1) + local end_time = system.gettime() + local elapsed_time = end_time - start_time + assert.is.near(elapsed_time, 1, 0.01) + end) + + + it("should sleep for the specified time; fractional", function() + local start_time = system.gettime() + system.sleep(0.5, 1) + local end_time = system.gettime() + local elapsed_time = end_time - start_time + assert.is.near(0.5, elapsed_time, 0.01) + end) + + + it("should return immediately for a non-positive sleep time", function() + local start_time = system.gettime() + system.sleep(-1) + local end_time = system.gettime() + local elapsed_time = end_time - start_time + assert.is.near(elapsed_time, 0, 0.01) + end) + + end) + +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 @@ +local system = require("system") + +describe("Random:", function() + + describe("random()", function() + + it("should return random bytes for a valid number of bytes", function() + local num_bytes = 1 + local result, err_msg = system.random(num_bytes) + assert.is_nil(err_msg) + assert.is.string(result) + assert.is_equal(num_bytes, #result) + end) + + + it("should return an empty string for 0 bytes", function() + local num_bytes = 0 + local result, err_msg = system.random(num_bytes) + assert.is_nil(err_msg) + assert.are.equal("", result) + end) + + + it("should return an error message for an invalid number of bytes", function() + local num_bytes = -1 + local result, err_msg = system.random(num_bytes) + assert.is.falsy(result) + assert.are.equal("invalid number of bytes, must not be less than 0", err_msg) + end) + + + it("should not return duplicate results", function() + local num_bytes = 1025 + local result1, err_msg = system.random(num_bytes) + assert.is_nil(err_msg) + assert.is.string(result1) + + local result2, err_msg = system.random(num_bytes) + assert.is_nil(err_msg) + assert.is.string(result2) + + assert.is_not.equal(result1, result2) + end) + + end) + +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 @@ +-- Import the library that contains the environment-related functions +local system = require("system") + +describe("Environment Variables:", function() + + describe("setenv()", function() + + it("should set an environment variable", function() + assert.is_true(system.setenv("TEST_VAR", "test_value")) + assert.is_equal("test_value", system.getenv("TEST_VAR")) + end) + + + local func = system.windows and pending or it --pending on Windows + -- Windows will unset a variable if set as an empty string + func("should set an empty environment variable value", function() + assert.is_true(system.setenv("TEST_VAR", "")) + assert.is_equal("", system.getenv("TEST_VAR")) + end) + + + it("should unset an environment variable on nil", function() + assert.is_true(system.setenv("TEST_VAR", "test_value")) + assert.is_equal("test_value", system.getenv("TEST_VAR")) + + assert.is_true(system.setenv("TEST_VAR", nil)) + assert.is_nil(system.getenv("TEST_VAR")) + end) + + + it("should error on input bad type", function() + assert.has_error(function() + system.setenv("TEST_VAR", {}) + end) + assert.has_error(function() + system.setenv({}, "test_value") + end) + end) + + + it("should return success on deleting a variable that doesn't exist", function() + if system.getenv("TEST_VAR") ~= nil then + -- clear if it was already set + assert.is_true(system.setenv("TEST_VAR", nil)) + end + + assert.is_true(system.setenv("TEST_VAR", nil)) -- clear again shouldn't fail + end) + + end) + + + + describe("getenvs()", function() + + it("should list environment variables", function() + assert.is_true(system.setenv("TEST_VAR1", nil)) + assert.is_true(system.setenv("TEST_VAR2", nil)) + assert.is_true(system.setenv("TEST_VAR3", nil)) + local envVars1 = system.getenvs() + assert.is_true(system.setenv("TEST_VAR1", "test_value1")) + assert.is_true(system.setenv("TEST_VAR2", "test_value2")) + assert.is_true(system.setenv("TEST_VAR3", "test_value3")) + local envVars2 = system.getenvs() + assert.is_true(system.setenv("TEST_VAR1", nil)) + assert.is_true(system.setenv("TEST_VAR2", nil)) + assert.is_true(system.setenv("TEST_VAR3", nil)) + + for k,v in pairs(envVars1) do + envVars2[k] = nil + end + assert.are.same({ + TEST_VAR1 = "test_value1", + TEST_VAR2 = "test_value2", + TEST_VAR3 = "test_value3", + }, envVars2) + end) + + end) + +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 @@ -local system = require 'system.core' - -describe('Test time functions', function() - it('gettime returns current time', function() - local starttime = system.gettime() - local expected = os.time() - local endtime = system.gettime() - local delta = endtime - starttime - local avg = starttime + delta/2 - assert.is_true(expected >= math.floor(starttime)) - assert.is_true(expected <= math.ceil(endtime)) - assert.is_near(expected, avg, 1 + delta) - end) - - it('monottime returns monotonically increasing time', function() - local starttime = system.monotime() - local endtime = system.monotime() - local delta = endtime - starttime - assert.is_true(starttime > 0) - assert.is_true(delta >= 0) - assert.is_true(system.monotime() - endtime >= 0) - end) - - it('sleep will wait for specified amount of time', function() - local starttime = system.gettime() - local starttick = system.monotime() - system.sleep(0.5) - assert.is_near(0.5, system.gettime() - starttime, 0.15) - assert.is_near(0.5, system.monotime() - starttick, 0.15) - end) -end) |