git.s-ol.nu mmm / 782d072
add key_spec s-ol 3 years ago
4 changed file(s) with 99 addition(s) and 34 deletion(s). Raw diff Collapse all Expand all
1919 if 'string' == type second
2020 @name, @type = (opts or ''), second
2121 elseif 'string' == type opts
22 @name, @type = opts\match '^([%w-_]+): *(.+)$'
22 @name, @type = opts\match '^([%w-_]*): *(.+)$'
2323 if not @name
2424 @name = ''
25 @type = opts
25 @type = opts\match '^ *(.+)$'
2626 elseif 'table' == type opts
27 @name = opts.name
27 @name = opts.name or ''
2828 @type = opts.type
29 @original = opts.original
30 @filename = opts.filename
3129 else
3230 error "wrong argument type: #{type opts}, #{type second}"
31
32 assert ('string' == type @name), "name is not a string: '#{@name}'"
33 assert ('string' == type @type), "type is not a string: '#{@type}'"
3334
3435 -- format as a string (see constructor)
3536 tostring: =>
0 require 'spec.test_util'
1 package.loaded['mmm.mmmfs.conversion'] = {}
2 import Key from require 'mmm.mmmfs.fileder'
3
4 nt = (name, type) -> :name, :type
5
6 describe "Key", ->
7 it "can be instantiated from a string", ->
8 assert.are.same (nt '', 'type/only'), Key 'type/only'
9 assert.are.same (nt '', 'type/only'), Key ':type/only'
10 assert.are.same (nt '', 'the/type'), Key ' the/type'
11 assert.are.same (nt '', 'the/type'), Key ': the/type'
12 assert.are.same (nt '', 'URL -> long/type -> some/type'), Key 'URL -> long/type -> some/type'
13
14 assert.are.same (nt 'facet_name', 'some/type'), Key 'facet_name: some/type'
15 assert.are.same (nt 'spacious_name', 'and/type'), Key 'spacious_name: and/type'
16 assert.are.same (nt 'name', 'URL -> long/type -> some/type'), Key 'name: URL -> long/type -> some/type'
17
18 it "can be instantiated from two strings", ->
19 assert.are.same (nt '', 'type/only'), Key '', 'type/only'
20 assert.are.same (nt '', 'type/only'), Key nil, 'type/only'
21 assert.are.same (nt 'facet_name', 'some/type'), Key 'facet_name', 'some/type'
22
23 assert.are.same (nt 'name', 'URL -> long/type -> some/type'), Key 'name', 'URL -> long/type -> some/type'
24 assert.are.same (nt '', 'URL -> long/type -> some/type'), Key '', 'URL -> long/type -> some/type'
25
26 assert.are.same (nt 'spacious_name', ' and/type'), Key 'spacious_name', ' and/type'
27 assert.are.same (nt '', ' the/type'), Key nil, ' the/type'
28
29 it "can be instantiated from a table or instance", ->
30 assert.are.same (nt '', 'type/only'), Key Key '', 'type/only'
31 assert.are.same (nt '', 'type/only'), Key nt '', 'type/only'
32 assert.are.same (nt '', 'type/only'), Key nt nil, 'type/only'
33
34 assert.are.same (nt 'facet', 'the/type+extra'), Key Key 'facet', 'the/type+extra'
35 assert.are.same (nt 'facet', 'the/type+extra'), Key nt 'facet', 'the/type+extra'
36
37 it "throws an error otherwise", ->
38 assert.has_error -> Key!
39 assert.has_error -> Key true
40 assert.has_error -> Key true, false
41 assert.has_error -> Key 4
42 assert.has_error -> Key 4, 5
43 assert.has_error -> Key {}
44 assert.has_error -> Key type: true
45
46 it "tostring formats the key", ->
47 assert.is.equal 'type/only', tostring Key 'type/only'
48 assert.is.equal 'type/only', tostring Key '', 'type/only'
49 assert.is.equal 'type/only', tostring Key ": type/only"
50
51 assert.is.equal 'facet: and/type+extra', tostring Key 'facet: and/type+extra'
52 assert.is.equal 'facet: and/type+extra', tostring Key 'facet', 'and/type+extra'
53 assert.is.equal 'facet: and/type+extra', tostring Key 'facet: and/type+extra'
54
55 assert.is.equal 'facet: and -> long -> type', tostring Key 'facet: and -> long -> type'
56 assert.is.equal 'facet: and -> long -> type', tostring Key 'facet', 'and -> long -> type'
57 assert.is.equal 'facet: and -> long -> type', tostring Key 'facet: and -> long -> type'
0 -- relative imports
1 _G.relative = do
2 _require = require
3
4 (base, sub) ->
5 sub = sub or 0
6
7 for i=1, sub
8 base = base\match '^(.*)%.%w+$'
9
10 (name, x) ->
11 if name == '.'
12 name = base
13 else if '.' == name\sub 1, 1
14 name = base .. name
15
16 _require name
17
18 sort2 = (a, b) ->
19 {ax, ay}, {bx, by} = a, b
20 "#{ax}//#{ay}" < "#{bx}//#{by}"
21
22 toseq = (iter) ->
23 with v = [x for x in iter]
24 table.sort v
25
26 toseq2 = (iter) ->
27 with v = [{x, y} for x, y in iter]
28 table.sort v, sort2
0 import toseq, toseq2 from require 'spec.test_util'
291
302 test_store = (ts) ->
313 randomize false
0 -- relative imports
1 _G.relative = do
2 _require = require
3
4 (base, sub) ->
5 sub = sub or 0
6
7 for i=1, sub
8 base = base\match '^(.*)%.%w+$'
9
10 (name, x) ->
11 if name == '.'
12 name = base
13 else if '.' == name\sub 1, 1
14 name = base .. name
15
16 _require name
17
18 sort2 = (a, b) ->
19 {ax, ay}, {bx, by} = a, b
20 "#{ax}//#{ay}" < "#{bx}//#{by}"
21
22 toseq = (iter) ->
23 with v = [x for x in iter]
24 table.sort v
25
26 toseq2 = (iter) ->
27 with v = [{x, y} for x, y in iter]
28 table.sort v, sort2
29
30 {
31 :toseq
32 :toseq2
33 }