Skip to content

Commit 0684ec7

Browse files
Embedded checks (#3611)
1 parent 8eac966 commit 0684ec7

14 files changed

+804
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
-- datetime/interval checkers
2+
local datetime = require('datetime')
3+
local is_datetime = checkers.datetime(datetime.new { day = 1, month = 6, year = 2023 })
4+
local is_interval = checkers.interval(datetime.interval.new { day = 1 })
5+
-- is_datetime/is_interval = true
6+
7+
-- decimal checker
8+
local decimal = require('decimal')
9+
local is_decimal = checkers.decimal(decimal.new(16))
10+
-- is_decimal = true
11+
12+
-- error checker
13+
local server_error = box.error.new({ code = 500, reason = 'Server error' })
14+
local is_error = checkers.error(server_error)
15+
-- is_error = true
16+
17+
-- int64/uint64 checkers
18+
local is_int64 = checkers.int64(-1024)
19+
local is_uint64 = checkers.uint64(2048)
20+
-- is_int64/is_uint64 = true
21+
22+
-- tuple checker
23+
local is_tuple = checkers.tuple(box.tuple.new(1, 'The Beatles', 1960))
24+
-- is_tuple = true
25+
26+
-- uuid checkers
27+
local uuid = require('uuid')
28+
local is_uuid = checkers.uuid(uuid())
29+
local is_uuid_bin = checkers.uuid_bin(uuid.bin())
30+
local is_uuid_str = checkers.uuid_str(uuid.str())
31+
-- is_uuid/is_uuid_bin/is_uuid_str = true
32+
33+
local luatest = require('luatest')
34+
local test_group = luatest.group()
35+
test_group.test_checks = function()
36+
luatest.assert_equals(is_datetime, true)
37+
luatest.assert_equals(is_interval, true)
38+
luatest.assert_equals(is_decimal, true)
39+
luatest.assert_equals(is_error, true)
40+
luatest.assert_equals(is_int64, true)
41+
luatest.assert_equals(is_uint64, true)
42+
luatest.assert_equals(is_tuple, true)
43+
luatest.assert_equals(is_uuid, true)
44+
luatest.assert_equals(is_uuid_bin, true)
45+
luatest.assert_equals(is_uuid_str, true)
46+
end
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
function greet_fullname_any(firstname, lastname)
2+
checks('string', '?')
3+
return 'Hello, ' .. firstname .. ' ' .. tostring(lastname)
4+
end
5+
--[[
6+
greet_fullname_any('John', 'Doe')
7+
-- returns 'Hello, John Doe'
8+
9+
greet_fullname_any('John', 1)
10+
-- returns 'Hello, John 1'
11+
--]]
12+
13+
-- Tests
14+
local luatest = require('luatest')
15+
local test_group = luatest.group()
16+
test_group.test_checks = function()
17+
luatest.assert_equals(greet_fullname_any('John', 'Doe'), 'Hello, John Doe')
18+
luatest.assert_equals(greet_fullname_any('John', 1), 'Hello, John 1')
19+
end
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
local decimal = require('decimal')
2+
function sqrt(value)
3+
checks('decimal')
4+
return decimal.sqrt(value)
5+
end
6+
--[[
7+
sqrt(decimal.new(16))
8+
-- returns 4
9+
10+
sqrt(16)
11+
-- raises an error: bad argument #1 to nil (decimal expected, got number)
12+
--]]
13+
14+
-- Tests
15+
local luatest = require('luatest')
16+
local test_group = luatest.group()
17+
test_group.test_checks = function()
18+
luatest.assert_equals(sqrt(decimal.new(16)), 4)
19+
luatest.assert_error_msg_contains('bad argument #1 to nil (decimal expected, got number)', sqrt, 16)
20+
end
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
function checkers.positive(value)
2+
return (type(value) == 'number') and (value > 0)
3+
end
4+
5+
function get_doubled_number(value)
6+
checks('positive')
7+
return value * 2
8+
end
9+
--[[
10+
get_doubled_number(10)
11+
-- returns 20
12+
13+
get_doubled_number(-5)
14+
-- raises an error: bad argument #1 to nil (positive expected, got number)
15+
--]]
16+
17+
-- Tests
18+
local luatest = require('luatest')
19+
local test_group = luatest.group()
20+
test_group.test_checks = function()
21+
luatest.assert_equals(get_doubled_number(10), 20)
22+
luatest.assert_error_msg_contains('bad argument #1 to nil (positive expected, got number)', get_doubled_number, -5)
23+
end
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
local blue = setmetatable({ 0, 0, 255 }, { __type = 'color' })
2+
function get_blue_value(color)
3+
checks('color')
4+
return color[3]
5+
end
6+
--[[
7+
get_blue_value(blue)
8+
-- returns 255
9+
10+
get_blue_value({0, 0, 255})
11+
-- raises an error: bad argument #1 to nil (color expected, got table)
12+
--]]
13+
14+
-- Tests
15+
local luatest = require('luatest')
16+
local test_group = luatest.group()
17+
test_group.test_checks = function()
18+
luatest.assert_equals(get_blue_value(blue), 255)
19+
luatest.assert_error_msg_contains('bad argument #1 to nil (color expected, got table)', get_blue_value, { 0, 0, 255 })
20+
end
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
function greet_fullname(firstname, lastname)
2+
checks('string', 'string')
3+
return 'Hello, ' .. firstname .. ' ' .. lastname
4+
end
5+
--[[
6+
greet_fullname('John', 'Smith')
7+
-- returns 'Hello, John Smith'
8+
9+
greet_fullname('John', 1)
10+
-- raises an error: bad argument #2 to nil (string expected, got number)
11+
--]]
12+
13+
-- Tests
14+
local luatest = require('luatest')
15+
local test_group = luatest.group()
16+
test_group.test_checks = function()
17+
luatest.assert_equals(greet_fullname('John', 'Smith'), 'Hello, John Smith')
18+
luatest.assert_error_msg_contains('bad argument #2 to nil (string expected, got number)', greet_fullname, 'John', 1)
19+
end
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
function greet(name)
2+
checks('?string')
3+
if name ~= nil then
4+
return 'Hello, ' .. name
5+
else
6+
return 'Hello from Tarantool'
7+
end
8+
end
9+
--[[
10+
greet('John')
11+
-- returns 'Hello, John'
12+
13+
greet()
14+
-- returns 'Hello from Tarantool'
15+
16+
greet(123)
17+
-- raises an error: bad argument #1 to nil (string expected, got number)
18+
--]]
19+
20+
-- Tests
21+
local luatest = require('luatest')
22+
local test_group = luatest.group()
23+
test_group.test_checks = function()
24+
luatest.assert_equals(greet('John'), 'Hello, John')
25+
luatest.assert_error_msg_contains('bad argument #1 to nil (string expected, got number)', greet, 123)
26+
end
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
function greet(name)
2+
checks('string')
3+
return 'Hello, ' .. name
4+
end
5+
--[[
6+
greet('John')
7+
-- returns 'Hello, John'
8+
9+
greet(123)
10+
-- raises an error: bad argument #1 to nil (string expected, got number)
11+
--]]
12+
13+
-- Tests
14+
local luatest = require('luatest')
15+
local test_group = luatest.group()
16+
test_group.test_checks = function()
17+
luatest.assert_equals(greet('John'), 'Hello, John')
18+
luatest.assert_error_msg_contains('bad argument #1 to nil (string expected, got number)', greet, 123)
19+
end
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
function configure_connection(options)
2+
checks({ 'string', 'number' })
3+
local ip_address = options[1] or '127.0.0.1'
4+
local port = options[2] or 3301
5+
return ip_address .. ':' .. port
6+
end
7+
--[[
8+
configure_connection({'0.0.0.0', 3303})
9+
-- returns '0.0.0.0:3303'
10+
11+
configure_connection({'0.0.0.0', '3303'})
12+
-- raises an error: bad argument options[2] to nil (number expected, got string)
13+
--]]
14+
15+
-- Tests
16+
local luatest = require('luatest')
17+
local test_group = luatest.group()
18+
test_group.test_checks = function()
19+
luatest.assert_equals(configure_connection({ '0.0.0.0', 3303 }), '0.0.0.0:3303')
20+
luatest.assert_error_msg_contains('bad argument options[2] to nil (number expected, got string)', configure_connection, { '0.0.0.0', '3303' })
21+
end
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
function configure_connection_opts(options)
2+
checks({ ip_address = 'string', port = 'number' })
3+
local ip_address = options.ip_address or '127.0.0.1'
4+
local port = options.port or 3301
5+
return ip_address .. ':' .. port
6+
end
7+
--[[
8+
configure_connection_opts({ip_address = '0.0.0.0', port = 3303})
9+
-- returns '0.0.0.0:3303'
10+
11+
configure_connection_opts({ip_address = '0.0.0.0', port = '3303'})
12+
-- raises an error: bad argument options.port to nil (number expected, got string)
13+
14+
configure_connection_opts({login = 'testuser', ip_address = '0.0.0.0', port = 3303})
15+
-- raises an error: unexpected argument options.login to nil
16+
--]]
17+
18+
-- Tests
19+
local luatest = require('luatest')
20+
local test_group = luatest.group()
21+
test_group.test_checks = function()
22+
luatest.assert_equals(configure_connection_opts({ ip_address = '0.0.0.0', port = 3303 }), '0.0.0.0:3303')
23+
luatest.assert_error_msg_contains('bad argument options.port to nil (number expected, got string)', configure_connection_opts, { ip_address = '0.0.0.0', port = '3303' })
24+
luatest.assert_error_msg_contains('unexpected argument options.login to nil', configure_connection_opts, { login = 'testuser', ip_address = '0.0.0.0', port = 3303 })
25+
end
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
function get_argument_type(value)
2+
checks('number|string')
3+
return type(value)
4+
end
5+
--[[
6+
get_argument_type(1)
7+
-- returns 'number'
8+
9+
get_argument_type('key1')
10+
-- returns 'string'
11+
12+
get_argument_type(true)
13+
-- raises an error: bad argument #1 to nil (number|string expected, got boolean)
14+
--]]
15+
16+
-- Tests
17+
local luatest = require('luatest')
18+
local test_group = luatest.group()
19+
test_group.test_checks = function()
20+
luatest.assert_equals(get_argument_type(1), 'number')
21+
luatest.assert_equals(get_argument_type('key1'), 'string')
22+
luatest.assert_error_msg_contains('bad argument #1 to nil (number|string expected, got boolean)', get_argument_type, true)
23+
end
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
function extra_arguments_num(a, b, ...)
2+
checks('string', 'number')
3+
return select('#', ...)
4+
end
5+
--[[
6+
extra_arguments_num('a', 2, 'c')
7+
-- returns 1
8+
9+
extra_arguments_num('a', 'b', 'c')
10+
-- raises an error: bad argument #1 to nil (string expected, got number)
11+
--]]
12+
13+
-- Tests
14+
local luatest = require('luatest')
15+
local test_group = luatest.group()
16+
test_group.test_checks = function()
17+
luatest.assert_equals(extra_arguments_num('a', 2, 'c'), 1)
18+
luatest.assert_error_msg_contains('bad argument #2 to nil (number expected, got string)', extra_arguments_num, 'a', 'b', 'c')
19+
end

0 commit comments

Comments
 (0)