Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions lib/node_modules/@stdlib/_tools/doctest/compare-values/lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

// MODULES //

var isNegativeZero = require( '@stdlib/math/base/assert/is-negative-zero' );
var isPositiveZero = require( '@stdlib/math/base/assert/is-positive-zero' );
var roundn = require( '@stdlib/math/base/special/roundn' );
var epsdiff = require( '@stdlib/math/base/utils/float64-epsilon-difference' );
var indexOf = require( '@stdlib/array/base/index-of' );
Expand Down Expand Up @@ -151,6 +153,9 @@
if ( isBigInt( value ) ) {
return value.toString() + 'n';
}
if ( isNegativeZero( value ) ) {
return '-0.0';
}
return value;
}

Expand Down Expand Up @@ -179,7 +184,7 @@
*
* @private
* @param {(string|boolean|number|bigint|null|void)} actual - primitive value
* @param {string} expected - return value annotation
* @param {(string|boolean|number|bigint|null|void)} expected - return value annotation
* @returns {boolean} boolean indicating whether a value matches a return annotation
*/
function checkPrimitive( actual, expected ) {
Expand All @@ -203,6 +208,10 @@
return false;
}
}
if ( isNegativeZero( expected ) ) {
expected = '-0.0';
}

expected = String( expected );
if ( type === 'number' ) {
if ( expected === 'Infinity' || expected === '+Infinity' ) {
Expand All @@ -214,6 +223,12 @@
if ( expected === 'NaN' ) {
return isnan( actual );
}
if ( expected === '-0.0' || expected === '-0' ) {
return isNegativeZero( actual );
}
if ( expected === '0.0' || expected === '0' || expected === '+0.0' || expected === '+0' ) {
return isPositiveZero( actual );
}
if ( startsWith( expected, '~' ) ) {
if ( contains( expected, 'e' ) ) {
dgts = indexOf( expected, 'e', 0 ) - indexOf( expected, '.', 0 );
Expand Down Expand Up @@ -362,7 +377,7 @@
default:
// Unknown type, let it slide...

// TODO: should we compare constructor names here at a minimum? or perhaps walk the prototype tree and compare constructor names to determine whether a value is an instance of?

Check warning on line 380 in lib/node_modules/@stdlib/_tools/doctest/compare-values/lib/main.js

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Unexpected 'todo' comment: 'TODO: should we compare constructor...'
return true;
}
}
Expand All @@ -378,11 +393,11 @@
function compareEntries( actual, expected ) {
var i;
if ( actual.length !== expected.length ) {
return format( 'Expected entries [%s], but observed [%s]', map( expected, formatValue ), map( actual, formatValue ) );
return format( 'Expected entries [%s], but observed [%s]', map( expected, formatValue ), map( copyArray( actual ), formatValue ) );
}
for ( i = 0; i < expected.length; i++ ) {
if ( !checkPrimitive( actual[ i ], expected[ i ] ) ) {
return format( 'Expected entries [%s], but observed [%s]', map( expected, formatValue ), map( actual, formatValue ) );
return format( 'Expected entries [%s], but observed [%s]', map( expected, formatValue ), map( copyArray( actual ), formatValue ) );
}
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,81 @@
t.end();
});

tape( 'the function differentiates between positive and negative zero', function test( t ) {
var expected;
var actual;
var msg;

actual = 0.0;
expected = '0.0';
t.strictEqual( compareValues( actual, expected ), null, 'returns expected value' );

actual = 0.0;
expected = '0';
t.strictEqual( compareValues( actual, expected ), null, 'returns expected value' );

actual = 0.0;
expected = '+0.0';
t.strictEqual( compareValues( actual, expected ), null, 'returns expected value' );

actual = -0.0;
expected = '-0.0';
t.strictEqual( compareValues( actual, expected ), null, 'returns expected value' );

actual = -0.0;
expected = '-0';
t.strictEqual( compareValues( actual, expected ), null, 'returns expected value' );

actual = -0.0;
expected = '0.0';
msg = 'Displayed return value is `0.0`, but expected `-0.0` instead';
t.strictEqual( compareValues( actual, expected ), msg, 'returns expected message' );

actual = -0.0;
expected = '0';
msg = 'Displayed return value is `0`, but expected `-0.0` instead';
t.strictEqual( compareValues( actual, expected ), msg, 'returns expected message' );

actual = 0.0;
expected = '-0.0';
msg = 'Displayed return value is `-0.0`, but expected `0` instead';
t.strictEqual( compareValues( actual, expected ), msg, 'returns expected message' );

actual = 0.0;
expected = '-0';
msg = 'Displayed return value is `-0`, but expected `0` instead';
t.strictEqual( compareValues( actual, expected ), msg, 'returns expected message' );

actual = [ +0, -0 ];
expected = '[ +0, -0 ]';
t.strictEqual( compareValues( actual, expected ), null, 'returns expected value' );

actual = [ -0, +0 ];
expected = '[ -0, +0 ]';
t.strictEqual( compareValues( actual, expected ), null, 'returns expected value' );

actual = [ -0 ];
expected = '[ +0 ]';
msg = 'Displayed return value is `[ +0 ]`, but expected `[ 0 ]` instead';
t.strictEqual( compareValues( actual, expected ), msg, 'returns expected message' );

actual = [ +0 ];
expected = '[ -0 ]';
msg = 'Displayed return value is `[ -0 ]`, but expected `[ 0 ]` instead';
t.strictEqual( compareValues( actual, expected ), msg, 'returns expected message' );

actual = new Float64Array( [ -0.0, 0.0 ] );
expected = '<Float64Array>[ -0.0, 0.0 ]';

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is definitely an improvement!

t.strictEqual( compareValues( actual, expected ), null, 'returns expected value' );

actual = new Float64Array( [ -0.0 ] );
expected = '<Float64Array>[ 0.0 ]';
msg = 'Expected entries [0], but observed [-0.0]';
t.strictEqual( compareValues( actual, expected ), msg, 'returns expected message' );

t.end();
});

tape( 'the function compares an array and a corresponding return annotation', function test( t ) { // eslint-disable-line max-statements
var expected;
var actual;
Expand Down Expand Up @@ -586,7 +661,7 @@

actual = new Int64( 1234 );
expected = '<Uint64>';
msg = 'Expected a <Uint64>, but received: `1234`'; // FIXME: `1234` doesn't seem right in this context, we should mention received `Int64`

Check warning on line 664 in lib/node_modules/@stdlib/_tools/doctest/compare-values/test/test.js

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Unexpected 'fixme' comment: 'FIXME: `1234` doesn't seem right in this...'
t.strictEqual( compareValues( actual, expected ), msg, 'returns expected message' );

actual = new Int64( 1234 );
Expand All @@ -596,7 +671,7 @@

actual = new Int64( 1234 );
expected = '<Something>';
msg = null; // FIXME: this should not return null, it's a false negative

Check warning on line 674 in lib/node_modules/@stdlib/_tools/doctest/compare-values/test/test.js

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Unexpected 'fixme' comment: 'FIXME: this should not return null, it's...'
t.strictEqual( compareValues( actual, expected ), null, 'returns expected message' );

actual = new Int64( 1234 );
Expand Down Expand Up @@ -630,7 +705,7 @@

actual = new Uint64( 1234 );
expected = '<Int64>';
msg = 'Expected a <Int64>, but received: `1234`'; // FIXME: `1234` doesn't seem right in this context, we should mention received `Uint64`

Check warning on line 708 in lib/node_modules/@stdlib/_tools/doctest/compare-values/test/test.js

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Unexpected 'fixme' comment: 'FIXME: `1234` doesn't seem right in this...'
t.strictEqual( compareValues( actual, expected ), msg, 'returns expected message' );

actual = new Uint64( 1234 );
Expand All @@ -640,7 +715,7 @@

actual = new Uint64( 1234 );
expected = '<Something>';
msg = null; // FIXME: this should not return null, it's a false negative

Check warning on line 718 in lib/node_modules/@stdlib/_tools/doctest/compare-values/test/test.js

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Unexpected 'fixme' comment: 'FIXME: this should not return null, it's...'
t.strictEqual( compareValues( actual, expected ), null, 'returns expected message' );

actual = new Uint64( 1234 );
Expand Down
Loading