AreWe

String Builder

Oct 27th, 2020 (edited)
820
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. describe('05. String Builder Tests', () => {
  2.   beforeEach(() => (sb = new StringBuilder()));
  3.   describe('Class properties Tests', () => {
  4.     it('Should contain functions append, prepend, insertAt, remove, toString', () => {
  5.       const { append, prepend, insertAt, remove, toString } = sb;
  6.       const actual = [append, prepend, insertAt, remove, toString].filter((p) => typeof p !== 'function').length;
  7.       assert.equal(actual, 0);
  8.     });
  9.  
  10.     it('Should instantiate with property _stringArray as empty array', () => {
  11.       assert.equal(sb._stringArray instanceof Array, true);
  12.       assert.equal(sb._stringArray.length, 0);
  13.     });
  14.  
  15.     it('Prototype should contain functions append, prepend, insertAt, remove, toString', () => {
  16.       const { append, prepend, insertAt, remove, toString } = Object.getPrototypeOf(sb);
  17.       const actual = [append, prepend, insertAt, remove, toString].filter((p) => typeof p !== 'function').length;
  18.       assert.equal(actual, 0);
  19.     });
  20.   });
  21.  
  22.   describe('Instantiation Tests', () => {
  23.     it('Should instantiate with string argument', () => {
  24.       assert.equal(new StringBuilder('text').toString(), 'text');
  25.     });
  26.  
  27.     it('Should instantiate without argument', () => {
  28.       assert.equal(new StringBuilder().toString(), '');
  29.     });
  30.  
  31.     it("Should instantiate with property _stringArray ['i', 'n', 'i', 't', 'i', 'a', 'l']) as expected", () => {
  32.       const sb = new StringBuilder('initial');
  33.       assert.deepEqual(sb._stringArray, ['i', 'n', 'i', 't', 'i', 'a', 'l']);
  34.     });
  35.  
  36.     it('Should not instantiate with wrong type argument', () => {
  37.       assert.throw(() => new StringBuilder({}), 'Argument must be string');
  38.       assert.throw(() => new StringBuilder(0), 'Argument must be string');
  39.       assert.throw(() => new StringBuilder(true), 'Argument must be string');
  40.     });
  41.   });
  42.  
  43.   describe('Function append Tests', () => {
  44.     it('Should append "abc"', () => {
  45.       const sb = new StringBuilder();
  46.       sb.append('abc');
  47.       assert.equal(sb.toString(), 'abc');
  48.     });
  49.  
  50.     it('Should append "abc"', () => {
  51.       const sb = new StringBuilder('abc');
  52.       sb.append('abc');
  53.       assert.equal(sb.toString(), 'abcabc');
  54.     });
  55.  
  56.     it('Should append "abc"', () => {
  57.       const sb = new StringBuilder('');
  58.       sb.append('abc');
  59.       assert.equal(sb.toString(), 'abc');
  60.     });
  61.  
  62.     it('Should append "abc", "text", "test"', () => {
  63.       const sb = new StringBuilder('');
  64.       sb.append('abc');
  65.       sb.append('text');
  66.       sb.append('test');
  67.       assert.equal(sb.toString(), 'abctexttest');
  68.     });
  69.  
  70.     it('Should append ""', () => {
  71.       const sb = new StringBuilder('');
  72.       sb.append('');
  73.       assert.equal(sb.toString(), '');
  74.     });
  75.  
  76.     it('Should throw Argument must be string for wrong argument type', () => {
  77.       const sb = new StringBuilder();
  78.       assert.throw(() => sb.append({}), 'Argument must be string');
  79.       assert.throw(() => sb.append(0), 'Argument must be string');
  80.       assert.throw(() => sb.append(true), 'Argument must be string');
  81.       assert.throw(() => sb.append(), 'Argument must be string');
  82.     });
  83.   });
  84.  
  85.   describe('Function prepend Tests', () => {
  86.     it('Should prepend "abc"', () => {
  87.       const sb = new StringBuilder();
  88.       sb.prepend('abc');
  89.       assert.equal(sb.toString(), 'abc');
  90.     });
  91.  
  92.     it('Should prepend "abc"', () => {
  93.       const sb = new StringBuilder('text');
  94.       sb.prepend('abc');
  95.       assert.equal(sb.toString(), 'abctext');
  96.     });
  97.  
  98.     it('Should prepend "abc"', () => {
  99.       const sb = new StringBuilder('');
  100.       sb.prepend('abc');
  101.       assert.equal(sb.toString(), 'abc');
  102.     });
  103.  
  104.     it('Should prepend "abc", "test", "test"', () => {
  105.       const sb = new StringBuilder('');
  106.       sb.prepend('abc');
  107.       sb.prepend('text');
  108.       sb.prepend('test');
  109.       assert.equal(sb.toString(), 'testtextabc');
  110.     });
  111.  
  112.     it('Should prepend ""', () => {
  113.       const sb = new StringBuilder('');
  114.       sb.prepend('');
  115.       assert.equal(sb.toString(), '');
  116.     });
  117.  
  118.     it('Should throw Argument must be string for wrong argument type', () => {
  119.       const sb = new StringBuilder();
  120.       assert.throw(() => sb.prepend({}), 'Argument must be string');
  121.       assert.throw(() => sb.prepend(0), 'Argument must be string');
  122.       assert.throw(() => sb.prepend(true), 'Argument must be string');
  123.       assert.throw(() => sb.prepend(), 'Argument must be string');
  124.     });
  125.   });
  126.  
  127.   describe('Function insertAt Tests', () => {
  128.     it('Should insert "abc" at 0', () => {
  129.       const sb = new StringBuilder();
  130.       sb.insertAt('abc', 0);
  131.       assert.equal(sb.toString(), 'abc');
  132.     });
  133.  
  134.     it('Should insert "test" at 0', () => {
  135.       const sb = new StringBuilder('text');
  136.       sb.insertAt('abc', 0);
  137.       assert.equal(sb.toString(), 'abctext');
  138.     });
  139.  
  140.     it('Should insert "se" at 2', () => {
  141.       const sb = new StringBuilder('inrt');
  142.       sb.insertAt('se', 2);
  143.       assert.equal(sb.toString(), 'insert');
  144.     });
  145.  
  146.     it('Should insert "abc" at 0', () => {
  147.       const sb = new StringBuilder('');
  148.       sb.insertAt('abc', 0);
  149.       assert.equal(sb.toString(), 'abc');
  150.     });
  151.  
  152.     it('Should insert "in" at 0, "se" at 2, and "rt" at 4', () => {
  153.       const sb = new StringBuilder('');
  154.       sb.insertAt('in', 0);
  155.       sb.insertAt('se', 2);
  156.       sb.insertAt('rt', 4);
  157.       assert.equal(sb.toString(), 'insert');
  158.     });
  159.  
  160.     it('Should throw Argument must be string for wrong argument type', () => {
  161.       const sb = new StringBuilder();
  162.       assert.throw(() => sb.insertAt({}, 0), 'Argument must be string');
  163.       assert.throw(() => sb.insertAt(0, 0), 'Argument must be string');
  164.       assert.throw(() => sb.insertAt(true, 0), 'Argument must be string');
  165.       assert.throw(() => sb.insertAt(), 'Argument must be string');
  166.     });
  167.  
  168.     it('Should throw silent error for wrong index argument type', () => {
  169.       const sb = new StringBuilder();
  170.       sb.insertAt('text', {});
  171.       assert.equal(sb.toString(), 'text');
  172.       sb.insertAt('text', []);
  173.       assert.equal(sb.toString(), 'texttext');
  174.     });
  175.   });
  176.  
  177.   describe('Function remove Tests', () => {
  178.     it('Should remove at 0 with length 2', () => {
  179.       const sb = new StringBuilder();
  180.       sb.remove(0, 2);
  181.       assert.equal(sb.toString(), '');
  182.     });
  183.  
  184.     it('Should remove at 0 with length 3', () => {
  185.       const sb = new StringBuilder('testtext');
  186.       sb.remove(0, 3);
  187.       assert.equal(sb.toString(), 'ttext');
  188.     });
  189.  
  190.     it('Should remove at 3 with length 2', () => {
  191.       const sb = new StringBuilder('testtext');
  192.       sb.remove(3, 2);
  193.       assert.equal(sb.toString(), 'tesext');
  194.     });
  195.  
  196.     it('Should remove at 3 with length 2, at 0 with length 3, at 6 with length 1', () => {
  197.       const sb = new StringBuilder('what a beautiful test mate');
  198.       sb.remove(3, 2);
  199.       sb.remove(0, 3);
  200.       sb.remove(6, 1);
  201.       assert.equal(sb.toString(), 'a beauiful test mate');
  202.     });
  203.  
  204.     it('Should throw silent error when wrong argument types are passed', () => {
  205.       const sb = new StringBuilder('insert');
  206.       sb.remove({}, 2);
  207.       assert.equal(sb.toString(), 'sert');
  208.       sb.remove(0, {});
  209.       assert.equal(sb.toString(), 'sert');
  210.       sb.remove();
  211.       assert.equal(sb.toString(), 'sert');
  212.     });
  213.   });
  214.  
  215.   describe('Functions append, prepend, insertAt and remove Tests using append, prepend, remove, insertAt', () => {
  216.     it('Should build the string "1337"', () => {
  217.       const sb = new StringBuilder('leet');
  218.       sb.append('7');
  219.       assert.equal(sb.toString(), 'leet7');
  220.       sb.prepend('1');
  221.       assert.equal(sb.toString(), '1leet7');
  222.       sb.remove(1, 4);
  223.       assert.equal(sb.toString(), '17');
  224.       sb.insertAt('33', 1);
  225.       assert.equal(sb.toString(), '1337');
  226.     });
  227.   });
  228.  
  229.   describe('Function toString Tests', () => {
  230.     it('Should return a string with all elements joined by an empty string', () => {
  231.       const sb = new StringBuilder('what dafuq mate');
  232.       assert.equal(sb.toString(), 'what dafuq mate');
  233.       assert.deepEqual([...sb.toString()], sb._stringArray);
  234.     });
  235.   });
  236. });
  237.  
Advertisement
Add Comment
Please, Sign In to add comment