Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80551 views
1
/**
2
* Copyright 2013 Facebook, Inc.
3
*
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
7
*
8
* http://www.apache.org/licenses/LICENSE-2.0
9
*
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
15
*/
16
17
/**
18
* @emails [email protected]
19
*/
20
21
/*jshint evil:true*/
22
23
require('mock-modules').autoMockOff();
24
25
describe('es6-object-short-notation-visitors', function() {
26
var transformFn;
27
28
var destructuringVisitors;
29
var shortObjectsVisitors;
30
31
var visitors;
32
33
beforeEach(function() {
34
require('mock-modules').dumpCache();
35
transformFn = require('../../src/jstransform').transform;
36
37
shortObjectsVisitors = require('../es6-object-short-notation-visitors').visitorList;
38
destructuringVisitors = require('../es6-destructuring-visitors').visitorList;
39
40
visitors = shortObjectsVisitors.concat(destructuringVisitors);
41
});
42
43
function transform(code) {
44
return transformFn(visitors, code).code;
45
}
46
47
function expectTransform(code, result) {
48
expect(transform(code)).toEqual(result);
49
}
50
51
// Functional tests.
52
53
it('should transform short notation and return 5', function() {
54
var code = transform([
55
'(function(x, y) {',
56
' var data = {x, y};',
57
' return data.x + data.y;',
58
'})(2, 3);'
59
].join('\n'));
60
61
expect(eval(code)).toEqual(5);
62
});
63
64
it('should transform work with destructuring and return 10', function() {
65
var code = transform([
66
'var x = 5, y = 5;',
67
'(function({x, y}) {',
68
' var data = {x, y};',
69
' return data.x + data.y;',
70
'})({x, y});'
71
].join('\n'));
72
73
expect(eval(code)).toEqual(10);
74
});
75
76
// Source code tests.
77
it('should transform simple short notation', function() {
78
79
// Should transform simple short notation.
80
expectTransform(
81
'function foo(x, y) { return {x, y}; }',
82
'function foo(x, y) { return {x:x, y:y}; }'
83
);
84
85
// Should preserve lines transforming ugly code.
86
expectTransform([
87
'function',
88
'',
89
'foo (',
90
' x,',
91
' y',
92
'',
93
')',
94
'',
95
' {',
96
' return {',
97
' x,',
98
' y};',
99
'}'
100
].join('\n'), [
101
'function',
102
'',
103
'foo (',
104
' x,',
105
' y',
106
'',
107
')',
108
'',
109
' {',
110
' return {',
111
' x:x,',
112
' y:y};',
113
'}'
114
].join('\n'));
115
});
116
117
});
118
119
120
121