File: urn-isbn.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 (110 lines) | stat: -rw-r--r-- 3,812 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
require "uri-test"
local URI = require "uri"
local Util = require "uri._util"

local have_isbn_module = Util.attempt_require("isbn")

module("test.urn_isbn", lunit.testcase, package.seeall)

function test_isbn ()
    -- Example from RFC 2288
    local u = URI:new("URN:ISBN:0-395-36341-1")
    is(have_isbn_module and "urn:isbn:0-395-36341-1" or "urn:isbn:0395363411",
       u:uri())
    is("urn", u:scheme())
    is("isbn", u:nid())
    is(have_isbn_module and "0-395-36341-1" or "0395363411", u:nss())
    is("0395363411", u:isbn_digits())

    u = URI:new("URN:ISBN:0395363411")
    is(have_isbn_module and "urn:isbn:0-395-36341-1" or "urn:isbn:0395363411",
       u:uri())
    is("urn", u:scheme())
    is("isbn", u:nid())
    is(have_isbn_module and "0-395-36341-1" or "0395363411", u:nss())
    is("0395363411", u:isbn_digits())

    if have_isbn_module then
        local isbn = u:isbn()
        assert_table(isbn)
        is("0-395-36341-1", tostring(isbn))
        is("0", isbn:group_code())
        is("395", isbn:publisher_code())
        is("978-0-395-36341-6", tostring(isbn:as_isbn13()))
    end

    assert_true(URI.eq("urn:isbn:088730866x", "URN:ISBN:0-88-73-08-66-X"))
end

function test_set_nss ()
    local uri = assert(URI:new("urn:isbn:039-53-63411"))
    is(have_isbn_module and "0-395-36341-1" or "0395363411",
       uri:nss("088-7308-66x"))
    is(have_isbn_module and "urn:isbn:0-88730-866-X" or "urn:isbn:088730866X",
       tostring(uri))
    is(have_isbn_module and "0-88730-866-X" or "088730866X", uri:nss())
end

function test_set_bad_nss ()
    local uri = assert(URI:new("urn:ISBN:039-53-63411"))
    assert_error("set NSS to non-string value", function () uri:nss({}) end)
    assert_error("set NSS to empty", function () uri:nss("") end)
    assert_error("set NSS to wrong length", function () uri:nss("123") end)

    -- None of that should have had any affect
    is(have_isbn_module and "urn:isbn:0-395-36341-1" or "urn:isbn:0395363411",
       tostring(uri))
    is(have_isbn_module and "0-395-36341-1" or "0395363411", uri:nss())
    is("0395363411", uri:isbn_digits())
    is("uri.urn.isbn", uri._NAME)
end

function test_set_path ()
    local uri = assert(URI:new("urn:ISBN:039-53-63411"))
    is(have_isbn_module and "isbn:0-395-36341-1" or "isbn:0395363411",
       uri:path("ISbn:088-73-0866x"))
    is(have_isbn_module and "urn:isbn:0-88730-866-X" or "urn:isbn:088730866X",
       tostring(uri))

    assert_error("bad path", function () uri:path("isbn:1234567") end)
    is(have_isbn_module and "urn:isbn:0-88730-866-X" or "urn:isbn:088730866X",
       tostring(uri))
    is(have_isbn_module and "isbn:0-88730-866-X" or "isbn:088730866X",
       uri:path())
end

function test_isbn_setting_digits ()
    local u = assert(URI:new("URN:ISBN:0395363411"))
    local old = u:isbn_digits("0-88730-866-x")
    is("0395363411", old)
    is("088730866X", u:isbn_digits())
    is(have_isbn_module and "0-88730-866-X" or "088730866X", u:nss())
    if have_isbn_module then
        is("0-88730-866-X", tostring(u:isbn()))
    end
end

function test_isbn_setting_object ()
    if have_isbn_module then
        local ISBN = require "isbn"
        local u = assert(URI:new("URN:ISBN:0395363411"))
        local old = u:isbn(ISBN:new("0-88730-866-x"))
        assert_table(old)
        is("0-395-36341-1", tostring(old))
        is("088730866X", u:isbn_digits())
        is("0-88730-866-X", u:nss())
        local new = u:isbn()
        assert_table(new)
        is("0-88730-866-X", tostring(new))
    end
end

function test_illegal_isbn ()
    is_bad_uri("invalid characters", "urn:ISBN:abc")
    if have_isbn_module then
        is_bad_uri("bad checksum", "urn:isbn:0395363412")
        is_bad_uri("wrong length", "urn:isbn:03953634101")
    end
end

-- vi:ts=4 sw=4 expandtab