Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Core: Fallback to typeof obj in QUnit.objectType. #1199

Merged
merged 1 commit into from
Jun 16, 2017

Conversation

rwjblue
Copy link
Contributor

@rwjblue rwjblue commented Jun 15, 2017

Prior to these changes, QUnit.objectType(async function() { }); would return undefined (because Object.prototype.toString.call(async function() {}) equals [object AsyncFunction])).

This updates QUnit.objectType to fallback to returning typeof obj if no match was found, and adds explicit tests for the various expected inputs and outputs.

I discovered during the investigation of #1197 which is failing in [email protected] due to QUnit.objectType not being aware of async functions (but master was fixed by a refactor done in #1188).

try {
var async = new Function( "return async function( ) { };" )( );
objects.push( async );
} catch ( e ) { }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why this try / catch + wrapping the function forms in the Function ctor? ES6 compat?

This might escalate in a weird way, as we might also have method definitions, generators, classes, async generators...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@leobalter - The same tests are ran on IE, Phantom, possibly Node 6, etc where async function() { } would be a parse error. The intent of the try / catch is to ensure that when async function() { } is not valid syntax, the tests for the plain function still runs.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I got it! Thanks for the clarification!

btw eval allows a shorter syntax, but does the same:
new Function( "return async function( ) { };" )( ) => eval( "async function( ) {};" )

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@leobalter - HA! Yes, I started by trying to use eval, but I do not think that it allows returning the eval'ed value. This is important for us, because we have to ensure that having the syntax does not throw during evaluation of the whole file (and therefore miss all these tests).

Have I missed something? Is there a better way?

validateObjectType( [ "", "foo", String( "bar" ) ], "string" );
validateObjectType( [ null ], "null" );
validateObjectType( [ true, false, Boolean( true ) ], "boolean" );
validateObjectType( [ [], new Array( ) ], "array" );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the literal and ctor forms of Array are well tested on JS (Test262), same for primitive Number, String and Boolean types.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome! Do you think I should just remove the non-primitive versions here then?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm +1 to keep only the primitive and literal values here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good, updated.

validateObjectType( buildFunctions(), "function" );
validateObjectType( typeof Symbol === "function" ? [ Symbol( "HI!" ) ] : [ ], "symbol" );
validateObjectType( typeof Map === "function" ? [ new Map( ) ] : [ ], "map" );
validateObjectType( typeof Set === "function" ? [ new Set( ) ] : [ ], "set" );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

where is the ordinary object? {}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and undefined

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! Added.

validateObjectType( [ [], new Array( ) ], "array" );
validateObjectType( [ new Date( ) ], "date" );
validateObjectType( [ /foo/, new RegExp( "foo" ) ], "regexp" );
validateObjectType( [ new Date( ) ], "date" );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

duplicate

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whoops! Thats what I get for copy and pasting 😝

@rwjblue rwjblue force-pushed the default-to-typeof branch 3 times, most recently from 22a8e96 to 08e1fae Compare June 15, 2017 22:24
Prior to these changes, `QUnit.objectType(async function() { });`
would return `undefined` (because
`Object.prototype.toString.call(async function() {})` equals
`[object AsyncFunction])`).

This updates `QUnit.objectType` to fallback to returning `typeof obj`
if no match was found, and adds explicit tests for the various expected
inputs and outputs.
@rwjblue
Copy link
Contributor Author

rwjblue commented Jun 15, 2017

Updated for feedback, and also updated Gruntfile.js so that the tests are also ran in node-land.

Copy link
Member

@trentmwillis trentmwillis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me. Thanks for adding tests (honestly forgot we exposed this as public API).

@leobalter will wait a bit before merging in case you want to do another review.

@trentmwillis trentmwillis merged commit 6f8ddd3 into qunitjs:master Jun 16, 2017
openui5bot pushed a commit to SAP/openui5 that referenced this pull request Jul 4, 2023
This is a cherry-pick of qunitjs/qunit#1199.

Change-Id: Ibc3a101575bfb57780e8838bc8b9e4ba4fe514f4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants