Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- describe('Test StringBuilder', () => {
- let obj = undefined;
- beforeEach(() => { obj = new StringBuilder('Simo'); });
- describe('Test constructor', () => {
- it('Checks constructor', () => {
- expect(obj.constructor.toString().substring(0, 5)).to.equal('class');
- assert.equal(obj instanceof StringBuilder, true);
- assert.equal(JSON.stringify(obj._stringArray), JSON.stringify(Array.from('Simo')));
- assert.equal(typeof obj, 'object');
- assert.equal(typeof StringBuilder, 'function');
- assert.equal(obj.toString(), 'Simo');
- assert.equal(obj instanceof StringBuilder, true);
- });
- it('checks if all functions are present', () => {
- expect(obj).to.respondTo('append');
- expect(obj).to.respondTo('prepend');
- expect(obj).to.respondTo('insertAt');
- expect(obj).to.respondTo('remove');
- expect('_vrfyParam' in StringBuilder).to.be.true;
- expect(obj).to.respondTo('toString');
- });
- it('Creates obj without param', () => {
- const emptyObj = new StringBuilder();
- assert.equal(emptyObj.toString(), '');
- });
- it('Returns empty string if we pass undefined', () => {
- const testEmpty = new StringBuilder(undefined);
- assert.equal(testEmpty.toString(), '');
- });
- });
- describe('Test append', () => {
- it('Makes the passed in string into array', () => {
- obj.append(' hello');
- assert.equal(JSON.stringify(obj._stringArray), JSON.stringify([...'Simo hello']));
- assert.equal(obj.toString(), 'Simo hello');
- });
- it('Throw error for non string param', () => {
- assert.throw(() => { obj.append(123); }, 'Argument must be а string');
- });
- });
- describe('Test prepend', () => {
- it('Return correct output', () => {
- const input = 'hello ';
- obj.prepend(input);
- expect(obj._stringArray.indexOf(input[0])).to.equal(0);
- expect(obj.toString()).to.equal('hello Simo');
- assert.equal(JSON.stringify(obj._stringArray), JSON.stringify([...'hello Simo']));
- });
- it('Throw error for non string param', () => { assert.throw(() => { obj.prepend(123); }, 'Argument must be а string'); });
- });
- describe('Test insertAt', () => {
- it('Returns correct output', () => {
- obj.insertAt('hello ', 2);
- assert.equal(obj.toString(), 'Sihello mo');
- });
- it('Throw error for non string param', () => { assert.throw(() => { obj.insertAt(undefined); }, 'Argument must be а string'); });
- it('Throw an error for non string first param', function () {
- const obj = new StringBuilder('Simo');
- assert.throw(function () { obj.insertAt(null, 1); }, 'Argument must be а string');
- });
- it('Throw error for only 1 param provided', function () {
- const obj = new StringBuilder('Simo');
- assert.throw(function () { obj.insertAt(1); }, 'Argument must be а string');
- });
- it('Throw error for not provided params', function () {
- const obj = new StringBuilder('Simo');
- assert.throw(function () { obj.insertAt(); }, 'Argument must be а string');
- });
- it('should return result if we pass string at begining', function () {
- let str = 'wtf';
- obj.insertAt(str, 0);
- let expected = [...'Simo'];
- expected.splice(0, 0, ...str);
- assert.equal(obj._stringArray[0], expected[0]);
- });
- });
- describe('Test toString', function () {
- it('Return correct output with not empty string', function () { expect(obj.toString()).to.equal('Simo'); });
- it('Return empty string', function () { const obj = new StringBuilder(); expect(obj.toString()).to.equal(''); });
- it('Returns a string type', function () { assert.equal(typeof obj.toString(), 'string'); });
- });
- describe('Test remove', function () {
- it('Remove all', function () { obj.remove(0, 1000); assert.equal(obj.toString(), ''); });
- it('Removes 0', function () { obj.remove(1, 0); assert.equal(obj.toString(), 'Simo'); });
- });
- });
Add Comment
Please, Sign In to add comment