1
0
mirror of https://github.com/natnat-mc/moonbuild synced 2026-05-21 17:41:14 +02:00

added spec for stringutil, tableutil and the pure part of fsutil

This commit is contained in:
Nathan DECHER
2020-09-25 19:49:25 +02:00
parent 0779ea3ad4
commit 80a5c54032
9 changed files with 392 additions and 41 deletions
+111
View File
@@ -0,0 +1,111 @@
describe 'fsutil', ->
describe 'normalizepath', ->
import normalizepath from require 'moonbuild.fsutil'
test = (expected, source) ->
it "normalizes #{source} correctly", ->
assert.equal expected, normalizepath source
testall = (tab) ->
for a, b in pairs tab
test b, a
describe 'handles already normalized paths', ->
testall {
'.': '.'
'..': '..'
'../..': '../..'
'/': '/'
'/a': '/a'
'/a/b': '/a/b'
'a': 'a'
'a/b': 'a/b'
}
describe 'trims leading slashes', ->
testall {
'a/': 'a'
'a/b/': 'a/b'
'/a/': '/a'
'/a/b/': '/a/b'
}
describe 'normalizes absolute paths', ->
testall {
'/a/a/../b': '/a/b'
'/a/./b': '/a/b'
'/a/b/c/..': '/a/b'
'/./a/./b/././.': '/a/b'
}
describe 'normalizes relative paths', ->
testall {
'../x/../../a': '../../a'
'../x/../a': '../a'
'x/..': '.'
'../.': '..'
'./a': 'a'
}
describe 'matchglob', ->
import matchglob from require 'moonbuild.fsutil'
test = (expected, source, glob) ->
if expected
it "matches #{glob} on #{source}", ->
assert.equal source, matchglob source, glob
else
it "doesn't match #{glob} on #{source}", ->
assert.equal nil, matchglob source, glob
testall = (tab) ->
for a, b in pairs tab
test b, a[1], a[2]
describe 'handles literal names', ->
testall {
[{'a', 'a'}]: true
[{'a.b', 'a.b'}]: true
[{'a/b', 'a/b'}]: true
[{'..', '..'}]: true
}
describe 'doesn\'t treat things as special chars', ->
testall {
[{'a', '.'}]: false
[{'a.b.c', '%S+'}]: false
[{'%S+', '%S+'}]: true
[{'%d', '%d'}]: true
[{'a', '%S'}]: false
[{'aaa', 'a+'}]: false
}
describe 'only matches fully', ->
testall {
[{'abcdef', 'bcde'}]: false
[{'a/b/c', 'b/c'}]: false
[{'a/b/c', 'a/b'}]: false
}
describe 'handles *', ->
testall {
[{'abcde', '*'}]: true
[{'a/b/c/d', 'a/*/c/d'}]: true
[{'a/b/c/d', 'a/*/d'}]: false
[{'abcde', 'a*e'}]: true
[{'abcde', 'a*f'}]: false
[{'a/b/c/d/e', 'a/*/*/*/e'}]: true
[{'a/b/c/d/e', 'a*/*/*e'}]: false
}
describe 'handles **', ->
testall {
[{'abcde', '**'}]: true
[{'a/b/c/d', 'a/**/c/d'}]: true
[{'abcde', 'a**e'}]: true
[{'a/b/c/d/e', 'a/**/**/**/e'}]: true
[{'a/b/c/d/e', 'a**e'}]: true
[{'a/b/c/d/e', 'a/**/e'}]: true
[{'a/b/c/d/e', 'a**f'}]: false
[{'abcde', 'a**f'}]: false
}
+78
View File
@@ -0,0 +1,78 @@
describe 'stringutil', ->
describe 'patsubst', ->
import patsubst from require 'moonbuild.stringutil'
test = (expected, source, patt, subst) ->
it "substitutes #{source} into #{expected} with #{patt} and #{subst}", ->
assert.equal expected, patsubst source, patt, subst
testall = (tab) ->
for a, b in pairs tab
test b, a[1], a[2], a[3]
describe 'handles just adding pre/suffix', ->
testall {
[{'a', '%', '_%'}]: '_a'
[{'tx', '%', 'a_%'}]: 'a_tx'
[{'a', '%', '%_'}]: 'a_'
[{'tx', '%', '%_a'}]: 'tx_a'
[{'a', '%', '_%_'}]: '_a_'
}
describe 'handles doing nothing', ->
for str in *({'a', 'aa', 'tx'})
test str, str, '%', '%'
describe 'handles literal change', ->
testall {
[{'a', 'a', 'b'}]: 'b'
[{'a', 'b', 'c'}]: 'a'
[{'aa', 'a', 'b'}]: 'aa'
}
describe 'handles match change', ->
testall {
[{'-a_', '-%_', 'b'}]: 'b'
[{'-a_', '-%', 'b'}]: 'b'
[{'-a_', '%_', 'b'}]: 'b'
[{'-a_', '_%-', 'b'}]: '-a_'
}
describe 'handles just removing pre/suffix', ->
testall {
[{'_a', '_%', '%'}]: 'a'
[{'a_', '%_', '%'}]: 'a'
[{'_a_', '_%_', '%'}]: 'a'
}
describe 'handles not matching', ->
testall {
[{'a-', '%_', '%'}]: 'a-'
[{'-a', '_%', '%'}]: '-a'
[{'-a-', '_%_', '%'}]: '-a-'
}
describe 'handles changing pre/suffix', ->
testall {
[{'a-', '%-', '%_'}]: 'a_'
[{'-a', '-%', '_%'}]: '_a'
[{'-a', '-%', '%_'}]: 'a_'
[{'_a-', '_%-', '-%_'}]: '-a_'
}
describe 'splitsp', ->
import splitsp from require 'moonbuild.stringutil'
test = (expected, source) ->
it "splits '#{source}' correctly", ->
assert.same expected, splitsp source
for source, expected in pairs {
'a b c': {'a', 'b', 'c'}
'abc': {'abc'}
'': {}
' a b c': {'a', 'b', 'c'}
' ': {}
' ab c': {'ab', 'c'}
}
test expected, source
+82
View File
@@ -0,0 +1,82 @@
describe 'tableutil', ->
describe 'sortedpairs', ->
import sortedpairs from require 'moonbuild.tableutil'
for src, dst in pairs {
[{a: '1', c: 2, b: 3}]: {'a', '1', 'b', 3, 'c', 2}
[{5, 4, 3}]: {1, 5, 2, 4, 3, 3}
}
it "works for #{src}", ->
i = 1
for k, v in sortedpairs src
assert.equal k, dst[i]
assert.equal v, dst[i+1]
i += 2
describe 'min and max', ->
import min, max from require 'moonbuild.tableutil'
for src, dst in pairs {
[{1, 2, 3, 4, 5}]: {1, 5}
[{5, 4, 3, 2, 1}]: {1, 5}
[{2, 4, 5, 1, 3}]: {1, 5}
[{1, 1, 1, 1, 1}]: {1, 1}
[{1}]: {1, 1}
}
it "min of #{table.concat src, ','} is #{dst[1]}", ->
assert.equal dst[1], min src
it "max of #{table.concat src, ','} is #{dst[2]}", ->
assert.equal dst[2], max src
describe 'foreach', ->
import foreach from require 'moonbuild.tableutil'
src = {1, 2, 5, '79'}
testall = (name, rst, fn) ->
it name, ->
assert.same rst, foreach src, fn
testall 'works with tostring', {'1', '2', '5', '79'}, tostring
testall 'works with tonumber', {1, 2, 5, 79}, tonumber
testall 'works with some mix of tonumber and comparison', {false, false, true, true}, => 3<tonumber @
describe 'first', ->
import first from require 'moonbuild.tableutil'
test = (name, src, rst, fn) ->
it name, ->
assert.equal rst, (first src, fn)
test 'works with == for first of list', {1, 3, 5}, 1, => @==1
test 'works with == for something else', {1, 3, 5}, 3, => @==3
test 'works with == for absent element', {1, 3, 5}, nil, => @==2
describe 'exclude', ->
import exclude from require 'moonbuild.tableutil'
unpack or= table.unpack
test = (name, src, rst, ...) ->
rest = {...}
it name, ->
assert.equal src, exclude src, unpack rest
assert.same rst, src
test 'works with nothing', {1, 2, 3}, {1, 2, 3}
test 'works with absent elements', {1, 2, 3}, {1, 2, 3}, 4, 5, 6
test 'works with some elements', {1, 2, 3}, {1}, 2, 3
test 'works with all elements', {1, 2, 3}, {}, 1, 2, 3
test 'works with a mix', {1, 2, 3}, {1, 3}, 2, 4, 5
describe 'flatten', ->
import flatten from require 'moonbuild.tableutil'
test = (name, src, rst) ->
it name, ->
assert.same rst, flatten src
test 'works with empty table', {}, {}
test 'works with flat table', {1, 2, 3}, {1, 2, 3}
test 'works with one level', {1, {2}, {3}}, {1, 2, 3}
test 'works with multiple levels', {{{1, {{2}}}, 3}}, {1, 2, 3}
test 'skips maps', {1, {a: 2}, 3}, {1, {a: 2}, 3}