Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80684 views
1
$(document).ready(function() {
2
3
module("Utility");
4
5
test("utility: noConflict", function() {
6
var underscore = _.noConflict();
7
ok(underscore.isUndefined(_), "The '_' variable has been returned to its previous state.");
8
var intersection = underscore.intersect([-1, 0, 1, 2], [1, 2, 3, 4]);
9
equals(intersection.join(', '), '1, 2', 'but the intersection function still works');
10
window._ = underscore;
11
});
12
13
test("utility: identity", function() {
14
var moe = {name : 'moe'};
15
equals(_.identity(moe), moe, 'moe is the same as his identity');
16
});
17
18
test("utility: uniqueId", function() {
19
var ids = [], i = 0;
20
while(i++ < 100) ids.push(_.uniqueId());
21
equals(_.uniq(ids).length, ids.length, 'can generate a globally-unique stream of ids');
22
});
23
24
test("utility: times", function() {
25
var vals = [];
26
_.times(3, function (i) { vals.push(i); });
27
ok(_.isEqual(vals, [0,1,2]), "is 0 indexed");
28
//
29
vals = [];
30
_(3).times(function (i) { vals.push(i); });
31
ok(_.isEqual(vals, [0,1,2]), "works as a wrapper");
32
});
33
34
test("utility: mixin", function() {
35
_.mixin({
36
myReverse: function(string) {
37
return string.split('').reverse().join('');
38
}
39
});
40
equals(_.myReverse('panacea'), 'aecanap', 'mixed in a function to _');
41
equals(_('champ').myReverse(), 'pmahc', 'mixed in a function to the OOP wrapper');
42
});
43
44
test("utility: _.escape", function() {
45
equals(_.escape("Curly & Moe"), "Curly &amp; Moe");
46
equals(_.escape("Curly &amp; Moe"), "Curly &amp;amp; Moe");
47
});
48
49
test("utility: template", function() {
50
var basicTemplate = _.template("<%= thing %> is gettin' on my noives!");
51
var result = basicTemplate({thing : 'This'});
52
equals(result, "This is gettin' on my noives!", 'can do basic attribute interpolation');
53
54
var backslashTemplate = _.template("<%= thing %> is \\ridanculous");
55
equals(backslashTemplate({thing: 'This'}), "This is \\ridanculous");
56
57
var fancyTemplate = _.template("<ul><% \
58
for (key in people) { \
59
%><li><%= people[key] %></li><% } %></ul>");
60
result = fancyTemplate({people : {moe : "Moe", larry : "Larry", curly : "Curly"}});
61
equals(result, "<ul><li>Moe</li><li>Larry</li><li>Curly</li></ul>", 'can run arbitrary javascript in templates');
62
63
var namespaceCollisionTemplate = _.template("<%= pageCount %> <%= thumbnails[pageCount] %> <% _.each(thumbnails, function(p) { %><div class=\"thumbnail\" rel=\"<%= p %>\"></div><% }); %>");
64
result = namespaceCollisionTemplate({
65
pageCount: 3,
66
thumbnails: {
67
1: "p1-thumbnail.gif",
68
2: "p2-thumbnail.gif",
69
3: "p3-thumbnail.gif"
70
}
71
});
72
equals(result, "3 p3-thumbnail.gif <div class=\"thumbnail\" rel=\"p1-thumbnail.gif\"></div><div class=\"thumbnail\" rel=\"p2-thumbnail.gif\"></div><div class=\"thumbnail\" rel=\"p3-thumbnail.gif\"></div>");
73
74
var noInterpolateTemplate = _.template("<div><p>Just some text. Hey, I know this is silly but it aids consistency.</p></div>");
75
result = noInterpolateTemplate();
76
equals(result, "<div><p>Just some text. Hey, I know this is silly but it aids consistency.</p></div>");
77
78
var quoteTemplate = _.template("It's its, not it's");
79
equals(quoteTemplate({}), "It's its, not it's");
80
81
var quoteInStatementAndBody = _.template("<%\
82
if(foo == 'bar'){ \
83
%>Statement quotes and 'quotes'.<% } %>");
84
equals(quoteInStatementAndBody({foo: "bar"}), "Statement quotes and 'quotes'.");
85
86
var withNewlinesAndTabs = _.template('This\n\t\tis: <%= x %>.\n\tok.\nend.');
87
equals(withNewlinesAndTabs({x: 'that'}), 'This\n\t\tis: that.\n\tok.\nend.');
88
89
var template = _.template("<i><%- value %></i>");
90
var result = template({value: "<script>"});
91
equals(result, '<i>&lt;script&gt;</i>');
92
93
if (!$.browser.msie) {
94
var fromHTML = _.template($('#template').html());
95
equals(fromHTML({data : 12345}).replace(/\s/g, ''), '<li>24690</li>');
96
}
97
98
_.templateSettings = {
99
evaluate : /\{\{([\s\S]+?)\}\}/g,
100
interpolate : /\{\{=([\s\S]+?)\}\}/g
101
};
102
103
var custom = _.template("<ul>{{ for (key in people) { }}<li>{{= people[key] }}</li>{{ } }}</ul>");
104
result = custom({people : {moe : "Moe", larry : "Larry", curly : "Curly"}});
105
equals(result, "<ul><li>Moe</li><li>Larry</li><li>Curly</li></ul>", 'can run arbitrary javascript in templates');
106
107
var customQuote = _.template("It's its, not it's");
108
equals(customQuote({}), "It's its, not it's");
109
110
var quoteInStatementAndBody = _.template("{{ if(foo == 'bar'){ }}Statement quotes and 'quotes'.{{ } }}");
111
equals(quoteInStatementAndBody({foo: "bar"}), "Statement quotes and 'quotes'.");
112
113
_.templateSettings = {
114
evaluate : /<\?([\s\S]+?)\?>/g,
115
interpolate : /<\?=([\s\S]+?)\?>/g
116
};
117
118
var customWithSpecialChars = _.template("<ul><? for (key in people) { ?><li><?= people[key] ?></li><? } ?></ul>");
119
result = customWithSpecialChars({people : {moe : "Moe", larry : "Larry", curly : "Curly"}});
120
equals(result, "<ul><li>Moe</li><li>Larry</li><li>Curly</li></ul>", 'can run arbitrary javascript in templates');
121
122
var customWithSpecialCharsQuote = _.template("It's its, not it's");
123
equals(customWithSpecialCharsQuote({}), "It's its, not it's");
124
125
var quoteInStatementAndBody = _.template("<? if(foo == 'bar'){ ?>Statement quotes and 'quotes'.<? } ?>");
126
equals(quoteInStatementAndBody({foo: "bar"}), "Statement quotes and 'quotes'.");
127
128
_.templateSettings = {
129
interpolate : /\{\{(.+?)\}\}/g
130
};
131
132
var mustache = _.template("Hello {{planet}}!");
133
equals(mustache({planet : "World"}), "Hello World!", "can mimic mustache.js");
134
});
135
136
});
137
138