-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathinflector-test.js
72 lines (55 loc) · 2.04 KB
/
inflector-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
var vows = require('vows'),
assert = require('assert');
var sys = require('sys');
var format = require('../index');
var inflector = format.toFu;
//to make porting from rails easier
var eachPair = function(fn){
for(i in this){
fn(i, this[i]);
};
};
//load test cases
var cases = require('./inflections');
var myVows = {};
//from and two are switched, as toSingle's test data is the inverse of toPlural
myVows["toSingle()"] = {};
var railsSingularizations = myVows["toSingle()"]["run through test cases"] = {
topc: "rails singularization cases"
};
eachPair.call(cases.SingularToPlural, function(singular, plural){
railsSingularizations["toSingle "+ plural] = function(){
var result = inflector.toSingle(plural);
var eql = !!(singular === result);
assert.ok(eql, plural + " should toSingle to: " + singular+ ", instead it was:"+result);
};
});
//generic form!
function testSuiteBuilder(kind, collections){
myVows[kind+"()"] = {};
myVows[kind+"()"]["run through test cases"] = {
topc: "rails "+kind+" cases"
};
collections.forEach(function(collection){
eachPair.call(collection, function(from, to){
myVows[kind+"()"]["run through test cases"][kind+" "+ from] = function(){
var result = inflector[kind](from);
var eql = !!(to === result);
assert.ok(eql, from + " should "+kind+"() to: " + to+ ", instead it was:"+result);
};
});
})
}
testSuiteBuilder("toPlural", [cases.SingularToPlural, cases.Irregularities]);
testSuiteBuilder("toTitle", [cases.MixtureToTitleCase]);
testSuiteBuilder("toCamel", [cases.UnderscoreToCamel]);
testSuiteBuilder("toUnderscore", [
cases.CamelToUnderscore,
cases.CamelToUnderscoreWithoutReverse,
cases.CamelWithModuleToUnderscoreWithSlash
]);
testSuiteBuilder("toHuman", [cases.UnderscoreToHuman]);
testSuiteBuilder("toDash", [cases.UnderscoresToDashes]);
testSuiteBuilder("toOrdinal", [cases.OrdinalNumbers]);
testSuiteBuilder("toParam", [cases.StringToParameterized])
vows.describe('format.js lib/inflector').addBatch(myVows).export(module);