File: 11-userdata-bc.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 (40 lines) | stat: -rwxr-xr-x 782 bytes parent folder | download | duplicates (2)
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
#! /usr/bin/lua

require 'Test.More'

if not pcall(require, 'bc') then
    skip_all 'no bc'
end

plan(3)

local mp = require 'MessagePack'
local bc = require 'bc'
local EXT_BC = 42
bc.digits(65)
local number = bc.number or bc.new

mp.packers['userdata'] = function (buffer, u)
    if getmetatable(u) == bc then
        mp.packers['ext'](buffer, EXT_BC, tostring(u))
    else
        error("pack 'userdata' is unimplemented")
    end
end

mp.build_ext = function (tag, data)
    if tag == EXT_BC then
        return number(data)
    end
end

local orig = bc.sqrt(2)
local dest = mp.unpack(mp.pack(orig))
is( dest, orig, "bc" )
nok( rawequal(orig, dest) )

error_like( function ()
                mp.pack( io.stdin )
            end,
            "pack 'userdata' is unimplemented" )