diff options
| author | Benoit Germain <bnt.germain@gmail.com> | 2025-07-25 12:48:37 +0200 |
|---|---|---|
| committer | Benoit Germain <bnt.germain@gmail.com> | 2025-07-25 12:48:37 +0200 |
| commit | cbcd7d3860db0124feb5e512d11ebee81ccd62c7 (patch) | |
| tree | 7507bdad1fa740f051b18c3be7da6238178acfa8 /unit_tests/linda_tests.cpp | |
| parent | ccf4ac6b44a378d8f0e32b8186fdc4d3b25c3bda (diff) | |
| download | lanes-cbcd7d3860db0124feb5e512d11ebee81ccd62c7.tar.gz lanes-cbcd7d3860db0124feb5e512d11ebee81ccd62c7.tar.bz2 lanes-cbcd7d3860db0124feb5e512d11ebee81ccd62c7.zip | |
Split lanes.sleep unit tests and some linda unit tests too
Diffstat (limited to 'unit_tests/linda_tests.cpp')
| -rw-r--r-- | unit_tests/linda_tests.cpp | 157 |
1 files changed, 82 insertions, 75 deletions
diff --git a/unit_tests/linda_tests.cpp b/unit_tests/linda_tests.cpp index 8d3cd50..90630a7 100644 --- a/unit_tests/linda_tests.cpp +++ b/unit_tests/linda_tests.cpp | |||
| @@ -109,103 +109,110 @@ TEST_CASE("linda.single_keeper.creation/wake_period") | |||
| 109 | 109 | ||
| 110 | // ################################################################################################# | 110 | // ################################################################################################# |
| 111 | 111 | ||
| 112 | TEST_CASE("linda.single_keeper.the_rest") | 112 | TEST_CASE("linda.single_keeper.indexing") |
| 113 | { | 113 | { |
| 114 | LuaState S{ LuaState::WithBaseLibs{ true }, LuaState::WithFixture{ true } }; | 114 | LuaState S{ LuaState::WithBaseLibs{ true }, LuaState::WithFixture{ true } }; |
| 115 | S.requireSuccess("lanes = require 'lanes'"); | 115 | S.requireSuccess("lanes = require 'lanes'.configure()"); |
| 116 | |||
| 117 | // indexing the linda with an unknown string key should fail | ||
| 118 | S.requireFailure("return lanes.linda().gouikra"); | ||
| 119 | // indexing the linda with an unsupported key type should fail | ||
| 120 | S.requireFailure("return lanes.linda()[5]"); | ||
| 121 | S.requireFailure("return lanes.linda()[false]"); | ||
| 122 | S.requireFailure("return lanes.linda()[{}]"); | ||
| 123 | S.requireFailure("return lanes.linda()[function() end]"); | ||
| 124 | } | ||
| 116 | 125 | ||
| 117 | // --------------------------------------------------------------------------------------------- | 126 | // ################################################################################################# |
| 118 | 127 | ||
| 119 | SECTION("error in close handler is propagated") | 128 | TEST_CASE("linda.single_keeper.send()") |
| 129 | { | ||
| 130 | LuaState S{ LuaState::WithBaseLibs{ true }, LuaState::WithFixture{ true } }; | ||
| 131 | S.requireSuccess("lanes = require 'lanes'.configure()"); | ||
| 132 | |||
| 133 | SECTION("timeout") | ||
| 120 | { | 134 | { |
| 121 | // if the function raises an error, we should get it | 135 | // timeout checks |
| 122 | S.requireFailure("local l <close> = lanes.linda{close_handler = function() error 'gluh' end}"); | 136 | // linda:send() should fail if the timeout is bad |
| 137 | S.requireFailure("lanes.linda():send(-1, 'k', 'v')"); | ||
| 138 | // any positive value is ok | ||
| 139 | S.requireSuccess("lanes.linda():send(0, 'k', 'v')"); | ||
| 140 | S.requireSuccess("lanes.linda():send(1e20, 'k', 'v')"); | ||
| 141 | // nil too (same as 'forever') | ||
| 142 | S.requireSuccess("lanes.linda():send(nil, 'k', 'v')"); | ||
| 123 | } | 143 | } |
| 124 | 144 | ||
| 125 | // --------------------------------------------------------------------------------------------- | 145 | // ----------------------------------------------------------------------------------------- |
| 126 | 146 | ||
| 127 | SECTION("Linda indexing") | 147 | SECTION("fails on bad keys") |
| 128 | { | 148 | { |
| 129 | // indexing the linda with an unknown string key should fail | 149 | // key checks |
| 130 | S.requireFailure("return lanes.linda().gouikra"); | 150 | // linda:send() should fail if the key is unsupported (nil, table, function, full userdata, reserved light userdata) |
| 131 | // indexing the linda with an unsupported key type should fail | 151 | S.requireFailure("lanes.linda():send(0, nil, 'v')"); |
| 132 | S.requireFailure("return lanes.linda()[5]"); | 152 | S.requireFailure("lanes.linda():send(0, {}, 'v')"); |
| 133 | S.requireFailure("return lanes.linda()[false]"); | 153 | S.requireFailure("lanes.linda():send(0, function() end, 'v')"); |
| 134 | S.requireFailure("return lanes.linda()[{}]"); | 154 | S.requireFailure("lanes.linda():send(0, io.stdin, 'v')"); |
| 135 | S.requireFailure("return lanes.linda()[function() end]"); | 155 | S.requireFailure("lanes.linda():send(0, lanes.null, 'v')"); |
| 156 | S.requireFailure("lanes.linda():send(0, lanes.cancel_error, 'v')"); | ||
| 136 | } | 157 | } |
| 137 | 158 | ||
| 138 | // --------------------------------------------------------------------------------------------- | 159 | // ----------------------------------------------------------------------------------------- |
| 139 | SECTION("linda:send()") | 160 | |
| 161 | SECTION("succeeds on supported keys") | ||
| 140 | { | 162 | { |
| 141 | SECTION("timeout") | 163 | // supported keys are ok: boolean, number, string, light userdata, deep userdata |
| 142 | { | 164 | S.requireSuccess("lanes.linda():send(0, true, 'v')"); |
| 143 | // timeout checks | 165 | S.requireSuccess("lanes.linda():send(0, false, 'v')"); |
| 144 | // linda:send() should fail if the timeout is bad | 166 | S.requireSuccess("lanes.linda():send(0, 99, 'v')"); |
| 145 | S.requireFailure("lanes.linda():send(-1, 'k', 'v')"); | 167 | S.requireSuccess("local l = lanes.linda(); l:send(0, l:deep(), 'v')"); |
| 146 | // any positive value is ok | 168 | } |
| 147 | S.requireSuccess("lanes.linda():send(0, 'k', 'v')"); | ||
| 148 | S.requireSuccess("lanes.linda():send(1e20, 'k', 'v')"); | ||
| 149 | // nil too (same as 'forever') | ||
| 150 | S.requireSuccess("lanes.linda():send(nil, 'k', 'v')"); | ||
| 151 | } | ||
| 152 | 169 | ||
| 153 | // ----------------------------------------------------------------------------------------- | 170 | // ----------------------------------------------------------------------------------------- |
| 154 | 171 | ||
| 155 | SECTION("fails on bad keys") | 172 | SECTION("succeeds on deep userdata key") |
| 156 | { | 173 | { |
| 157 | // key checks | 174 | S.requireSuccess("local l = lanes.linda(); l:send(0, l, 'v')"); |
| 158 | // linda:send() should fail if the key is unsupported (nil, table, function, full userdata, reserved light userdata) | 175 | } |
| 159 | S.requireFailure("lanes.linda():send(0, nil, 'v')"); | ||
| 160 | S.requireFailure("lanes.linda():send(0, {}, 'v')"); | ||
| 161 | S.requireFailure("lanes.linda():send(0, function() end, 'v')"); | ||
| 162 | S.requireFailure("lanes.linda():send(0, io.stdin, 'v')"); | ||
| 163 | S.requireFailure("lanes.linda():send(0, lanes.null, 'v')"); | ||
| 164 | S.requireFailure("lanes.linda():send(0, lanes.cancel_error, 'v')"); | ||
| 165 | } | ||
| 166 | 176 | ||
| 167 | // ----------------------------------------------------------------------------------------- | 177 | // ----------------------------------------------------------------------------------------- |
| 168 | 178 | ||
| 169 | SECTION("succeeds on supported keys") | 179 | SECTION(". fails") |
| 170 | { | 180 | { |
| 171 | // supported keys are ok: boolean, number, string, light userdata, deep userdata | 181 | // misuse checks, . instead of : |
| 172 | S.requireSuccess("lanes.linda():send(0, true, 'v')"); | 182 | S.requireFailure("lanes.linda().send(nil, 'k', 'v')"); |
| 173 | S.requireSuccess("lanes.linda():send(0, false, 'v')"); | 183 | } |
| 174 | S.requireSuccess("lanes.linda():send(0, 99, 'v')"); | ||
| 175 | S.requireSuccess("local l = lanes.linda(); l:send(0, l:deep(), 'v')"); | ||
| 176 | } | ||
| 177 | 184 | ||
| 178 | // ----------------------------------------------------------------------------------------- | 185 | // ----------------------------------------------------------------------------------------- |
| 179 | 186 | ||
| 180 | SECTION("succeeds on deep userdata key") | 187 | SECTION("unsupported values fail") |
| 181 | { | 188 | { |
| 182 | S.requireSuccess("local l = lanes.linda(); l:send(0, l, 'v')"); | 189 | // value checks |
| 183 | } | 190 | // linda:send() should fail if we don't send anything |
| 191 | S.requireFailure("lanes.linda():send()"); | ||
| 192 | S.requireFailure("lanes.linda():send(0)"); | ||
| 193 | S.requireFailure("lanes.linda():send(0, 'k')"); | ||
| 194 | // or non-deep userdata | ||
| 195 | S.requireFailure("lanes.linda():send(0, 'k', fixture.newuserdata())"); | ||
| 196 | // or something with a converter that raises an error (maybe that should go to a dedicated __lanesconvert test!) | ||
| 197 | S.requireFailure("lanes.linda():send(0, 'k', setmetatable({}, {__lanesconvert = function(where_) error (where_ .. ': should not send me' end}))"); | ||
| 198 | // but a registered non-deep userdata should work | ||
| 199 | S.requireSuccess("lanes.linda():send(0, 'k', io.stdin)"); | ||
| 200 | } | ||
| 201 | } | ||
| 184 | 202 | ||
| 185 | // ----------------------------------------------------------------------------------------- | 203 | // ################################################################################################# |
| 186 | 204 | ||
| 187 | SECTION(". fails") | 205 | TEST_CASE("linda.single_keeper.the_rest") |
| 188 | { | 206 | { |
| 189 | // misuse checks, . instead of : | 207 | LuaState S{ LuaState::WithBaseLibs{ true }, LuaState::WithFixture{ true } }; |
| 190 | S.requireFailure("lanes.linda().send(nil, 'k', 'v')"); | 208 | S.requireSuccess("lanes = require 'lanes'"); |
| 191 | } | ||
| 192 | 209 | ||
| 193 | // ----------------------------------------------------------------------------------------- | 210 | // --------------------------------------------------------------------------------------------- |
| 194 | 211 | ||
| 195 | SECTION("unsupported values fail") | 212 | SECTION("error in close handler is propagated") |
| 196 | { | 213 | { |
| 197 | // value checks | 214 | // if the function raises an error, we should get it |
| 198 | // linda:send() should fail if we don't send anything | 215 | S.requireFailure("local l <close> = lanes.linda{close_handler = function() error 'gluh' end}"); |
| 199 | S.requireFailure("lanes.linda():send()"); | ||
| 200 | S.requireFailure("lanes.linda():send(0)"); | ||
| 201 | S.requireFailure("lanes.linda():send(0, 'k')"); | ||
| 202 | // or non-deep userdata | ||
| 203 | S.requireFailure("lanes.linda():send(0, 'k', fixture.newuserdata())"); | ||
| 204 | // or something with a converter that raises an error (maybe that should go to a dedicated __lanesconvert test!) | ||
| 205 | S.requireFailure("lanes.linda():send(0, 'k', setmetatable({}, {__lanesconvert = function(where_) error (where_ .. ': should not send me' end}))"); | ||
| 206 | // but a registered non-deep userdata should work | ||
| 207 | S.requireSuccess("lanes.linda():send(0, 'k', io.stdin)"); | ||
| 208 | } | ||
| 209 | } | 216 | } |
| 210 | 217 | ||
| 211 | // --------------------------------------------------------------------------------------------- | 218 | // --------------------------------------------------------------------------------------------- |
