Advertisement
xapu

Untitled

Jul 11th, 2017
370
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.72 KB | None | 0 0
  1. let assert = require('chai').assert
  2.  
  3. describe('testing a createList func',function(){
  4.  
  5. let test=createList()
  6.  
  7. beforeEach(function(){
  8. test=createList()
  9. })
  10.  
  11. it('the list should be empty at the start',function(){
  12.  
  13.  
  14. assert(test.toString(),'', 'bambam')
  15. })
  16.  
  17. it('the list should have all initial functions', function(){
  18.  
  19. assert.isFunction(test.add,'add is not a function')
  20. assert.isFunction(test.shiftLeft,'shiftLeft is not a function')
  21. assert.isFunction(test.shiftRight,'shiftRight is not a function')
  22. assert.isFunction(test.swap,'swap is not a function')
  23. assert.isFunction(test.toString,'to string is not a function')
  24.  
  25.  
  26. })
  27.  
  28.  
  29. })
  30.  
  31. function createList() {
  32. let data = [];
  33. return {
  34. add: function (item) {
  35. data.push(item)
  36. },
  37. shiftLeft: function () {
  38. if (data.length > 1) {
  39. let first = data.shift();
  40. data.push(first);
  41. }
  42. },
  43. shiftRight: function () {
  44. if (data.length > 1) {
  45. let last = data.pop();
  46. data.unshift(last);
  47. }
  48. },
  49. swap: function (index1, index2) {
  50. if (!Number.isInteger(index1) || index1 < 0 || index1 >= data.length ||
  51. !Number.isInteger(index2) || index2 < 0 || index2 >= data.length ||
  52. index1 === index2) {
  53. return false;
  54. }
  55. let temp = data[index1];
  56. data[index1] = data[index2];
  57. data[index2] = temp;
  58. return true;
  59. },
  60. toString: function () {
  61. return data.join(", ");
  62. }
  63. };
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement