This repository has been archived by the owner on Dec 22, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtest.js
159 lines (130 loc) · 5.15 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/* eslint-env mocha */
'use strict';
const chaiAsPromised = require('chai-as-promised');
const chai = require('chai').use(chaiAsPromised);
const expect = chai.expect;
const Plugin = require('.');
describe('Plugin', () => {
describe('with no options', () => {
const plugin = new Plugin({plugins: {}});
it('should not modify falsy file', () => {
const file1 = null;
const file2 = null;
return expect(plugin.compile(file1)).to.eventually.deep.equal(file2);
});
it('should not modify empty file', () => {
const file1 = {};
const file2 = {};
return expect(plugin.compile(file1)).to.eventually.deep.equal(file2);
});
it('should not modify file without data', () => {
const file1 = {test: 'test'};
const file2 = {test: 'test'};
return expect(plugin.compile(file1)).to.eventually.deep.equal(file2);
});
it('should not modify file with data', () => {
const file1 = {data: 'test'};
const file2 = {data: 'test'};
return expect(plugin.compile(file1)).to.eventually.deep.equal(file2);
});
});
describe('using string as key', () => {
const plugin = new Plugin({plugins: {replacer: {
dict: [{key: '__KEY__', value: '__VALUE__'}],
}}});
it('should not replace when key is absent', () => {
const file1 = {data: 'test'};
const file2 = {data: 'test'};
return expect(plugin.compile(file1)).to.eventually.deep.equal(file2);
});
it('should replace key with value', () => {
const file1 = {data: '__KEY__'};
const file2 = {data: '__VALUE__'};
return expect(plugin.compile(file1)).to.eventually.deep.equal(file2);
});
it('should not replace anything', () => {
const file1 = {data: '__KEY'};
const file2 = {data: '__KEY'};
return expect(plugin.compile(file1)).to.eventually.deep.equal(file2);
});
});
describe('using global regex as key', () => {
const plugin = new Plugin({plugins: {replacer: {
dict: [{key: /__KEY__/g, value: '__VALUE__'}],
}}});
it('should replace key with value', () => {
const file1 = {data: '__KEY__'};
const file2 = {data: '__VALUE__'};
return expect(plugin.compile(file1)).to.eventually.deep.equal(file2);
});
it('should replace multiple occurrences', () => {
const file1 = {data: '__KEY__ --- __KEY__'};
const file2 = {data: '__VALUE__ --- __VALUE__'};
return expect(plugin.compile(file1)).to.eventually.deep.equal(file2);
});
});
describe('using non-strings as value', () => {
describe('with unspecified replacement value', () => {
const plugin = new Plugin({plugins: {replacer: {
dict: [{key: /__KEY__/g}],
}}});
it('should replace key with empty string', () => {
const file1 = {data: '__KEY__'};
const file2 = {data: ''};
return expect(plugin.compile(file1)).to.eventually.deep.equal(file2);
});
});
describe('with object', () => {
const plugin = new Plugin({plugins: {replacer: {
dict: [{key: /__KEY__/g, value: {one: 1, two: 'two'}}],
}}});
it('should replace key with value', () => {
const file1 = {data: '__KEY__'};
const file2 = {data: '{"one":1,"two":"two"}'};
return expect(plugin.compile(file1)).to.eventually.deep.equal(file2);
});
});
describe('with Number', () => {
const plugin = new Plugin({plugins: {replacer: {
dict: [{key: /__KEY__/g, value: 123456}],
}}});
it('should replace key with value', () => {
const file1 = {data: '__KEY__'};
const file2 = {data: '123456'};
return expect(plugin.compile(file1)).to.eventually.deep.equal(file2);
});
});
describe('with null', () => {
const plugin = new Plugin({plugins: {replacer: {
dict: [{key: /__KEY__/g, value: null}],
}}});
it('should replace key with value', () => {
const file1 = {data: '__KEY__'};
const file2 = {data: 'null'};
return expect(plugin.compile(file1)).to.eventually.deep.equal(file2);
});
});
});
describe('using custom replace function', () => {
const plugin = new Plugin({plugins: {replacer: {
dict: [{key: '__KEY__', value: '__VALUE__'}],
replace: (str, key, value) => str.split(key).join(value),
}}});
it('should replace key with value', () => {
const file1 = {data: 'The quick brown __KEY__ jumps over the lazy __KEY__'};
const file2 = {data: 'The quick brown __VALUE__ jumps over the lazy __VALUE__'};
return expect(plugin.compile(file1)).to.eventually.deep.equal(file2);
});
});
describe('using custom replace function to add path', () => {
const plugin = new Plugin({plugins: {replacer: {
dict: [{key: '__KEY__', value: '__VALUE__'}],
replace: (str, key, value, path) => str.split(key).join(path),
}}});
it('should replace key with path', () => {
const file1 = {path: 'hello.js', data: 'The quick brown __KEY__ jumps over the lazy __KEY__'};
const file2 = {path: 'hello.js', data: 'The quick brown hello.js jumps over the lazy hello.js'};
return expect(plugin.compile(file1)).to.eventually.deep.equal(file2);
});
});
});