aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authors-ol <s-ol@users.noreply.github.com>2019-10-11 11:00:16 +0000
committers-ol <s-ol@users.noreply.github.com>2019-10-11 11:00:16 +0000
commit782d0725f3f29eaa7d4a12213fb00c6643795348 (patch)
tree78700d48ebb82fd92b24dbe2fa9e18188d4f0e52
parentfileders load via get_index rather than list_facets/list_fileders (diff)
downloadmmm-782d0725f3f29eaa7d4a12213fb00c6643795348.tar.gz
mmm-782d0725f3f29eaa7d4a12213fb00c6643795348.zip
add key_spec
-rw-r--r--mmm/mmmfs/fileder.moon11
-rw-r--r--spec/key_spec.moon58
-rw-r--r--spec/stores_spec.moon30
-rw-r--r--spec/test_util.moon34
4 files changed, 99 insertions, 34 deletions
diff --git a/mmm/mmmfs/fileder.moon b/mmm/mmmfs/fileder.moon
index 0d484b5..8c89083 100644
--- a/mmm/mmmfs/fileder.moon
+++ b/mmm/mmmfs/fileder.moon
@@ -20,18 +20,19 @@ class Key
if 'string' == type second
@name, @type = (opts or ''), second
elseif 'string' == type opts
- @name, @type = opts\match '^([%w-_]+): *(.+)$'
+ @name, @type = opts\match '^([%w-_]*): *(.+)$'
if not @name
@name = ''
- @type = opts
+ @type = opts\match '^ *(.+)$'
elseif 'table' == type opts
- @name = opts.name
+ @name = opts.name or ''
@type = opts.type
- @original = opts.original
- @filename = opts.filename
else
error "wrong argument type: #{type opts}, #{type second}"
+ assert ('string' == type @name), "name is not a string: '#{@name}'"
+ assert ('string' == type @type), "type is not a string: '#{@type}'"
+
-- format as a string (see constructor)
tostring: =>
if @name == ''
diff --git a/spec/key_spec.moon b/spec/key_spec.moon
new file mode 100644
index 0000000..9b7dde4
--- /dev/null
+++ b/spec/key_spec.moon
@@ -0,0 +1,58 @@
+require 'spec.test_util'
+package.loaded['mmm.mmmfs.conversion'] = {}
+import Key from require 'mmm.mmmfs.fileder'
+
+nt = (name, type) -> :name, :type
+
+describe "Key", ->
+ it "can be instantiated from a string", ->
+ assert.are.same (nt '', 'type/only'), Key 'type/only'
+ assert.are.same (nt '', 'type/only'), Key ':type/only'
+ assert.are.same (nt '', 'the/type'), Key ' the/type'
+ assert.are.same (nt '', 'the/type'), Key ': the/type'
+ assert.are.same (nt '', 'URL -> long/type -> some/type'), Key 'URL -> long/type -> some/type'
+
+ assert.are.same (nt 'facet_name', 'some/type'), Key 'facet_name: some/type'
+ assert.are.same (nt 'spacious_name', 'and/type'), Key 'spacious_name: and/type'
+ assert.are.same (nt 'name', 'URL -> long/type -> some/type'), Key 'name: URL -> long/type -> some/type'
+
+ it "can be instantiated from two strings", ->
+ assert.are.same (nt '', 'type/only'), Key '', 'type/only'
+ assert.are.same (nt '', 'type/only'), Key nil, 'type/only'
+ assert.are.same (nt 'facet_name', 'some/type'), Key 'facet_name', 'some/type'
+
+ assert.are.same (nt 'name', 'URL -> long/type -> some/type'), Key 'name', 'URL -> long/type -> some/type'
+ assert.are.same (nt '', 'URL -> long/type -> some/type'), Key '', 'URL -> long/type -> some/type'
+
+ assert.are.same (nt 'spacious_name', ' and/type'), Key 'spacious_name', ' and/type'
+ assert.are.same (nt '', ' the/type'), Key nil, ' the/type'
+
+ it "can be instantiated from a table or instance", ->
+ assert.are.same (nt '', 'type/only'), Key Key '', 'type/only'
+ assert.are.same (nt '', 'type/only'), Key nt '', 'type/only'
+ assert.are.same (nt '', 'type/only'), Key nt nil, 'type/only'
+
+ assert.are.same (nt 'facet', 'the/type+extra'), Key Key 'facet', 'the/type+extra'
+ assert.are.same (nt 'facet', 'the/type+extra'), Key nt 'facet', 'the/type+extra'
+
+ it "throws an error otherwise", ->
+ assert.has_error -> Key!
+ assert.has_error -> Key true
+ assert.has_error -> Key true, false
+ assert.has_error -> Key 4
+ assert.has_error -> Key 4, 5
+ assert.has_error -> Key {}
+ assert.has_error -> Key type: true
+
+ it "tostring formats the key", ->
+ assert.is.equal 'type/only', tostring Key 'type/only'
+ assert.is.equal 'type/only', tostring Key '', 'type/only'
+ assert.is.equal 'type/only', tostring Key ": type/only"
+
+ assert.is.equal 'facet: and/type+extra', tostring Key 'facet: and/type+extra'
+ assert.is.equal 'facet: and/type+extra', tostring Key 'facet', 'and/type+extra'
+ assert.is.equal 'facet: and/type+extra', tostring Key 'facet: and/type+extra'
+
+ assert.is.equal 'facet: and -> long -> type', tostring Key 'facet: and -> long -> type'
+ assert.is.equal 'facet: and -> long -> type', tostring Key 'facet', 'and -> long -> type'
+ assert.is.equal 'facet: and -> long -> type', tostring Key 'facet: and -> long -> type'
diff --git a/spec/stores_spec.moon b/spec/stores_spec.moon
index 742d8c8..7fa2793 100644
--- a/spec/stores_spec.moon
+++ b/spec/stores_spec.moon
@@ -1,32 +1,4 @@
--- relative imports
-_G.relative = do
- _require = require
-
- (base, sub) ->
- sub = sub or 0
-
- for i=1, sub
- base = base\match '^(.*)%.%w+$'
-
- (name, x) ->
- if name == '.'
- name = base
- else if '.' == name\sub 1, 1
- name = base .. name
-
- _require name
-
-sort2 = (a, b) ->
- {ax, ay}, {bx, by} = a, b
- "#{ax}//#{ay}" < "#{bx}//#{by}"
-
-toseq = (iter) ->
- with v = [x for x in iter]
- table.sort v
-
-toseq2 = (iter) ->
- with v = [{x, y} for x, y in iter]
- table.sort v, sort2
+import toseq, toseq2 from require 'spec.test_util'
test_store = (ts) ->
randomize false
diff --git a/spec/test_util.moon b/spec/test_util.moon
new file mode 100644
index 0000000..b2ac026
--- /dev/null
+++ b/spec/test_util.moon
@@ -0,0 +1,34 @@
+-- relative imports
+_G.relative = do
+ _require = require
+
+ (base, sub) ->
+ sub = sub or 0
+
+ for i=1, sub
+ base = base\match '^(.*)%.%w+$'
+
+ (name, x) ->
+ if name == '.'
+ name = base
+ else if '.' == name\sub 1, 1
+ name = base .. name
+
+ _require name
+
+sort2 = (a, b) ->
+ {ax, ay}, {bx, by} = a, b
+ "#{ax}//#{ay}" < "#{bx}//#{by}"
+
+toseq = (iter) ->
+ with v = [x for x in iter]
+ table.sort v
+
+toseq2 = (iter) ->
+ with v = [{x, y} for x, y in iter]
+ table.sort v, sort2
+
+{
+ :toseq
+ :toseq2
+}