2016-10-05 03:38:12 +02:00
|
|
|
global.stub_out_jquery();
|
|
|
|
|
2013-08-21 20:27:14 +02:00
|
|
|
add_dependencies({
|
|
|
|
util: 'js/util.js'
|
|
|
|
});
|
|
|
|
|
2013-08-08 22:04:41 +02:00
|
|
|
var util = global.util;
|
2014-03-13 16:39:29 +01:00
|
|
|
var _ = global._;
|
2013-08-08 22:04:41 +02:00
|
|
|
|
|
|
|
(function test_CachedValue() {
|
|
|
|
var x = 5;
|
|
|
|
|
|
|
|
var cv = new util.CachedValue({
|
|
|
|
compute_value: function () {
|
|
|
|
return x * 2;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
assert.equal(cv.get(), 10);
|
|
|
|
|
|
|
|
x = 6;
|
|
|
|
assert.equal(cv.get(), 10);
|
|
|
|
cv.reset();
|
|
|
|
assert.equal(cv.get(), 12);
|
|
|
|
|
|
|
|
}());
|
|
|
|
|
2016-11-15 06:40:19 +01:00
|
|
|
(function test_extract_pm_recipients() {
|
|
|
|
assert.equal(util.extract_pm_recipients('bob@foo.com, alice@foo.com').length, 2);
|
|
|
|
assert.equal(util.extract_pm_recipients('bob@foo.com, ').length, 1);
|
|
|
|
}());
|
|
|
|
|
2013-08-08 22:04:41 +02:00
|
|
|
(function test_lower_bound() {
|
|
|
|
var arr = [10, 20, 30, 40, 50];
|
|
|
|
assert.equal(util.lower_bound(arr, 5), 0);
|
|
|
|
assert.equal(util.lower_bound(arr, 10), 0);
|
|
|
|
assert.equal(util.lower_bound(arr, 15), 1);
|
|
|
|
assert.equal(util.lower_bound(arr, 50), 4);
|
|
|
|
assert.equal(util.lower_bound(arr, 55), 5);
|
|
|
|
assert.equal(util.lower_bound(arr, 2, 4, 31), 3);
|
|
|
|
|
|
|
|
arr = [{x: 10}, {x: 20}, {x:30}];
|
|
|
|
|
|
|
|
function compare (a, b) {
|
|
|
|
return a.x < b;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.equal(util.lower_bound(arr, 5, compare), 0);
|
|
|
|
assert.equal(util.lower_bound(arr, 10, compare), 0);
|
|
|
|
assert.equal(util.lower_bound(arr, 15, compare), 1);
|
|
|
|
|
|
|
|
}());
|
|
|
|
|
|
|
|
(function test_same_recipient() {
|
2014-03-13 16:39:29 +01:00
|
|
|
_.each([util.same_recipient, util.same_major_recipient], function (same) {
|
|
|
|
assert(same(
|
|
|
|
{type: 'stream', stream: 'Foo', subject: 'Bar'},
|
|
|
|
{type: 'stream', stream: 'fOO', subject: 'bar'}
|
|
|
|
));
|
|
|
|
|
|
|
|
assert(!same(
|
|
|
|
{type: 'stream', stream: 'Foo', subject: 'Bar'},
|
|
|
|
{type: 'stream', stream: 'yo', subject: 'whatever'}
|
|
|
|
));
|
|
|
|
|
|
|
|
assert(same(
|
|
|
|
{type: 'private', reply_to: 'fred@zulip.com,melissa@zulip.com'},
|
|
|
|
{type: 'private', reply_to: 'fred@zulip.com,melissa@zulip.com'}
|
|
|
|
));
|
|
|
|
|
2016-03-26 13:16:30 +01:00
|
|
|
assert(same(
|
2014-03-13 16:39:29 +01:00
|
|
|
{type: 'private', reply_to: 'fred@zulip.com'},
|
|
|
|
{type: 'private', reply_to: 'Fred@zulip.com'}
|
|
|
|
));
|
|
|
|
|
|
|
|
assert(!same(
|
|
|
|
{type: 'stream', stream: 'Foo', subject: 'Bar'},
|
|
|
|
{type: 'private', reply_to: 'Fred@zulip.com'}
|
|
|
|
));
|
|
|
|
});
|
2013-08-08 22:04:41 +02:00
|
|
|
|
2014-03-13 16:39:29 +01:00
|
|
|
assert(util.same_major_recipient(
|
|
|
|
{type: 'stream', stream: 'Foo', subject: 'sub1'},
|
|
|
|
{type: 'stream', stream: 'fOO', subject: 'sub2'}
|
2013-08-08 22:04:41 +02:00
|
|
|
));
|
|
|
|
|
|
|
|
assert(!util.same_recipient(
|
2014-03-13 16:39:29 +01:00
|
|
|
{type: 'stream', stream: 'Foo', subject: 'sub1'},
|
|
|
|
{type: 'stream', stream: 'fOO', subject: 'sub2'}
|
2013-08-08 22:04:41 +02:00
|
|
|
));
|
2014-03-13 16:39:29 +01:00
|
|
|
|
2013-08-08 22:04:41 +02:00
|
|
|
}());
|
|
|
|
|
|
|
|
(function test_robust_uri_decode() {
|
|
|
|
assert.equal(util.robust_uri_decode('xxx%3Ayyy'), 'xxx:yyy');
|
|
|
|
assert.equal(util.robust_uri_decode('xxx%3'), 'xxx');
|
|
|
|
}());
|
|
|
|
|
|
|
|
|
|
|
|
(function test_array_compare() {
|
|
|
|
assert(util.array_compare([], []));
|
|
|
|
assert(util.array_compare([1,2,3], [1,2,3]));
|
|
|
|
assert(!util.array_compare([1,2], [1,2,3]));
|
|
|
|
assert(!util.array_compare([1,2,3], [1,2]));
|
2014-03-13 17:01:42 +01:00
|
|
|
assert(!util.array_compare([1,2,3,4], [1,2,3,5]));
|
2013-08-08 22:04:41 +02:00
|
|
|
}());
|
|
|
|
|
2014-03-13 16:55:22 +01:00
|
|
|
(function test_normalize_recipients() {
|
2016-11-19 00:33:32 +01:00
|
|
|
assert.equal(
|
|
|
|
util.normalize_recipients('ZOE@foo.com, bob@foo.com, alice@foo.com, AARON@foo.com '),
|
|
|
|
'aaron@foo.com,alice@foo.com,bob@foo.com,zoe@foo.com'
|
|
|
|
);
|
2014-03-13 16:55:22 +01:00
|
|
|
}());
|
|
|
|
|
2014-03-13 17:28:50 +01:00
|
|
|
(function test_random_int() {
|
|
|
|
var min = 0;
|
|
|
|
var max = 100;
|
|
|
|
|
|
|
|
_.times(500, function () {
|
|
|
|
var val = util.random_int(min, max);
|
|
|
|
assert(min <= val);
|
|
|
|
assert(val <= max);
|
|
|
|
assert.equal(val, Math.floor(val));
|
|
|
|
});
|
|
|
|
}());
|
|
|
|
|
2013-08-08 20:22:33 +02:00
|
|
|
(function test_enforce_arity() {
|
|
|
|
function f1() {}
|
|
|
|
var eaf1 = util.enforce_arity(f1);
|
|
|
|
|
|
|
|
assert.doesNotThrow(function () { f1('foo'); });
|
|
|
|
assert.doesNotThrow(function () { eaf1(); });
|
|
|
|
assert.throws(function () { eaf1('foo'); }, /called with \d+ arguments/);
|
|
|
|
assert.throws(function () { eaf1('foo', 'bar'); }, /called with \d+ arguments/);
|
|
|
|
|
|
|
|
function f2(x) {}
|
|
|
|
var eaf2 = util.enforce_arity(f2);
|
|
|
|
|
|
|
|
assert.doesNotThrow(function () { f2(); });
|
|
|
|
assert.doesNotThrow(function () { eaf2('foo'); });
|
|
|
|
assert.throws(function () { eaf2(); }, /called with \d+ arguments/);
|
|
|
|
assert.throws(function () { eaf2('foo', 'bar'); }, /called with \d+ arguments/);
|
|
|
|
|
|
|
|
function f3(x, y) {}
|
|
|
|
var eaf3 = util.enforce_arity(f3);
|
|
|
|
|
|
|
|
assert.doesNotThrow(function () { f3(); });
|
|
|
|
assert.doesNotThrow(function () { eaf3('foo', 'bar'); });
|
|
|
|
assert.throws(function () { eaf3(); }, /called with \d+ arguments/);
|
|
|
|
assert.throws(function () { eaf3('foo'); }, /called with \d+ arguments/);
|
|
|
|
assert.throws(function () { eaf3('foo','bar', 'baz'); }, /called with \d+ arguments/);
|
|
|
|
}());
|
2016-08-15 22:24:40 +02:00
|
|
|
|
|
|
|
(function test_all_and_everyone_mentions_regexp() {
|
|
|
|
var messages_with_all_mentions = [
|
|
|
|
'@all',
|
|
|
|
'some text before @all some text after',
|
|
|
|
'@all some text after only',
|
|
|
|
'some text before only @all',
|
|
|
|
'@**all**',
|
|
|
|
'some text before @**all** some text after',
|
|
|
|
'@**all** some text after only',
|
|
|
|
'some text before only @**all**'
|
|
|
|
];
|
|
|
|
|
|
|
|
var messages_with_everyone_mentions = [
|
|
|
|
'@everyone',
|
|
|
|
'some text before @everyone some text after',
|
|
|
|
'@everyone some text after only',
|
|
|
|
'some text before only @everyone',
|
|
|
|
'@**everyone**',
|
|
|
|
'some text before @**everyone** some text after',
|
|
|
|
'@**everyone** some text after only',
|
|
|
|
'some text before only @**everyone**'
|
|
|
|
];
|
|
|
|
|
|
|
|
var messages_without_all_mentions = [
|
|
|
|
'`@everyone`',
|
|
|
|
'some_email@everyone.com',
|
|
|
|
'`@**everyone**`',
|
|
|
|
'some_email@**everyone**.com'
|
|
|
|
];
|
|
|
|
|
|
|
|
var messages_without_everyone_mentions = [
|
|
|
|
'`@everyone`',
|
|
|
|
'some_email@everyone.com',
|
|
|
|
'`@**everyone**`',
|
|
|
|
'some_email@**everyone**.com'
|
|
|
|
];
|
|
|
|
var i;
|
2016-11-30 19:05:04 +01:00
|
|
|
for (i=0; i<messages_with_all_mentions.length; i += 1) {
|
2016-08-15 22:24:40 +02:00
|
|
|
assert(util.is_all_or_everyone_mentioned(messages_with_all_mentions[i]));
|
|
|
|
}
|
|
|
|
|
2016-11-30 19:05:04 +01:00
|
|
|
for (i=0; i<messages_with_everyone_mentions.length; i += 1) {
|
2016-08-15 22:24:40 +02:00
|
|
|
assert(util.is_all_or_everyone_mentioned(messages_with_everyone_mentions[i]));
|
|
|
|
}
|
|
|
|
|
2016-11-30 19:05:04 +01:00
|
|
|
for (i=0; i<messages_without_all_mentions.length; i += 1) {
|
2016-08-15 22:24:40 +02:00
|
|
|
assert(!util.is_all_or_everyone_mentioned(messages_without_everyone_mentions[i]));
|
|
|
|
}
|
|
|
|
|
2016-11-30 19:05:04 +01:00
|
|
|
for (i=0; i<messages_without_everyone_mentions.length; i += 1) {
|
2016-08-15 22:24:40 +02:00
|
|
|
assert(!util.is_all_or_everyone_mentioned(messages_without_everyone_mentions[i]));
|
|
|
|
}
|
|
|
|
}());
|