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
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.vscode
!.vscode/launch.json

# Logs
logs
Expand Down Expand Up @@ -37,3 +38,10 @@ jspm_packages

# Optional REPL history
.node_repl_history

.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
*.code-workspace
23 changes: 22 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,25 @@ Use build in `__proto__` property as return value.
- If you need new folders, add the folder to the top of `./bin/concat.js` into the `folders` array



## Tests

To run and write tests in VSCode you'll need this `.vscode/launch.json` file

```json
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [

{
"type": "extendscript-debug",
"request": "launch",
"name": "tests",
"program": "${workspaceFolder}/__tests__/manual.js",
"stopOnEntry": false
}
]
}
```
10 changes: 10 additions & 0 deletions __tests__/assertion_tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// @target indesign
// @include ./assertions.js

describe("Testing test functions", function() {
test("toBe", function() {
expect(1).toBe(1);
expect(true).toBe(true);
expect("foo").toBe("foo");
});
});
57 changes: 57 additions & 0 deletions __tests__/assertions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/* globals $ */

function describe(description, descCB) {
if ($.writeln !== undefined) {
$.writeln(description);
}
descCB();
}

function test(description, testCB) {
try {
testCB();
if ($.writeln !== undefined) {
$.writeln("✓ " + description);
}
} catch (error) {
if ($.writeln !== undefined) {
$.writeln("✘ " + description);
$.writeln(error);
}
}
}

function expect(actual) {
return {
toBe: function(expected) {
if (actual !== expected) {
throw new Error(actual + " is not equal to " + expected);
}
},
toEqual: function(expected) {
if (actual !== expected) {
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);
}
},
toBeLessThen: function(expected) {
if (actual >= expected) {
throw new Error(actual + " is not less then " + expected);
}
}
};
}
87 changes: 87 additions & 0 deletions __tests__/manual.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// @target indesign
// @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(
arr.every(function(ele) {
return ele < 4;
})
).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);
});
});
1 change: 1 addition & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module.exports = {
testEnvironment: "jest-environment-node",
collectCoverage: true,
coverageReporters: ["lcov", "text"],
testPathIgnorePatterns: ["<rootDir>/__tests__/assertions.js"],
collectCoverageFrom: [
"Array/**/*.{js}",
"Date/**/*.{js}",
Expand Down
7 changes: 7 additions & 0 deletions jsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"compilerOptions": {
"module": "none",
"target": "es3"
},
"exclude": ["node_modules", "__tests__"]
}