From e794b64c31e748c5aaeff860b566a1d8ed2ee758 Mon Sep 17 00:00:00 2001 From: humbletim Date: Thu, 23 Feb 2017 18:13:24 -0500 Subject: [PATCH] Add resolvePath and include characterization/unit tests --- .../tests/unit_tests/scriptTests/error.js | 6 + .../unit_tests/scriptTests/nested-error.js | 10 ++ .../scriptTests/nested-syntax-error.js | 10 ++ .../unit_tests/scriptTests/nested/error.js | 5 + .../unit_tests/scriptTests/nested/lib.js | 5 + .../unit_tests/scriptTests/nested/sibling.js | 3 + .../scriptTests/nested/syntax-error.js | 3 + .../unit_tests/scriptTests/top-level-error.js | 11 ++ .../tests/unit_tests/scriptTests/top-level.js | 5 + .../tests/unit_tests/scriptUnitTests.js | 126 ++++++++++++++++++ 10 files changed, 184 insertions(+) create mode 100644 scripts/developer/tests/unit_tests/scriptTests/error.js create mode 100644 scripts/developer/tests/unit_tests/scriptTests/nested-error.js create mode 100644 scripts/developer/tests/unit_tests/scriptTests/nested-syntax-error.js create mode 100644 scripts/developer/tests/unit_tests/scriptTests/nested/error.js create mode 100644 scripts/developer/tests/unit_tests/scriptTests/nested/lib.js create mode 100644 scripts/developer/tests/unit_tests/scriptTests/nested/sibling.js create mode 100644 scripts/developer/tests/unit_tests/scriptTests/nested/syntax-error.js create mode 100644 scripts/developer/tests/unit_tests/scriptTests/top-level-error.js create mode 100644 scripts/developer/tests/unit_tests/scriptTests/top-level.js create mode 100644 scripts/developer/tests/unit_tests/scriptUnitTests.js diff --git a/scripts/developer/tests/unit_tests/scriptTests/error.js b/scripts/developer/tests/unit_tests/scriptTests/error.js new file mode 100644 index 0000000000..6b9a7c7445 --- /dev/null +++ b/scripts/developer/tests/unit_tests/scriptTests/error.js @@ -0,0 +1,6 @@ +afterError = false; +throw new Error('error.js'); +afterError = true; + +(1,eval)('this').$finishes.push(Script.resolvePath('')); + diff --git a/scripts/developer/tests/unit_tests/scriptTests/nested-error.js b/scripts/developer/tests/unit_tests/scriptTests/nested-error.js new file mode 100644 index 0000000000..3a190545ef --- /dev/null +++ b/scripts/developer/tests/unit_tests/scriptTests/nested-error.js @@ -0,0 +1,10 @@ +afterError = false; +outer = null; +Script.include('./nested/error.js?' + Settings.getValue('cache_buster')); +outer = { + inner: inner.lib, + sibling: sibling.lib, +}; +afterError = true; + +(1,eval)("this").$finishes.push(Script.resolvePath('')); diff --git a/scripts/developer/tests/unit_tests/scriptTests/nested-syntax-error.js b/scripts/developer/tests/unit_tests/scriptTests/nested-syntax-error.js new file mode 100644 index 0000000000..fb0e3679ff --- /dev/null +++ b/scripts/developer/tests/unit_tests/scriptTests/nested-syntax-error.js @@ -0,0 +1,10 @@ +afterError = false; +outer = null; +Script.include('./nested/syntax-error.js?' + Settings.getValue('cache_buster')); +outer = { + inner: inner.lib, + sibling: sibling.lib, +}; +afterError = true; + +(1,eval)("this").$finishes.push(Script.resolvePath('')); diff --git a/scripts/developer/tests/unit_tests/scriptTests/nested/error.js b/scripts/developer/tests/unit_tests/scriptTests/nested/error.js new file mode 100644 index 0000000000..aeb76eec01 --- /dev/null +++ b/scripts/developer/tests/unit_tests/scriptTests/nested/error.js @@ -0,0 +1,5 @@ +afterError = false; +throw new Error('nested/error.js'); +afterError = true; + +(1,eval)("this").$finishes.push(Script.resolvePath('')); diff --git a/scripts/developer/tests/unit_tests/scriptTests/nested/lib.js b/scripts/developer/tests/unit_tests/scriptTests/nested/lib.js new file mode 100644 index 0000000000..1c2cf3b885 --- /dev/null +++ b/scripts/developer/tests/unit_tests/scriptTests/nested/lib.js @@ -0,0 +1,5 @@ +Script.include('sibling.js'); +inner = { + lib: "nested/lib.js", +}; + diff --git a/scripts/developer/tests/unit_tests/scriptTests/nested/sibling.js b/scripts/developer/tests/unit_tests/scriptTests/nested/sibling.js new file mode 100644 index 0000000000..33fa068079 --- /dev/null +++ b/scripts/developer/tests/unit_tests/scriptTests/nested/sibling.js @@ -0,0 +1,3 @@ +sibling = { + lib: "nested/sibling", +}; diff --git a/scripts/developer/tests/unit_tests/scriptTests/nested/syntax-error.js b/scripts/developer/tests/unit_tests/scriptTests/nested/syntax-error.js new file mode 100644 index 0000000000..3b578c2674 --- /dev/null +++ b/scripts/developer/tests/unit_tests/scriptTests/nested/syntax-error.js @@ -0,0 +1,3 @@ +function() { + // intentional SyntaxError... + diff --git a/scripts/developer/tests/unit_tests/scriptTests/top-level-error.js b/scripts/developer/tests/unit_tests/scriptTests/top-level-error.js new file mode 100644 index 0000000000..4ef90ec238 --- /dev/null +++ b/scripts/developer/tests/unit_tests/scriptTests/top-level-error.js @@ -0,0 +1,11 @@ +afterError = false; +outer = null; +Script.include('./nested/lib.js'); +Undefined_symbol; +outer = { + inner: inner.lib, + sibling: sibling.lib, +}; +afterError = true; + +(1,eval)("this").$finishes.push(Script.resolvePath('')); diff --git a/scripts/developer/tests/unit_tests/scriptTests/top-level.js b/scripts/developer/tests/unit_tests/scriptTests/top-level.js new file mode 100644 index 0000000000..ab55007fe9 --- /dev/null +++ b/scripts/developer/tests/unit_tests/scriptTests/top-level.js @@ -0,0 +1,5 @@ +Script.include('./nested/lib.js'); +outer = { + inner: inner.lib, + sibling: sibling.lib, +}; diff --git a/scripts/developer/tests/unit_tests/scriptUnitTests.js b/scripts/developer/tests/unit_tests/scriptUnitTests.js new file mode 100644 index 0000000000..d464682ca9 --- /dev/null +++ b/scripts/developer/tests/unit_tests/scriptUnitTests.js @@ -0,0 +1,126 @@ +/* eslint-env jasmine */ + +instrument_testrunner(); + +describe('Script', function () { + // get the current filename without calling Script.resolvePath('') + try { + throw new Error('stack'); + } catch(e) { + var filename = e.fileName; + var dirname = filename.split('/').slice(0, -1).join('/') + '/'; + var parentdir = dirname.split('/').slice(0, -2).join('/') + '/'; + } + + // characterization tests + // initially these are just to capture how the app works currently + var testCases = { + '': filename, + '.': dirname, + '..': parentdir, + 'about:Entities 1': '', + 'Entities 1': dirname + 'Entities 1', + './file.js': dirname + 'file.js', + 'c:/temp/': 'file:///c:/temp/', + 'c:/temp': 'file:///c:/temp', + '/temp/': 'file:///temp/', + 'c:/': 'file:///c:/', + 'c:': 'file:///c:', + 'file:///~/libraries/a.js': 'file:///~/libraries/a.js', + '/temp/tested/../file.js': 'file:///temp/tested/../file.js', + '/~/libraries/utils.js': 'file:///~/libraries/utils.js', + '/temp/file.js': 'file:///temp/file.js', + '/~/': 'file:///~/', + }; + } + describe('resolvePath', function () { + Object.keys(testCases).forEach(function(input) { + it('(' + JSON.stringify(input) + ')', function () { + expect(Script.resolvePath(input)).toEqual(testCases[input]); + }); + }); + }); + + describe('include', function () { + var old_cache_buster; + var cache_buster = '#' + +new Date; + beforeAll(function() { + old_cache_buster = Settings.getValue('cache_buster'); + Settings.setValue('cache_buster', cache_buster); + }); + afterAll(function() { + Settings.setValue('cache_buster', old_cache_buster); + }); + beforeEach(function() { + vec3toStr = undefined; + }); + it('file:///~/system/libraries/utils.js' + cache_buster, function() { + Script.include('file:///~/system/libraries/utils.js' + cache_buster); + expect(vec3toStr).toEqual(jasmine.any(Function)); + }); + it('nested' + cache_buster, function() { + Script.include('./scriptTests/top-level.js' + cache_buster); + expect(outer).toEqual(jasmine.any(Object)); + expect(inner).toEqual(jasmine.any(Object)); + expect(sibling).toEqual(jasmine.any(Object)); + expect(outer.inner).toEqual(inner.lib); + expect(outer.sibling).toEqual(sibling.lib); + }); + describe('errors' + cache_buster, function() { + var finishes, oldFinishes; + beforeAll(function() { + oldFinishes = (1,eval)('this').$finishes; + }); + afterAll(function() { + (1,eval)('this').$finishes = oldFinishes; + }); + beforeEach(function() { + finishes = (1,eval)('this').$finishes = []; + }); + it('error', function() { + // a thrown Error in top-level include aborts that include, but does not throw the error back to JS + expect(function() { + Script.include('./scriptTests/error.js' + cache_buster); + }).not.toThrowError("error.js"); + expect(finishes.length).toBe(0); + }); + it('top-level-error', function() { + // an organice Error in a top-level include aborts that include, but does not throw the error + expect(function() { + Script.include('./scriptTests/top-level-error.js' + cache_buster); + }).not.toThrowError(/Undefined_symbol/); + expect(finishes.length).toBe(0); + }); + it('nested', function() { + // a thrown Error in a nested include aborts the nested include, but does not abort the top-level script + expect(function() { + Script.include('./scriptTests/nested-error.js' + cache_buster); + }).not.toThrowError("nested/error.js"); + expect(finishes.length).toBe(1); + }); + it('nested-syntax-error', function() { + // a SyntaxError in a nested include breaks only that include (the main script should finish unimpeded) + expect(function() { + Script.include('./scriptTests/nested-syntax-error.js' + cache_buster); + }).not.toThrowError(/SyntaxEror/); + expect(finishes.length).toBe(1); + }); + }); + }); +}); + +// enable scriptUnitTests to be loaded directly +function run() {} +function instrument_testrunner() { + if (typeof describe === 'undefined') { + print('instrumenting jasmine', Script.resolvePath('')); + Script.include('../../libraries/jasmine/jasmine.js'); + Script.include('../../libraries/jasmine/hifi-boot.js'); + jasmine.getEnv().addReporter({ jasmineDone: Script.stop }); + run = function() { + print('executing jasmine', Script.resolvePath('')); + jasmine.getEnv().execute(); + }; + } +} +run();