2017-01-28 03:03:37 +08:00
|
|
|
/**
|
2022-03-10 20:56:34 +08:00
|
|
|
* Created by Tivie on 27/01/2017.
|
2017-01-28 03:03:37 +08:00
|
|
|
*/
|
2022-03-27 08:05:30 +08:00
|
|
|
chai.should();
|
2017-01-28 09:19:07 +08:00
|
|
|
/*jshint expr: true*/
|
|
|
|
/*jshint -W053 */
|
|
|
|
/*jshint -W010 */
|
|
|
|
/*jshint -W009 */
|
2017-01-28 03:03:37 +08:00
|
|
|
|
2017-01-28 06:07:55 +08:00
|
|
|
describe('encodeEmailAddress()', function () {
|
2017-01-28 03:03:37 +08:00
|
|
|
'use strict';
|
2022-03-27 08:05:30 +08:00
|
|
|
let encoder = showdown.helper.encodeEmailAddress,
|
2017-01-28 06:07:55 +08:00
|
|
|
email = 'foobar@example.com',
|
2022-03-26 08:31:41 +08:00
|
|
|
encodedEmail = encoder(email),
|
|
|
|
encodedEmail2 = encoder(email);
|
2017-01-28 03:03:37 +08:00
|
|
|
|
|
|
|
it('should encode email', function () {
|
|
|
|
encodedEmail.should.not.equal(email);
|
|
|
|
});
|
|
|
|
|
2022-03-26 08:31:41 +08:00
|
|
|
it('should encode email determinated', function () {
|
|
|
|
encodedEmail.should.equal(encodedEmail2);
|
|
|
|
});
|
|
|
|
|
2017-01-28 03:03:37 +08:00
|
|
|
it('should decode to original email', function () {
|
2022-03-27 08:05:30 +08:00
|
|
|
let decodedEmail = encodedEmail.replace(/&#(.+?);/g, function (wm, cc) {
|
2017-01-28 03:03:37 +08:00
|
|
|
if (cc.charAt(0) === 'x') {
|
|
|
|
//hex
|
|
|
|
return String.fromCharCode('0' + cc);
|
|
|
|
} else {
|
|
|
|
//dec
|
|
|
|
return String.fromCharCode(cc);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
decodedEmail.should.equal(email);
|
|
|
|
});
|
|
|
|
});
|
2017-01-28 09:19:07 +08:00
|
|
|
|
|
|
|
describe('isString()', function () {
|
|
|
|
'use strict';
|
2022-03-27 08:05:30 +08:00
|
|
|
let isString = showdown.helper.isString;
|
2017-01-28 09:19:07 +08:00
|
|
|
|
|
|
|
it('should return true for new String Object', function () {
|
|
|
|
isString(new String('some string')).should.be.true;
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return true for String Object', function () {
|
|
|
|
isString(String('some string')).should.be.true;
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return true for string literal', function () {
|
|
|
|
isString('some string').should.be.true;
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return false for integers', function () {
|
|
|
|
isString(5).should.be.false;
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return false for random objects', function () {
|
|
|
|
isString({foo: 'bar'}).should.be.false;
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return false for arrays', function () {
|
|
|
|
isString(['bar']).should.be.false;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('isFunction()', function () {
|
|
|
|
'use strict';
|
2022-03-27 08:05:30 +08:00
|
|
|
let isFunction = showdown.helper.isFunction;
|
2017-01-28 09:19:07 +08:00
|
|
|
|
|
|
|
it('should return true for closures', function () {
|
|
|
|
isFunction(function () {}).should.be.true;
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return true for defined functions', function () {
|
|
|
|
function foo () {}
|
|
|
|
isFunction(foo).should.be.true;
|
|
|
|
});
|
|
|
|
|
2022-03-27 08:05:30 +08:00
|
|
|
it('should return true for function letiables', function () {
|
|
|
|
let bar = function () {};
|
2017-01-28 09:19:07 +08:00
|
|
|
isFunction(bar).should.be.true;
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return false for hash objects', function () {
|
|
|
|
isFunction({}).should.be.false;
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return false for objects', function () {
|
|
|
|
isFunction(new Object ()).should.be.false;
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return false for string primitives', function () {
|
|
|
|
isFunction('foo').should.be.false;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('isArray()', function () {
|
|
|
|
'use strict';
|
2022-03-27 08:05:30 +08:00
|
|
|
let isArray = showdown.helper.isArray;
|
2017-01-28 09:19:07 +08:00
|
|
|
|
|
|
|
it('should return true for short syntax arrays', function () {
|
|
|
|
isArray([]).should.be.true;
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return true for array objects', function () {
|
2022-03-27 08:05:30 +08:00
|
|
|
let myArr = new Array();
|
2017-01-28 09:19:07 +08:00
|
|
|
isArray(myArr).should.be.true;
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return false for functions', function () {
|
|
|
|
isArray(function () {}).should.be.false;
|
|
|
|
function baz () {}
|
|
|
|
isArray(baz).should.be.false;
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return false for objects', function () {
|
|
|
|
isArray({}).should.be.false;
|
|
|
|
isArray(new Object ()).should.be.false;
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return false for strings', function () {
|
|
|
|
isArray('foo').should.be.false;
|
|
|
|
isArray(new String('foo')).should.be.false;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('isUndefined()', function () {
|
|
|
|
'use strict';
|
2022-03-27 08:05:30 +08:00
|
|
|
let isUndefined = showdown.helper.isUndefined;
|
2017-01-28 09:19:07 +08:00
|
|
|
|
|
|
|
it('should return true if nothing is passed', function () {
|
|
|
|
isUndefined().should.be.true;
|
|
|
|
});
|
|
|
|
|
2022-03-27 08:05:30 +08:00
|
|
|
it('should return true if a letiable is initialized but not defined', function () {
|
|
|
|
let myVar;
|
2017-01-28 09:19:07 +08:00
|
|
|
isUndefined(myVar).should.be.true;
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return false for null', function () {
|
|
|
|
isUndefined(null).should.be.false;
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return false for 0', function () {
|
|
|
|
isUndefined(0).should.be.false;
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return false for empty string', function () {
|
|
|
|
isUndefined('').should.be.false;
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return false for empty booleans false or true', function () {
|
|
|
|
isUndefined(false).should.be.false;
|
|
|
|
isUndefined(true).should.be.false;
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return false for anything not undefined', function () {
|
|
|
|
isUndefined('foo').should.be.false;
|
|
|
|
isUndefined(2).should.be.false;
|
|
|
|
isUndefined({}).should.be.false;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('stdExtName()', function () {
|
|
|
|
'use strict';
|
2022-03-27 08:05:30 +08:00
|
|
|
let stdExtName = showdown.helper.stdExtName;
|
2017-01-28 09:19:07 +08:00
|
|
|
|
|
|
|
it('should remove certain chars', function () {
|
2022-03-27 08:05:30 +08:00
|
|
|
let str = 'bla_- \nbla';
|
2017-01-28 09:19:07 +08:00
|
|
|
//[_?*+\/\\.^-]
|
|
|
|
stdExtName(str).should.not.match(/[_?*+\/\\.^-]/g);
|
|
|
|
});
|
|
|
|
it('should make everything lowercase', function () {
|
2022-03-27 08:05:30 +08:00
|
|
|
let str = 'BLABLA';
|
2017-01-28 09:19:07 +08:00
|
|
|
//[_?*+\/\\.^-]
|
|
|
|
stdExtName(str).should.equal('blabla');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('forEach()', function () {
|
|
|
|
'use strict';
|
2022-03-27 08:05:30 +08:00
|
|
|
let forEach = showdown.helper.forEach;
|
2017-01-28 09:19:07 +08:00
|
|
|
|
|
|
|
it('should throw an error if first parameter is undefined', function () {
|
|
|
|
(function () {forEach();}).should.throw('obj param is required');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should throw an error if second parameter is undefined', function () {
|
|
|
|
(function () {forEach([]);}).should.throw('callback param is required');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should throw an error if second parameter is not a function', function () {
|
|
|
|
(function () {forEach([], 'foo');}).should.throw('callback param must be a function/closure');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should throw an error if first parameter is not an object or an array', function () {
|
|
|
|
(function () {forEach('foo', function () {});}).should.throw('obj does not seem to be an array or an iterable object');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not throw even if object is empty', function () {
|
|
|
|
(function () {forEach({}, function () {});}).should.not.throw();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should iterate array items', function () {
|
2022-03-27 08:05:30 +08:00
|
|
|
let myArray = ['banana', 'orange', 'grape'];
|
2017-01-28 09:19:07 +08:00
|
|
|
forEach(myArray, function (val, key, obj) {
|
|
|
|
key.should.be.a('number');
|
|
|
|
(key % 1).should.equal(0);
|
|
|
|
val.should.equal(myArray[key]);
|
|
|
|
obj.should.equal(myArray);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should iterate over object properties', function () {
|
2022-03-27 08:05:30 +08:00
|
|
|
let myObj = {foo: 'banana', bar: 'orange', baz: 'grape'};
|
2017-01-28 09:19:07 +08:00
|
|
|
forEach(myObj, function (val, key, obj) {
|
|
|
|
myObj.should.have.ownProperty(key);
|
|
|
|
val.should.equal(myObj[key]);
|
|
|
|
obj.should.equal(myObj);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should iterate only over object own properties', function () {
|
2022-03-27 08:05:30 +08:00
|
|
|
let Obj1 = {foo: 'banana'},
|
2017-01-28 09:19:07 +08:00
|
|
|
myObj = Object.create(Obj1);
|
|
|
|
myObj.bar = 'orange';
|
|
|
|
myObj.baz = 'grape';
|
|
|
|
|
|
|
|
myObj.should.have.ownProperty('bar');
|
|
|
|
myObj.should.have.ownProperty('baz');
|
|
|
|
myObj.should.not.have.ownProperty('foo');
|
|
|
|
|
|
|
|
forEach(myObj, function (val, key) {
|
|
|
|
key.should.not.equal('foo');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2017-05-29 00:20:07 +08:00
|
|
|
|
|
|
|
describe('matchRecursiveRegExp()', function () {
|
|
|
|
'use strict';
|
|
|
|
|
2022-03-27 08:05:30 +08:00
|
|
|
let rRegExp = showdown.helper.matchRecursiveRegExp;
|
2017-05-29 00:20:07 +08:00
|
|
|
|
|
|
|
it('should match nested elements', function () {
|
2022-03-27 08:05:30 +08:00
|
|
|
let result = rRegExp('<div><div>a</div></div>', '<div\\b[^>]*>', '</div>', 'gim');
|
2017-05-29 00:20:07 +08:00
|
|
|
result.should.deep.equal([['<div><div>a</div></div>', '<div>a</div>', '<div>', '</div>']]);
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2022-03-10 20:56:34 +08:00
|
|
|
describe('repeat()', function () {
|
|
|
|
'use strict';
|
|
|
|
it('work produce the same output as String.prototype.repeat()', function () {
|
2022-03-27 08:05:30 +08:00
|
|
|
if (typeof String.prototype.repeat !== 'undefined') {
|
|
|
|
let str = 'foo',
|
|
|
|
expected = str.repeat(100),
|
|
|
|
actual = showdown.helper.repeat(str, 100);
|
|
|
|
expected.should.equal(actual);
|
|
|
|
}
|
2022-03-10 20:56:34 +08:00
|
|
|
});
|
|
|
|
});
|