62115fc4c0e61717b69815c4834197b6ff6c0bf9.svn-base
2.77 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
'use strict'
/* eslint-disable no-template-curly-in-string */
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 optsi = inspect(opts)
var result = redeyed(code, opts).code
this.equals(result
, expected
, util.format('%s: %s => %s', optsi, inspect(code), inspect(expected))
)
return this
}
t.end()
})
test('types', function(t) {
t.test('\n# Boolean', function(t) {
t.assertSurrounds('return true;', { 'Boolean': { _default: '$:%' } }, 'return $true%;')
t.assertSurrounds('return true; return false;'
, { 'Boolean': { 'false': '#:', _default: '$:%' } }
, 'return $true%; return #false%;')
t.end()
})
t.test('\n# Identifier', function(t) {
t.assertSurrounds('var a = 1;', { 'Identifier': { _default: '$:%' } }, 'var $a% = 1;')
t.assertSurrounds('var a = 1; const b = 2;'
, { 'Identifier': { 'b': '#:', _default: '$:%' } }
, 'var $a% = 1; const #b% = 2;')
t.end()
})
t.test('\n# Null', function(t) {
t.assertSurrounds('return null;', { 'Null': { _default: '$:%' } }, 'return $null%;').end()
})
t.test('\n# Numeric', function(t) {
t.assertSurrounds('return 1;', { 'Numeric': { _default: '$:%' } }, 'return $1%;')
t.assertSurrounds('return 1; return 2;'
, { 'Numeric': { '2': '#:', _default: '$:%' } }
, 'return $1%; return #2%;')
t.end()
})
t.test('\n# Punctuator', function(t) {
var punctuator = { 'Punctuator': { _default: '$:%' } }
t.assertSurrounds('return 2 * 2;', punctuator, 'return 2 $*% 2$;%')
t.assertSurrounds('return 2 * 2;'
, { 'Punctuator': { '*': '#:', _default: '$:%' } }
, 'return 2 #*% 2$;%')
t.assertSurrounds('var {op, lhs, rhs} = getASTNode()', punctuator, 'var ${%op$,% lhs$,% rhs$}% $=% getASTNode$(%$)%')
t.assertSurrounds('function f(x, y=12) { return x + y;}', punctuator, 'function f$(%x$,% y$=%12$)% ${% return x $+% y$;%$}%')
t.assertSurrounds('function f(x, ...y) { return x * y.length;}', punctuator, 'function f$(%x$,% $...%y$)% ${% return x $*% y$.%length$;%$}%')
t.end()
})
t.test('\n# String', function(t) {
t.assertSurrounds('return "hello";', { 'String': { _default: '$:%' } }, 'return $"hello"%;')
t.assertSurrounds('return "hello"; return "world";'
, { 'String': { '"world"': '#:', _default: '$:%' } }
, 'return $"hello"%; return #"world"%;')
t.end()
})
t.end()
})