File: uri-test.lua

package info (click to toggle)
lua-uri 0.1%2B20130926%2Bgit14fa255d-1
  • links: PTS, VCS
  • area: main
  • in suites: buster, jessie, jessie-kfreebsd, stretch
  • size: 408 kB
  • ctags: 332
  • sloc: makefile: 53
file content (85 lines) | stat: -rw-r--r-- 2,546 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
require "lunit"
local URI = require "uri"

is = lunit.assert_equal

function is_one_of (expecteds, actual, msg)
    for _, v in ipairs(expecteds) do
        if actual == v then return end
    end

    -- Not any of the expected answers matched.  In order to report the error
    -- usefully, we have to list the alternatives in the error message.
    local err = "expected one of {"
    for i, v in ipairs(expecteds) do
        if i > 1 then err = err .. ", " end
        err = err .. "'" .. tostring(v) .. "'"
    end
    err = err .. "}, but was '" .. tostring(actual) .. "'"
    if msg then err = err .. ": " .. msg end
    lunit.fail(err)
end

function assert_isa(actual, class)
    lunit.assert_table(actual)
    lunit.assert_table(class)
    local mt = actual
    while true do
        mt = getmetatable(mt)
        if not mt then error"class not found as metatable at any level" end
        if mt == actual then error"circular metatables" end
        if mt == class then return nil end
    end
end

function assert_array_shallow_equal (expected, actual, msg)
    if not msg then msg = "assert_array_shallow_equal" end
    lunit.assert_table(actual, msg .. ", is table")
    is(#expected, #actual, msg .. ", same size")
    if #expected == #actual then
        for i = 1, #expected do
            is(expected[i], actual[i], msg .. ", element " .. i)
        end
    end
    for key in pairs(actual) do
        lunit.assert_number(key, msg .. ", non-number key in array")
    end
end

local function _count_hash_pairs (hash)
    local count = 0
    for _, _ in pairs(hash) do count = count + 1 end
    return count
end

function assert_hash_shallow_equal (expected, actual, msg)
    if not msg then msg = "assert_hash_shallow_equal" end
    lunit.assert_table(actual, msg .. ", is table")
    local expsize, actualsize = _count_hash_pairs(expected),
                                _count_hash_pairs(actual)
    is(expsize, actualsize, msg .. ", same size")
    if expsize == actualsize then
        for k, v in pairs(expected) do
            is(expected[k], actual[k], msg .. ", element " .. tostring(k))
        end
    end
end

function is_bad_uri (msg, uri)
    local ok, err = URI:new(uri)
    lunit.assert_nil(ok, msg)
    lunit.assert_string(err, msg)
end

function test_norm (expected, input)
    local uri = assert(URI:new(input))
    is(expected, uri:uri())
    is(expected, tostring(uri))
    lunit.assert_false(uri:is_relative())
end

function test_norm_already (input)
    test_norm(input, input)
end

-- vi:ts=4 sw=4 expandtab