File: issue6.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 (59 lines) | stat: -rwxr-xr-x 989 bytes parent folder | download | duplicates (4)
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#! /usr/bin/lua

require 'Test.More'

local mp = require 'MessagePack'

local data = {
    '1',
    '12',
    '123',
    '1234',
    '12345',
    '123456',
    '1234567',
    '12345678',
    '123456789',
    '12345678',
    '1234567',
    '123456',
    '12345',
    '1234',
    '123',
    '12',
    '1',
}

plan(#data)

local f = io.open('data.mpac', 'w')
for i = 1, #data do
    local mpac = mp.pack(data[i])
    f:write(mpac)
end
f:close()

local ltn12 = { source = {} }
function ltn12.source.file (handle)
    if handle then
        return function ()
            local chunk = handle:read(16)
            if not chunk then
                handle:close()
            end
            return chunk
        end
    else return function ()
            return nil, "unable to open file"
        end
    end
end

f = io.open('data.mpac', 'r')
local s = ltn12.source.file(f)
local i = 1
for _, val in mp.unpacker(s) do
    is(val, data[i])
    i = i + 1
end
os.remove 'data.mpac'  -- clean up