-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
143 lines (123 loc) · 3.63 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*!
* limon <https://github.com/limonjs/limon>
*
* Copyright (c) 2016 Charlike Mike Reagent <@tunnckoCore> (http://www.tunnckocore.tk)
* Released under the MIT license.
*/
'use strict'
var test = require('mukla')
var Limon = require('./index').Limon
var limon = require('./index')
var isArray = require('isarray')
test('should exposed constructor be signleton', function (done) {
test.strictEqual(typeof Limon === 'function', true)
test.strictEqual(typeof Limon() === 'object', true)
test.strictEqual(typeof (new Limon()) === 'object', true)
done()
})
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 throw TypeError on missing input', function (done) {
function fixture () {
limon.tokenize(123)
}
test.throws(fixture, TypeError)
test.throws(fixture, /expect `input` be non-empty string/)
done()
})
test('should throw TypeError if not function passed to `.use` method', function (done) {
function fixture () {
limon.use(123)
}
test.throws(fixture, TypeError)
test.throws(fixture, /expect `fn` be function/)
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 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 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()
})
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 extend options passed to `.tokenize` with that from constructor', function (done) {
var limon = new Limon({foo: 'bar'})
limon.tokenize('a.b.c', {qux: 123})
test.deepEqual(limon.options, { foo: 'bar', qux: 123 })
done()
})
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()
})
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()
})