-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathmain.js
More file actions
200 lines (148 loc) · 6.15 KB
/
main.js
File metadata and controls
200 lines (148 loc) · 6.15 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
var assert = require("assert");
var Sentencer = require('../index.js');
describe('Sentencer:', function() {
it('should exist', function() {
assert(Sentencer);
});
describe('Default', function() {
describe('# Words', function() {
it('should include a list of nouns', function() {
assert(Sentencer._nouns.length);
});
it('should include a list of adjectives', function() {
assert(Sentencer._adjectives.length);
});
});
describe('# Actions', function() {
it('should include `noun`', function() { assert(Sentencer.actions.noun); });
it('should include `a_noun`', function() { assert(Sentencer.actions.a_noun); });
it('should include `nouns`', function() { assert(Sentencer.actions.nouns); });
it('should include `adjective`', function() { assert(Sentencer.actions.adjective); });
it('should include `an_adjective`', function() { assert(Sentencer.actions.an_adjective); });
});
});
describe('API', function() {
it('should include a `configure` function', function() {
assert(Sentencer.configure);
});
it('should merge a new action', function() {
Sentencer.configure({
actions: {
firstNewAction: function() { return 'hello'; }
}
});
assert.equal(Sentencer.actions.firstNewAction(), 'hello');
});
it('should accept another action merge later', function() {
Sentencer.configure({
actions: {
secondNewAction: function() { return 'hello again'; }
}
});
assert.equal(Sentencer.actions.firstNewAction(), 'hello', 'first action still exists');
assert.equal(Sentencer.actions.secondNewAction(), 'hello again', 'second action exists as well');
});
it('should include a `make` function', function() {
assert(Sentencer.make);
});
it('should merge a new custom list', function() {
Sentencer.configure({
customLists: [
{
key: "animal",
values: ["dog", "cat", "elephant"],
articlize: "an_animal", // if named, add action that calls articlize
pluralize: "animals" // if named, add action that calls pluralize
}
]
});
assert(Sentencer.actions.animal);
assert(Sentencer.actions.an_animal);
assert(Sentencer.actions.animals);
assert.notEqual(["dog", "cat", "elephant"].indexOf(Sentencer.actions.animal()), -1, "missing animal");
assert.notEqual(["a dog", "a cat", "an elephant"].indexOf(Sentencer.actions.an_animal()), -1, "missing an_animal");
assert.notEqual(["dogs", "cats", "elephants"].indexOf(Sentencer.actions.animals()), -1, "missing animals");
});
});
describe('Templating', function() {
describe('# Default Actions', function() {
it('{{ noun }}', function() { assert(Sentencer.make('{{ noun }}')); });
it('{{ a_noun }}', function() { assert(Sentencer.make('{{ a_noun }}')); });
it('{{ nouns }}', function() { assert(Sentencer.make('{{ nouns }}')); });
it('{{ adjective }}', function() { assert(Sentencer.make('{{ adjective }}')); });
it('{{ an_adjective }}', function() { assert(Sentencer.make('{{ an_adjective }}')); });
});
describe('# Custom Actions', function() {
it('{{ firstNewAction }}', function() {
assert.equal(Sentencer.make('{{ firstNewAction }}'), 'hello');
});
it('{{ secondNewAction }}', function() {
assert.equal(Sentencer.make('{{ secondNewAction }}'), 'hello again');
});
it('should return {{ action }} if it does not exist', function() {
assert.equal(Sentencer.make('{{ nonexistant thing }}'), '{{ nonexistant thing }}');
});
});
describe('# Custom Actions With Arguments', function() {
Sentencer.configure({
actions: {
withArgument: function(number) {
return number;
},
withArguments: function() {
return arguments.length;
}
}
});
it('should allow an action with one argument', function() {
assert.equal( Sentencer.make('{{ withArgument(1) }}'), 1 );
});
it('should allow an action with multiple arguments', function() {
assert.equal( Sentencer.make('{{ withArguments(1,2,3) }}'), 3 );
});
it('should cast arguments as numbers when possible, otherwise strings', function() {
var result = null;
Sentencer.configure({
actions: {
test: function() {
result = Array.prototype.slice.call(arguments);
}
}
});
Sentencer.make('{{ test(1, hey hello, 2) }}');
assert.deepEqual(result, [1, 'hey hello', 2]);
});
it('should fail silently if an action with arguments does not exist', function() {
assert.deepEqual( Sentencer.make('{{ nonExistantThing(1,2,3) }}'), '' );
});
it('pass text through if someone tries to exploit eval', function() {
assert.deepEqual(
Sentencer.make('{{ nothing; console.log("This should not evaluate"); }}'),
'{{ nothing; console.log("This should not evaluate"); }}'
);
});
it('should pass text through when handed some garbage', function() {
assert.deepEqual(
Sentencer.make('{{ &@#&(%*@$UU#I$HTRIGUHW$@) }}'),
'{{ &@#&(%*@$UU#I$HTRIGUHW$@) }}'
);
});
});
describe('# Custom Lists', function() {
it('{{ animal }}', function() {
assert.notEqual(["dog", "cat", "elephant"].indexOf(Sentencer.make('{{ animal }}')), -1, "missing animal");
});
it('{{ an_animal }}', function() {
assert.notEqual(["a dog", "a cat", "an elephant"].indexOf(Sentencer.make('{{ an_animal }}')), -1, "missing an_animal");
});
it('{{ animals }}', function() {
assert.notEqual(["dogs", "cats", "elephants"].indexOf(Sentencer.make('{{ animals }}')), -1, "missing animals");
});
});
});
describe('Test Print', function() {
it('should have logged a sentence', function() {
console.log( Sentencer.make(" Here is {{ an_adjective }} sentence generated by Sentencer's {{ nouns }}.") );
});
});
});