Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tests/setup #17

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
10 changes: 10 additions & 0 deletions __tests__/assertions.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@ function expect(actual) {
throw new Error(actual + " is not equal to " + expected);
}
},
toEqualArray: function(expected) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this function taken from jest source?
Would be good if we not introduce new functions. This would allow to use jest on the CI server.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it's one i wrote.
Current functions don't allow to compare arrays, so that's how this one ended up here.

We should only stick to those 4 functions we already have?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope. My master plan is to write only functions jest also understands. So we can have tests running on Adobe products and also on CI server.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://repl.it/repls/NormalEmptyClicks

It seems that we should correct our implementation of toEqual, as in jest it can easily compare arrays. Testing our implementation in InDesign doesn't pass.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Totally. I just hacked that together. Really needs some love. :) Maybe I'll find some space at the weekend for it

if (Array.isArray(actual)) {
for(var i = 0; i < actual.length; ++i) {
if (actual[i] !== expected[i])
throw new Error(actual + " is not equal to " + expected);
}
} else {
throw new Error(actual + " is not equal to " + expected);
}
},
toBeGreaterThen: function(expected) {
if (actual <= expected) {
throw new Error(actual + " is not greater then " + expected);
Expand Down
75 changes: 74 additions & 1 deletion __tests__/manual.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
// @target indesign
// @target illustrator
// @include ./assertions.js
// @include ../index.js

describe("Array functions", function() {

// array.every
test("array every element of [1,2,3] should be smaller then 4", function() {
const arr = [1, 2, 3];
expect(
Expand All @@ -11,4 +13,75 @@ describe("Array functions", function() {
})
).toBe(true);
});

// array.filter
test("array filter (x<3) of [1, 2, 3, 4] should return [1, 2]", function() {
const arr = [1, 2, 3, 4];
expect(
arr.filter(function(ele) {
return ele < 3 ? true : false;
})
).toEqualArray([1,2]);
});

// array.forEach
test("array forEach (val + 1) of [1, 2, 3] should return [2, 3, 4]", function() {
const arr = [1, 2, 3];
const newArr = [];

arr.forEach(function(ele) {
newArr.push(ele + 1)
});

expect(newArr).toEqualArray([2, 3, 4]);
});

// array.indexOf
test("array indexOf (4) in [1, 2, 3, 4, 5, 6] should return 3", function() {
const arr = [1, 2, 3, 4, 5, 6];
expect(arr.indexOf(4)).toBe(3);
});

// array.isArray
test('array isArray on [1,2,3], should return true\n\tarray isArray on 1 should return false', function() {
const arr = [1,2,3];
expect(Array.isArray(arr)).toBe(true);
expect(Array.isArray(1)).toBe(false);
});

// array.lastIndexOf
test('array lastIndexOf(4) on [4, 1, 2, 3, 4] should return 4', function() {
const arr = [4, 1, 2, 3, 4];
expect(arr.lastIndexOf(4)).toBe(4);
});

// array.map
test('array map (val * 2) on [4, 1, 2, 3, 4] should return [8, 2, 4, 6, 8]', function() {
const arr = [4, 1, 2, 3, 4];
expect(arr.map(function(x) {return x * 2})).toEqualArray([8, 2, 4, 6, 8]);
});

// array.reduce
test('array reduce (sum) on [1, 2, 3, 4, 5] should return 15', function() {
const arr = [1, 2, 3, 4, 5];
expect(arr.reduce(function(acc, val) { return acc += val},0)).toBe(15);
});

// array.reduceRight
test('array reduceRight on [1, 2, 3, 4, 5] should return 15, and index should iterate this way [4, 3, 2, 1, 0]', function(){
const arr = [1, 2, 3, 4, 5];
const indexArr = [];
const sum = arr.reduceRight(function(acc, val, index) {
indexArr.push(index);
return acc += val;
}, 0);
expect(sum).toBe(15);
expect(indexArr).toEqualArray([4, 3, 2, 1, 0]);
});

// array.some
test('array some (val % 2 === 0) should return true', function() {
const arr = [1, 2, 3, 4, 5];
expect(arr.some(function(val) {return val % 2 === 0})).toBe(true);
});
});