12cc2e871d062f42e3c3fa858964456a24e4fc92.svn-base
4.13 KB
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
'use strict'
var test = require('tape')
var util = require('util')
var redeyed = require('..')
function inspect(obj) {
return util.inspect(obj, false, 5, true)
}
test('adding custom asserts ... ', function(t) {
t.constructor.prototype.assertSurrounds = function(code, opts, expected) {
var result = redeyed(code, opts).code
this.equals(result, expected, inspect(code) + ' => ' + inspect(expected))
return this
}
t.end()
})
test('\nstring config, keywords', function(t) {
var opts001 = { Keyword: { _default: '*:&' } }
t.test('\n# ' + inspect(opts001), function(t) {
t.assertSurrounds('this', opts001, '*this&')
t.assertSurrounds('if (a == 1) return', opts001, '*if& (a == 1) *return&')
t.assertSurrounds('var n = new Test();', opts001, '*var& n = *new& Test();')
t.end()
})
var opts002 = {
Keyword: {
'function': '^:'
, 'return': '(:)'
, _default: '*:&'
}
}
t.test('\n# ' + inspect(opts002), function(t) {
t.assertSurrounds(
[ 'function foo (bar) {'
, ' var a = 3;'
, ' return bar + a;'
, '}'
].join('\n')
, opts002
, [ '^function& foo (bar) {'
, ' *var& a = 3;'
, ' (return) bar + a;'
, '}'
].join('\n'))
t.end()
})
t.end()
})
test('\nstring configs resolve from type and root', function(t) {
var code = 'var a = new Test();'
function run(t, conf, expected, code_) {
t.test('\n# ' + inspect(conf), function(t) {
t.assertSurrounds(code_ || code, conf, expected)
t.end()
})
}
// at least the token kind has to be configured in order for the root_default to be applied
// otherwise a root._default would affect all tokens, even the ones we want to leave unchanged
run(t, { _default: '*:' }, 'var a = new Test();')
t.test('\n\n# only before or after specified, but no root._default', function(t) {
run(t, { Keyword: { _default: '*:' } }, '*var a = *new Test();')
run(t, { Keyword: { _default: ':-' } }, 'var- a = new- Test();')
t.end()
})
t.test('\n\n# resolve missing from root._default', function(t) {
run(t, { Keyword: { _default: '*:' }, _default: '(:-' }, '*var- a = *new- Test();')
run(t, { Keyword: { _default: ':-' }, _default: '*:)' }, '*var- a = *new- Test();')
t.end()
})
t.test('\n\n# no resolve if all specified', function(t) {
run(t, { Keyword: { _default: '+:-' }, _default: '*:)' }, '+var- a = +new- Test();')
run(t, { Keyword: { _default: ':-' }, _default: ':)' }, 'var- a = new- Test();')
t.end()
})
t.test('\n\n# resolve specific token no defaults', function(t) {
run(t, { Keyword: { 'var': '*:' } }, '*var a = new Test();')
run(t, { Keyword: { 'var': ':-' } }, 'var- a = new Test();')
t.end()
})
t.test('\n\n# resolve specific token with type defaults', function(t) {
run(t, { Keyword: { 'var': '*:', _default: ':-' } }, '*var- a = new- Test();')
run(t, { Keyword: { 'var': '*:', _default: '(:-' } }, '*var- a = (new- Test();')
run(t, { Keyword: { 'var': ':-', _default: '*:' } }, '*var- a = *new Test();')
run(t, { Keyword: { 'var': ':-', _default: '*:)' } }, '*var- a = *new) Test();')
run(t, { Keyword: { 'var': ':-', 'new': ':&', _default: '*:' } }, '*var- a = *new& Test();')
t.end()
})
t.test(
'\n\n# resolve specific token with root defaults, but no type default - root default not applied to unspecified tokens'
, function(t) {
run(t, { Keyword: { 'var': '*:' }, _default: ':-' }, '*var- a = new Test();')
run(t, { Keyword: { 'var': ':-' }, _default: '*:' }, '*var- a = new Test();')
t.end()
}
)
t.test('\n\n# resolve specific token with type and root defaults', function(t) {
run(t, { Keyword: { 'var': '*:', _default: '+:-' }, _default: ':)' }, '*var- a = +new- Test();')
run(t, { Keyword: { 'var': ':-', _default: '*:+' }, _default: '(:' }, '*var- a = *new+ Test();')
t.end()
})
t.test('all exact tokens undefined, but type default', function(t) {
run(t, { 'Boolean': { 'true': undefined, 'false': undefined, _default: '+:-' } }, 'return +true- || +false-;', 'return true || false;')
t.end()
})
t.end()
})