Skip to content
Open
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
11 changes: 11 additions & 0 deletions lib/node_modules/@stdlib/_tools/doctest/compare-values/lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@
var RE_INSTANCE_ANNOTATION = /<([^>]+)>(\[?[^\]]*\]?)/;
var RE_ARRAY = /(\[[\s\S]+\])/;
var RE_NDARRAY = /<([^>]+)>(\[[\s\S]+\])/;
var RE_LEADING_INSTANCE_ANNOTATION = /^\s*<([^>]+)>/; // eslint-disable-line id-length

var RE_UNESCAPED_QUOTE = /[^\\]'/;
var BOXED_TYPE_ANNOTATIONS = [
Expand Down Expand Up @@ -377,7 +378,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 381 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 Down Expand Up @@ -523,6 +524,10 @@
if ( actual.length === 0 ) {
return expected === '[]';
}
// Reject instance annotations (e.g., `<Float64Array>[ 1.0, 2.0 ]`):
if ( RE_LEADING_INSTANCE_ANNOTATION.test( expected ) ) {
return false;
}
match = RE_ARRAY.exec( expected );
if ( match ) {
entries = parse( match[ 1 ] );
Expand Down Expand Up @@ -635,6 +640,7 @@
var expectedValue;
var actualString;
var actualValue;
var match;
var parts;
var msg1;
var msg2;
Expand Down Expand Up @@ -711,6 +717,11 @@
}
// Case: compareValues( [ 1.0, 2.0 ], '[ 1.0, 2.0 ]' )
if ( isArray( actual ) ) {
// Case: compareValues( [ 1.0, 2.0 ], '<Float64Array>[ 1.0, 2.0 ]' )
match = RE_LEADING_INSTANCE_ANNOTATION.exec( expected );
if ( match ) {
return format( 'Expected instance type <%s>, but observed <%s>', match[ 1 ], constructorName( actual ) );
}
if ( !checkArray( actual, expected ) ) {
return format( 'Displayed return value is `%s`, but expected `%s` instead', expected, createAnnotationValue( actual ) );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,52 @@
t.strictEqual( compareValues( actual, expected ), null, 'returns expected value' );
}

actual = new Float64Array( [ 1.0, 2.0, 3.0 ] );
expected = '[ 1.0, 2.0, 3.0 ]';
msg = 'Typed arrays should be documented using instance annotation';
t.strictEqual( compareValues( actual, expected ), msg, 'returns expected message' );

t.end();
});

tape( 'the function detects a mismatch when a generic array is documented using a typed array instance annotation', function test( t ) {
var expected;
var actual;
var msg;

actual = [ 1.0, 2.0, 3.0 ];
expected = '<Float64Array>[ 1.0, 2.0, 3.0 ]';
msg = 'Expected instance type <Float64Array>, but observed <Array>';
t.strictEqual( compareValues( actual, expected ), msg, 'returns expected message' );

actual = [ 1, 2, 3 ];
expected = '<Int32Array>[ 1, 2, 3 ]';
msg = 'Expected instance type <Int32Array>, but observed <Array>';
t.strictEqual( compareValues( actual, expected ), msg, 'returns expected message' );

actual = [ 1.0, 2.0, 3.0 ];
expected = '<Float32Array>[ 1.0, ... ]';
msg = 'Expected instance type <Float32Array>, but observed <Array>';
t.strictEqual( compareValues( actual, expected ), msg, 'returns expected message' );

actual = [ 1.0, 2.0, 3.0 ];
expected = '<Float64Array>';
msg = 'Expected a <Float64Array>, but received: `1,2,3`';
t.strictEqual( compareValues( actual, expected ), msg, 'returns expected message' );

actual = [ [ 1.0, 2.0 ] ];
expected = '[ <Float64Array>[ 1.0, 2.0 ] ]';
msg = 'Displayed return value is `[ <Float64Array>[ 1.0, 2.0 ] ]`, but expected `[ [ 1, 2 ] ]` instead';
t.strictEqual( compareValues( actual, expected ), msg, 'returns expected message' );

actual = [ 1.0, 2.0, 3.0 ];
expected = '[ 1.0, 2.0, 3.0 ]';
t.strictEqual( compareValues( actual, expected ), null, 'returns expected value' );

actual = [ [ 1.0, 2.0 ] ];
expected = '[ [ 1.0, 2.0 ] ]';
t.strictEqual( compareValues( actual, expected ), null, 'returns expected value' );

t.end();
});

Expand Down Expand Up @@ -661,7 +707,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 710 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 @@ -671,7 +717,7 @@

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

Check warning on line 720 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 @@ -705,7 +751,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 754 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 @@ -715,7 +761,7 @@

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

Check warning on line 764 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
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@
'console.log( tokenize( \'\' ) );',
'// => [ \'should be empty\' ]',
'',
'var str = \'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod.\';',

Check warning on line 158 in lib/node_modules/@stdlib/_tools/eslint/rules/doctest/test/fixtures/invalid.js

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Unknown word: "eirmod"

Check warning on line 158 in lib/node_modules/@stdlib/_tools/eslint/rules/doctest/test/fixtures/invalid.js

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Unknown word: "elitr"

Check warning on line 158 in lib/node_modules/@stdlib/_tools/eslint/rules/doctest/test/fixtures/invalid.js

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Unknown word: "sadipscing"

Check warning on line 158 in lib/node_modules/@stdlib/_tools/eslint/rules/doctest/test/fixtures/invalid.js

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Unknown word: "consetetur"

Check warning on line 158 in lib/node_modules/@stdlib/_tools/eslint/rules/doctest/test/fixtures/invalid.js

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Unknown word: "amet"
'console.log( tokenize( str ) );',
'/* =>',
' [',
Expand Down Expand Up @@ -447,6 +447,26 @@
};
invalid.push( test );

test = {
'code': [
'var x = [ 1.0, 2.0, 3.0 ];',
'var y = x.slice();',
'// returns <Float64Array>[ 1.0, 2.0, 3.0 ]'
].join( '\n' ),
'errors': [
{
'message': 'Expected instance type <Float64Array>, but observed <Array>',
'type': null
}
],
'output': [
'var x = [ 1.0, 2.0, 3.0 ];',
'var y = x.slice();',
'// returns [ 1, 2, 3 ]'
].join( '\n' )
};
invalid.push( test );


// EXPORTS //

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1155,6 +1155,50 @@ test = {
};
invalid.push( test );

test = {
'code': [
'\'use strict\'',
'',
'/**',
'* Copies an array.',
'*',
'* @param {Array} x - input array',
'* @returns {Array} output array',
'*',
'* @example',
'* var y = copy( [ 1.0, 2.0, 3.0 ] );',
'* // returns <Float64Array>[ 1.0, 2.0, 3.0 ]',
'*/',
'function copy( x ) {',
' return x.slice();',
'}'
].join( '\n' ),
'errors': [
{
'message': 'Expected instance type <Float64Array>, but observed <Array>',
'type': null
}
],
'output': [
'\'use strict\'',
'',
'/**',
'* Copies an array.',
'*',
'* @param {Array} x - input array',
'* @returns {Array} output array',
'*',
'* @example',
'* var y = copy( [ 1.0, 2.0, 3.0 ] );',
'* // returns [ 1, 2, 3 ]',
'*/',
'function copy( x ) {',
' return x.slice();',
'}'
].join( '\n' )
};
invalid.push( test );


// EXPORTS //

Expand Down