', '{div:{=attribute=:x y z}' )
18 | t( rawhtml'
', '{div:{=attribute=:x y z}' )
19 | t( rawhtml'
', '{div:{=attribute=:x y z}}' )
20 | t( rawhtml'
', '}' )
21 |
22 | t( rawhtml'', 'bla bla}' )
24 | t( rawhtml'', '{=comment=:bla bla}' )
25 |
26 | local r = require 'rawmark'
27 |
28 | t( r( rawhtml'
x< b />y
' ), {type='',{type='=comment=','{',':','}'},{type='div',{type='=attribute=','my-attr="hi"'},'x',{type='b',''},'y'}}, t.deepsame )
29 |
30 | t.test_embedded_example()
31 |
32 | t()
33 |
34 |
--------------------------------------------------------------------------------
/test/rawmark.ex1.lua:
--------------------------------------------------------------------------------
1 | local rawmark = require 'rawmark'
2 | local t = require 'testhelper'
3 |
4 | local v = function(...) print('# '..((require'valueprint'(...)):gsub('\r?\n','\n# '))) end
5 |
6 | t( rawmark( '' ), { '', type='' }, t.deepsame )
7 | t( rawmark( 'a' ), { 'a', type='' }, t.deepsame )
8 |
9 | t( rawmark( '{}' ), {{ '', type='' }, type='' }, t.deepsame )
10 | t( rawmark( '{a}' ), {{ 'a', type='' }, type='' }, t.deepsame )
11 | t( rawmark( '{:a}' ), {{ 'a', type='' }, type='' }, t.deepsame )
12 |
13 | t( rawmark( 'a{}' ), { 'a', { "", type='' }, type='' }, t.deepsame )
14 |
15 | t( rawmark( 'a{b}' ), { 'a', { "b", type='' }, type='' }, t.deepsame )
16 | t( rawmark( '{b}a' ), {{ "b", type='' }, 'a', type='' }, t.deepsame )
17 | t( rawmark( 'a{b}c' ), { 'a', { "b", type='' }, 'c', type='' }, t.deepsame )
18 |
19 | t( rawmark( '{a}b{c}' ), {{ "a", type='' }, 'b', { "c", type='' }, type='' }, t.deepsame )
20 | t( rawmark( 'a{b}{c}' ), { "a", { "b", type='' }, { "c", type='' }, type='' }, t.deepsame )
21 | t( rawmark( '{a}{b}c' ), {{ "a", type='' }, { "b", type='' }, "c", type='' }, t.deepsame )
22 |
23 | t( rawmark( '{{a}}' ), {{{ 'a', type='' }, type='' }, type='' }, t.deepsame )
24 | t( rawmark( '{{{a}}}' ), {{{{ 'a', type='' }, type='' }, type='' }, type='' }, t.deepsame )
25 | t( rawmark( '{{a}}b{{c}}' ), {{{ 'a', type='' }, type='' }, 'b', {{ 'c', type='' }, type='' }, type='' }, t.deepsame )
26 |
27 | t( rawmark( '', 't' ), { '', type='t' }, t.deepsame )
28 | t( rawmark( '{t:}' ), {{ '', type='t' }, type='' }, t.deepsame )
29 | t( rawmark( '{t:a}' ), {{ 'a', type='t' }, type='' }, t.deepsame )
30 | t( rawmark( '{t::}' ), {{ ':', type='t' }, type='' }, t.deepsame )
31 | t( rawmark( '{t:a:c}' ), {{ 'a:c', type='t' }, type='' }, t.deepsame )
32 | t( rawmark( '{tt:a}b{TT2:c}', 't3' ), {{ "a", type='tt' }, 'b', { "c", type='TT2' }, type='t3' }, t.deepsame )
33 |
34 | t( rawmark( '{a-+_b:}' ), {{ '', type='a-+_b' }, type='' }, t.deepsame )
35 |
36 | t( rawmark( '{+}' ), {'{', type='' }, t.deepsame )
37 | t( rawmark( '{-}' ), {'}', type='' }, t.deepsame )
38 | t( rawmark( '{=}' ), {':', type='' }, t.deepsame )
39 | t( rawmark( 'a{+}b{=}c{:d}e{-}' ), { 'a','{','b',':','c', {'d', type=''} ,'e','}', type='' }, t.deepsame )
40 |
41 | t( rawmark( 'a{b' ), {'a{b', type='' }, t.deepsame )
42 | t( rawmark( 'a}b' ), {'a}b', type='' }, t.deepsame )
43 | t( rawmark( 'a{b{c}d' ), {type='', 'a{b', { type='', 'c'}, 'd' }, t.deepsame ) -- TODO : change this ?
44 | t( rawmark( 'a{b}c}d' ), {type='', 'a', { type='', 'b'}, 'c}d' }, t.deepsame ) -- TODO : change this ?
45 |
46 | t.test_embedded_example()
47 |
48 | t()
49 |
50 |
--------------------------------------------------------------------------------
/test/readfile.ex1.lua:
--------------------------------------------------------------------------------
1 | local readfile = require 'readfile'
2 | local t = require 'testhelper'
3 |
4 | t.writefile('tmp_1.txt', '')
5 | t( readfile('tmp_1.txt'), '' )
6 |
7 | t.writefile('tmp_1.txt', 'aaa\naaa')
8 | t( readfile('tmp_1.txt'), 'aaa\naaa' )
9 |
10 | t.writefile('tmp_1.txt', 'aaa\naaa')
11 | t( readfile('tmp_1.txt', 'l'), {'aaa','aaa'}, t.deepsame )
12 |
13 | t.writefile('tmp_1.txt', 'aaa\n')
14 | t( readfile('tmp_1.txt', 'l'), 'aaa' )
15 |
16 | t.writefile('tmp_1.txt', 'aaa\n\raaa')
17 | t( readfile('tmp_1.txt', 'L'), {'aaa\n','\raaa'}, t.deepsame )
18 |
19 | t.writefile('tmp_1.txt', 'aaaa')
20 | t( readfile('tmp_1.txt', 2), {'aa','aa'}, t.deepsame )
21 |
22 | t.writefile('tmp_1.txt', '1 1.2 -1e3')
23 | t( readfile('tmp_1.txt', 'n'), {1,1.2,-1e3}, t.deepsame )
24 |
25 | t.test_embedded_example()
26 | os.remove('tmp.txt')
27 |
28 | t()
29 |
--------------------------------------------------------------------------------
/test/searchluakeyword.ex1.lua:
--------------------------------------------------------------------------------
1 | local searchluakeyword = require 'searchluakeyword'
2 | local t = require 'testhelper'
3 |
4 | t( searchluakeyword'', {}, t.deepsame )
5 |
6 | t( searchluakeyword"do", {['do']={1}}, t.deepsame )
7 | t( searchluakeyword("<<",'s'), {['<<']={1}}, t.deepsame )
8 | t( searchluakeyword"::xxx::", {['::label::']={1}}, t.deepsame )
9 | t( searchluakeyword"::yy::", {['::label::']={1}}, t.deepsame )
10 |
11 | t( searchluakeyword"do do", {['do']={1,4}}, t.deepsame )
12 | t( searchluakeyword"dodo", {}, t.deepsame )
13 | t( searchluakeyword"ado", {}, t.deepsame )
14 | t( searchluakeyword"doa", {}, t.deepsame )
15 | t( searchluakeyword"do1", {}, t.deepsame )
16 | t( searchluakeyword"do_", {}, t.deepsame )
17 |
18 | t( searchluakeyword("for goto function nil false do and",'i'), {['goto']={5},['function']={10}}, t.deepsame )
19 |
20 | t( searchluakeyword"do -- do \n", {['do']={1}}, t.deepsame )
21 | t( searchluakeyword"do -- do \ndo", {['do']={1,11}}, t.deepsame )
22 | t( searchluakeyword"do -- do", {['do']={1}}, t.deepsame )
23 |
24 | t( searchluakeyword"do do end end", {['do']={1,5},['end']={9,13}}, t.deepsame )
25 | t( searchluakeyword"do 'do' end end", {['do']={1},['end']={9,13}}, t.deepsame )
26 | t( searchluakeyword'do "do" end end', {['do']={1},['end']={9,13}}, t.deepsame )
27 | t( searchluakeyword"do '\"do\"' end end", {['do']={1},['end']={11,15}}, t.deepsame )
28 | t( searchluakeyword'do "\'do\'" end end', {['do']={1},['end']={11,15}}, t.deepsame )
29 | t( searchluakeyword"do [[do]] end end", {['do']={1},['end']={11,15}}, t.deepsame )
30 | t( searchluakeyword"do [=[do]=] end end", {['do']={1},['end']={13,17}}, t.deepsame )
31 | t( searchluakeyword"do [==[do]=] end]==] end", {['do']={1},['end']={22}}, t.deepsame )
32 | t( searchluakeyword"do [[do end end", {['do']={1}}, t.deepsame )
33 |
34 | t( searchluakeyword"for", {['for']={1}}, t.deepsame )
35 | t( searchluakeyword"break", {['break']={1}}, t.deepsame )
36 | t( searchluakeyword"goto", {['goto']={1}}, t.deepsame )
37 | t( searchluakeyword"end", {['end']={1}}, t.deepsame )
38 | t( searchluakeyword"while", {['while']={1}}, t.deepsame )
39 | t( searchluakeyword"repeat", {['repeat']={1}}, t.deepsame )
40 | t( searchluakeyword"until", {['until']={1}}, t.deepsame )
41 | t( searchluakeyword"if", {['if']={1}}, t.deepsame )
42 | t( searchluakeyword"then", {['then']={1}}, t.deepsame )
43 | t( searchluakeyword"elseif", {['elseif']={1}}, t.deepsame )
44 | t( searchluakeyword"else", {['else']={1}}, t.deepsame )
45 | t( searchluakeyword"in", {['in']={1}}, t.deepsame )
46 | t( searchluakeyword"function", {['function']={1}}, t.deepsame )
47 | t( searchluakeyword"nil", {['nil']={1}}, t.deepsame )
48 | t( searchluakeyword"false", {['false']={1}}, t.deepsame )
49 | t( searchluakeyword"true", {['true']={1}}, t.deepsame )
50 | t( searchluakeyword"and", {['and']={1}}, t.deepsame )
51 | t( searchluakeyword"or", {['or']={1}}, t.deepsame )
52 | t( searchluakeyword"not", {['not']={1}}, t.deepsame )
53 | t( searchluakeyword(';' ,'s'), {[';' ]={1}}, t.deepsame )
54 | t( searchluakeyword('{' ,'s'), {['{' ]={1}}, t.deepsame )
55 | t( searchluakeyword('{' ,'s'), {['{' ]={1}}, t.deepsame )
56 | t( searchluakeyword('}' ,'s'), {['}' ]={1}}, t.deepsame )
57 | t( searchluakeyword('[' ,'s'), {['[' ]={1}}, t.deepsame )
58 | t( searchluakeyword(']' ,'s'), {[']' ]={1}}, t.deepsame )
59 | t( searchluakeyword('...','s'), {['...']={1}}, t.deepsame )
60 | t( searchluakeyword('(' ,'s'), {['(' ]={1}}, t.deepsame )
61 | t( searchluakeyword(')' ,'s'), {[')' ]={1}}, t.deepsame )
62 | t( searchluakeyword(':' ,'s'), {[':' ]={1}}, t.deepsame )
63 | t( searchluakeyword('.' ,'s'), {['.' ]={1}}, t.deepsame )
64 | t( searchluakeyword('=' ,'s'), {['=' ]={1}}, t.deepsame )
65 | t( searchluakeyword('+' ,'s'), {['+' ]={1}}, t.deepsame )
66 | t( searchluakeyword('-' ,'s'), {['-' ]={1}}, t.deepsame )
67 | t( searchluakeyword('*' ,'s'), {['*' ]={1}}, t.deepsame )
68 | t( searchluakeyword('/' ,'s'), {['/' ]={1}}, t.deepsame )
69 | t( searchluakeyword('//' ,'s'), {['//' ]={1}}, t.deepsame )
70 | t( searchluakeyword('^' ,'s'), {['^' ]={1}}, t.deepsame )
71 | t( searchluakeyword('%' ,'s'), {['%' ]={1}}, t.deepsame )
72 | t( searchluakeyword('&' ,'s'), {['&' ]={1}}, t.deepsame )
73 | t( searchluakeyword('~' ,'s'), {['~' ]={1}}, t.deepsame )
74 | t( searchluakeyword('|' ,'s'), {['|' ]={1}}, t.deepsame )
75 | t( searchluakeyword('>>' ,'s'), {['>>' ]={1}}, t.deepsame )
76 | t( searchluakeyword('<<' ,'s'), {['<<' ]={1}}, t.deepsame )
77 | t( searchluakeyword('..' ,'s'), {['..' ]={1}}, t.deepsame )
78 | t( searchluakeyword('<' ,'s'), {['<' ]={1}}, t.deepsame )
79 | t( searchluakeyword('<=' ,'s'), {['<=' ]={1}}, t.deepsame )
80 | t( searchluakeyword('>' ,'s'), {['>' ]={1}}, t.deepsame )
81 | t( searchluakeyword('>=' ,'s'), {['>=' ]={1}}, t.deepsame )
82 | t( searchluakeyword('==' ,'s'), {['==' ]={1}}, t.deepsame )
83 | t( searchluakeyword('~=' ,'s'), {['~=' ]={1}}, t.deepsame )
84 | t( searchluakeyword('-' ,'s'), {['-' ]={1}}, t.deepsame )
85 | t( searchluakeyword('#' ,'s'), {['#' ]={1}}, t.deepsame )
86 |
87 | t()
88 |
89 |
--------------------------------------------------------------------------------
/test/serialize.ex1.lua:
--------------------------------------------------------------------------------
1 | local serialize = require "serialize"
2 | local t = require "testhelper"
3 |
4 | local function reco( v )
5 | return load( 'return ' .. serialize( v ) )()
6 | end
7 |
8 | -- Simple values
9 | t( reco( nil ), nil, t.deepsame )
10 | t( reco( true ), true, t.deepsame )
11 | t( reco( 1 ), 1, t.deepsame )
12 | t( reco( "hi" ), "hi", t.deepsame )
13 | t( reco( {} ), {}, t.deepsame )
14 |
15 | t( serialize( "\n" ), '"\\n"' )
16 | t( serialize( "\r" ), '"\\13"' )
17 |
18 | -- Table with values
19 | t( reco( { a = true, b = { "c", 1, { d = "e" } }, } ),
20 | { a = true, b = { "c", 1, { d = "e" } }, },
21 | t.deepsame )
22 |
23 | -- Table table key
24 | t( reco( { [ { ok = "ok" } ] = "ok", } ),
25 | { [ { ok = "ok" } ] = "ok", },
26 | t.deepsame )
27 |
28 | -- Multiple table values
29 | t( reco( { ["a"] = { [ "a" ] = "a", }, ["b"] = { [ "b" ] = "b", }, } ),
30 | { ["a"] = { [ "a" ] = "a", }, ["b"] = { [ "b" ] = "b", }, },
31 | t.deepsame )
32 |
33 | -- Mixed key/value Table
34 | t( reco( { ["ok"] = { [ { ok = "ok" } ] = "ok", }, } ),
35 | { ["ok"] = { [ { ok = "ok" } ] = "ok", }, },
36 | t.deepsame )
37 |
38 | -- Sequence
39 | t( reco( { 'a','b',{'c','d'},'e'} ),
40 | { 'a','b',{'c','d'},'e'},
41 | t.deepsame )
42 |
43 | -- Sequence with holes
44 | t( reco( { nil,nil,nil,'a',{'c','d'},} ),
45 | { nil,nil,nil,'a',{'c','d'},},
46 | t.deepsame )
47 |
48 | -- Table with reference
49 | local atab = { a = "a" }
50 | t( reco( { atab, a = atab, } ),
51 | { atab, a = atab, },
52 | t.deepsame )
53 |
54 | -- Sequence with tables
55 | local atab = {}
56 | t( reco( { 1, atab, 2, atab, 3,} ),
57 | { 1, atab, 2, atab, 3,},
58 | t.deepsame )
59 |
60 | -- Table with cycle
61 | atab = {}
62 | atab.a = {}
63 | atab.a.a = atab
64 | t( reco( atab ), atab, t.deepsame )
65 |
66 | -- Table with multiple cycle
67 | atab = {}
68 | atab.kv = {}
69 | atab[atab.kv] = atab.kv
70 | atab[atab.kv][atab.kv] = atab[atab.kv]
71 | t( reco( atab ), atab, t.deepsame )
72 |
73 | -- Too deep table
74 | local cur = atab
75 | for n = 1, 200 do
76 | cur.q = {}
77 | cur = cur.q
78 | end
79 | t( reco( atab ), atab, t.deepsame )
80 |
81 | -- Output function
82 | local atab = {'a',2,[{}]={},}
83 | local exp = serialize(atab)
84 | local got = ''
85 | t( serialize(atab, function(d) got = got .. d end), nil )
86 | t( got, exp )
87 |
88 | t.test_embedded_example()
89 |
90 | t()
91 |
92 |
--------------------------------------------------------------------------------
/test/sha2.ex1.lua:
--------------------------------------------------------------------------------
1 | local sha2 = require 'sha2'
2 | local t = require 'testhelper'
3 |
4 | t( sha2( "Hello world!" ), "C0535E4BE2B79FFD93291305436BF889314E4A3FAEC05ECFFCBB7DF31AD9E51A", t.hexsame )
5 |
6 | t( sha2( '' ), "E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B855", t.hexsame )
7 | t( sha2(( "a" ):rep( 1 )), "CA978112CA1BBDCAFAC231B39A23DC4DA786EFF8147C4E72B9807785AFEE48BB", t.hexsame )
8 | t( sha2(( "b" ):rep( 63 )), "94E419FABAC7F930810F9636354042F8C1426D2F834D4AB65C93DC1E69326B13", t.hexsame )
9 | t( sha2(( "c" ):rep( 64 )), "52B6419D27BD7F547CEE3B92F8C17A908B8A49601ECBEC161E5030DE1DFE9E0A", t.hexsame )
10 | t( sha2(( "d" ):rep( 65 )), "899987F295364060C6ABD752A7E895124B467FD7CF56B52CE22F4A684A5723F4", t.hexsame )
11 | t( sha2(( "e" ):rep( 130 )), "C78A24F98CC9596CAFD6FC954A0664CA5CAD156AD406A8CC246B5E1F56864DB7", t.hexsame )
12 |
13 | t( sha2(( "\xFF" ), 1), "B9DEBF7D52F36E6468A54817C1FA071166C3A63D384850E1575B42F702DC5AA1", t.hexsame )
14 | t( sha2(( "\x00" ), 1), "BD4F9E98BEB68C6EAD3243B1B4C7FED75FA4FEAAB1F84795CBD8A98676A2A375", t.hexsame )
15 | t( sha2(( "a" ):rep( 70 ), 69*8+1), "056ECB5B2DB796F0E49B5A7F3010C5DB3ECA6E87E03EB45F4E618F4867D002A9", t.hexsame )
16 | t( sha2(( "a" ):rep( 70 ), 69*8+2), "0926C28F521555DB93892916F22414353234FCAB237AC5DC3AE6FA41A51BE15B", t.hexsame )
17 | t( sha2(( "a" ):rep( 70 ), 69*8+7), "6759507E5A185A774D2C980067B4451671AA70705A35080779AAA6D3CEAA00FC", t.hexsame )
18 |
19 | local function sha256(x,y) return sha2( x, y, 256 ) end
20 | local function sha224(x,y) return sha2( x, y, 224 ) end
21 | local function sha512(x,y) return sha2( x, y, 512 ) end
22 | local function sha384(x,y) return sha2( x, y, 384 ) end
23 |
24 | t( sha256( "Hello world!" ), "C0535E4BE2B79FFD93291305436BF889314E4A3FAEC05ECFFCBB7DF31AD9E51A", t.hexsame )
25 | t( sha224( "Hello world!" ), "7E81EBE9E604A0C97FEF0E4CFE71F9BA0ECBA13332BDE953AD1C66E4", t.hexsame )
26 | t( sha512( "Hello world!" ), "F6CDE2A0F819314CDDE55FC227D8D7DAE3D28CC556222A0A8AD66D91CCAD4AAD6094F517A2182360C9AACF6A3DC323162CB6FD8CDFFEDB0FE038F55E85FFB5B6", t.hexsame )
27 | t( sha384( "Hello world!" ), "86255FA2C36E4B30969EAE17DC34C772CBEBDFC58B58403900BE87614EB1A34B8780263F255EB5E65CA9BBB8641CCCFE", t.hexsame )
28 |
29 | t( sha256( '' ), "E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B855", t.hexsame )
30 | t( sha256(( "a" ):rep( 1 )), "CA978112CA1BBDCAFAC231B39A23DC4DA786EFF8147C4E72B9807785AFEE48BB", t.hexsame )
31 | t( sha256(( "b" ):rep( 63 )), "94E419FABAC7F930810F9636354042F8C1426D2F834D4AB65C93DC1E69326B13", t.hexsame )
32 | t( sha256(( "c" ):rep( 64 )), "52B6419D27BD7F547CEE3B92F8C17A908B8A49601ECBEC161E5030DE1DFE9E0A", t.hexsame )
33 | t( sha256(( "d" ):rep( 65 )), "899987F295364060C6ABD752A7E895124B467FD7CF56B52CE22F4A684A5723F4", t.hexsame )
34 | t( sha256(( "e" ):rep( 130 )), "C78A24F98CC9596CAFD6FC954A0664CA5CAD156AD406A8CC246B5E1F56864DB7", t.hexsame )
35 |
36 | t( sha512( '' ), "CF83E1357EEFB8BDF1542850D66D8007D620E4050B5715DC83F4A921D36CE9CE47D0D13C5D85F2B0FF8318D2877EEC2F63B931BD47417A81A538327AF927DA3E", t.hexsame )
37 | t( sha512(( "a" ):rep( 1 )), "1F40FC92DA241694750979EE6CF582F2D5D7D28E18335DE05ABC54D0560E0F5302860C652BF08D560252AA5E74210546F369FBBBCE8C12CFC7957B2652FE9A75", t.hexsame )
38 | t( sha512(( "b" ):rep( 127 )), "1FB5054735807A95088312066BDD2ACEC2EB8F65454BF77873CDF93998F79C75FC0F229AB4A8FFE0BFD5310A3357272ADCECB378D1F310EE43ED4A0634C6E5B8", t.hexsame )
39 | t( sha512(( "c" ):rep( 128 )), "1CADAE2171FD051AA72F31D7D11D232D867E9823E0DA1FAB3F40288C46C009ABA8A378454514FA6756D00C1037FFBC32B3716DF881569C545A2E190CE426C79B", t.hexsame )
40 | t( sha512(( "d" ):rep( 129 )), "30E54405DCC986AE90F830E01FC144190FF756EFD6E7E9FE4BDF9D6416B54C63E5CE18BFCE172DC360436052DB834A37317D0E2085FAF11E3C69A59020CDD8FC", t.hexsame )
41 | t( sha512(( "e" ):rep( 300 )), "C2202F2BC948039340224757BF24A0B59A24737A3083DF9A8DD062AB7A0717147E025FDAB38CFC5DED56B3E8AC8072D87457AFA143DBD4F26ACF4CB26BB35266", t.hexsame )
42 |
43 | t.test_embedded_example()
44 |
45 | t()
46 |
--------------------------------------------------------------------------------
/test/shellcommand.ex1.lua:
--------------------------------------------------------------------------------
1 | local shellcommand = require 'shellcommand'
2 | local t = require 'testhelper'
3 |
4 | local lua, argdumputiil, outpath = t.argdumputil()
5 |
6 | t( shellcommand(), '' )
7 | t( shellcommand{}, '' )
8 |
9 | os.execute( shellcommand{lua, argdumputiil, 'x'} )
10 | t( t.readfile(outpath), 'x' )
11 |
12 | os.execute( shellcommand{lua, argdumputiil, 'x', 'y'} )
13 | t( t.readfile(outpath), 'xy' )
14 |
15 | t( shellcommand{lua, argdumputiil, '-i'}, shellcommand({lua, argdumputiil})..' -i' )
16 | t( shellcommand{lua, argdumputiil, '-o'}, shellcommand({lua, argdumputiil})..' -o' )
17 | t( shellcommand{lua, argdumputiil, '-e'}, shellcommand({lua, argdumputiil})..' -e' )
18 |
19 | t.writefile('tmp_1.txt','abc')
20 | os.execute( shellcommand{lua, argdumputiil, '-i', input='tmp_1.txt'} )
21 | t( t.readfile(outpath), 'abc' )
22 |
23 | os.execute( shellcommand{lua, argdumputiil, 'z', 'w', '-o', output='tmp_2.txt'} )
24 | t( t.readfile(outpath), 'zw' )
25 | t( t.readfile( 'tmp_2.txt'), 'zw' )
26 |
27 | os.execute( shellcommand{lua, argdumputiil, '-i', '-o', input='tmp_1.txt', output='tmp_2.txt'} )
28 | t( t.readfile(outpath), 'abc' )
29 | t( t.readfile( 'tmp_2.txt'), 'abc' )
30 |
31 | os.execute( shellcommand{lua, argdumputiil, 'z', 'w', '-o', append=true, output='tmp_2.txt'} )
32 | t( t.readfile(outpath), 'zw' )
33 | t( t.readfile( 'tmp_2.txt'), 'abczw' )
34 |
35 | os.execute( shellcommand{lua, argdumputiil, '-i', '-e', input='tmp_1.txt', output='tmp_2.txt', error='tmp_3.txt'} )
36 | t( t.readfile(outpath), 'abc' )
37 | t( t.readfile( 'tmp_2.txt'), '' )
38 | t( t.readfile( 'tmp_3.txt'), 'abc' )
39 |
40 | os.execute( shellcommand{lua, argdumputiil, 'z', 'w', '-e', append=true, output='tmp_2.txt', error='tmp_3.txt'} )
41 | t( t.readfile(outpath), 'zw' )
42 | t( t.readfile( 'tmp_2.txt'), '' )
43 | t( t.readfile( 'tmp_3.txt'), 'abczw' )
44 |
45 | os.execute( shellcommand{lua, argdumputiil, 'a', 'b', '-o', 'c', 'd', '-e', output='tmp_2.txt', error='tmp_2.txt'} )
46 | local o = t.readfile('tmp_2.txt')
47 | t( #o, 4 )
48 | t( o, 'ab', t.patsame )
49 | t( o, 'cd', t.patsame )
50 |
51 | t.test_embedded_example()
52 |
53 | t()
54 |
55 |
--------------------------------------------------------------------------------
/test/simplepath.ex1.lua:
--------------------------------------------------------------------------------
1 | local simplepath_c = require 'simplepath'
2 | local t = require 'testhelper'
3 |
4 | local sps = function(x) return x:gsub("/", package.config:sub(1,1)) end
5 | local simplepath = function(x) return simplepath_c(sps(x)) end
6 |
7 | t( simplepath'a/b/c./d/e', sps'a/b/c./d/e' )
8 | t( simplepath'a/b/.c/d/e', sps'a/b/.c/d/e' )
9 | t( simplepath'a/b./.c/d/e', sps'a/b./.c/d/e' )
10 | t( simplepath'a/b/c../d/e', sps'a/b/c../d/e' )
11 | t( simplepath'a/b/..c/d/e', sps'a/b/..c/d/e' )
12 | t( simplepath'a/b../..c/d/e', sps'a/b../..c/d/e' )
13 |
14 | t( simplepath'', '.' )
15 |
16 | t( simplepath'./', sps'.' )
17 | t( simplepath'./.', sps'.' )
18 | t( simplepath'././', sps'.' )
19 | t( simplepath'./././.', sps'.' )
20 |
21 | t( simplepath'./asd', sps'asd' )
22 | t( simplepath'./.asd', sps'.asd' )
23 | t( simplepath'./asd/q', sps'asd/q' )
24 |
25 | t( simplepath'./asd/.', sps'asd' )
26 | t( simplepath'./asd/q/.', sps'asd/q' )
27 |
28 | t( simplepath'/asd/q/./w', sps'/asd/q/w' )
29 | t( simplepath'/asd/q/./w/./e', sps'/asd/q/w/e' )
30 |
31 | t( simplepath'././asd', sps'asd' )
32 |
33 | t( simplepath'../', sps'..' )
34 | t( simplepath'../asd', sps'../asd' )
35 | t( simplepath'../asd/q', sps'../asd/q' )
36 |
37 | t( simplepath'a/b/..', sps'a' )
38 | t( simplepath'a/b/c/..', sps'a/b' )
39 |
40 | t( simplepath'a/b/../c', sps'a/c' )
41 | t( simplepath'a/b/../c/d/..', sps'a/c' )
42 |
43 | t( simplepath'a/b/c/../../d', sps'a/d' )
44 | t( simplepath'a/b/c/d/../../../e', sps'a/e' )
45 | t( simplepath'a/b/c/d/../../..', sps'a' )
46 |
47 | t( simplepath'../../a/b/c', sps'../../a/b/c' )
48 |
49 | t( simplepath'./..', sps'..' )
50 | t( simplepath'../.', sps'..' )
51 | t( simplepath'././../../a/b/././.././..', sps'../..' )
52 |
53 | t.test_embedded_example()
54 |
55 | t()
56 |
--------------------------------------------------------------------------------
/test/stepdebug.ex1.lua:
--------------------------------------------------------------------------------
1 | local stepdebug = require 'stepdebug'
2 | local t = require 'testhelper'
3 |
4 | -- Test setup
5 | local input=''
6 | local output={}
7 | stepdebug(function()
8 | if input~=nil then
9 | output[1+#output] = stepdebug(input)
10 | end
11 | stepdebug'next'
12 | end)
13 |
14 | stepdebug'break'
15 | input="1"
16 | input='continue'
17 |
18 | t( #output, 1 )
19 | t( output[1], 1 )
20 |
21 | output, input = {}, nil
22 |
23 | stepdebug'break'
24 | input="return 2"
25 | input='continue'
26 |
27 | t( #output, 1 )
28 | t( output[1], 2 )
29 |
30 | output, input = {}, nil
31 |
32 | input='break'
33 | input='1'
34 | input='2'
35 | stepdebug'break'
36 | input='3'
37 | input='4'
38 | input='continue'
39 | input='5'
40 | input='6'
41 |
42 | t( #output, 3 )
43 | t( output[1], 2 )
44 | t( output[2], 3 )
45 | t( output[3], 4 )
46 |
47 | output, input = {}, nil
48 |
49 | stepdebug'break'
50 | input='1'
51 | input='2'
52 | ;(function()
53 | input='3'
54 | input='4'
55 | end)()
56 | input='5'
57 | input='6'
58 | input='continue'
59 |
60 | t( #output, 6 )
61 | t( output[1], 1 )
62 | t( output[2], 2 )
63 | t( output[3], 2 )
64 | t( output[4], 4 )
65 | t( output[5], 5 )
66 | t( output[6], 6 )
67 |
68 | output, input = {}, nil
69 |
70 | stepdebug'break'
71 | input='step'
72 | input='1'
73 | input='2'
74 | ;(function()
75 | input='3'
76 | input='4'
77 | ;(function()
78 | input='5'
79 | input='6'
80 | end)()
81 | input='7'
82 | input='8'
83 | end)()
84 | input='9'
85 | input='10'
86 | input='continue'
87 |
88 | t( #output, 13 )
89 | t( output[1], 1 )
90 | t( output[2], 2 )
91 | t( output[3], 2 )
92 | t( output[4], 2 )
93 | t( output[5], 3 )
94 | t( output[6], 4 )
95 | t( output[7], 4 )
96 | t( output[8], 6 )
97 | t( output[9], 7 )
98 | t( output[10], 8 )
99 | t( output[11], 8 )
100 | t( output[12], 9 )
101 | t( output[13], 10 )
102 |
103 | output, input = {}, nil
104 |
105 | stepdebug'break'
106 | input='step'
107 | input='1'
108 | input='2'
109 | ;(function()
110 | input='3'
111 | input='4'
112 | end)()
113 | input='5'
114 | input='6'
115 | ;(function()
116 | input='7'
117 | input='8'
118 | end)()
119 | input='9'
120 | input='10'
121 | input='continue'
122 |
123 | t( #output, 13 )
124 | t( output[1], 1 )
125 | t( output[2], 2 )
126 | t( output[3], 2 )
127 | t( output[4], 2 )
128 | t( output[5], 3 )
129 | t( output[6], 4 )
130 | t( output[7], 4 )
131 | t( output[8], 5 )
132 | t( output[9], 6 )
133 | t( output[10], 6 )
134 | t( output[11], 8 )
135 | t( output[12], 9 )
136 | t( output[13], 10 )
137 |
138 | output, input = {}, nil
139 |
140 | stepdebug'break'
141 | input='step'
142 | input='1'
143 | input='2'
144 | ;(function()
145 | input='3'
146 | input='4'
147 | input='finish'
148 | input='5'
149 | input='6'
150 | input='7'
151 | end)()
152 | input='8'
153 | input='9'
154 | input='continue'
155 |
156 | t( #output, 9 )
157 | t( output[1], 1 )
158 | t( output[2], 2 )
159 | t( output[3], 2 )
160 | t( output[4], 2 )
161 | t( output[5], 3 )
162 | t( output[6], 4 )
163 | t( output[7], 7 )
164 | t( output[8], 8 )
165 | t( output[9], 9 )
166 |
167 | output, input = {}, nil
168 |
169 | function gupnam() -- must be global
170 | return debug.getinfo(6).name or ''
171 | end
172 |
173 | stepdebug'break'
174 | input='step'
175 | input='gupnam()'
176 | function NL()
177 | input='gupnam()'
178 | end
179 | NL()
180 | input='gupnam()'
181 | input='continue'
182 |
183 | t( #output, 7 )
184 | t( output[4], 'NL' )
185 | t( output[5], 'NL' )
186 |
187 | t()
188 |
189 |
--------------------------------------------------------------------------------
/test/subbytebase.ex1.lua:
--------------------------------------------------------------------------------
1 | local subbytebase = require 'subbytebase'
2 | local t = require 'testhelper'
3 |
4 | t( subbytebase(6,'' ) , '' , t.bytesame )
5 | t( subbytebase(6,'a' ) , 'YQ==' , t.bytesame )
6 | t( subbytebase(6,'aa' ) , 'YWE=' , t.bytesame )
7 | t( subbytebase(6,'aaa' ) , 'YWFh' , t.bytesame )
8 | t( subbytebase(6,'aaaa' ) , 'YWFhYQ==' , t.bytesame )
9 | t( subbytebase(6,'aaaaa' ) , 'YWFhYWE=' , t.bytesame )
10 | t( subbytebase(6,'aaaaaa' ) , 'YWFhYWFh' , t.bytesame )
11 |
12 | t( subbytebase(-6, '' ) , '' , t.bytesame )
13 | t( subbytebase(-6, 'YQ==' ) , 'a' , t.bytesame )
14 | t( subbytebase(-6, 'YWE=' ) , 'aa' , t.bytesame )
15 | t( subbytebase(-6, 'YWFh' ) , 'aaa' , t.bytesame )
16 | t( subbytebase(-6, 'YWFhYQ==') , 'aaaa' , t.bytesame )
17 | t( subbytebase(-6, 'YWFhYWE=') , 'aaaaa' , t.bytesame )
18 | t( subbytebase(-6, 'YWFhYWFh') , 'aaaaaa' , t.bytesame )
19 |
20 | t( subbytebase(1, 'hi' ), '0110100001101001', t.bytesame )
21 | t( subbytebase(-1, '0110100001101001' ), 'hi', t.bytesame )
22 |
23 | t( subbytebase(2, 'hi' ), '12201221', t.bytesame )
24 | t( subbytebase(-2, '12201221' ), 'hi', t.bytesame )
25 |
26 | t( subbytebase(3, 'hi' ), '320644=', t.bytesame )
27 | t( subbytebase(-3, '320644=' ), 'hi', t.bytesame )
28 |
29 | t( subbytebase(4, 'hi' ), '6869', t.bytesame )
30 | t( subbytebase(-4, '6869' ), 'hi', t.bytesame )
31 |
32 | t( subbytebase(5, 'hi' ), 'D1MG===', t.bytesame )
33 | t( subbytebase(-5, 'D1MG===' ), 'hi', t.bytesame )
34 |
35 | t( subbytebase(6, 'hi' ), 'aGk=', t.bytesame )
36 | t( subbytebase(-6, 'aGk=' ), 'hi', t.bytesame )
37 |
38 | t( subbytebase(7, '\xFF' ), '\x7F\x40======', t.bytesame )
39 | t( subbytebase(-7, '\x7F\x40======' ), '\xFF', t.bytesame )
40 |
41 | t( subbytebase(8, 'hi' ), 'hi', t.bytesame )
42 | t( subbytebase(-8, 'hi' ), 'hi', t.bytesame )
43 |
44 | t.test_embedded_example()
45 |
46 | t()
47 |
48 |
--------------------------------------------------------------------------------
/test/tapfail.ex1.lua:
--------------------------------------------------------------------------------
1 | local tapfail = require 'tapfail'
2 | local t = require 'testhelper'
3 |
4 | local ts = tapfail()
5 | t( type(ts), 'function' )
6 | t( ts'', nil )
7 |
8 | local ts = tapfail()
9 | t( type(ts), 'function' )
10 | t( ts'not ok', 'no diagnostic or ok line' )
11 |
12 | local ts = tapfail()
13 | t( type(ts), 'function' )
14 | t( ts'ok 1', nil )
15 | t( ts'1..1', nil )
16 | t( ts(), nil )
17 |
18 | local ts = tapfail()
19 | t( type(ts), 'function' )
20 | t( ts'ok 1', nil )
21 | t( ts'ok 2', nil )
22 | t( ts'1..2', nil )
23 |
24 | local ts = tapfail()
25 | t( type(ts), 'function' )
26 | t( ts'ok 1', nil )
27 | t( ts'#not ok masked by diagnostic', nil )
28 | t( ts'1..1', nil )
29 |
30 | local ts = tapfail()
31 | t( type(ts), 'function' )
32 | t( ts'ok 2', 'invalid count sequence' )
33 | t( ts'ok 1', 'invalid count sequence' )
34 |
35 | local ts = tapfail()
36 | t( type(ts), 'function' )
37 | t( ts'ok 1', nil )
38 | t( ts'ok 3', 'invalid count sequence' )
39 | t( ts'ok 2', 'invalid count sequence' )
40 |
41 | local ts = tapfail()
42 | t( type(ts), 'function' )
43 | t( ts'ok 1', nil )
44 | t( ts'1..2', 'invalid test count' )
45 |
46 | local ts = tapfail()
47 | t( type(ts), 'function' )
48 | t( ts'ok 1', nil )
49 | t( ts'not ok', 'no diagnostic or ok line' )
50 | t( ts'1..2', 'invalid test count' )
51 |
52 | local ts = tapfail()
53 | t( type(ts), 'function' )
54 | t( ts'ok 1', nil )
55 | t( ts'not ok', 'no diagnostic or ok line' )
56 | t( ts'1..1', nil )
57 |
58 | local ts = tapfail()
59 | t( type(ts), 'function' )
60 | t( ts'ok 1', nil )
61 | t( ts(), 'summary missing' )
62 |
63 | local ts = tapfail()
64 | t( type(ts), 'function' )
65 | t( ts'ok 1', nil )
66 | t( ts'1..1', nil )
67 | t( ts'ok 2', 'line after summary' )
68 |
69 | local ts = tapfail()
70 | t( type(ts), 'function' )
71 | t( ts'ok 1', nil )
72 | t( ts'1..2', 'invalid test count' )
73 | t( ts'ok 2', 'line after summary' )
74 |
75 | local ts = tapfail()
76 | t( type(ts), 'function' )
77 | t( ts'ok 1', nil )
78 | t( ts'ok 2', nil )
79 | t( ts'ok 3', nil )
80 | t( ts'1..N', nil )
81 | t( ts(), nil )
82 |
83 | local ts = tapfail()
84 | t( type(ts), 'function' )
85 | t( ts'1..3', nil )
86 | t( ts'ok 1', nil )
87 | t( ts'ok 2', nil )
88 | t( ts'ok 3', nil )
89 | t( ts(), nil )
90 |
91 | local ts = tapfail()
92 | t( type(ts), 'function' )
93 | t( ts'1..2', nil )
94 | t( ts'ok 1', nil )
95 | t( ts'ok 2', nil )
96 | t( ts'ok 3', 'invalid count sequence' )
97 |
98 | local ts = tapfail()
99 | t( type(ts), 'function' )
100 | t( ts'1..N', nil )
101 | t( ts'ok 1', nil )
102 | t( ts'ok 2', nil )
103 | t( ts'ok 3', nil )
104 | t( ts(), nil )
105 |
106 | local ts = tapfail()
107 | t( type(ts), 'function' )
108 | t( ts'1..3', nil )
109 | t( ts'ok 1', nil )
110 | t( ts'ok 2', nil )
111 | t( ts(), 'missing test' )
112 |
113 | local ts = tapfail()
114 | t( type(ts), 'function' )
115 | t( ts'1..N', nil )
116 | t( ts'ok 1', nil )
117 | t( ts'ok 2', nil )
118 | t( ts'ok 3', nil )
119 | t( ts'1..N', 'summary already found at line 1' )
120 |
121 | local ts = tapfail()
122 | t( type(ts), 'function' )
123 | t( ts'1..N', nil )
124 | t( ts'ok 1', nil )
125 | t( ts'ok 2', nil )
126 | t( ts'1..N', 'summary already found at line 1' )
127 | t( ts'ok 3', nil )
128 |
129 | t.test_embedded_example()
130 |
131 | t()
132 |
--------------------------------------------------------------------------------
/test/taptest.ex1.lua:
--------------------------------------------------------------------------------
1 | local taptest = require "taptest"
2 | local t = require "testhelper"
3 | -- taptest is both the "Unit under test" (taptest) and the "Test framework" (t - testhelper)
4 |
5 | -- To avoid confusion (as much as it is possible) t will be always used in its
6 | -- easest form: it just checks that the two argument are equals.
7 | -- Since taptest always returns what it print on stdout, the returned
8 | -- value of taptest is checked
9 |
10 | -- wrap the taptest to simplify line number checks
11 | local fake_test_count = 1
12 | local taptest_wrapped = taptest
13 | local function taptest( ... )
14 | fake_test_count = fake_test_count + 2
15 | local result = taptest_wrapped( ... )
16 | return result -- avoid tail call
17 | end
18 |
19 | t( taptest( 1, 1 ), "ok 1" )
20 | t( taptest( 1, 1 ), "ok 2" )
21 |
22 | -- Additional infos when the test fails
23 | t( taptest( 1, 2 ),
24 | "not ok 3 - taptest.ex1.lua:15. Mismatch: [1] VS [2]. " )
25 |
26 | -- Custom infos on fail
27 | t( taptest( 1, 2, "Not good!" ),
28 | "not ok 4 - taptest.ex1.lua:15. Mismatch: [1] VS [2]. Not good!" )
29 |
30 | -- Custom compare function
31 | t( taptest( 1, 2, function( a, b ) return a < b end ),
32 | "ok 5" )
33 | t( taptest( 2, 1, function( a, b ) return a < b end ),
34 | "not ok 6 - taptest.ex1.lua:15. Mismatch: [2] VS [1]. " )
35 |
36 | -- Custom compare function and message
37 | t( taptest( 1, 1, function( a, b ) return a ~= b end, "Not good!" ),
38 | "not ok 7 - taptest.ex1.lua:15. Mismatch: [1] VS [1]. Not good!" )
39 | t( taptest( 1, 1, "Not good!", function( a, b ) return a ~= b end ),
40 | "not ok 8 - taptest.ex1.lua:15. Mismatch: [1] VS [1]. Not good!" )
41 |
42 | -- Single argument = Tap diagnostic
43 | t( taptest( "new\nsuite" ), "# new\n# suite" )
44 |
45 | -- Checker function can add useful information
46 | t( taptest( 1, 1, function( a, b ) return a == b, "- additional info" end ),
47 | "ok 9 - additional info" )
48 |
49 | t( taptest( 1, 2, function( a, b ) return a == b, "- additional info" end ),
50 | "not ok 10 - taptest.ex1.lua:15. - additional info" )
51 |
52 | -- No argument = Summary and final plan
53 | t( taptest(), "# \n# 6 tests failed\n# \n1..10" )
54 |
55 | local function taptest_masked(...)
56 | local taptest_blame_caller = true
57 | r = taptest_wrapped(...)
58 | return r -- no tail call
59 | end
60 |
61 | t( taptest_masked( 1, 2 ), "not ok 11 - taptest.ex1.lua:61. Mismatch: [1] VS [2]. " )
62 |
63 | t( taptest( nil, nil ), "ok 12" )
64 | t( taptest( nil, true, function(a,b) return a~=b end ), "ok 13" )
65 | t( taptest( true, nil, function(a,b) return a~=b end ), "ok 14" )
66 |
67 | t.test_embedded_example()
68 |
69 | t()
70 |
71 | -- In case all the tests are successful, the line
72 | -- # all is right
73 | -- will be substitued to the '# 5 tests failed' one
74 |
--------------------------------------------------------------------------------
/test/templua.ex1.lua:
--------------------------------------------------------------------------------
1 | local templua = require( "templua" )
2 | local t = require 'testhelper'
3 |
4 | -- Blank templates are not touched
5 | local m = templua( "ok" )
6 | t( 'ok', m{} )
7 |
8 | -- Lua expression expansion with @{}
9 | m = templua( "@{item.a} @{item.b:upper()}" )
10 | t( 'a B', m{ item = { a = 'a', b = 'b' } } )
11 | m = templua( "@{item.a} @{item.b:upper()}" )
12 | t( 'B A', m{ item = { a = 'B', b = 'a' } } )
13 |
14 | -- Call same template multiple times
15 | m = templua( "@{x}" )
16 | t( '1', m{ x=1 } )
17 | t( '2', m{ x=2 } )
18 |
19 | -- Mix lua statements and text with @{{}}
20 | m = templua( "@{{for i=1,3 do}} hello @{item}!@{{end}}" )
21 | t( ' hello world! hello world! hello world!', m{ item = 'world' } )
22 |
23 | -- Use the output function to expand text from complex lua code
24 | m = templua( "@{{for i=1,3 do out(' hello '..item..'!') end}}" )
25 | t( ' hello dude! hello dude! hello dude!', m{ item = 'dude' } )
26 |
27 | -- Statement that do not cover all the string
28 | m = templua( "hello @{{a=1}} world" )
29 | t( 'hello world', m{} )
30 |
31 | -- Last text appending
32 | m = templua( "@{'true'} ok" )
33 | t( 'true ok', m{} )
34 |
35 | -- Value cast in the output function
36 | m = templua( "@{true} ok" )
37 | t( 'true ok', m{} )
38 |
39 | -- Compile error is found
40 | local m, e = templua( "@{{][}}" )
41 | t( m, nil )
42 | t( e, 'string "templua_script"%]:1: unexpected symbol', t.patsame )
43 |
44 | -- Error while expanding
45 | m = templua( "@{{undefined_function()}}" )
46 | local s, e = m{}
47 | t( s, nil )
48 | t( e, 'string "templua_script"%]:1: attempt to call a nil value', t.patsame )
49 |
50 | -- Ignoring @ between templates
51 | m = templua( "@{x} @ @{x}" )
52 | t( 'y @ y', m{ x='y' } )
53 |
54 | -- Nested template
55 | local s = {}
56 | function s.nestcall()
57 | return templua( "@{'B'}" )( s )
58 | end
59 | t( templua( "@{nestcall()}@{nestcall()}" )( s ), 'BB' )
60 |
61 | t.test_embedded_example()
62 |
63 | t()
64 |
65 |
--------------------------------------------------------------------------------
/test/timeprof.ex1.lua:
--------------------------------------------------------------------------------
1 | local timeprof = require 'timeprof'
2 | local t = require 'testhelper'
3 |
4 | local e02, e002 = t.number_tollerance(0.02), t.number_tollerance(0.002)
5 | local a, b, c, f, m, s
6 |
7 | -- Get timers, same are returned if same argument is passed (except nil)
8 |
9 | a = timeprof'a'
10 | t( a, timeprof'a' )
11 | b = timeprof'b'
12 | c = timeprof()
13 | t( b, a, t.diff )
14 | t( b, c, t.diff )
15 | t( a, c, t.diff )
16 |
17 | -- Timer Api
18 |
19 | a = timeprof()
20 |
21 | t( a:start(), nil )
22 | t( a:stop(), nil )
23 | t( a:reset(), nil )
24 |
25 | -- Single time measurement
26 |
27 | a = timeprof()
28 | f, m, s = a:summary()
29 |
30 | t( f, 0 )
31 | t( m, 0 )
32 | t( s, 0 )
33 |
34 | a:start()
35 | t.wait(0.2)
36 | a:stop()
37 | f, m, s = a:summary()
38 |
39 | t( f, 0.2, e02 )
40 | t( m, 0.2, e02 )
41 | t( s, 0, e02 )
42 |
43 | t.wait(0.1)
44 |
45 | -- Adding two time measurement
46 |
47 | a:start()
48 | t.wait(0.1)
49 | a:stop()
50 | f, m, s = a:summary()
51 |
52 | t( f, 0.3, e02 )
53 | t( m, 0.15, e02 )
54 | t( s, 0.0707, e002 )
55 |
56 | a:start()
57 | t.wait(0.3)
58 | a:stop()
59 | f, m, s = a:summary()
60 |
61 | t( f, 0.6, e02 )
62 | t( m, 0.2, e02 )
63 | t( s, 0.1, e002 )
64 |
65 | -- Resetting measurements
66 |
67 | a:reset()
68 | f, m, s = a:summary()
69 |
70 | t( f, 0 )
71 | t( m, 0 )
72 | t( s, 0 )
73 |
74 | a:start()
75 | t.wait(0.01)
76 | a:stop()
77 | f, m, s = a:summary()
78 |
79 | t( f, 0.01, e002 )
80 | t( m, 0.01, e002 )
81 | t( s, 0 )
82 |
83 | -- Multiple timers
84 |
85 | a = timeprof()
86 | b = timeprof()
87 |
88 | a:start()
89 | t.wait(0.01)
90 | b:start()
91 | t.wait(0.02)
92 | a:stop()
93 | b:stop()
94 | f, m, s = a:summary()
95 |
96 | t( f, 0.03, e002 )
97 | t( m, 0.03, e002 )
98 | t( s, 0 )
99 |
100 | f, m, s = b:summary()
101 |
102 | t( f, 0.02, e002 )
103 | t( m, 0.02, e002 )
104 | t( s, 0 )
105 |
106 | t()
107 |
108 |
--------------------------------------------------------------------------------
/test/toposort.ex1.lua:
--------------------------------------------------------------------------------
1 | local toposort = require 'toposort'
2 | local t = require 'testhelper'
3 |
4 | t( toposort(), {}, t.deepsame )
5 |
6 | t( toposort{a={'b'}}, {'b','a'}, t.deepsame )
7 |
8 | t( toposort{a={'b','c'}}, {'b','a'}, t.itemorder )
9 | t( toposort{a={'b','c'}}, {'c','a'}, t.itemorder )
10 |
11 | t( toposort{a={'b'},b={'c'}}, {'b','a'}, t.itemorder )
12 | t( toposort{a={'b'},b={'c'}}, {'c','b'}, t.itemorder )
13 |
14 | t( toposort{a={'b','c'},b={'d'}}, {'b','a'}, t.itemorder )
15 | t( toposort{a={'b','c'},b={'d'}}, {'c','a'}, t.itemorder )
16 | t( toposort{a={'b','c'},b={'d'}}, {'d','b'}, t.itemorder )
17 |
18 | t( toposort{a={'b','c'},c={'b'}}, {'b','a'}, t.itemorder )
19 | t( toposort{a={'b','c'},c={'b'}}, {'c','a'}, t.itemorder )
20 | t( toposort{a={'b','c'},c={'b'}}, {'b','c'}, t.itemorder )
21 |
22 | t( toposort{a={'c','b'},b={'d'}}, {'c','a'}, t.itemorder )
23 | t( toposort{a={'c','b'},b={'d'}}, {'b','a'}, t.itemorder )
24 | t( toposort{a={'c','b'},b={'d'}}, {'d','b'}, t.itemorder )
25 |
26 | t( toposort{a={'b','c'},d={'b','c'},e={'f','g'}}, {'b','a'}, t.itemorder )
27 | t( toposort{a={'b','c'},d={'b','c'},e={'f','g'}}, {'c','a'}, t.itemorder )
28 | t( toposort{a={'b','c'},d={'b','c'},e={'f','g'}}, {'b','d'}, t.itemorder )
29 | t( toposort{a={'b','c'},d={'b','c'},e={'f','g'}}, {'c','d'}, t.itemorder )
30 | t( toposort{a={'b','c'},d={'b','c'},e={'f','g'}}, {'f','e'}, t.itemorder )
31 | t( toposort{a={'b','c'},d={'b','c'},e={'f','g'}}, {'g','e'}, t.itemorder )
32 |
33 | local _, err = toposort{a={'b'},b={'a'}}
34 | t( err, 'cycle detected' )
35 |
36 | t.test_embedded_example()
37 |
38 | t()
39 |
40 |
--------------------------------------------------------------------------------
/test/trimstring.ex1.lua:
--------------------------------------------------------------------------------
1 | local trimstring = require 'trimstring'
2 | local t = require 'testhelper'
3 |
4 | t( trimstring(''), '' )
5 | t( trimstring('a'), 'a' )
6 |
7 | t( trimstring(' a'), 'a' )
8 | t( trimstring('a '), 'a' )
9 | t( trimstring(' a '), 'a' )
10 |
11 | t( trimstring(' a a'), 'a a' )
12 | t( trimstring('a a '), 'a a' )
13 | t( trimstring(' a a '), 'a a' )
14 |
15 | t( trimstring(' \nstr\r\t '), 'str' )
16 |
17 | t.test_embedded_example()
18 |
19 | t()
20 |
--------------------------------------------------------------------------------
/test/tuple.ex1.lua:
--------------------------------------------------------------------------------
1 | local tuple = require 'tuple'
2 | local t = require 'testhelper'
3 |
4 | -- Equality operation
5 | t( type(tuple(1,'a',true,3)), 'table' )
6 | t( tuple(1,'a',true,3), tuple(1,'a',true,3) )
7 |
8 | -- Read fields
9 | local field = tuple(1,'a',true,3)
10 | t( field.n, 4 )
11 | t( field[1], 1 )
12 | t( field[2], 'a' )
13 | t( field[3], true )
14 | t( field[4], 3 )
15 |
16 | -- Store nil and NaN
17 | field = tuple(1,nil,0/0,3)
18 | t( field.n, 4 )
19 | t( field[1], 1 )
20 | t( field[2], nil )
21 | t( field[3], field[3], t.diff )
22 | t( field[4], 3 )
23 |
24 | -- Can not change field
25 | local a, b = pcall(function() tuple( 1, nil, 0/0, 3 )[1] = 2 end)
26 | t( a, false )
27 | t( b:match( 'can not change tuple field' ), 'can not change tuple field' )
28 |
29 | -- Garbage collection test
30 |
31 | local gccount = 0
32 | local x = tuple(2,nil,0/0,4)
33 | x = setmetatable( x, {__gc=function(t) gccount = gccount + 1 end} )
34 |
35 | -- No collection if some reference is still around
36 | collectgarbage('collect')
37 | t( gccount, 0 )
38 |
39 | -- Automatic collection
40 | x = nil
41 | collectgarbage('collect')
42 | t( gccount, 1 )
43 |
44 | t.test_embedded_example()
45 |
46 | t()
47 |
48 |
--------------------------------------------------------------------------------
/test/uniontab.ex1.lua:
--------------------------------------------------------------------------------
1 | local uniontab = require 'uniontab'
2 | local t = require 'testhelper'
3 |
4 | t( uniontab(), {}, t.deepsame )
5 | t( uniontab({}), {}, t.deepsame )
6 | t( uniontab({},{}), {}, t.deepsame )
7 |
8 | t( uniontab({a='a'}), {a='a'}, t.deepsame )
9 | t( uniontab({},{a='a'}), {a='a'}, t.deepsame )
10 |
11 | t( uniontab({a='a'},{b='b'}), {a='a',b='b'}, t.deepsame )
12 | t( uniontab({a='a'},{a='b'}), {a='a'}, t.deepsame )
13 |
14 | t( uniontab({a='a'},{a='b'},function(a,b) return a..b end), {a='ab'}, t.deepsame )
15 |
16 | t( uniontab({a='a',b='b',c='c'},{a='A',d='d'}), {a='a',b='b',c='c',d='d'}, t.deepsame )
17 |
18 | t.test_embedded_example()
19 |
20 | t()
21 |
--------------------------------------------------------------------------------
/test/valueprint.ex1.lua:
--------------------------------------------------------------------------------
1 | local valueprint = require "valueprint"
2 | local t = require "testhelper"
3 |
4 | t( valueprint(1), '1' )
5 | t( valueprint(true), 'true' )
6 | t( valueprint("hi"), '"hi"' )
7 | t( valueprint({}), '^table 0?x?%x*$', t.patsame )
8 | t( valueprint(nil), 'nil' )
9 |
10 | t( valueprint({1,2}), '^table 0?x?%x*\n| 1: 1\n| 2: 2$' , t.patsame )
11 | t( valueprint({a="b",c="d"}), '^table 0?x?%x*\n.*| "a": "b"' , t.patsame )
12 | t( valueprint({a="b",c="d"}), '^table 0?x?%x*\n.*| "c": "d"' , t.patsame )
13 | t( valueprint({a={b="c"}}), '^table 0?x?%x*\n| "a": table 0?x?%x*\n| | "b": "c"$' , t.patsame )
14 |
15 | local at = {}
16 | at[1] = {}
17 | at[1][1] = at[1]
18 | at[1][at[1]]=true
19 | local r = tostring(at):gsub(':','')
20 | local r1 = tostring(at[1]):gsub(':','')
21 |
22 | local v = valueprint( at )
23 | t( v, '^'
24 | .."table 0?x?%x*\n"
25 | .."| 1: (table 0?x?%x*)\n"
26 | .."| | 1: %1 content is not shown here\n"
27 | .."| | %1 content is not shown here: true"
28 | ..'$', t.patsame )
29 |
30 | local function p(k,v,d,i)
31 | local y = '<'..(k or 'nil')..'|'..v..'|'..d..'|'..i..'>'
32 | x = x..y
33 | return y
34 | end
35 |
36 | x = ''
37 | local v = valueprint({101,102,{b="c"},true,x=nil}, p)
38 | t( v, x )
39 |
40 | t( v, '^'
41 | ..'
'
42 | ..'<1|101|1|number>'
43 | ..'<2|102|1|number>'
44 | ..'<3|(table 0?x?%x*)|1|table>'
45 | ..''
46 | ..'<"b"|"c"|2|string>'
47 | ..''
48 | ..'<4|true|1|boolean>'
49 | ..''
50 | ..'$', t.patsame )
51 |
52 | t.test_embedded_example()
53 |
54 | t()
55 |
56 |
--------------------------------------------------------------------------------
/tool/debugger_stdinout.lua:
--------------------------------------------------------------------------------
1 | --[===[DOC
2 |
3 | = Command line debugger
4 |
5 | The 'debugger_stdinout' module provide a full command line debugger for lua. It
6 | is based on 'stepdebug' module of Luasnip. It install a step-debug handler that
7 | read from the standard input and passes the line to 'stepdebug', so you can
8 | refer to <> documentation for a list of accepted commands.
9 |
10 | As described in the <> documentataion, it does not support classical
11 | breakpoint through filename and line number, but the execution can be
12 | explicitally stopped in the debugged source code with somethig like
13 |
14 | ```
15 | LD = require 'debug_stdinout' ; LD"break"
16 | ```
17 |
18 | Moreover the 'localbind' module of Luasnip is exposed in the 'L' global, so you
19 | can access locals and upvalues of the current executed line with simple
20 | expressions like `print(L.a_local_variable)`. Values can be changed with the
21 | intuitive syntax `L.a_local_variable = "New value"`. Also deeper stack
22 | inspections are possible with expressions like
23 | `print(L(1).a_caller_local_variable)`.
24 |
25 | No command line history or completion is supported.
26 |
27 | ]===]
28 |
29 | local ls = require 'luasnip'
30 | local stepdebug = ls.stepdebug
31 | local localbind = ls.localbind
32 |
33 | do
34 |
35 | local cachesource = {}
36 |
37 | function getsource( level, pre, post )
38 | if not level then level = 1 end
39 | local info = debug.getinfo( level )
40 | if not info then return nil, 'Invalid level' end
41 | local cur = info.currentline
42 | local fil = info.short_src
43 | local path = info.source
44 | path = path:sub(2)
45 | local s = cachesource[ path ]
46 | if not s then
47 | local f = io.open( path, 'r' )
48 | if not f then error() end
49 | s = {}
50 | while true do
51 | local line = f:read('l')
52 | if not line then break end
53 | s[1+#s] = line
54 | end
55 | f:close()
56 | cachesource[ path ] = s
57 | end
58 | local result = {}
59 | if not pre then pre = 1 end
60 | if not post then post = #s end
61 | if post < 0 then post = #s +1 -post end
62 | for l = cur-pre, cur+post do
63 | result[1+#result] = s[l]
64 | end
65 | return result, fil, cur
66 | end
67 | end
68 |
69 | stepdebug(function(b,e)
70 | L = localbind(b) -- global table to access local variables
71 | local pre, post = 5, 5
72 | local src, fil, lnn = getsource(b+1, pre, post)
73 | print('> '..b..' @ '..fil..' : '..lnn)
74 | print('+---------------------')
75 | for i, s in ipairs(src) do
76 | if i == pre+1 then
77 | print('> '..s)
78 | else
79 | print('| '..s)
80 | end
81 | end
82 | print('+---------------------')
83 | io.write('> ') io.flush()
84 | print(stepdebug(io.read("*l")))
85 | end)
86 |
87 |
--------------------------------------------------------------------------------