Skip to content

Commit 3b3a197

Browse files
committed
Embedded checks
1 parent 2535467 commit 3b3a197

12 files changed

+312
-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+
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+
local luatest = require('luatest')
15+
local test_group = luatest.group()
16+
test_group.test_checks = function()
17+
luatest.assert_equals(sqrt(decimal.new(16)), 4)
18+
luatest.assert_error_msg_contains('bad argument #1 to nil (decimal expected, got number)', sqrt, 16)
19+
end
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
local luatest = require('luatest')
18+
local test_group = luatest.group()
19+
test_group.test_checks = function()
20+
luatest.assert_equals(get_doubled_number(10), 20)
21+
luatest.assert_error_msg_contains('bad argument #1 to nil (positive expected, got number)', get_doubled_number, -5)
22+
end
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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+
local luatest = require('luatest')
15+
local test_group = luatest.group()
16+
test_group.test_checks = function()
17+
luatest.assert_equals(get_blue_value(blue), 255)
18+
luatest.assert_error_msg_contains('bad argument #1 to nil (color expected, got table)', get_blue_value, { 0, 0, 255 })
19+
end
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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+
local luatest = require('luatest')
14+
local test_group = luatest.group()
15+
test_group.test_checks = function()
16+
luatest.assert_equals(greet_fullname('John', 'Smith'), 'Hello, John Smith')
17+
luatest.assert_error_msg_contains('bad argument #2 to nil (string expected, got number)', greet_fullname, 'John', 1)
18+
end
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
local luatest = require('luatest')
21+
local test_group = luatest.group()
22+
test_group.test_checks = function()
23+
luatest.assert_equals(greet('John'), 'Hello, John')
24+
luatest.assert_error_msg_contains('bad argument #1 to nil (string expected, got number)', greet, 123)
25+
end
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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+
local luatest = require('luatest')
14+
local test_group = luatest.group()
15+
test_group.test_checks = function()
16+
luatest.assert_equals(greet('John'), 'Hello, John')
17+
luatest.assert_error_msg_contains('bad argument #1 to nil (string expected, got number)', greet, 123)
18+
end
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
function configure_connection(options)
2+
checks({
3+
ip_address = 'string',
4+
port = '?number'
5+
})
6+
local ip_address = options.ip_address or '127.0.0.1'
7+
local port = options.port or 3301
8+
if port ~= nil then
9+
return ip_address..':'..port
10+
else
11+
return ip_address
12+
end
13+
end
14+
--[[
15+
configure_connection({ip_address = '0.0.0.0', port = 3303})
16+
-- returns '0.0.0.0:3303'
17+
18+
configure_connection({ip_address = '0.0.0.0', port = '3303'})
19+
-- raises an error: bad argument options.port to nil (?number expected, got string)
20+
21+
configure_connection({login = 'testuser', ip_address = '0.0.0.0'})
22+
-- raises an error: unexpected argument options.login to nil
23+
--]]
24+
25+
local luatest = require('luatest')
26+
local test_group = luatest.group()
27+
test_group.test_checks = function()
28+
luatest.assert_equals(configure_connection({ip_address = '0.0.0.0', port = 3303}), '0.0.0.0:3303')
29+
luatest.assert_error_msg_contains('bad argument options.port to nil (?number expected, got string)', configure_connection, {ip_address = '0.0.0.0', port = '3303'})
30+
luatest.assert_error_msg_contains('unexpected argument options.login to nil', configure_connection, {login = 'testuser', ip_address = '0.0.0.0'})
31+
end
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
local luatest = require('luatest')
17+
local test_group = luatest.group()
18+
test_group.test_checks = function()
19+
luatest.assert_equals(get_argument_type(1), 'number')
20+
luatest.assert_equals(get_argument_type('key1'), 'string')
21+
luatest.assert_error_msg_contains('bad argument #1 to nil (number|string expected, got boolean)', get_argument_type, true)
22+
end
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
function extra_arguments_num(a, ...)
2+
checks('string')
3+
return select('#', ...)
4+
end
5+
--[[
6+
extra_arguments_num('a', 'b', 'c')
7+
-- returns 2
8+
9+
extra_arguments_num(1, 'b', 'c')
10+
-- raises an error: bad argument #1 to nil (string expected, got number)
11+
--]]
12+
13+
local luatest = require('luatest')
14+
local test_group = luatest.group()
15+
test_group.test_checks = function()
16+
luatest.assert_equals(extra_arguments_num('a', 'b', 'c'), 2)
17+
luatest.assert_error_msg_contains('bad argument #1 to nil (string expected, got number)', extra_arguments_num, 1, 'b', 'c')
18+
end
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
.. checks-module:
2+
3+
Module checks
4+
=============
5+
6+
**Since:** :doc:`2.11.0 </release/2.11.0>`
7+
8+
9+
10+
11+
12+
13+
.. _checks_api_reference:
14+
15+
API Reference
16+
-------------
17+
18+
.. container:: table
19+
20+
.. rst-class:: left-align-column-1
21+
.. rst-class:: left-align-column-2
22+
23+
.. list-table::
24+
:widths: 35 65
25+
26+
* - **Functions**
27+
-
28+
29+
* - :ref:`checks <checks-checks>`
30+
- Check argument types
31+
32+
* - **Objects**
33+
-
34+
35+
* - :ref:`checkers <checks-checkers>`
36+
- Checkers for different types
37+
38+
39+
.. _checks-checks:
40+
41+
checks()
42+
~~~~~~~~
43+
44+
.. function:: checks(type_specifier, ...)
45+
46+
When called inside a function, checks that the function's arguments conforms to the specified types.
47+
48+
:param string/table type_specifier: a type specifier
49+
50+
.. _checks-checkers:
51+
52+
checkers
53+
~~~~~~~~
54+
55+
.. class:: checkers
56+
57+
Checkers for different types.
58+
59+
.. _checks-checkers-datetime:
60+
61+
.. function:: datetime(object)
62+
63+
Check whether the specified object has the :doc:`datetime </reference/reference_lua/datetime>` type.
64+
65+
:param any object: an object to check the type for
66+
67+
**Example**
68+
69+
.. literalinclude:: /code_snippets/test/checks/checkers_test.lua
70+
:language: lua
71+
:start-after: -- datetime/interval checkers
72+
:end-before: -- is_datetime/is_interval = true
73+
:dedent:

doc/reference/reference_lua/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ This reference covers Tarantool's built-in Lua modules.
2222

2323
box
2424
buffer
25+
checks
2526
clock
2627
compat
2728
console

0 commit comments

Comments
 (0)