diff --git a/.gitignore b/.gitignore index 584e45b..9817db1 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ .vscode +!.vscode/launch.json # Logs logs @@ -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 diff --git a/README.md b/README.md index fdcd9c1..00f2303 100644 --- a/README.md +++ b/README.md @@ -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 + } + ] +} +``` diff --git a/__tests__/assertion_tests.js b/__tests__/assertion_tests.js new file mode 100644 index 0000000..aebfac5 --- /dev/null +++ b/__tests__/assertion_tests.js @@ -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"); + }); +}); diff --git a/__tests__/assertions.js b/__tests__/assertions.js new file mode 100644 index 0000000..c6608fd --- /dev/null +++ b/__tests__/assertions.js @@ -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) { + 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); + } + } + }; +} diff --git a/__tests__/manual.js b/__tests__/manual.js new file mode 100644 index 0000000..62407b8 --- /dev/null +++ b/__tests__/manual.js @@ -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); + }); +}); diff --git a/jest.config.js b/jest.config.js index 2289901..d144b2d 100644 --- a/jest.config.js +++ b/jest.config.js @@ -2,6 +2,7 @@ module.exports = { testEnvironment: "jest-environment-node", collectCoverage: true, coverageReporters: ["lcov", "text"], + testPathIgnorePatterns: ["/__tests__/assertions.js"], collectCoverageFrom: [ "Array/**/*.{js}", "Date/**/*.{js}", diff --git a/jsconfig.json b/jsconfig.json new file mode 100644 index 0000000..db015a7 --- /dev/null +++ b/jsconfig.json @@ -0,0 +1,7 @@ +{ + "compilerOptions": { + "module": "none", + "target": "es3" + }, + "exclude": ["node_modules", "__tests__"] +} \ No newline at end of file