mirror of
https://github.com/overte-org/overte.git
synced 2025-04-08 07:12:40 +02:00
Add resolvePath and include characterization/unit tests
This commit is contained in:
parent
f82a0196a8
commit
e794b64c31
10 changed files with 184 additions and 0 deletions
6
scripts/developer/tests/unit_tests/scriptTests/error.js
Normal file
6
scripts/developer/tests/unit_tests/scriptTests/error.js
Normal file
|
@ -0,0 +1,6 @@
|
|||
afterError = false;
|
||||
throw new Error('error.js');
|
||||
afterError = true;
|
||||
|
||||
(1,eval)('this').$finishes.push(Script.resolvePath(''));
|
||||
|
|
@ -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(''));
|
|
@ -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(''));
|
|
@ -0,0 +1,5 @@
|
|||
afterError = false;
|
||||
throw new Error('nested/error.js');
|
||||
afterError = true;
|
||||
|
||||
(1,eval)("this").$finishes.push(Script.resolvePath(''));
|
|
@ -0,0 +1,5 @@
|
|||
Script.include('sibling.js');
|
||||
inner = {
|
||||
lib: "nested/lib.js",
|
||||
};
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
sibling = {
|
||||
lib: "nested/sibling",
|
||||
};
|
|
@ -0,0 +1,3 @@
|
|||
function() {
|
||||
// intentional SyntaxError...
|
||||
|
|
@ -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(''));
|
|
@ -0,0 +1,5 @@
|
|||
Script.include('./nested/lib.js');
|
||||
outer = {
|
||||
inner: inner.lib,
|
||||
sibling: sibling.lib,
|
||||
};
|
126
scripts/developer/tests/unit_tests/scriptUnitTests.js
Normal file
126
scripts/developer/tests/unit_tests/scriptUnitTests.js
Normal file
|
@ -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();
|
Loading…
Reference in a new issue