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

Assert: Support compare primitive objects in deepEqual #905

Closed
wants to merge 5 commits into from

Conversation

mixed
Copy link
Contributor

@mixed mixed commented Dec 15, 2015

Ref #895

@mixed mixed changed the title Assert: Support compare primitive objects. Assert: Support compare primitive objects in deepEqual Dec 15, 2015
QUnit.test( "Compare primitive objects", function( assert ) {
var SafeNumber = Number,
SafeString = String,
SafeBoolean = Boolean;
Copy link
Member

Choose a reason for hiding this comment

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

why saving these references? Does not look necessary.

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
For lint validation. like it link

:)

Copy link
Member

Choose a reason for hiding this comment

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

Oops, makes sense.

We should fix the code style here, that's relatively new on the code style, but we should declare all the variables with a new var statement.

var SafeNumber = Number;
var SafeString = String;
var SafeBoolean = Boolean;

@leobalter
Copy link
Member

I know I commented on several details, but this is a great work adding a great value to QUnit.equiv.

I'm looking forward to merge this PR. Thanks, @mixed!

@mixed
Copy link
Contributor Author

mixed commented Dec 15, 2015

@leobalter
Thank you. Good review for me.

@@ -1581,6 +1581,39 @@ QUnit.test( "Test that must be done at the end because they extend some primitiv
}
);

QUnit.test( "Compare Number values", function( assert ) {
Copy link
Member

Choose a reason for hiding this comment

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

@mixed: would you create a single module on the top of these tests called "Compare primitive values"?

Example:

QUnit.module( "Compare primitive values" );

@leobalter
Copy link
Member

We're close!


// Compare same constructor.
return a.toString() == b.toString();
Copy link
Member

Choose a reason for hiding this comment

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

I think this could be generalized and simplified a bit further, to something like

if ( typeof a === "object" ) {
    a = a.valueOf();
}
if ( typeof b === "object" ) {
    b = b.valueOf();
}
return a === b;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@gibson042
Thank you. I think of it again. I think this don't have to check type. Could you please explain to me more detail about check type? And How about think below logic?

function useStrictEquality( b, a ) {
    if ( b instanceof a.constructor || a instanceof b.constructor ) {
        return a.valueOf() === b.valueOf();
    } else {
        return a === b;
    }
}

Copy link
Member

Choose a reason for hiding this comment

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

Well, there are two issues. First, and most importantly, we should probably have an automatic return true in a separate guaranteed function for null, undefined, and nan "types". Having cleared those out of the way, there would be a few options for detecting wrapped primitives, and I like typeof object because it strikes me as the most semantic. And separate from all of that, it's just nice to see a single return with uniform comparison logic.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@gibson042

Thank you. :)
I have misunderstood it. but I get it now. I think that check 'constructor' and check 'typeof' before I understand your comment.

Good comments. I updating it soon.

@leobalter
Copy link
Member

@gibson042 can we merge this?

@gibson042
Copy link
Member

I increased test coverage a little and restored an explanatory comment, but yes! 😀

@leobalter
Copy link
Member

This is awesome! thanks, @mixed and @gibson042

flore77 pushed a commit to flore77/qunit that referenced this pull request Aug 10, 2016
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.

None yet

4 participants