Muthu Kumar
7 years ago
1 changed files with 154 additions and 29 deletions
@ -1,47 +1,172 @@ |
|||||
'use strict'; |
'use strict'; |
||||
|
|
||||
const InfiniteList = require('..'); |
const InfiniteList = require('../es5/infinity.min'); |
||||
const { InfiniteListItem } = require('..'); |
|
||||
|
const { |
||||
|
InfiniteListItem |
||||
|
} = require('..'); |
||||
|
|
||||
|
String.prototype.trimify = function() { |
||||
|
return this.replace(/\s/g, ''); |
||||
|
}; |
||||
|
|
||||
|
const testStrings = [ |
||||
|
`InfiniteListItem[.. {
|
||||
|
"i": 35, |
||||
|
"a": { |
||||
|
"$ref": "$" |
||||
|
} |
||||
|
} ..]`,
|
||||
|
`InfiniteList[{
|
||||
|
"i": 10, |
||||
|
"a": { |
||||
|
"$ref": "$" |
||||
|
} |
||||
|
}, { |
||||
|
"i": 15, |
||||
|
"a": { |
||||
|
"$ref": "$" |
||||
|
} |
||||
|
}, ...]` |
||||
|
]; |
||||
|
|
||||
/* global describe it expect */ |
/* global describe it expect */ |
||||
describe("InfiniteList", () => { |
describe("InfiniteList", () => { |
||||
|
|
||||
it("Should be true", () => { |
it("Should be true", () => { |
||||
const infinite = InfiniteList.create(0, x => x + 2); |
const Infinite = new InfiniteList(0, x => x + 2); |
||||
expect(infinite.get(5) instanceof InfiniteListItem).toBe(true); |
expect(Infinite instanceof InfiniteList).toBe(true); |
||||
}) |
}); |
||||
|
|
||||
it("Should be 0", () => { |
it("Should be true", () => { |
||||
const infinite = InfiniteList.create(0, x => x + 2); |
const Infinite = new InfiniteList(0, x => x + 2); |
||||
expect(infinite.first().value).toBe(0); |
expect(Infinite.get(5) instanceof InfiniteListItem).toBe(true); |
||||
expect(infinite.top().value).toBe(0); |
}); |
||||
expect(infinite.last().value).toBe(Infinity); |
|
||||
expect(infinite.end().value).toBe(Infinity); |
|
||||
}) |
|
||||
|
|
||||
it("Should be 22", () => { |
it("Should be 22", () => { |
||||
const infinite = InfiniteList.create(0, x => x + 2); |
const Infinite = new InfiniteList(0, x => x + 2); |
||||
expect(infinite.get(11).value).toBe(22); |
expect(Infinite.get(11).value).toBe(22); |
||||
}) |
}); |
||||
|
|
||||
it("Should be [ 0, 2, 4, 6, 8 ]", () => { |
it("Should be 0", () => { |
||||
const infinite = InfiniteList.create(0, x => x + 2); |
const Infinite = new InfiniteList(0, x => x + 2); |
||||
expect(infinite.take(5).map(x => x.value)).toEqual([ 0, 2, 4, 6, 8 ]); |
expect(Infinite.first().value).toBe(0); |
||||
}) |
expect(Infinite.top().value).toBe(0); |
||||
|
expect(Infinite.last().value).toBe(Infinity); |
||||
|
expect(Infinite.end().value).toBe(Infinity); |
||||
|
}); |
||||
|
|
||||
|
it("Should be 102", () => { |
||||
|
const Infinite = new InfiniteList(0, x => x + 2); |
||||
|
const hundred = Infinite.get(50); |
||||
|
expect(hundred.next().value).toBe(102); |
||||
|
}); |
||||
|
|
||||
|
it("Should be [ 50, 52, 54, 56 ]", () => { |
||||
|
const Infinite = new InfiniteList(0, x => x + 2); |
||||
|
|
||||
|
const fourtyEight = Infinite.get(24); |
||||
|
const arr = [ |
||||
|
fourtyEight.next(), |
||||
|
fourtyEight.next(2), |
||||
|
fourtyEight.next(3), |
||||
|
fourtyEight.next(4) |
||||
|
] |
||||
|
|
||||
|
expect(arr.map(x => x.value)).toEqual([ 50, 52, 54, 56 ]); |
||||
|
}); |
||||
|
|
||||
|
it("Should be 98", () => { |
||||
|
const Infinite = new InfiniteList(0, x => x + 2); |
||||
|
const hundred = Infinite.get(50); |
||||
|
expect(hundred.previous().value).toBe(98); |
||||
|
}); |
||||
|
|
||||
|
it("Should be [ 46, 44, 42, 40 ]", () => { |
||||
|
const Infinite = new InfiniteList(0, x => x + 2); |
||||
|
|
||||
|
const fourtyEight = Infinite.get(24); |
||||
|
const arr = [ |
||||
|
fourtyEight.previous(), |
||||
|
fourtyEight.previous(2), |
||||
|
fourtyEight.previous(3), |
||||
|
fourtyEight.previous(4) |
||||
|
] |
||||
|
|
||||
|
expect(arr.map(x => x.value)).toEqual([ 46, 44, 42, 40 ]); |
||||
|
}); |
||||
|
|
||||
it("Should be [ 16, 18, 20 ]", () => { |
it("Should equal []", () => { |
||||
const infinite = InfiniteList.create(0, x => x + 2); |
const Infinite = new InfiniteList(0, x => x + 2); |
||||
expect(infinite.take(8, 10).map(x => x.value)).toEqual([ 16, 18, 20 ]); |
|
||||
}) |
expect(Infinite.take()).toEqual([ ]); |
||||
|
expect(Infinite.take(0)).toEqual([ ]); |
||||
|
}); |
||||
|
|
||||
|
it("Should equal [ 0 ]", () => { |
||||
|
const Infinite = new InfiniteList(0, x => x + 2); |
||||
|
|
||||
|
expect(Infinite.take(0, 0).map(x => x.value)).toEqual([ 0 ]); |
||||
|
}); |
||||
|
|
||||
|
it("Should equal [ 0, 2, 4, 6, 8 ]", () => { |
||||
|
const Infinite = new InfiniteList(0, x => x + 2); |
||||
|
expect(Infinite.take(5).map(x => x.value)).toEqual([0, 2, 4, 6, 8]); |
||||
|
}); |
||||
|
|
||||
|
it("Should equal [ 16, 18, 20 ]", () => { |
||||
|
const Infinite = new InfiniteList(0, x => x + 2); |
||||
|
expect(Infinite.take(8, 10).map(x => x.value)).toEqual([16, 18, 20]); |
||||
|
}); |
||||
|
|
||||
it("Should be [ 0, 2, 4, 6, 8 ]", () => { |
it("Should be [ 0, 2, 4, 6, 8 ]", () => { |
||||
const infinite = InfiniteList.create(0, x => x + 2); |
const Infinite = new InfiniteList(0, x => x + 2); |
||||
|
|
||||
const acc = []; |
const acc = []; |
||||
for(let i of infinite) { |
for (let i of Infinite) { |
||||
if(i.index >= 5) break; |
if (i.index >= 5) break; |
||||
acc.push(i.value) |
acc.push(i.value) |
||||
} |
} |
||||
expect(acc).toEqual([ 0, 2, 4, 6, 8 ]); |
|
||||
}) |
|
||||
|
|
||||
}) |
expect(acc).toEqual([0, 2, 4, 6, 8]); |
||||
|
}); |
||||
|
|
||||
|
it("Stringified InfiniteList should equal test string [0]", () => { |
||||
|
const a = { |
||||
|
i: 10 |
||||
|
} |
||||
|
a.a = a; |
||||
|
const next = x => { |
||||
|
const y = { |
||||
|
i: x.i + 5 |
||||
|
}; |
||||
|
y.a = y; |
||||
|
return y; |
||||
|
} |
||||
|
|
||||
|
const str = new InfiniteList(a, next) |
||||
|
.get(5) |
||||
|
.toString(); |
||||
|
|
||||
|
expect(str.trimify()).toBe(testStrings[0].trimify()); |
||||
|
}); |
||||
|
|
||||
|
it("Stringified InfiniteListItem should equal test string [1]", () => { |
||||
|
const a = { |
||||
|
i: 10 |
||||
|
} |
||||
|
a.a = a; |
||||
|
const next = x => { |
||||
|
const y = { |
||||
|
i: x.i + 5 |
||||
|
}; |
||||
|
y.a = y; |
||||
|
return y; |
||||
|
} |
||||
|
|
||||
|
const str = new InfiniteList(a, next).toString(); |
||||
|
|
||||
|
expect(str.trimify()).toBe(testStrings[1].trimify()); |
||||
|
}); |
||||
|
|
||||
|
}); |
||||
|
Loading…
Reference in new issue