2015-06-01 03:56:28 +08:00
|
|
|
require('source-map-support').install();
|
|
|
|
require('chai').should();
|
2022-03-10 20:56:34 +08:00
|
|
|
require('sinon');
|
2015-06-01 03:56:28 +08:00
|
|
|
var expect = require('chai').expect,
|
2017-12-22 23:28:41 +08:00
|
|
|
showdown = require('../../.build/showdown.js');
|
2015-06-01 03:56:28 +08:00
|
|
|
|
2022-03-03 20:15:50 +08:00
|
|
|
|
2015-06-01 03:56:28 +08:00
|
|
|
describe('showdown.options', function () {
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
describe('setOption() and getOption()', function () {
|
|
|
|
it('should set option foo=bar', function () {
|
|
|
|
showdown.setOption('foo', 'bar');
|
|
|
|
showdown.getOption('foo').should.equal('bar');
|
|
|
|
showdown.resetOptions();
|
2017-01-28 06:07:55 +08:00
|
|
|
(typeof showdown.getOption('foo')).should.equal('undefined');
|
2015-06-01 03:56:28 +08:00
|
|
|
});
|
|
|
|
});
|
2015-06-15 21:56:30 +08:00
|
|
|
|
|
|
|
describe('getDefaultOptions()', function () {
|
|
|
|
it('should get default options', function () {
|
2017-12-22 23:28:41 +08:00
|
|
|
var opts = require('./optionswp.js').getDefaultOpts(true);
|
2015-06-15 21:56:30 +08:00
|
|
|
expect(showdown.getDefaultOptions()).to.be.eql(opts);
|
|
|
|
});
|
|
|
|
});
|
2015-06-01 03:56:28 +08:00
|
|
|
});
|
|
|
|
|
2015-06-08 02:02:45 +08:00
|
|
|
describe('showdown.extension()', function () {
|
2015-06-01 03:56:28 +08:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var extObjMock = {
|
|
|
|
type: 'lang',
|
|
|
|
filter: function () {}
|
|
|
|
},
|
|
|
|
extObjFunc = function () {
|
|
|
|
return extObjMock;
|
|
|
|
};
|
|
|
|
|
2022-03-03 20:15:50 +08:00
|
|
|
describe('file loading', function () {
|
|
|
|
|
|
|
|
beforeEach(function () {
|
|
|
|
this.extension = require('../mocks/mock-extension');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should register an extension from a file', function () {
|
|
|
|
showdown.extension('mockextension').should.be.an('array');
|
|
|
|
showdown.extension('mockextension').should.eql([this.extension]);
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(function () {
|
2015-06-01 03:56:28 +08:00
|
|
|
showdown.resetExtensions();
|
|
|
|
});
|
|
|
|
|
2022-03-03 20:15:50 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
describe('objects', function () {
|
|
|
|
it('should register an extension object', function () {
|
|
|
|
showdown.extension('foo', extObjMock);
|
2015-06-08 02:02:45 +08:00
|
|
|
showdown.extension('foo').should.eql([extObjMock]);
|
2015-06-01 03:56:28 +08:00
|
|
|
});
|
2015-08-03 10:47:49 +08:00
|
|
|
|
2022-03-03 20:15:50 +08:00
|
|
|
it('should register an extension function', function () {
|
|
|
|
showdown.extension('bar', extObjFunc);
|
|
|
|
showdown.extension('bar').should.eql([extObjMock]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should register a listener extension', function () {
|
|
|
|
showdown.extension('baz', {
|
2015-08-03 10:47:49 +08:00
|
|
|
type: 'listener',
|
|
|
|
listeners: {
|
|
|
|
foo: function (name, txt) {
|
|
|
|
return txt;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2015-06-01 03:56:28 +08:00
|
|
|
|
2022-03-03 20:15:50 +08:00
|
|
|
it('should refuse to register a generic object', function () {
|
2015-06-01 03:56:28 +08:00
|
|
|
var fn = function () {
|
|
|
|
showdown.extension('foo', {});
|
|
|
|
};
|
|
|
|
expect(fn).to.throw();
|
|
|
|
});
|
|
|
|
|
2022-03-03 20:15:50 +08:00
|
|
|
it('should refuse to register an extension with invalid type', function () {
|
2015-06-01 03:56:28 +08:00
|
|
|
var fn = function () {
|
|
|
|
showdown.extension('foo', {
|
|
|
|
type: 'foo'
|
|
|
|
});
|
|
|
|
};
|
2015-08-03 10:47:49 +08:00
|
|
|
expect(fn).to.throw(/type .+? is not recognized\. Valid values: "lang\/language", "output\/html" or "listener"/);
|
2015-06-01 03:56:28 +08:00
|
|
|
});
|
|
|
|
|
2022-03-03 20:15:50 +08:00
|
|
|
it('should refuse to register an extension without regex or filter', function () {
|
2015-06-01 03:56:28 +08:00
|
|
|
var fn = function () {
|
|
|
|
showdown.extension('foo', {
|
|
|
|
type: 'lang'
|
|
|
|
});
|
|
|
|
};
|
|
|
|
expect(fn).to.throw(/extensions must define either a "regex" property or a "filter" method/);
|
|
|
|
});
|
2015-08-03 10:47:49 +08:00
|
|
|
|
2022-03-03 20:15:50 +08:00
|
|
|
it('should refuse to register a listener extension without a listeners property', function () {
|
2015-08-03 10:47:49 +08:00
|
|
|
var fn = function () {
|
|
|
|
showdown.extension('foo', {
|
|
|
|
type: 'listener'
|
|
|
|
});
|
|
|
|
};
|
|
|
|
expect(fn).to.throw(/Extensions of type "listener" must have a property called "listeners"/);
|
|
|
|
});
|
2022-03-03 20:15:50 +08:00
|
|
|
|
|
|
|
afterEach(function () {
|
|
|
|
showdown.resetExtensions();
|
|
|
|
});
|
|
|
|
|
2015-06-01 03:56:28 +08:00
|
|
|
});
|
2022-03-03 20:15:50 +08:00
|
|
|
|
2015-06-01 03:56:28 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('showdown.getAllExtensions()', function () {
|
|
|
|
'use strict';
|
|
|
|
var extObjMock = {
|
2017-01-31 13:46:25 +08:00
|
|
|
type: 'lang',
|
|
|
|
filter: function () {}
|
|
|
|
};
|
2015-06-01 03:56:28 +08:00
|
|
|
|
|
|
|
it('should return all extensions', function () {
|
|
|
|
showdown.extension('bar', extObjMock);
|
2015-06-08 02:02:45 +08:00
|
|
|
showdown.getAllExtensions().should.eql({bar: [extObjMock]});
|
2015-06-01 03:56:28 +08:00
|
|
|
});
|
|
|
|
});
|
2017-01-09 03:09:12 +08:00
|
|
|
|
|
|
|
describe('showdown.setFlavor()', function () {
|
|
|
|
'use strict';
|
|
|
|
it('should set flavor to github', function () {
|
|
|
|
showdown.setFlavor('github');
|
|
|
|
showdown.getFlavor().should.equal('github');
|
|
|
|
showdown.setFlavor('vanilla');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should set options correctly', function () {
|
|
|
|
showdown.setFlavor('github');
|
|
|
|
var ghOpts = showdown.getFlavorOptions('github'),
|
|
|
|
shOpts = showdown.getOptions();
|
|
|
|
for (var opt in ghOpts) {
|
|
|
|
if (ghOpts.hasOwnProperty(opt)) {
|
|
|
|
shOpts.should.have.property(opt);
|
|
|
|
shOpts[opt].should.equal(ghOpts[opt]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
showdown.setFlavor('vanilla');
|
|
|
|
});
|
2017-03-31 06:17:10 +08:00
|
|
|
|
|
|
|
it('should switch between flavors correctly', function () {
|
|
|
|
showdown.setFlavor('github');
|
|
|
|
var ghOpts = showdown.getFlavorOptions('github'),
|
|
|
|
shOpts = showdown.getOptions(),
|
|
|
|
dfOpts = showdown.getDefaultOptions();
|
|
|
|
for (var opt in dfOpts) {
|
|
|
|
if (ghOpts.hasOwnProperty(opt)) {
|
|
|
|
shOpts[opt].should.equal(ghOpts[opt]);
|
|
|
|
} else {
|
|
|
|
shOpts[opt].should.equal(dfOpts[opt]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
showdown.setFlavor('original');
|
|
|
|
var orOpts = showdown.getFlavorOptions('original');
|
|
|
|
shOpts = showdown.getOptions();
|
|
|
|
for (opt in dfOpts) {
|
|
|
|
if (orOpts.hasOwnProperty(opt)) {
|
|
|
|
shOpts[opt].should.equal(orOpts[opt]);
|
|
|
|
} else {
|
|
|
|
shOpts[opt].should.equal(dfOpts[opt]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
showdown.setFlavor('vanilla');
|
|
|
|
});
|
2017-01-09 03:09:12 +08:00
|
|
|
});
|