diff options
| author | Alexander Pickering <alexandermpickering@gmail.com> | 2016-07-01 22:08:45 -0400 |
|---|---|---|
| committer | Alexander Pickering <alexandermpickering@gmail.com> | 2016-07-01 22:08:45 -0400 |
| commit | 774b296d3e49b8be3b0feaee8b5d3154fcec73b6 (patch) | |
| tree | e076254b6332c177dc34b4d87bc222f52ca49646 /test | |
| download | glum-774b296d3e49b8be3b0feaee8b5d3154fcec73b6.tar.gz glum-774b296d3e49b8be3b0feaee8b5d3154fcec73b6.tar.bz2 glum-774b296d3e49b8be3b0feaee8b5d3154fcec73b6.zip | |
Initial commit
Diffstat (limited to 'test')
| -rw-r--r-- | test/glum_spec.lua | 31 | ||||
| -rw-r--r-- | test/glum_test.lua | 212 | ||||
| -rw-r--r-- | test/tests/call.lua | 22 | ||||
| -rw-r--r-- | test/tests/forinloop.lua | 10 | ||||
| -rw-r--r-- | test/tests/fornumloop.lua | 10 | ||||
| -rw-r--r-- | test/tests/invoke.lua | 12 | ||||
| -rw-r--r-- | test/tests/string.lua | 8 |
7 files changed, 305 insertions, 0 deletions
diff --git a/test/glum_spec.lua b/test/glum_spec.lua new file mode 100644 index 0000000..e4c5749 --- /dev/null +++ b/test/glum_spec.lua @@ -0,0 +1,31 @@ +local tests = {} + +describe("Initializeing tests",function() + local testnames = { + {"forinloop.lua", "forin loops"}, + {"fornumloop.lua","fornum loops"}, + {"invoke.lua","invokes"}, + {"call.lua","calls"}, + {"string.lua","strings"}, + } + for k,v in pairs(testnames) do + local file = io.open("./tests/"..v[1],"r") + tests[v[2]] = file:read("*a") + end +end) + +describe("GLuM should", function() + local glum + it("initialize without error",function() + glum = dofile("../src/glum.lua") + end) + describe("output non-erroring minified code for",function() + for k,v in pairs(tests) do + it(k,function() + local miniftxt = glum.minify(v) + local minified = loadstring(miniftxt) + assert.has_no.errors(minified) + end) + end + end) +end) diff --git a/test/glum_test.lua b/test/glum_test.lua new file mode 100644 index 0000000..3b8e70f --- /dev/null +++ b/test/glum_test.lua @@ -0,0 +1,212 @@ +local glum = dofile("../src/glum.lua") +local str1 = [[ +--This is a test +print("hello") +local abracadabra = "b" +print(abracadabra) +]] +local str2 = [[ +--This is another test +local somtbl = {} +somtbl.test = function(self, what, who, where, when) + print("Thanks, I love " .. what .. "!") +end +somtbl:test(23) +]] +local str3 = [[ + +--Assign locals to these to the minifier can compress the file better +local strlen,chrat,min,asrt,prs,iprs,typ,upack,tblins,tblsrt,strsub,tru,fal = string.len,string.byte,math.min,assert,pairs,ipairs,type,unpack,table.insert,table.sort,string.sub,true,false + +local fuzzel = {} + +--A clever way to allow the minifier to minify function names, this basically just assigns variables with their string equivalent. +local da, le, di, ra, fu, fi, so, ex, ha, au = "Damerau", "Levenshtein", "Distance", "Ratio", "Fuzzy", "Find", "Sort", "_extended", "Hamming", "Autocomplete" +local LevenshteinDistance_extended,LevenshteinDistance,LevenshteinRatio,DamerauLevenshteinDistance_extended,DamerauLevenshteinDistance,DamerauLevenshteinRatio,FuzzyFindDistance,FuzzyFindRatio,FuzzySortDistance,FuzzySortRatio,HammingDistance,HammingRatio,FuzzyAutocompleteDistance,FuzzyAutocompleteRatio = le..di..ex,le..di,le..ra,da..le..di..ex,da..le..di,da..le..ra,fu..fi..di,fu..fi..ra,fu..so..di,fu..so..ra,ha..di,ha..ra,fu..au..di,fu..au..ra + +local function genericDistance( stringa, stringb, addcost, subcost, delcost, ...) + local arg = {...} + + --Length of each string + local salen, sblen = strlen(stringa), strlen(stringb) + + --Create a 0 matrix the size of len(a) x len(b) + local dyntbl = {} + for i = 0,salen do + dyntbl[i] = {} + for j = 0,sblen do + dyntbl[i][j] = 0 + end + end + + --Initalize the matrix + for i = 1,salen do + dyntbl[i][0] = i + end + for j = 1,sblen do + dyntbl[0][j] = j + end + + --And build up the matrix based on costs-so-far + for j = 1,sblen do + for i = 1,salen do + local ca,cb = chrat(stringa,i),chrat(stringb,j) + dyntbl[i][j] = min( + dyntbl[i-1][j] + delcost, --deletion + dyntbl[i][j-1] + addcost, --insertion + dyntbl[i-1][j-1] + (ca == cb and 0 or subcost) --substituion + ) + if arg[1] and i > 1 and j > 1 and ca == chrat(stringb,j-1) and chrat(stringa,i-1) == cb then + dyntbl[i][j] = min(dyntbl[i][j], + dyntbl[i-2][j-2] + (ca == cb and 0 or arg[2])) --transposition + end + end + end + + return dyntbl[salen][sblen] +end + +fuzzel[LevenshteinDistance_extended] = function(stringa, stringb, addcost, subcost, delcost) + return genericDistance(stringa, stringb, addcost, subcost, delcost) +end +fuzzel.ld_e = fuzzel[LevenshteinDistance_extended] + +fuzzel[LevenshteinDistance] = function(stringa,stringb) + return fuzzel.ld_e(stringa,stringb,1,1,1) +end +fuzzel.ld = fuzzel[LevenshteinDistance] + +fuzzel[LevenshteinRatio] = function(stringa,stringb) + return fuzzel.ld(stringa,stringb) / strlen(stringa) +end +fuzzel.lr = fuzzel[LevenshteinRatio] + +fuzzel[DamerauLevenshteinDistance_extended] = function(stringa, stringb, addcost, subcost, delcost, trncost) + return genericDistance(stringa,stringb,addcost,subcost,delcost,tru,trncost) +end +fuzzel.dld_e = fuzzel[DamerauLevenshteinDistance_extended] + +fuzzel[DamerauLevenshteinDistance] = function(stringa,stringb) + return fuzzel.dld_e(stringa,stringb,1,1,1,1) +end +fuzzel.dld = fuzzel[DamerauLevenshteinDistance] + +fuzzel[DamerauLevenshteinRatio] = function(stringa,stringb) + return fuzzel.dld(stringa,stringb) / strlen(stringa) +end +fuzzel.dlr = fuzzel[DamerauLevenshteinRatio] + +fuzzel[HammingDistance] = function(stringa,stringb) + local len,dist = strlen(stringa),0 + asrt(len == strlen(stringb), ha.." "..di.." cannot be calculated on two strings of different lengths:\"" .. stringa .. "\" \"" .. stringb .. "\"") + for i = 1,len do + dist = dist + ((chrat(stringa,i) ~= chrat(stringb,i)) and 1 or 0) + end + return dist +end +fuzzel.hd = fuzzel[HammingDistance] + +fuzzel[HammingRatio] = function(stringa,stringb) + return fuzzel.hd(stringa,stringb) / strlen(stringa) +end +fuzzel.hr = fuzzel[HammingRatio] + +local function FuzzySearch(str,func,...) + local arg = {...} + + --Allow varargs, or a table + local looparg = typ(arg[1]) == "table" and arg[1] or arg + + --Find the string with the shortest distance to the string we were supplied + local tmin,sout = func(looparg[1],str),looparg[1] + for k,v in prs(looparg) do + local t = func(v,str) + if t <= tmin then + tmin,sout = t,k + end + end + return looparg[sout], tmin +end + +fuzzel[FuzzyFindDistance] = function(str,...) + return upack{FuzzySearch(str,fuzzel.dld,...)} +end +fuzzel.ffd = fuzzel[FuzzyFindDistance] + +fuzzel[FuzzyFindRatio] = function(str,...) + return upack{FuzzySearch(str,fuzzel.dlr,...)} +end +fuzzel.ffr = fuzzel[FuzzyFindRatio] + +local function FuzzySort(str, func, short, ...) + local arg = {...} + + --allow varargs, or a table + local looparg = typ(arg[1]) == "table" and arg[1] or arg + + --Roughly sort everything by it's distance to the string + local usorted,sorted,otbl,slen = {},{},{},strlen(str) + for k,v in prs(looparg) do + local sstr = short and strsub(v,0,slen) or v + local dist = func(str,sstr) + if usorted[dist] == nil then + usorted[dist] = {} + tblins(sorted,dist) + end + tblins(usorted[dist],v) + end + + --Actually sort them into something can can be iterated with ipairs + tblsrt(sorted) + + --Then build our output table + for k,v in iprs(sorted) do + for i,j in prs(usorted[v]) do + tblins(otbl,j) + end + end + return otbl +end +fuzzel.ffr = fuzzel[FuzzyFindRatio] + +fuzzel[FuzzySortDistance] = function(str,...) + return FuzzySort(str,fuzzel.dld,fal,...) +end +fuzzel.fsd = fuzzel[FuzzySortDistance] + +fuzzel[FuzzySortRatio] = function(str,...) + return FuzzySort(str,fuzzel.dlr,fal,...) +end +fuzzel.fsr = fuzzel[FuzzySortRatio] + +fuzzel[FuzzyAutocompleteDistance] = function(str, ...) + return FuzzySort(str,fuzzel.dld,tru,...) +end +fuzzel.fad = fuzzel[FuzzyAutocompleteDistance] + +fuzzel[FuzzyAutocompleteRatio] = function(str,...) + return FuzzySort(str,fuzzel.dlr,tru,...) +end +fuzzel.far = fuzzel[FuzzyAutocompleteRatio] + +return fuzzel +]] + +local str4 = [[ +--Fixing that wierd problem with local functions +local blah = "something" +global[blah](global,1) +global:lol(1) +]] +local f = glum.minify(str4) +print(f) +--[[ +local fuzzel = loadstring(f)() +local options = { + "test", + "toste", + "testing", + "teste", +} +print(fuzzel.ffd("tennor",options)) +]] diff --git a/test/tests/call.lua b/test/tests/call.lua new file mode 100644 index 0000000..8549b7f --- /dev/null +++ b/test/tests/call.lua @@ -0,0 +1,22 @@ + +function afunction() + print("You called a function!") +end + +function anotherfunction(num) + print("Thanks, I love " .. num .. "!") + return num == 0 and 1 or anotherfunction(num-1) +end + +local function yetanotherfunction(num) + print("Ew, I don't like " .. num .. ".") + return num <= 0 and 1 or yetanotherfunction(num/2) +end + +afunction() + +for k = 100,1000,100 do + anotherfunction(k) +end + +yetanotherfunction(2000) diff --git a/test/tests/forinloop.lua b/test/tests/forinloop.lua new file mode 100644 index 0000000..b9ee57b --- /dev/null +++ b/test/tests/forinloop.lua @@ -0,0 +1,10 @@ + +local tbl = { + "Alpha", + "Beta", + "Charlie", +} + +for k,v in pairs(tbl) do + print(v) +end diff --git a/test/tests/fornumloop.lua b/test/tests/fornumloop.lua new file mode 100644 index 0000000..19ee201 --- /dev/null +++ b/test/tests/fornumloop.lua @@ -0,0 +1,10 @@ + +local tbl = { + "Alpha", + "Beta", + "Charlie", +} + +for k = 1,#tbl do + print(v) +end diff --git a/test/tests/invoke.lua b/test/tests/invoke.lua new file mode 100644 index 0000000..568bc2d --- /dev/null +++ b/test/tests/invoke.lua @@ -0,0 +1,12 @@ +local something = {} + +something.givenumber = function(self,num) + print("Thanks, I love " .. num .. "! It's much better than " .. self.lastnum) + self.lastnum = num +end + +something.lastnum = 0 + +something:givenumber(1) +something:givenumber(2) +something:givenumber(10) diff --git a/test/tests/string.lua b/test/tests/string.lua new file mode 100644 index 0000000..769d528 --- /dev/null +++ b/test/tests/string.lua @@ -0,0 +1,8 @@ +local string = "what" + +local tbl = {} +local tbl2 = {} + +tbl[string] = 10 +tbl2.what = 10 +assert(tbl.what == tbl2[string],"Not equal") |
