[gjs/wip/ptomato/develop: 5/11] tests: Create test ASTs by parsing JS
- From: Philip Chimento <pchimento src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gjs/wip/ptomato/develop: 5/11] tests: Create test ASTs by parsing JS
- Date: Tue, 26 Sep 2017 06:52:21 +0000 (UTC)
commit 9001b6845b6ab3756cd2cca5b32c741a6c439735
Author: Philip Chimento <philip chimento gmail com>
Date: Fri Sep 22 23:37:28 2017 -0700
tests: Create test ASTs by parsing JS
The previous approach of object literals as ASTs is not robust when
SpiderMonkey adds to or changes the AST structure that its parser
returns.
https://bugzilla.gnome.org/show_bug.cgi?id=788166
installed-tests/js/testCoverage.js | 118 +++---------------------------------
1 files changed, 8 insertions(+), 110 deletions(-)
---
diff --git a/installed-tests/js/testCoverage.js b/installed-tests/js/testCoverage.js
index f2b723c..a5f8d30 100644
--- a/installed-tests/js/testCoverage.js
+++ b/installed-tests/js/testCoverage.js
@@ -826,55 +826,21 @@ describe('Coverage', function () {
});
it('function key from function with name matches schema', function () {
+ let ast = Reflect.parse('function f(a, b) {}').body[0];
let functionKeyForFunctionName =
- Coverage._getFunctionKeyFromReflectedFunction({
- id: {
- name: 'f'
- },
- loc: {
- start: {
- line: 1
- }
- },
- params: ['a', 'b']
- });
+ Coverage._getFunctionKeyFromReflectedFunction(ast);
expect(functionKeyForFunctionName).toEqual('f:1:2');
});
it('function key from function without name is anonymous', function () {
+ let ast = Reflect.parse('\nvoid function (a, b, c) {}').body[0].expression.argument;
let functionKeyForAnonymousFunction =
- Coverage._getFunctionKeyFromReflectedFunction({
- id: null,
- loc: {
- start: {
- line: 2
- }
- },
- params: ['a', 'b', 'c']
- });
+ Coverage._getFunctionKeyFromReflectedFunction(ast);
expect(functionKeyForAnonymousFunction).toEqual('(anonymous):2:3');
});
it('returns a function counter map for function keys', function () {
- let ast = {
- body: [{
- type: 'FunctionDeclaration',
- id: {
- name: 'name'
- },
- loc: {
- start: {
- line: 1
- }
- },
- params: [],
- body: {
- type: 'BlockStatement',
- body: []
- }
- }]
- };
-
+ let ast = Reflect.parse('function name() {}');
let detectedFunctions = Coverage.functionsForAST(ast);
let functionCounters =
Coverage._functionsToFunctionCounters('script', detectedFunctions);
@@ -883,40 +849,7 @@ describe('Coverage', function () {
it('reports an error when two indistinguishable functions are present', function () {
spyOn(window, 'log');
- let ast = {
- body: [{
- type: 'FunctionDeclaration',
- id: {
- name: '(anonymous)'
- },
- loc: {
- start: {
- line: 1
- }
- },
- params: [],
- body: {
- type: 'BlockStatement',
- body: []
- }
- }, {
- type: 'FunctionDeclaration',
- id: {
- name: '(anonymous)'
- },
- loc: {
- start: {
- line: 1
- }
- },
- params: [],
- body: {
- type: 'BlockStatement',
- body: []
- }
- }]
- };
-
+ let ast = Reflect.parse('() => {}; () => {}');
let detectedFunctions = Coverage.functionsForAST(ast);
Coverage._functionsToFunctionCounters('script', detectedFunctions);
@@ -1030,25 +963,7 @@ describe('Coverage.incrementFunctionCounters', function () {
});
it('increments for function on earlier start line', function () {
- let ast = {
- body: [{
- type: 'FunctionDeclaration',
- id: {
- name: 'name'
- },
- loc: {
- start: {
- line: 1
- }
- },
- params: [],
- body: {
- type: 'BlockStatement',
- body: []
- }
- }]
- };
-
+ let ast = Reflect.parse('function name() {}');
let detectedFunctions = Coverage.functionsForAST(ast);
let knownFunctionsArray = Coverage._populateKnownFunctions(detectedFunctions, 3);
let functionCounters = Coverage._functionsToFunctionCounters('script',
@@ -1062,24 +977,7 @@ describe('Coverage.incrementFunctionCounters', function () {
});
it('throws an error on unexpected function', function () {
- let ast = {
- body: [{
- type: 'FunctionDeclaration',
- id: {
- name: 'name'
- },
- loc: {
- start: {
- line: 1
- }
- },
- params: [],
- body: {
- type: 'BlockStatement',
- body: []
- }
- }]
- };
+ let ast = Reflect.parse('function name() {}');
let detectedFunctions = Coverage.functionsForAST(ast);
let knownFunctionsArray = Coverage._populateKnownFunctions(detectedFunctions, 3);
let functionCounters = Coverage._functionsToFunctionCounters('script',
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]