diff options
Diffstat (limited to 'unit_tests/linda_tests.cpp')
-rw-r--r-- | unit_tests/linda_tests.cpp | 169 |
1 files changed, 85 insertions, 84 deletions
diff --git a/unit_tests/linda_tests.cpp b/unit_tests/linda_tests.cpp index 9dbaa85..90630a7 100644 --- a/unit_tests/linda_tests.cpp +++ b/unit_tests/linda_tests.cpp | |||
@@ -35,7 +35,7 @@ TEST_CASE("linda.single_keeper.creation/close_handler") | |||
35 | LuaState S{ LuaState::WithBaseLibs{ true }, LuaState::WithFixture{ true } }; | 35 | LuaState S{ LuaState::WithBaseLibs{ true }, LuaState::WithFixture{ true } }; |
36 | S.requireSuccess("lanes = require 'lanes'"); | 36 | S.requireSuccess("lanes = require 'lanes'"); |
37 | 37 | ||
38 | if constexpr (LUA_VERSION_NUM == 504) { | 38 | if constexpr (LUA_VERSION_NUM >= 504) { |
39 | // a function is acceptable as a __close handler | 39 | // a function is acceptable as a __close handler |
40 | S.requireSuccess("local l <close> = lanes.linda{close_handler = function() end}"); | 40 | S.requireSuccess("local l <close> = lanes.linda{close_handler = function() end}"); |
41 | // a callable table too (a callable full userdata as well, but I have none here) | 41 | // a callable table too (a callable full userdata as well, but I have none here) |
@@ -104,117 +104,115 @@ TEST_CASE("linda.single_keeper.creation/wake_period") | |||
104 | S.requireFailure("lanes.linda{wake_period = -1}"); | 104 | S.requireFailure("lanes.linda{wake_period = -1}"); |
105 | S.requireFailure("lanes.linda{wake_period = 0}"); | 105 | S.requireFailure("lanes.linda{wake_period = 0}"); |
106 | S.requireSuccess("lanes.linda{wake_period = 0.0001}"); | 106 | S.requireSuccess("lanes.linda{wake_period = 0.0001}"); |
107 | S.requireSuccess("lanes.linda{wake_period = 'never'}"); | ||
107 | } | 108 | } |
108 | 109 | ||
109 | // ################################################################################################# | 110 | // ################################################################################################# |
110 | 111 | ||
111 | TEST_CASE("linda.single_keeper.wake_period") | 112 | TEST_CASE("linda.single_keeper.indexing") |
112 | { | 113 | { |
113 | FAIL("TODO: check that wake_period works as expected"); | 114 | LuaState S{ LuaState::WithBaseLibs{ true }, LuaState::WithFixture{ true } }; |
114 | // - use configure default if not provided | 115 | S.requireSuccess("lanes = require 'lanes'.configure()"); |
115 | // - overrides default when provided | 116 | |
116 | // - blocking operation wakes at the specified period | 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]"); | ||
117 | } | 124 | } |
118 | 125 | ||
119 | // ################################################################################################# | 126 | // ################################################################################################# |
120 | 127 | ||
121 | TEST_CASE("linda.single_keeper.the_rest") | 128 | TEST_CASE("linda.single_keeper.send()") |
122 | { | 129 | { |
123 | LuaState S{ LuaState::WithBaseLibs{ true }, LuaState::WithFixture{ true } }; | 130 | LuaState S{ LuaState::WithBaseLibs{ true }, LuaState::WithFixture{ true } }; |
124 | S.requireSuccess("lanes = require 'lanes'"); | 131 | S.requireSuccess("lanes = require 'lanes'.configure()"); |
125 | 132 | ||
126 | // --------------------------------------------------------------------------------------------- | 133 | SECTION("timeout") |
127 | |||
128 | SECTION("error in close handler is propagated") | ||
129 | { | 134 | { |
130 | // if the function raises an error, we should get it | 135 | // timeout checks |
131 | 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')"); | ||
132 | } | 143 | } |
133 | 144 | ||
134 | // --------------------------------------------------------------------------------------------- | 145 | // ----------------------------------------------------------------------------------------- |
135 | 146 | ||
136 | SECTION("Linda indexing") | 147 | SECTION("fails on bad keys") |
137 | { | 148 | { |
138 | // indexing the linda with an unknown string key should fail | 149 | // key checks |
139 | S.requireFailure("return lanes.linda().gouikra"); | 150 | // linda:send() should fail if the key is unsupported (nil, table, function, full userdata, reserved light userdata) |
140 | // indexing the linda with an unsupported key type should fail | 151 | S.requireFailure("lanes.linda():send(0, nil, 'v')"); |
141 | S.requireFailure("return lanes.linda()[5]"); | 152 | S.requireFailure("lanes.linda():send(0, {}, 'v')"); |
142 | S.requireFailure("return lanes.linda()[false]"); | 153 | S.requireFailure("lanes.linda():send(0, function() end, 'v')"); |
143 | S.requireFailure("return lanes.linda()[{}]"); | 154 | S.requireFailure("lanes.linda():send(0, io.stdin, 'v')"); |
144 | 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')"); | ||
145 | } | 157 | } |
146 | 158 | ||
147 | // --------------------------------------------------------------------------------------------- | 159 | // ----------------------------------------------------------------------------------------- |
148 | SECTION("linda:send()") | 160 | |
161 | SECTION("succeeds on supported keys") | ||
149 | { | 162 | { |
150 | SECTION("timeout") | 163 | // supported keys are ok: boolean, number, string, light userdata, deep userdata |
151 | { | 164 | S.requireSuccess("lanes.linda():send(0, true, 'v')"); |
152 | // timeout checks | 165 | S.requireSuccess("lanes.linda():send(0, false, 'v')"); |
153 | // linda:send() should fail if the timeout is bad | 166 | S.requireSuccess("lanes.linda():send(0, 99, 'v')"); |
154 | S.requireFailure("lanes.linda():send(-1, 'k', 'v')"); | 167 | S.requireSuccess("local l = lanes.linda(); l:send(0, l:deep(), 'v')"); |
155 | // any positive value is ok | 168 | } |
156 | S.requireSuccess("lanes.linda():send(0, 'k', 'v')"); | ||
157 | S.requireSuccess("lanes.linda():send(1e20, 'k', 'v')"); | ||
158 | // nil too (same as 'forever') | ||
159 | S.requireSuccess("lanes.linda():send(nil, 'k', 'v')"); | ||
160 | } | ||
161 | 169 | ||
162 | // ----------------------------------------------------------------------------------------- | 170 | // ----------------------------------------------------------------------------------------- |
163 | 171 | ||
164 | SECTION("fails on bad keys") | 172 | SECTION("succeeds on deep userdata key") |
165 | { | 173 | { |
166 | // key checks | 174 | S.requireSuccess("local l = lanes.linda(); l:send(0, l, 'v')"); |
167 | // linda:send() should fail if the key is unsupported (nil, table, function, full userdata, reserved light userdata) | 175 | } |
168 | S.requireFailure("lanes.linda():send(0, nil, 'v')"); | ||
169 | S.requireFailure("lanes.linda():send(0, {}, 'v')"); | ||
170 | S.requireFailure("lanes.linda():send(0, function() end, 'v')"); | ||
171 | S.requireFailure("lanes.linda():send(0, io.stdin, 'v')"); | ||
172 | S.requireFailure("lanes.linda():send(0, lanes.null, 'v')"); | ||
173 | S.requireFailure("lanes.linda():send(0, lanes.cancel_error, 'v')"); | ||
174 | } | ||
175 | 176 | ||
176 | // ----------------------------------------------------------------------------------------- | 177 | // ----------------------------------------------------------------------------------------- |
177 | 178 | ||
178 | SECTION("succeeds on supported keys") | 179 | SECTION(". fails") |
179 | { | 180 | { |
180 | // supported keys are ok: boolean, number, string, light userdata, deep userdata | 181 | // misuse checks, . instead of : |
181 | S.requireSuccess("lanes.linda():send(0, true, 'v')"); | 182 | S.requireFailure("lanes.linda().send(nil, 'k', 'v')"); |
182 | S.requireSuccess("lanes.linda():send(0, false, 'v')"); | 183 | } |
183 | S.requireSuccess("lanes.linda():send(0, 99, 'v')"); | ||
184 | S.requireSuccess("local l = lanes.linda(); l:send(0, l:deep(), 'v')"); | ||
185 | } | ||
186 | 184 | ||
187 | // ----------------------------------------------------------------------------------------- | 185 | // ----------------------------------------------------------------------------------------- |
188 | 186 | ||
189 | SECTION("succeeds on deep userdata key") | 187 | SECTION("unsupported values fail") |
190 | { | 188 | { |
191 | S.requireSuccess("local l = lanes.linda(); l:send(0, l, 'v')"); | 189 | // value checks |
192 | } | 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 | } | ||
193 | 202 | ||
194 | // ----------------------------------------------------------------------------------------- | 203 | // ################################################################################################# |
195 | 204 | ||
196 | SECTION(". fails") | 205 | TEST_CASE("linda.single_keeper.the_rest") |
197 | { | 206 | { |
198 | // misuse checks, . instead of : | 207 | LuaState S{ LuaState::WithBaseLibs{ true }, LuaState::WithFixture{ true } }; |
199 | S.requireFailure("lanes.linda().send(nil, 'k', 'v')"); | 208 | S.requireSuccess("lanes = require 'lanes'"); |
200 | } | ||
201 | 209 | ||
202 | // ----------------------------------------------------------------------------------------- | 210 | // --------------------------------------------------------------------------------------------- |
203 | 211 | ||
204 | SECTION("unsupported values fail") | 212 | SECTION("error in close handler is propagated") |
205 | { | 213 | { |
206 | // value checks | 214 | // if the function raises an error, we should get it |
207 | // linda:send() should fail if we don't send anything | 215 | S.requireFailure("local l <close> = lanes.linda{close_handler = function() error 'gluh' end}"); |
208 | S.requireFailure("lanes.linda():send()"); | ||
209 | S.requireFailure("lanes.linda():send(0)"); | ||
210 | S.requireFailure("lanes.linda():send(0, 'k')"); | ||
211 | // or non-deep userdata | ||
212 | S.requireFailure("lanes.linda():send(0, 'k', fixture.newuserdata())"); | ||
213 | // or something with a converter that raises an error (maybe that should go to a dedicated __lanesconvert test!) | ||
214 | S.requireFailure("lanes.linda():send(0, 'k', setmetatable({}, {__lanesconvert = function(where_) error (where_ .. ': should not send me' end}))"); | ||
215 | // but a registered non-deep userdata should work | ||
216 | S.requireSuccess("lanes.linda():send(0, 'k', io.stdin)"); | ||
217 | } | ||
218 | } | 216 | } |
219 | 217 | ||
220 | // --------------------------------------------------------------------------------------------- | 218 | // --------------------------------------------------------------------------------------------- |
@@ -397,17 +395,20 @@ TEST_CASE("scripted_tests." #DIR "." #FILE) \ | |||
397 | _runner.performTest(FileRunnerParam{ #DIR "/" #FILE, TestType::AssertNoLuaError }); \ | 395 | _runner.performTest(FileRunnerParam{ #DIR "/" #FILE, TestType::AssertNoLuaError }); \ |
398 | } | 396 | } |
399 | 397 | ||
398 | MAKE_TEST_CASE(linda, multiple_keepers) | ||
400 | MAKE_TEST_CASE(linda, send_receive) | 399 | MAKE_TEST_CASE(linda, send_receive) |
400 | MAKE_TEST_CASE(linda, send_receive_func_and_string) | ||
401 | MAKE_TEST_CASE(linda, send_registered_userdata) | 401 | MAKE_TEST_CASE(linda, send_registered_userdata) |
402 | MAKE_TEST_CASE(linda, multiple_keepers) | 402 | MAKE_TEST_CASE(linda, wake_period) |
403 | 403 | ||
404 | /* | 404 | /* |
405 | TEST_CASE("linda.scripted_tests") | 405 | TEST_CASE("linda.scripted_tests") |
406 | { | 406 | { |
407 | auto const& _testParam = GENERATE( | 407 | auto const& _testParam = GENERATE( |
408 | FileRunnerParam{ "linda/multiple_keepers", TestType::AssertNoLuaError }, | ||
408 | FileRunnerParam{ "linda/send_receive", TestType::AssertNoLuaError }, | 409 | FileRunnerParam{ "linda/send_receive", TestType::AssertNoLuaError }, |
409 | FileRunnerParam{ "linda/send_registered_userdata", TestType::AssertNoLuaError }, | 410 | FileRunnerParam{ "linda/send_registered_userdata", TestType::AssertNoLuaError }, |
410 | FileRunnerParam{ "linda/multiple_keepers", TestType::AssertNoLuaError } | 411 | FileRunnerParam{ "linda/wake_period", TestType::AssertNoLuaError } |
411 | ); | 412 | ); |
412 | 413 | ||
413 | FileRunner _runner(R"(.\unit_tests\scripts)"); | 414 | FileRunner _runner(R"(.\unit_tests\scripts)"); |