Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- let assert = require('chai').assert
- describe('testing a createList func',function(){
- let test=createList()
- beforeEach(function(){
- test=createList()
- })
- it('the list should be empty at the start',function(){
- assert(test.toString(),'', 'bambam')
- })
- it('the list should have all initial functions', function(){
- assert.isFunction(test.add,'add is not a function')
- assert.isFunction(test.shiftLeft,'shiftLeft is not a function')
- assert.isFunction(test.shiftRight,'shiftRight is not a function')
- assert.isFunction(test.swap,'swap is not a function')
- assert.isFunction(test.toString,'to string is not a function')
- })
- })
- function createList() {
- let data = [];
- return {
- add: function (item) {
- data.push(item)
- },
- shiftLeft: function () {
- if (data.length > 1) {
- let first = data.shift();
- data.push(first);
- }
- },
- shiftRight: function () {
- if (data.length > 1) {
- let last = data.pop();
- data.unshift(last);
- }
- },
- swap: function (index1, index2) {
- if (!Number.isInteger(index1) || index1 < 0 || index1 >= data.length ||
- !Number.isInteger(index2) || index2 < 0 || index2 >= data.length ||
- index1 === index2) {
- return false;
- }
- let temp = data[index1];
- data[index1] = data[index2];
- data[index2] = temp;
- return true;
- },
- toString: function () {
- return data.join(", ");
- }
- };
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement