Advertisement
xapu

Untitled

Jul 11th, 2017
347
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.08 KB | None | 0 0
  1.  
  2. let assert = require('chai').assert
  3.  
  4. describe('testing a createList func',function(){
  5.  
  6. let test=createList()
  7.  
  8. beforeEach(function(){
  9. test=createList()
  10. })
  11.  
  12. it('the list should be empty at the start',function(){
  13.  
  14. assert(test.toString(),'', 'list is not empty')
  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. // it('add should add element to the end of the list')
  29.  
  30. // it('shift left - rotata left 1 elem')
  31.  
  32. // it('shift right - rotata right 1 elem')
  33.  
  34. // it('swap should swap to elems by two given index and'+
  35. // ' return true if swaped, else return false')
  36.  
  37. // it('to string should return string of all elems joined by comma and space')
  38. })
  39.  
  40. function createList() {
  41. let data = [];
  42. return {
  43. add: function (item) {
  44. data.push(item)
  45. },
  46. shiftLeft: function () {
  47. if (data.length > 1) {
  48. let first = data.shift();
  49. data.push(first);
  50. }
  51. },
  52. shiftRight: function () {
  53. if (data.length > 1) {
  54. let last = data.pop();
  55. data.unshift(last);
  56. }
  57. },
  58. swap: function (index1, index2) {
  59. if (!Number.isInteger(index1) || index1 < 0 || index1 >= data.length ||
  60. !Number.isInteger(index2) || index2 < 0 || index2 >= data.length ||
  61. index1 === index2) {
  62. return false;
  63. }
  64. let temp = data[index1];
  65. data[index1] = data[index2];
  66. data[index2] = temp;
  67. return true;
  68. },
  69. toString: function () {
  70. return data.join(", ");
  71. }
  72. };
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement