From 6e163c1791b68398834af9ce8b83fc051132ceb1 Mon Sep 17 00:00:00 2001
From: VitaliyaIoffe <vitaioffe@yandex.ru>
Date: Tue, 29 Jun 2021 14:24:45 +0300
Subject: [PATCH] Migrate msgpack.test.lua to luatest

Use luatest for checking correctness behavioral for encode/decode
functions

Delete msgpack.test.lua.

Part of tarantool/test-run#304
---
 test/app-luatest/msgpack_test.lua | 183 +++++++++++++++++
 test/app-luatest/suite.ini        |   3 +
 test/app/msgpack.result           | 319 ------------------------------
 test/app/msgpack.test.lua         | 115 -----------
 4 files changed, 186 insertions(+), 434 deletions(-)
 create mode 100644 test/app-luatest/msgpack_test.lua
 create mode 100644 test/app-luatest/suite.ini
 delete mode 100644 test/app/msgpack.result
 delete mode 100644 test/app/msgpack.test.lua

diff --git a/test/app-luatest/msgpack_test.lua b/test/app-luatest/msgpack_test.lua
new file mode 100644
index 0000000000..f9ece70dca
--- /dev/null
+++ b/test/app-luatest/msgpack_test.lua
@@ -0,0 +1,183 @@
+local buffer = require('buffer')
+local msgpack = require('msgpack')
+local ffi = require('ffi')
+local t = require('luatest')
+local g = t.group()
+
+g.test_errors = function()
+    -- Encode/decode error messages
+    local buf = buffer.ibuf()
+    t.assert_error_msg_content_equals("msgpack.encode: a Lua object expected",
+        function() msgpack.encode() end)
+    t.assert_error_msg_content_equals(
+        "msgpack.encode: argument 2 must be of type 'struct ibuf'",
+        function() msgpack.encode('test', 'str') end)
+    t.assert_error_msg_content_equals(
+        "msgpack.encode: argument 2 must be of type 'struct ibuf'",
+        function() msgpack.encode('test', buf.buf) end)
+
+    t.assert_error_msg_content_equals(
+        "msgpack.decode: a Lua string or 'char *' expected",
+        function() msgpack.decode() end)
+    t.assert_error_msg_content_equals(
+        "msgpack.decode: a Lua string or 'char *' expected",
+        function() msgpack.decode(123) end)
+    t.assert_error_msg_content_equals(
+        "msgpack.decode: a Lua string or 'char *' expected",
+        function() msgpack.decode(buf) end)
+
+    t.assert_error_msg_content_equals(
+        "bad argument #2 to 'decode' (number expected, got string)",
+        function() msgpack.decode(buf.buf, 'size') end)
+    t.assert_error_msg_content_equals(
+        "msgpack.decode: offset is out of bounds",
+        function() msgpack.decode('test', 0) end)
+    t.assert_error_msg_content_equals(
+        "msgpack.decode: offset is out of bounds",
+        function() msgpack.decode('test', 5) end)
+
+    t.assert_error_msg_content_equals(
+        "bad argument #2 to 'decode' (number expected, got string)",
+        function() msgpack.decode('test', 'offset') end)
+    t.assert_error_msg_content_equals(
+        "msgpack.decode: a Lua string or 'char *' expected",
+        function() msgpack.decode_unchecked() end)
+    t.assert_error_msg_content_equals(
+        "msgpack.decode: a Lua string or 'char *' expected",
+        function() msgpack.decode_unchecked(123) end)
+    t.assert_error_msg_content_equals(
+        "msgpack.decode: a Lua string or 'char *' expected",
+        function() msgpack.decode_unchecked(buf) end)
+
+    t.assert_error_msg_content_equals(
+        "msgpack.decode: offset is out of bounds",
+        function() msgpack.decode_unchecked('test', 0) end)
+    t.assert_error_msg_content_equals(
+        "msgpack.decode: offset is out of bounds",
+        function() msgpack.decode_unchecked('test', 5) end)
+    t.assert_error_msg_content_equals(
+        "bad argument #2 to 'decode_unchecked' (number expected, got string)",
+        function() msgpack.decode_unchecked('test', 'offset') end)
+end
+
+g.test_encode_decode_strings = function()
+    -- Encode/decode strings.
+    local first_table = {1, 2, 3}
+    local second_table = {4, 5, 6}
+    local s = msgpack.encode(first_table) .. msgpack.encode(second_table)
+    local obj, offset = msgpack.decode(s)
+
+    t.assert_equals(obj, first_table)
+    obj, offset = msgpack.decode(s, offset)
+    t.assert_equals(obj, second_table)
+    t.assert_equals(offset, #s + 1)
+
+    obj, offset = msgpack.decode_unchecked(s)
+    t.assert_equals(obj, first_table)
+    obj, offset = msgpack.decode_unchecked(s, offset)
+    t.assert_equals(obj, second_table)
+    t.assert_equals(offset, #s + 1)
+end
+
+g.test_encode_decode_buffer = function()
+    -- Encode/decode a buffer.
+    local first_buffer = {1, 2, 3}
+    local second_buffer = {4, 5, 6}
+    local buf = buffer.ibuf()
+    local len = msgpack.encode(first_buffer, buf)
+    len = msgpack.encode(second_buffer, buf) + len
+    t.assert_equals(buf:size(), len)
+
+    local orig_rpos = buf.rpos
+    local obj, rpos = msgpack.decode(buf.rpos, buf:size())
+    t.assert_equals(obj, first_buffer)
+
+    buf.rpos = rpos
+    obj, rpos = msgpack.decode(buf.rpos, buf:size())
+    t.assert_equals(obj, second_buffer)
+
+    buf.rpos = rpos
+    t.assert_equals(buf:size(), 0)
+
+    buf.rpos = orig_rpos
+    obj, rpos = msgpack.decode_unchecked(buf.rpos, buf:size())
+    t.assert_equals(obj, first_buffer)
+
+    buf.rpos = rpos
+    obj, rpos = msgpack.decode_unchecked(buf.rpos, buf:size())
+    t.assert_equals(obj, second_buffer)
+
+    buf.rpos = rpos
+    t.assert_equals(buf:size(), 0)
+end
+
+g.test_invalid_msgpack = function()
+    -- Invalid msgpack.
+    local first_buffer = {1, 2, 3}
+    local s = msgpack.encode(first_buffer)
+    s = s:sub(1, -2)
+    t.assert_error_msg_content_equals(
+        "msgpack.decode: invalid MsgPack",
+        function() msgpack.decode(s) end)
+
+    local buf = buffer.ibuf()
+    t.assert_equals(msgpack.encode(first_buffer, buf), 4)
+    t.assert_error_msg_content_equals(
+        "msgpack.decode: invalid MsgPack",
+        function() msgpack.decode(buf.rpos, buf:size() - 1) end)
+end
+
+g.test_encode_decode_struct_buffer = function()
+    -- Provide a buffer. Try both 'struct ibuf' and 'struct ibuf *'.
+    local buf_storage = buffer.ibuf()
+    local buf = ffi.cast('struct ibuf *', buf_storage)
+    local size = msgpack.encode({a = 1, b = 2}, buf)
+    t.assert_equals((msgpack.decode(buf.rpos, size)), {a = 1, b = 2})
+
+    buf = buffer.ibuf()
+    size = msgpack.encode({c = 3, d = 4}, buf)
+    t.assert_equals((msgpack.decode(buf.rpos, size)), {c = 3, d = 4})
+end
+
+g.test_encode_decode_char_buffer = function()
+    -- Decode should accept both 'char *' and 'const char *'.
+    local buf = buffer.ibuf()
+    local elem = 100
+    local size = msgpack.encode(elem, buf)
+    t.assert_equals((msgpack.decode(ffi.cast('char *', buf.rpos), size)), elem)
+    t.assert_equals(
+        (msgpack.decode(ffi.cast('const char *', buf.rpos), size)), elem)
+end
+
+g.test_size_not_negative = function()
+    -- gh-4224: msgpack.decode(cdata, size) should check, that size
+    -- is not negative.
+    t.assert_error_msg_content_equals(
+        "msgpack.decode: size can't be negative",
+        function() msgpack.decode(ffi.cast('char *', '\x04\x05\x06'), -1) end)
+end
+
+g.test_encode_decode_decimals = function()
+    -- gh-4333: msgpack encode/decode decimals.
+    local decimal = require('decimal')
+    local a = decimal.new('1e37')
+    local b = decimal.new('1e-38')
+    local c = decimal.new('1')
+    local d = decimal.new('0.1234567')
+    local e = decimal.new('123.4567')
+    t.assert_equals(msgpack.decode(msgpack.encode(a)), a)
+    t.assert_equals(msgpack.decode(msgpack.encode(b)), b)
+    t.assert_equals(msgpack.decode(msgpack.encode(c)), c)
+    t.assert_equals(msgpack.decode(msgpack.encode(d)), d)
+    t.assert_equals(msgpack.decode(msgpack.encode(e)), e)
+end
+
+g.test_encode_decode_uuid = function()
+    -- encode/decode uuid
+    local uuid = require('uuid')
+    for _ = 1, 10 do
+        local src = uuid.new()
+        local res = msgpack.decode(msgpack.encode(src))
+        t.assert_equals(src, res)
+    end
+end
diff --git a/test/app-luatest/suite.ini b/test/app-luatest/suite.ini
new file mode 100644
index 0000000000..4dff490ec8
--- /dev/null
+++ b/test/app-luatest/suite.ini
@@ -0,0 +1,3 @@
+[default]
+core = luatest
+description = application server tests on luatest
diff --git a/test/app/msgpack.result b/test/app/msgpack.result
deleted file mode 100644
index faee66029e..0000000000
--- a/test/app/msgpack.result
+++ /dev/null
@@ -1,319 +0,0 @@
-buffer = require 'buffer'
----
-...
-msgpack = require 'msgpack'
----
-...
-ffi = require 'ffi'
----
-...
--- Arguments check.
-buf = buffer.ibuf()
----
-...
-msgpack.encode()
----
-- error: 'msgpack.encode: a Lua object expected'
-...
-msgpack.encode('test', 'str')
----
-- error: 'msgpack.encode: argument 2 must be of type ''struct ibuf'''
-...
-msgpack.encode('test', buf.buf)
----
-- error: 'msgpack.encode: argument 2 must be of type ''struct ibuf'''
-...
-msgpack.decode()
----
-- error: 'msgpack.decode: a Lua string or ''char *'' expected'
-...
-msgpack.decode(123)
----
-- error: 'msgpack.decode: a Lua string or ''char *'' expected'
-...
-msgpack.decode(buf)
----
-- error: 'msgpack.decode: a Lua string or ''char *'' expected'
-...
-msgpack.decode(buf.buf, 'size')
----
-- error: 'bad argument #2 to ''?'' (number expected, got string)'
-...
-msgpack.decode('test', 0)
----
-- error: 'msgpack.decode: offset is out of bounds'
-...
-msgpack.decode('test', 5)
----
-- error: 'msgpack.decode: offset is out of bounds'
-...
-msgpack.decode('test', 'offset')
----
-- error: 'bad argument #2 to ''?'' (number expected, got string)'
-...
-msgpack.decode_unchecked()
----
-- error: 'msgpack.decode: a Lua string or ''char *'' expected'
-...
-msgpack.decode_unchecked(123)
----
-- error: 'msgpack.decode: a Lua string or ''char *'' expected'
-...
-msgpack.decode_unchecked(buf)
----
-- error: 'msgpack.decode: a Lua string or ''char *'' expected'
-...
-msgpack.decode_unchecked('test', 0)
----
-- error: 'msgpack.decode: offset is out of bounds'
-...
-msgpack.decode_unchecked('test', 5)
----
-- error: 'msgpack.decode: offset is out of bounds'
-...
-msgpack.decode_unchecked('test', 'offset')
----
-- error: 'bad argument #2 to ''?'' (number expected, got string)'
-...
--- Encode/decode a string.
-s = msgpack.encode({1, 2, 3}) .. msgpack.encode({4, 5, 6})
----
-...
-obj, offset = msgpack.decode(s)
----
-...
-obj
----
-- [1, 2, 3]
-...
-obj, offset = msgpack.decode(s, offset)
----
-...
-obj
----
-- [4, 5, 6]
-...
-offset == #s + 1
----
-- true
-...
-obj, offset = msgpack.decode_unchecked(s)
----
-...
-obj
----
-- [1, 2, 3]
-...
-obj, offset = msgpack.decode_unchecked(s, offset)
----
-...
-obj
----
-- [4, 5, 6]
-...
-offset == #s + 1
----
-- true
-...
--- Encode/decode a buffer.
-buf = buffer.ibuf()
----
-...
-len = msgpack.encode({1, 2, 3}, buf)
----
-...
-len = msgpack.encode({4, 5, 6}, buf) + len
----
-...
-buf:size() == len
----
-- true
-...
-orig_rpos = buf.rpos
----
-...
-obj, rpos = msgpack.decode(buf.rpos, buf:size())
----
-...
-obj
----
-- [1, 2, 3]
-...
-buf.rpos = rpos
----
-...
-obj, rpos = msgpack.decode(buf.rpos, buf:size())
----
-...
-obj
----
-- [4, 5, 6]
-...
-buf.rpos = rpos
----
-...
-buf:size() == 0
----
-- true
-...
-buf.rpos = orig_rpos
----
-...
-obj, rpos = msgpack.decode_unchecked(buf.rpos, buf:size())
----
-...
-obj
----
-- [1, 2, 3]
-...
-buf.rpos = rpos
----
-...
-obj, rpos = msgpack.decode_unchecked(buf.rpos, buf:size())
----
-...
-obj
----
-- [4, 5, 6]
-...
-buf.rpos = rpos
----
-...
-buf:size() == 0
----
-- true
-...
--- Invalid msgpack.
-s = msgpack.encode({1, 2, 3})
----
-...
-s = s:sub(1, -2)
----
-...
-msgpack.decode(s)
----
-- error: 'msgpack.decode: invalid MsgPack'
-...
-buf = buffer.ibuf()
----
-...
-msgpack.encode({1, 2, 3}, buf)
----
-- 4
-...
-msgpack.decode(buf.rpos, buf:size() - 1)
----
-- error: 'msgpack.decode: invalid MsgPack'
-...
--- Provide a buffer. Try both 'struct ibuf' and 'struct ibuf *'.
-buf_storage = buffer.ibuf()
----
-...
-buf = ffi.cast('struct ibuf *', buf_storage)
----
-...
-size = msgpack.encode({a = 1, b = 2}, buf)
----
-...
-(msgpack.decode(buf.rpos, size))
----
-- {'a': 1, 'b': 2}
-...
-buf_storage = nil
----
-...
-buf = buffer.ibuf()
----
-...
-size = msgpack.encode({c = 3, d = 4}, buf)
----
-...
-(msgpack.decode(buf.rpos, size))
----
-- {'c': 3, 'd': 4}
-...
--- Decode should accept both 'char *' and 'const char *'.
-buf:reset()
----
-...
-size = msgpack.encode(100, buf)
----
-...
-(msgpack.decode(ffi.cast('char *', buf.rpos), size))
----
-- 100
-...
-(msgpack.decode(ffi.cast('const char *', buf.rpos), size))
----
-- 100
-...
---
--- gh-4224: msgpack.decode(cdata, size) should check, that size
--- is not negative.
---
-msgpack.decode(ffi.cast('char *', '\x04\x05\x06'), -1)
----
-- error: 'msgpack.decode: size can''t be negative'
-...
---
--- gh-4333: msgpack encode/decode decimals.
---
-decimal = require('decimal')
----
-...
-a = decimal.new('1e37')
----
-...
-b = decimal.new('1e-38')
----
-...
-c = decimal.new('1')
----
-...
-d = decimal.new('0.1234567')
----
-...
-e = decimal.new('123.4567')
----
-...
-msgpack.decode(msgpack.encode(a)) == a
----
-- true
-...
-msgpack.decode(msgpack.encode(b)) == b
----
-- true
-...
-msgpack.decode(msgpack.encode(c)) == c
----
-- true
-...
-msgpack.decode(msgpack.encode(d)) == d
----
-- true
-...
-msgpack.decode(msgpack.encode(e)) == e
----
-- true
-...
---
--- gh-4268: msgpack encode/decode UUID
---
-uuid = require('uuid')
----
-...
-fail = nil
----
-...
-for i = 1,10 do\
-    local a = uuid.new()\
-    if msgpack.decode(msgpack.encode(a)) ~= a then\
-        fail = a\
-    end\
-end
----
-...
-fail
----
-- null
-...
diff --git a/test/app/msgpack.test.lua b/test/app/msgpack.test.lua
deleted file mode 100644
index f5299f5507..0000000000
--- a/test/app/msgpack.test.lua
+++ /dev/null
@@ -1,115 +0,0 @@
-buffer = require 'buffer'
-msgpack = require 'msgpack'
-ffi = require 'ffi'
-
--- Arguments check.
-buf = buffer.ibuf()
-msgpack.encode()
-msgpack.encode('test', 'str')
-msgpack.encode('test', buf.buf)
-msgpack.decode()
-msgpack.decode(123)
-msgpack.decode(buf)
-msgpack.decode(buf.buf, 'size')
-msgpack.decode('test', 0)
-msgpack.decode('test', 5)
-msgpack.decode('test', 'offset')
-msgpack.decode_unchecked()
-msgpack.decode_unchecked(123)
-msgpack.decode_unchecked(buf)
-msgpack.decode_unchecked('test', 0)
-msgpack.decode_unchecked('test', 5)
-msgpack.decode_unchecked('test', 'offset')
-
--- Encode/decode a string.
-s = msgpack.encode({1, 2, 3}) .. msgpack.encode({4, 5, 6})
-obj, offset = msgpack.decode(s)
-obj
-obj, offset = msgpack.decode(s, offset)
-obj
-offset == #s + 1
-obj, offset = msgpack.decode_unchecked(s)
-obj
-obj, offset = msgpack.decode_unchecked(s, offset)
-obj
-offset == #s + 1
-
--- Encode/decode a buffer.
-buf = buffer.ibuf()
-len = msgpack.encode({1, 2, 3}, buf)
-len = msgpack.encode({4, 5, 6}, buf) + len
-buf:size() == len
-orig_rpos = buf.rpos
-obj, rpos = msgpack.decode(buf.rpos, buf:size())
-obj
-buf.rpos = rpos
-obj, rpos = msgpack.decode(buf.rpos, buf:size())
-obj
-buf.rpos = rpos
-buf:size() == 0
-buf.rpos = orig_rpos
-obj, rpos = msgpack.decode_unchecked(buf.rpos, buf:size())
-obj
-buf.rpos = rpos
-obj, rpos = msgpack.decode_unchecked(buf.rpos, buf:size())
-obj
-buf.rpos = rpos
-buf:size() == 0
-
--- Invalid msgpack.
-s = msgpack.encode({1, 2, 3})
-s = s:sub(1, -2)
-msgpack.decode(s)
-buf = buffer.ibuf()
-msgpack.encode({1, 2, 3}, buf)
-msgpack.decode(buf.rpos, buf:size() - 1)
-
--- Provide a buffer. Try both 'struct ibuf' and 'struct ibuf *'.
-buf_storage = buffer.ibuf()
-buf = ffi.cast('struct ibuf *', buf_storage)
-size = msgpack.encode({a = 1, b = 2}, buf)
-(msgpack.decode(buf.rpos, size))
-buf_storage = nil
-buf = buffer.ibuf()
-size = msgpack.encode({c = 3, d = 4}, buf)
-(msgpack.decode(buf.rpos, size))
-
--- Decode should accept both 'char *' and 'const char *'.
-buf:reset()
-size = msgpack.encode(100, buf)
-(msgpack.decode(ffi.cast('char *', buf.rpos), size))
-(msgpack.decode(ffi.cast('const char *', buf.rpos), size))
-
---
--- gh-4224: msgpack.decode(cdata, size) should check, that size
--- is not negative.
---
-msgpack.decode(ffi.cast('char *', '\x04\x05\x06'), -1)
-
---
--- gh-4333: msgpack encode/decode decimals.
---
-decimal = require('decimal')
-a = decimal.new('1e37')
-b = decimal.new('1e-38')
-c = decimal.new('1')
-d = decimal.new('0.1234567')
-e = decimal.new('123.4567')
-msgpack.decode(msgpack.encode(a)) == a
-msgpack.decode(msgpack.encode(b)) == b
-msgpack.decode(msgpack.encode(c)) == c
-msgpack.decode(msgpack.encode(d)) == d
-msgpack.decode(msgpack.encode(e)) == e
-
---
--- gh-4268: msgpack encode/decode UUID
---
-uuid = require('uuid')
-fail = nil
-for i = 1,10 do\
-    local a = uuid.new()\
-    if msgpack.decode(msgpack.encode(a)) ~= a then\
-        fail = a\
-    end\
-end
-fail
-- 
GitLab