Skip to content

Commit

Permalink
Add in tests for non-native dataview
Browse files Browse the repository at this point in the history
This includes a new module that will run about 1 out of every 3
runs that will make sure the browser DataView is replaced with an
object. Without the updates to supportsDataView this would fail
since stringTagBug would try to call the constructor on it.
  • Loading branch information
colingm committed Sep 8, 2023
1 parent 0b5d30f commit 6967907
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 9 deletions.
1 change: 1 addition & 0 deletions karma.conf-sauce.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ module.exports = function(config) {
files: [
'test/vendor/qunit-extras.js',
'test/qunit-setup.js',
'test/overrides.js',
'underscore-umd.js',
'test/*.js'
],
Expand Down
1 change: 1 addition & 0 deletions karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ module.exports = function(config) {
// list of files / patterns to load in the browser
files: [
'test/qunit-setup.js',
'test/overrides.js',
'underscore-umd.js',
'test/*.js'
],
Expand Down
1 change: 1 addition & 0 deletions test/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<div id="qunit"></div>
<div id="exports" style="display: none"></div>
<script src="vendor/qunit.js"></script>
<script src="overrides.js"></script>
<script src="../underscore-umd.js"></script>
<script src="qunit-setup.js"></script>
<script src="collections.js"></script>
Expand Down
18 changes: 9 additions & 9 deletions test/objects.js
Original file line number Diff line number Diff line change
Expand Up @@ -609,12 +609,12 @@
var view2 = new Uint8Array(shared.buffer, 2, 2);
assert.notOk(_.isEqual(view1, view2), 'same buffer with different offset is not equal');

// Some older browsers support typed arrays but not DataView.
if (typeof DataView !== 'undefined') {
assert.ok(_.isEqual(new DataView(u8.buffer), new DataView(u8b.buffer)), 'Identical DataViews are equal');
assert.ok(_.isEqual(new DataView(u8.buffer), new DataView(i8.buffer)), 'Identical DataViews of different typed arrays are equal');
assert.notOk(_.isEqual(new DataView(u8.buffer), new DataView(u16.buffer)), 'Different DataViews with different length are not equal');
assert.notOk(_.isEqual(new DataView(u8.buffer), new DataView(u16one.buffer)), 'Different DataViews with different byte data are not equal');
var DataViewImpl = typeof NativeDataView !== 'undefined' ? NativeDataView : typeof DataView !== 'undefined' ? DataView : undefined; // Browser could override DataView
if (DataViewImpl) {
assert.ok(_.isEqual(new DataViewImpl(u8.buffer), new DataViewImpl(u8b.buffer)), 'Identical DataViews are equal');
assert.ok(_.isEqual(new DataViewImpl(u8.buffer), new DataViewImpl(i8.buffer)), 'Identical DataViews of different typed arrays are equal');
assert.notOk(_.isEqual(new DataViewImpl(u8.buffer), new DataViewImpl(u16.buffer)), 'Different DataViews with different length are not equal');
assert.notOk(_.isEqual(new DataViewImpl(u8.buffer), new DataViewImpl(u16one.buffer)), 'Different DataViews with different byte data are not equal');
}
}
});
Expand Down Expand Up @@ -930,9 +930,9 @@
'an ArrayBuffer': buffer,
'a TypedArray': new Uint8Array(buffer)
};
// Some older browsers support typed arrays but not DataView.
if (typeof DataView !== 'undefined') {
checkValues['a DataView'] = new DataView(buffer);
var DataViewImpl = typeof NativeDataView !== 'undefined' ? NativeDataView : typeof DataView !== 'undefined' ? DataView : undefined; // Browser could override DataView
if (DataViewImpl) {
checkValues['a DataView'] = new DataViewImpl(buffer);
}
var types = ['an ArrayBuffer', 'a DataView', 'a TypedArray'];
_.each(types, function(type) {
Expand Down
11 changes: 11 additions & 0 deletions test/overrides.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
(function() {
function overrideDataView() {
NativeDataView = DataView;
DataView = {};
}

var runOverrides = Math.floor(Math.random() * 3) === 0;
if (runOverrides) { // Only override browser functions roughly 1/3rd of the time
overrideDataView();
}
})();

0 comments on commit 6967907

Please sign in to comment.