test('should `.tokenize` return array with tokens', function (done) { limon.use(function () { return function (ch, i) { if (/\s/.test(ch)) { this.tokens.push(['whitespace', ch, i]) return } if (/\W/.test(ch)) { this.tokens.push(['symbol', ch, i]) return } if (/\d/.test(ch)) { this.tokens.push(['digit', ch, i]) return } this.tokens.push(['letter', ch, i]) } }) var tokens = limon.tokenize('a > (b + 2)') test.strictEqual(isArray(tokens), true) test.strictEqual(tokens.length, 11) test.deepEqual(tokens[0], ['letter', 'a', 0]) test.deepEqual(tokens[2], ['symbol', '>', 2]) done() })
handler: function * handler (ctx, opts) { test.strictEqual(typeof ctx, 'object') test.strictEqual(typeof this, 'object') test.strictEqual(typeof ctx.request.text, 'function') test.strictEqual(typeof this.request.text, 'function') this.request.body = yield this.request.text(opts) }
server.use(function * () { test.ok(this.request.files) test.ok(this.request.fields) test.strictEqual(this.request.files[0].name, 'package.json') test.strictEqual(this.request.fields.a, 'b') test.strictEqual(this.request.fields.pkg[0].name, 'package.json') this.body = 'ok1' })
server.use(function * () { test.ok(this.request.files) test.ok(this.request.fields) test.strictEqual(this.request.files[0].name, 'package.json') test.strictEqual(this.request.files[1].name, 'utils.js') test.strictEqual(this.request.fields.pkg[0].name, 'package.json') test.strictEqual(this.request.fields.pkg[1].name, 'utils.js') test.deepEqual(this.request.fields.aaa, ['bbb', 'ddd']) this.body = 'ok4' })
test('should not add to `this.plugins` if plugin not returns function', function (done) { var lexer = new Limon() .use(function (app) { app.foo = 'bar' app.qux = 12345 }) test.strictEqual(lexer.plugins.length, 1) // one, because we have one internal function test.strictEqual(lexer.foo, 'bar') test.strictEqual(lexer.qux, 12345) done() })
test('should have non-strict mode to return index', function (done) { var res = isAsyncFunction(fs.stat, null, false) // fs.stat uses `callback` as callbacka rgument test.strictEqual(typeof res, 'boolean') test.strictEqual(res, true) // `next` is 6th item in `common-callback-names` function fn (foo, bar, next) {} function fn2 (foo, cb_) {} test.strictEqual(isAsyncFunction(fn, null, false), 5) test.strictEqual(isAsyncFunction(fn2, false), 3) done() })
test('should be able to pass `input` from constructor not from `.tokenize` method', function (done) { var lexer = new Limon('a > (b + 2)') lexer.tokenize({ aaa: 'bbb' }) test.strictEqual(lexer.input, 'a > (b + 2)') test.deepEqual(lexer.options, { aaa: 'bbb' }) done() })
test('should immediately invoke a plugin function', function (done) { var called = false var app = new Limon() app.use(function () { called = true }) test.strictEqual(called, true) done() })
test('should be able to bass buffer as `input` instead of string', function (done) { var app = new Limon() app .use(function () { return function (ch) { this.tokens.push(ch) } }) .tokenize(new Buffer('a,2,c')) test.strictEqual(app.tokens.length, 5) done() })
app.use(function * () { test.strictEqual(typeof this.request.fields, 'object') test.deepEqual(this.request.fields, { foo: { bar: { baz: 'qux', cc: 'ccc' }, aa: 'bb' } }) this.body = JSON.stringify(this.request.fields) })
server.use(function * () { test.ok(this.request.files) // possible fails, because it not respect order, it's async // test.strictEqual(this.request.files[0].name, 'LICENSE') // test.strictEqual(this.request.files[1].name, 'README.md') // test.strictEqual(this.request.files[2].name, 'utils.js') // test.strictEqual(this.request.fields.foo[0].name, 'LICENSE') // test.strictEqual(this.request.fields.foo[1].name, 'README.md') // test.strictEqual(this.request.fields.bar[0].name, 'utils.js') test.strictEqual(this.request.files.length, 3, 'should be 3 files') test.strictEqual( this.request.fields.foo.length, 2, 'should fields.foo to have 2 files' ) test.strictEqual( this.request.fields.bar.length, 1, 'should fields.bar to have 1 file' ) this.body = 'ok1' })
test('should push returned functions onto `this.plugins`', function (done) { var lexer = new Limon() lexer.use(function () { return function () {} }) lexer.use(function () { return function () {} }) lexer.use(function () { return function () {} }) test.strictEqual(lexer.plugins.length, 4) done() })
onerror: function (err, ctx) { test.ifError(!err) test.strictEqual(err.status, 400) ctx.throw('custom error', 422) }
app.use(function * () { test.strictEqual(typeof this.request.fields, 'object') test.deepEqual(this.request.fields, { a: { b: { c: '1' } }, c: '2' }) this.body = JSON.stringify(this.request.fields) })
.use(function * () { test.strictEqual(this.body, 'message=lol') })
tryReadJson('{"foo":"bar"}', function (err, obj) { test.ok(err === null) test.strictEqual(typeof obj, 'object') test.strictEqual(obj.foo, 'bar') done() })
tryReadJson(123, function (err, res) { test.strictEqual(err, null) test.strictEqual(res, 123) done() })
server.use(function * () { test.ok(this.request.fields) test.deepEqual(this.request.fields.foo, ['bar', 'baz']) test.strictEqual(this.request.fields.baz, 'qux') this.body = 'ok3' })
test('should have `.use`, `.run` and `.tokenize` method on prototype', function (done) { test.strictEqual(typeof limon.use, 'function') test.strictEqual(typeof limon.run, 'function') test.strictEqual(typeof limon.tokenize, 'function') done() })
test('should return error if `input` is `undefined` and no callback', function (done) { var err = tryReadJson() test.strictEqual(err.name, 'SyntaxError') test.strictEqual(/Unexpected/.test(err.message), true) done() })
test('should have an option to supply an extension', (done) => { test.strictEqual(/png$/.test(tmpFilepath('.png')), true) done() })
test('should generate a random temp file path', (done) => { test.strictEqual(tmpFilepath().indexOf(os.tmpdir()) !== -1, true) done() })
server.use(function * () { test.strictEqual(this.body, undefined) test.strictEqual(this.request.fields, undefined) test.strictEqual(this.request.files, undefined) this.body = 'abc' })
test('should accept second argument `names` to be boolean true', function (done) { test.strictEqual(isAsyncFunction(fs.readFile, true), true) done() })
server.use(function * () { test.strictEqual(typeof this.request.fields, 'object') test.strictEqual(this.request.fields.a, 'b') this.body = this.request.fields })
tryReadJson(undefined, function (err) { test.ok(/Unexpected/.test(err.message)) test.strictEqual(err.name, 'SyntaxError') })
server.use(function * () { test.strictEqual(this.body, undefined) test.strictEqual(this.request.fields, undefined) this.status = 204 })
test('should return error if JSON.parse throws', function (done) { var ret = tryReadJson('{boo":"bar') test.ok(/Unexpected token b/.test(ret.message)) test.strictEqual(ret.name, 'SyntaxError') done() })
.use(function * (next) { test.strictEqual(typeof this.request.body, 'string') test.strictEqual(this.request.body, 'message=lol') this.body = this.request.body yield * next })
test('should allow passing custom names', function (done) { var result = isAsyncFunction(fs.readFile, ['foo', 'bar']) test.strictEqual(result, false) done() })