Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1761)

Unified Diff: mojo/public/js/codec_unittests.js

Issue 803173003: Add more type validation to codec.js (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Updated comment Created 6 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« mojo/public/js/codec.js ('K') | « mojo/public/js/codec.js ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: mojo/public/js/codec_unittests.js
diff --git a/mojo/public/js/codec_unittests.js b/mojo/public/js/codec_unittests.js
index 82e7c5e3314da64140514f8d0bde85eb06a5f300..80aa7bdda23302eeb240c6e505ee178f5dba8fd7 100644
--- a/mojo/public/js/codec_unittests.js
+++ b/mojo/public/js/codec_unittests.js
@@ -15,7 +15,7 @@ define([
testTypes();
testAlign();
testUtf8();
- testArrayPointerValidation();
+ testTypedPointerValidation();
this.result = "PASS";
function testBar() {
@@ -257,17 +257,35 @@ define([
expect(str2).toEqual(str);
}
- function testArrayPointerValidation() {
+ function testTypedPointerValidation() {
+ var encoder = new codec.MessageBuilder(42, 24).createEncoder(8);
function DummyClass() {};
- var builder = new codec.MessageBuilder(42, 24);
- var encoder = builder.createEncoder(8);
- expect(function() {
- // Pass something that is not an array it should throw.
- encoder.encodeArrayPointer(DummyClass, 75);
- }).toThrow();
-
- // An empty array or null should do nothing and be fine.
- encoder.encodeArrayPointer(DummyClass, []);
- encoder.encodeArrayPointer(DummyClass, null);
+ var testCases = [
+ // method, args, invalid examples, valid examples
+ [encoder.encodeArrayPointer, [DummyClass], [75], [[], null, undefined]],
+ [encoder.encodeStringPointer, [], [75, new String("foo")],
+ ["", "bar", null, undefined]],
+ [encoder.encodeMapPointer, [DummyClass, DummyClass], [75],
+ [new Map(), null, undefined]],
+ ];
+
+ testCases.forEach(function(test) {
+ var method = test[0];
+ var baseArgs = test[1];
+ var invalidExamples = test[2];
+ var validExamples = test[3];
+
+ var encoder = new codec.MessageBuilder(42, 24).createEncoder(8);
+ invalidExamples.forEach(function(invalid) {
+ expect(function() {
+ method.apply(encoder, baseArgs.concat(invalid));
+ }).toThrow();
+ });
+
+ validExamples.forEach(function(valid) {
+ var encoder = new codec.MessageBuilder(42, 24).createEncoder(8);
+ method.apply(encoder, baseArgs.concat(valid));
+ });
+ });
}
});
« mojo/public/js/codec.js ('K') | « mojo/public/js/codec.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698