File: 31-custom-unpack.lua

package info (click to toggle)
lua-messagepack 0.5.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 364 kB
  • sloc: makefile: 108
file content (41 lines) | stat: -rwxr-xr-x 838 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#! /usr/bin/lua

require 'Test.More'

plan(3)

local mp = require 'MessagePack'

local function my_cursor_string (str, i, j)
    return {
        s = str,
        i = i or 1,
        j = j or #str,
        underflow = function ()
            error "missing bytes"
        end,
    }
end

local function my_unpack (s, i, j)
    local cursor = my_cursor_string(s, i, j)
    local data = mp.unpack_cursor(cursor)
    if cursor.i < cursor.j then
        error "extra bytes"
    end
    return data
end

local mpac = mp.pack({1, 2, 3})
local s = 'BAR' .. mpac .. 'BAZ'
eq_array( my_unpack(s, 4, #s - 3), {1, 2, 3}, "my_unpack" )

error_like( function ()
                my_unpack(s, 4)
            end,
            "extra bytes" )

error_like( function ()
                my_unpack(s, 4, #s - 4)
            end,
            "missing bytes" )