4e82edd935abb3035b7ab243cf9e46da7b7132f8.svn-base
4.12 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
'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('\nfunction config, keywords', function(t) {
var opts001 = { Keyword: { _default: function(s) { return '*' + s + '&' } } }
t.test('\n# ' + inspect(opts001), function(t) {
t.assertSurrounds('this', opts001, '*this&')
t.assertSurrounds('this ', opts001, '*this& ')
t.assertSurrounds(' this', opts001, ' *this&')
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.assertSurrounds(
[ 'function foo (bar) {'
, ' var a = 3;'
, ' return bar + a;'
, '}'
].join('\n')
, opts001
, [ '*function& foo (bar) {'
, ' *var& a = 3;'
, ' *return& bar + a;'
, '}'
].join('\n'))
t.end()
})
var opts002 = {
Keyword: {
'function': function(s) { return '^' + s + '&' }
, 'return': function(s) { return '(' + s + ')' }
, _default: function(s) { return '*' + s + '&' }
}
}
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('#\n functin config - resolving', function(t) {
var opts001 = {
Keyword: {
'var': function(s) { return '^' + s + '&' }
}
, _default: function(s) { return '*' + s + '&' }
}
t.test('\n# specific but no type default and root default - root default not applied' + inspect(opts001), function(t) {
t.assertSurrounds('var n = new Test();', opts001, '^var& n = new Test();').end()
})
var opts002 = {
Keyword: {
'var': function(s) { return '^' + s + '&' }
, _default: function(s) { return '*' + s + '&' }
}
, _default: function(s) { return '(' + s + ')' }
}
t.test('\n# no type default but root default' + inspect(opts002), function(t) {
t.assertSurrounds('var n = new Test();', opts002, '^var& n = *new& Test();').end()
})
t.end()
})
test('#\n function config - replacing', function(t) {
var opts001 = {
Keyword: {
'var': function() { return 'const' }
}
}
t.test('\n# type default and root default (type wins)' + inspect(opts001), function(t) {
t.assertSurrounds('var n = new Test();', opts001, 'const n = new Test();').end()
})
var opts002 = {
Keyword: {
_default: function() { return 'const' }
}
}
t.test('\n# type default' + inspect(opts002), function(t) {
t.assertSurrounds('var n = new Test();', opts002, 'const n = const Test();').end()
})
var opts003 = {
Keyword: {
'new': function() { return 'NEW' }
, _default: function() { return 'const' }
}
}
t.test('\n# specific and type default' + inspect(opts003), function(t) {
t.assertSurrounds('var n = new Test();', opts003, 'const n = NEW Test();').end()
})
var opts004 = {
Keyword: {
_default: function(s) { return s.toUpperCase() }
}
, _default: function(s) { return 'not applied' }
}
t.test('\n# type default and root default (type wins)' + inspect(opts004), function(t) {
t.assertSurrounds('var n = new Test();', opts004, 'VAR n = NEW Test();').end()
})
var opts005 = {
Keyword: { }
, _default: function(s) { return s.toUpperCase() }
}
t.test('\n# no type default only root default - not applied' + inspect(opts005), function(t) {
t.assertSurrounds('var n = new Test();', opts005, 'var n = new Test();').end()
})
t.end()
})