From 8fa1073076aea65f67d63f8b356eb20b5c24e266 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabian=20Mor=C3=B3n=20Zirfas?= Date: Mon, 28 Oct 2019 06:05:11 +0100 Subject: [PATCH 01/13] Assertion functions --- __tests__/assertions.js | 47 +++++++++++++++++++++++++++++++++++++++++ __tests__/manual.js | 14 ++++++++++++ jsconfig.json | 7 ++++++ 3 files changed, 68 insertions(+) create mode 100644 __tests__/assertions.js create mode 100644 __tests__/manual.js create mode 100644 jsconfig.json diff --git a/__tests__/assertions.js b/__tests__/assertions.js new file mode 100644 index 0000000..86c8152 --- /dev/null +++ b/__tests__/assertions.js @@ -0,0 +1,47 @@ +/* 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); + } + }, + 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..55bb965 --- /dev/null +++ b/__tests__/manual.js @@ -0,0 +1,14 @@ +// @target indesign +// @include ./assertions.js +// @include ../index.js + +describe("Array functions", function() { + 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); + }); +}); 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 From 47432585b77e007494e63328c7e718bb0dcad090 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabian=20Mor=C3=B3n=20Zirfas?= Date: Mon, 28 Oct 2019 06:06:09 +0100 Subject: [PATCH 02/13] allow launch.json for vscode --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 584e45b..0a6737c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ .vscode +!.vscode/launch.json # Logs logs From 12336d49d722165b84c6ef3a7999c122e0db070d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabian=20Mor=C3=B3n=20Zirfas?= Date: Mon, 28 Oct 2019 06:07:31 +0100 Subject: [PATCH 03/13] allow vscode settings --- .gitignore | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/.gitignore b/.gitignore index 0a6737c..9817db1 100644 --- a/.gitignore +++ b/.gitignore @@ -38,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 From a9f0552e417c8a51350e3f5795bb34d093be9081 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabian=20Mor=C3=B3n=20Zirfas?= Date: Mon, 28 Oct 2019 06:11:30 +0100 Subject: [PATCH 04/13] add test section --- README.md | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) 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 + } + ] +} +``` From ab8050c0dd380167921b5a3bc87267fce6f1ccb9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabian=20Mor=C3=B3n=20Zirfas?= Date: Mon, 28 Oct 2019 08:12:12 +0100 Subject: [PATCH 05/13] adds tests to test test functions --- __tests__/assertion_tests.js | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 __tests__/assertion_tests.js 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"); + }); +}); From 17c7091497ee91bce6dc5e8a18aa64a066734f53 Mon Sep 17 00:00:00 2001 From: lumenn Date: Wed, 30 Oct 2019 13:06:23 +0100 Subject: [PATCH 06/13] Added filter, forEach tests, and toEqualArray assertion --- __tests__/assertions.js | 10 ++++++++++ __tests__/manual.js | 24 +++++++++++++++++++++++- 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/__tests__/assertions.js b/__tests__/assertions.js index 86c8152..c6608fd 100644 --- a/__tests__/assertions.js +++ b/__tests__/assertions.js @@ -33,6 +33,16 @@ function expect(actual) { 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); diff --git a/__tests__/manual.js b/__tests__/manual.js index 55bb965..b841fb3 100644 --- a/__tests__/manual.js +++ b/__tests__/manual.js @@ -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( @@ -11,4 +13,24 @@ 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]); + }); }); From fce54395ba604e06d91bc16532a38d68e160df8c Mon Sep 17 00:00:00 2001 From: lumenn Date: Wed, 30 Oct 2019 13:09:11 +0100 Subject: [PATCH 07/13] Added indexOf test --- __tests__/manual.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/__tests__/manual.js b/__tests__/manual.js index b841fb3..3d22463 100644 --- a/__tests__/manual.js +++ b/__tests__/manual.js @@ -33,4 +33,9 @@ describe("Array functions", function() { 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); + }); }); From 2cb2f5b271e00ae985cf44f889d37d3cc5da1a54 Mon Sep 17 00:00:00 2001 From: lumenn Date: Wed, 30 Oct 2019 13:23:11 +0100 Subject: [PATCH 08/13] Added tests for isArray, lastIndexOf, map --- __tests__/manual.js | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/__tests__/manual.js b/__tests__/manual.js index 3d22463..4232908 100644 --- a/__tests__/manual.js +++ b/__tests__/manual.js @@ -13,6 +13,7 @@ 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]; @@ -22,6 +23,7 @@ describe("Array functions", function() { }) ).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]; @@ -33,9 +35,29 @@ describe("Array functions", function() { 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]); + }); }); From 3b5abd57dd84c848cd0c1ae234a4c39143cf0d1f Mon Sep 17 00:00:00 2001 From: lumenn Date: Wed, 30 Oct 2019 13:26:42 +0100 Subject: [PATCH 09/13] Added reduce test --- __tests__/manual.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/__tests__/manual.js b/__tests__/manual.js index 4232908..a49d36c 100644 --- a/__tests__/manual.js +++ b/__tests__/manual.js @@ -60,4 +60,10 @@ describe("Array functions", 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); + }) }); From de245893c77517776e3486e0cfb8c4480e40ff5b Mon Sep 17 00:00:00 2001 From: lumenn Date: Wed, 30 Oct 2019 13:32:16 +0100 Subject: [PATCH 10/13] Added reduceRight test --- __tests__/manual.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/__tests__/manual.js b/__tests__/manual.js index a49d36c..7a95842 100644 --- a/__tests__/manual.js +++ b/__tests__/manual.js @@ -65,5 +65,17 @@ describe("Array functions", function() { 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]); }) }); From 54080cc85ee987b465087d090102d789989047f9 Mon Sep 17 00:00:00 2001 From: lumenn Date: Wed, 30 Oct 2019 13:39:27 +0100 Subject: [PATCH 11/13] Added some tests --- __tests__/manual.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/__tests__/manual.js b/__tests__/manual.js index 7a95842..472a906 100644 --- a/__tests__/manual.js +++ b/__tests__/manual.js @@ -77,5 +77,11 @@ describe("Array functions", function() { }, 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); + }); }); From b1e9d768239fa155145a121b19eb098f97f04938 Mon Sep 17 00:00:00 2001 From: lumenn Date: Wed, 30 Oct 2019 13:43:55 +0100 Subject: [PATCH 12/13] Checked also inside InDesign, and rolled back to InDesign as standard testing env --- __tests__/manual.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/__tests__/manual.js b/__tests__/manual.js index 472a906..62407b8 100644 --- a/__tests__/manual.js +++ b/__tests__/manual.js @@ -1,4 +1,4 @@ -// @target illustrator +// @target indesign // @include ./assertions.js // @include ../index.js From c3eacc35770491bf619cdb49035bba37b57df84e Mon Sep 17 00:00:00 2001 From: fabianmoronzirfas Date: Tue, 6 Oct 2020 07:41:03 +0200 Subject: [PATCH 13/13] test setup stashing --- jest.config.js | 1 + 1 file changed, 1 insertion(+) 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}",