simeonshopov

String Builder 100

Jun 18th, 2021
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. describe('Test StringBuilder', () => {
  2.   let obj = undefined;
  3.   beforeEach(() => { obj = new StringBuilder('Simo'); });
  4.  
  5.   describe('Test constructor', () => {
  6.     it('Checks constructor', () => {
  7.       expect(obj.constructor.toString().substring(0, 5)).to.equal('class');
  8.       assert.equal(obj instanceof StringBuilder, true);
  9.       assert.equal(JSON.stringify(obj._stringArray), JSON.stringify(Array.from('Simo')));
  10.       assert.equal(typeof obj, 'object');
  11.       assert.equal(typeof StringBuilder, 'function');
  12.       assert.equal(obj.toString(), 'Simo');
  13.       assert.equal(obj instanceof StringBuilder, true);
  14.     });
  15.     it('checks if all functions are present', () => {
  16.       expect(obj).to.respondTo('append');
  17.       expect(obj).to.respondTo('prepend');
  18.       expect(obj).to.respondTo('insertAt');
  19.       expect(obj).to.respondTo('remove');
  20.       expect('_vrfyParam' in StringBuilder).to.be.true;
  21.       expect(obj).to.respondTo('toString');
  22.     });
  23.     it('Creates obj without param', () => {
  24.       const emptyObj = new StringBuilder();
  25.       assert.equal(emptyObj.toString(), '');
  26.     });
  27.     it('Returns empty string if we pass undefined', () => {
  28.       const testEmpty = new StringBuilder(undefined);
  29.       assert.equal(testEmpty.toString(), '');
  30.     });
  31.   });
  32.  
  33.   describe('Test append', () => {
  34.     it('Makes the passed in string into array', () => {
  35.       obj.append(' hello');
  36.       assert.equal(JSON.stringify(obj._stringArray), JSON.stringify([...'Simo hello']));
  37.       assert.equal(obj.toString(), 'Simo hello');
  38.     });
  39.     it('Throw error for non string param', () => {
  40.       assert.throw(() => { obj.append(123); }, 'Argument must be а string');
  41.     });
  42.   });
  43.  
  44.   describe('Test prepend', () => {
  45.     it('Return correct output', () => {
  46.       const input = 'hello ';
  47.       obj.prepend(input);
  48.       expect(obj._stringArray.indexOf(input[0])).to.equal(0);
  49.       expect(obj.toString()).to.equal('hello Simo');
  50.       assert.equal(JSON.stringify(obj._stringArray), JSON.stringify([...'hello Simo']));
  51.     });
  52.     it('Throw error for non string param', () => { assert.throw(() => { obj.prepend(123); }, 'Argument must be а string'); });
  53.   });
  54.  
  55.   describe('Test insertAt', () => {
  56.     it('Returns correct output', () => {
  57.       obj.insertAt('hello ', 2);
  58.       assert.equal(obj.toString(), 'Sihello mo');
  59.     });
  60.     it('Throw error for non string param', () => { assert.throw(() => { obj.insertAt(undefined); }, 'Argument must be а string'); });
  61.     it('Throw an error for non string first param', function () {
  62.       const obj = new StringBuilder('Simo');
  63.       assert.throw(function () { obj.insertAt(null, 1); }, 'Argument must be а string');
  64.     });
  65.     it('Throw error for only 1 param provided', function () {
  66.       const obj = new StringBuilder('Simo');
  67.       assert.throw(function () { obj.insertAt(1); }, 'Argument must be а string');
  68.     });
  69.     it('Throw error for not provided params', function () {
  70.       const obj = new StringBuilder('Simo');
  71.       assert.throw(function () { obj.insertAt(); }, 'Argument must be а string');
  72.     });
  73.     it('should return result if we pass string at begining', function () {
  74.       let str = 'wtf';
  75.       obj.insertAt(str, 0);
  76.       let expected = [...'Simo'];
  77.       expected.splice(0, 0, ...str);
  78.       assert.equal(obj._stringArray[0], expected[0]);
  79.     });
  80.   });
  81.  
  82.   describe('Test toString', function () {
  83.     it('Return correct output with not empty string', function () { expect(obj.toString()).to.equal('Simo'); });
  84.     it('Return empty string', function () { const obj = new StringBuilder(); expect(obj.toString()).to.equal(''); });
  85.     it('Returns a string type', function () { assert.equal(typeof obj.toString(), 'string'); });
  86.   });
  87.  
  88.   describe('Test remove', function () {
  89.     it('Remove all', function () { obj.remove(0, 1000); assert.equal(obj.toString(), ''); });
  90.     it('Removes 0', function () { obj.remove(1, 0); assert.equal(obj.toString(), 'Simo'); });
  91.   });
  92.  
  93. });
Add Comment
Please, Sign In to add comment