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 27, 2023
1 parent fcc3b61 commit dbb5475
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 7 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
17 changes: 10 additions & 7 deletions test/objects.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@

var testElement = typeof document === 'object' ? document.createElement('div') : void 0;

// Some browsers support typed arrays but not DataView and some applications could override DataView
var DataViewImpl = typeof NativeDataView !== 'undefined' ? NativeDataView : typeof DataView !== 'undefined' ? DataView : undefined;

QUnit.test('keys', function(assert) {
assert.deepEqual(_.keys({one: 1, two: 2}), ['one', 'two'], 'can extract the keys from an object');
// the test above is not safe because it relies on for-in enumeration order
Expand Down Expand Up @@ -610,11 +613,11 @@
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');
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 @@ -931,8 +934,8 @@
'a TypedArray': new Uint8Array(buffer)
};
// Some older browsers support typed arrays but not DataView.
if (typeof DataView !== 'undefined') {
checkValues['a DataView'] = new DataView(buffer);
if (DataViewImpl) {
checkValues['a DataView'] = new DataViewImpl(buffer);
}
var types = ['an ArrayBuffer', 'a DataView', 'a TypedArray'];
_.each(types, function(type) {
Expand Down
12 changes: 12 additions & 0 deletions test/overrides.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
(function() {
function overrideDataView() {
NativeDataView = DataView;
DataView = {};
}

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

0 comments on commit dbb5475

Please sign in to comment.