aboutsummaryrefslogtreecommitdiffstats
path: root/spec
diff options
context:
space:
mode:
authors-ol <s-ol@users.noreply.github.com>2020-01-25 12:57:36 +0000
committers-ol <s-ol@users.noreply.github.com>2020-01-25 12:57:36 +0000
commit2b91c3d0af33099f7b260a41efac252655104c50 (patch)
tree4636152e118d90381a584f2636da34efbe8dd568 /spec
parentcorrection in portfolio (diff)
parentupdate README.md (diff)
downloadmmm-2b91c3d0af33099f7b260a41efac252655104c50.tar.gz
mmm-2b91c3d0af33099f7b260a41efac252655104c50.zip
Merge branch 'ba'
Diffstat (limited to 'spec')
-rw-r--r--spec/key_spec.moon68
-rw-r--r--spec/queue_spec.moon88
-rw-r--r--spec/stores_spec.moon149
-rw-r--r--spec/test_util.moon34
4 files changed, 339 insertions, 0 deletions
diff --git a/spec/key_spec.moon b/spec/key_spec.moon
new file mode 100644
index 0000000..edd5ccf
--- /dev/null
+++ b/spec/key_spec.moon
@@ -0,0 +1,68 @@
+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'
+
+ it ":tostring formats the key", ->
+ assert.is.equal 'type/only', (Key 'type/only')\tostring!
+ assert.is.equal 'type/only', (Key '', 'type/only')\tostring!
+ assert.is.equal 'type/only', (Key ": type/only")\tostring!
+
+ it ":tostring supports strict mode", ->
+ assert.is.equal ': type/only', (Key 'type/only')\tostring true
+ assert.is.equal ': type/only', (Key '', 'type/only')\tostring true
+ assert.is.equal ': type/only', (Key ": type/only")\tostring true
diff --git a/spec/queue_spec.moon b/spec/queue_spec.moon
new file mode 100644
index 0000000..d095398
--- /dev/null
+++ b/spec/queue_spec.moon
@@ -0,0 +1,88 @@
+import Queue from require 'mmm.mmmfs.queue'
+
+describe "Queue", ->
+ it "stores things", ->
+ queue = Queue!
+ queue\add "test", 1
+ queue\add "toast", 2
+ queue\add "spice", 3
+
+ assert.is.equal "test", queue\pop!
+ assert.is.equal "toast", queue\pop!
+ assert.is.equal "spice", queue\pop!
+ assert.is.nil queue\pop!
+
+ it "doesnt care about the order", ->
+ queue = Queue!
+ queue\add "spice", 3
+ queue\add "test", 1
+ queue\add "toast", 2
+
+ assert.is.equal "test", queue\pop!
+ assert.is.equal "toast", queue\pop!
+
+ queue\add "pepper", 5
+ queue\add "salt", .5
+ assert.is.equal "salt", queue\pop!
+ assert.is.equal "spice", queue\pop!
+ assert.is.equal "pepper", queue\pop!
+
+ it "can be peeked", ->
+ queue = Queue!
+ queue\add "spice", 3
+ queue\add "test", 1
+ queue\add "toast", 2
+
+ assert.is.equal "test", queue\peek!
+ assert.is.equal "test", queue\pop!
+ queue\pop!
+
+ queue\add "pepper", 5
+ queue\add "salt", .5
+
+ assert.is.equal "salt", queue\peek!
+ queue\pop!
+ queue\pop!
+
+ assert.is.equal "pepper", queue\peek!
+ queue\pop!
+
+ assert.is.nil queue\peek!
+
+ it "keeps keys in an index", ->
+ queue = Queue!
+ queue\add "test", 1, 'test'
+ queue\add "toast", 2, 'toast'
+ queue\add "spice", 3, 'spice'
+
+ assert.is.equal "test", queue\peek!
+ queue\add "spice2", .5, 'spice'
+ assert.is.equal "spice2", queue\pop!
+ assert.is.equal "test", queue\pop!
+
+ queue\add "bad toast", 5, 'toast'
+ assert.is.equal "toast", queue\pop!
+ assert.is.nil queue\pop!
+
+ it "provides an iterator", ->
+ queue = Queue!
+ queue\add "test", 1
+ queue\add "spice", 3
+ queue\add "toast", 2
+
+ expect = {'test', 'toast', 'late', 'spice'}
+ expect_next = 1
+ report = spy.new (v, i) ->
+ assert.is.equal expect[expect_next], v
+ expect_next += 1
+
+ for value, prio in queue\poll!
+ report value, prio
+
+ if value == 'toast'
+ queue\add "late", 0.5
+
+ assert.stub(report).was.called_with('test', 1)
+ assert.stub(report).was.called_with('toast', 2)
+ assert.stub(report).was.called_with('spice', 3)
+ assert.stub(report).was.called_with('late', 0.5)
diff --git a/spec/stores_spec.moon b/spec/stores_spec.moon
new file mode 100644
index 0000000..7fa2793
--- /dev/null
+++ b/spec/stores_spec.moon
@@ -0,0 +1,149 @@
+import toseq, toseq2 from require 'spec.test_util'
+
+test_store = (ts) ->
+ randomize false
+
+ it "starts out empty", ->
+ assert.are.same {}, toseq ts\list_fileders_in!
+ assert.are.same {}, toseq ts\list_all_fileders!
+
+ it "can't create fileders without missing parents", ->
+ assert.has_error ->
+ ts\create_fileder '/hello', 'world'
+
+ it "can create root fileders", ->
+ assert.are.same '/hello', ts\create_fileder '', 'hello'
+ assert.are.same {'/hello'}, toseq ts\list_all_fileders!
+
+ it "can create and list child fileders recursively", ->
+ assert.are.same '/hello/world',
+ ts\create_fileder '/hello', 'world'
+ assert.are.same '/hello/world/again',
+ ts\create_fileder '/hello/world', 'again'
+
+ assert.are.same {'/hello', '/hello/world', '/hello/world/again'},
+ toseq ts\list_all_fileders!
+
+ it "can list immediate children", ->
+ assert.are.same {"/hello"}, toseq ts\list_fileders_in!
+ assert.are.same {"/hello/world"}, toseq ts\list_fileders_in "/hello"
+ assert.are.same {"/hello/world/again"}, toseq ts\list_fileders_in "/hello/world"
+
+ describe "can create and list facets", ->
+ ts\create_facet '/hello', 'name', 'alpha', 'hello'
+ ts\create_facet '/hello/world', 'name', 'alpha', 'world'
+ ts\create_facet '/hello/world', '', 'text/markdown', '# Helau World!'
+
+ it "but can't create facet for nonexistant fileders", ->
+ assert.has_error -> ts\create_facet '/hello/orldw', 'name', 'alpha', 'foo'
+
+ it "but can't create facet without value", ->
+ assert.has_error -> ts\create_facet '/hello/world', 'other', 'alpha', nil
+
+ it "but can't create facet for duplicate keys", ->
+ assert.has_error -> ts\create_facet '/hello/world', 'name', 'alpha', 'foo'
+
+ assert.are.same {{'name', 'alpha'}}, toseq2 ts\list_facets '/hello'
+ assert.are.same {{'', 'text/markdown'}, {'name', 'alpha'}},
+ toseq2 ts\list_facets '/hello/world'
+
+ describe "can get indexes", ->
+ hello_index = {
+ path: '/hello'
+ children: {
+ '/hello/world'
+ }
+ facets: {
+ { name: 'name', type: 'alpha' }
+ }
+ }
+
+ it "for a single level", ->
+ assert.are.same hello_index, ts\get_index '/hello'
+
+
+ root_index = {
+ path: ''
+ children: {
+ hello_index
+ }
+ facets: {}
+ }
+ it "for a limited number of levels", ->
+ assert.are.same root_index, ts\get_index '', 2
+
+ it "recursively", ->
+ hello_index.children[1] = {
+ path: '/hello/world'
+ children: {
+ {
+ path: '/hello/world/again'
+ children: {}
+ facets: {}
+ }
+ }
+ facets: {
+ { name: '', type: 'text/markdown' }
+ { name: 'name', type: 'alpha' }
+ }
+ }
+
+ assert.are.same root_index, ts\get_index '', -1
+
+ it "can get indexes recursively", ->
+
+ it "can load facets", ->
+ assert.are.equal 'hello', ts\load_facet '/hello', 'name', 'alpha'
+ assert.are.equal 'world', ts\load_facet '/hello/world', 'name', 'alpha'
+ assert.are.equal '# Helau World!', ts\load_facet '/hello/world', '', 'text/markdown'
+ assert.is_falsy ts\load_facet '/hello', 'nonexistant', 'facet'
+
+ it "can rename facets", ->
+ ts\rename_facet '/hello/world', 'name', 'alpha', 'gnome'
+ assert.are.same {{'', 'text/markdown'}, {'gnome', 'alpha'}},
+ toseq2 ts\list_facets '/hello/world'
+ assert.are.equal 'world', ts\load_facet '/hello/world', 'gnome', 'alpha'
+
+ it "can update facets", ->
+ ts\update_facet '/hello/world', '', 'text/markdown', '# Hello World!'
+ assert.are.same {{'', 'text/markdown'}, {'gnome', 'alpha'}},
+ toseq2 ts\list_facets '/hello/world'
+ assert.are.equal '# Hello World!', ts\load_facet '/hello/world', '', 'text/markdown'
+
+ it "can remove facets", ->
+ ts\remove_facet '/hello/world', 'gnome', 'alpha'
+ assert.are.same {{'', 'text/markdown'}}, toseq2 ts\list_facets '/hello/world'
+
+ assert.has_error -> ts\remove_facet '/hello/world', 'gnome', 'alpha'
+
+ it "can delete fileders", ->
+ ts\remove_fileder '/hello/world'
+ assert.is_falsy ts\load_facet '/hello/world', 'gnome', 'alpha'
+ assert.are.same {'/hello'}, toseq ts\list_all_fileders!
+
+ ts\remove_fileder '/hello'
+ assert.are.same {}, toseq ts\list_all_fileders!
+
+ it "can be closed", ->
+ ts\close!
+
+describe "SQL store", ->
+ import SQLStore from require 'mmm.mmmfs.stores.sql'
+
+ test_store SQLStore memory: true
+
+describe "FS store", ->
+ import FSStore from require 'mmm.mmmfs.stores.fs'
+
+ lfs = require 'lfs'
+
+ root = os.tmpname!
+
+ setup ->
+ assert os.remove root
+ assert lfs.mkdir root
+
+ test_store FSStore :root
+
+ teardown ->
+ assert lfs.rmdir root
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
+}