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

Side by Side Diff: mojo/public/js/bindings/struct_unittests.js

Issue 654843005: Mojo JS Bindings: add support for associative arrays (Mojo map type) (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Corrected validation for array map values Created 6 years, 2 months 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 unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 define([ 5 define([
6 "gin/test/expect", 6 "gin/test/expect",
7 "mojo/public/interfaces/bindings/tests/rect.mojom", 7 "mojo/public/interfaces/bindings/tests/rect.mojom",
8 "mojo/public/interfaces/bindings/tests/test_structs.mojom" 8 "mojo/public/interfaces/bindings/tests/test_structs.mojom",
9 "mojo/public/js/bindings/codec",
10 "mojo/public/js/bindings/validator",
9 ], function(expect, 11 ], function(expect,
10 rect, 12 rect,
11 testStructs) { 13 testStructs,
14 codec,
15 validator) {
12 16
13 function testConstructors() { 17 function testConstructors() {
14 var r = new rect.Rect(); 18 var r = new rect.Rect();
15 expect(r).toEqual(new rect.Rect({x:0, y:0, width:0, height:0})); 19 expect(r).toEqual(new rect.Rect({x:0, y:0, width:0, height:0}));
16 expect(r).toEqual(new rect.Rect({foo:100, bar:200})); 20 expect(r).toEqual(new rect.Rect({foo:100, bar:200}));
17 21
18 r.x = 10; 22 r.x = 10;
19 r.y = 20; 23 r.y = 20;
20 r.width = 30; 24 r.width = 30;
21 r.height = 40; 25 r.height = 40;
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
86 var s = new testStructs.ScopedConstants(); 90 var s = new testStructs.ScopedConstants();
87 expect(s.f0).toEqual(0); 91 expect(s.f0).toEqual(0);
88 expect(s.f1).toEqual(1); 92 expect(s.f1).toEqual(1);
89 expect(s.f2).toEqual(10); 93 expect(s.f2).toEqual(10);
90 expect(s.f3).toEqual(10); 94 expect(s.f3).toEqual(10);
91 expect(s.f4).toEqual(11); 95 expect(s.f4).toEqual(11);
92 expect(s.f5).toEqual(10); 96 expect(s.f5).toEqual(10);
93 expect(s.f6).toEqual(10); 97 expect(s.f6).toEqual(10);
94 } 98 }
95 99
100 function structEncodeDecode(struct) {
101 var structClass = struct.constructor;
102 var builder = new codec.MessageBuilder(1234, structClass.encodedSize);
103 builder.encodeStruct(structClass, struct);
104 var message = builder.finish();
105
106 var messageValidator = new validator.Validator(message);
107 var err = structClass.validate(messageValidator, codec.kMessageHeaderSize);
108 expect(err).toEqual(validator.validationError.NONE);
109
110 var reader = new codec.MessageReader(message);
111 return reader.decodeStruct(structClass);
112 }
113
114 function testMapKeyTypes() {
115 var mapFieldsStruct = new testStructs.MapKeyTypes({
116 f0: new Map([[true, false], [false, true]]), // map<bool, bool>
117 f1: new Map([[0, 0], [1, 127], [-1, -128]]), // map<int8, int8>
118 f2: new Map([[0, 0], [1, 127], [2, 255]]), // map<uint8, uint8>
119 f3: new Map([[0, 0], [1, 32767], [2, -32768]]), // map<int16, int16>
120 f4: new Map([[0, 0], [1, 32768], [2, 0xFFFF]]), // map<uint16, uint16>
121 f5: new Map([[0, 0], [1, 32767], [2, -32768]]), // map<int32, int32>
122 f6: new Map([[0, 0], [1, 32768], [2, 0xFFFF]]), // map<uint32, uint32>
123 f7: new Map([[0, 0], [1, 32767], [2, -32768]]), // map<int64, int64>
124 f8: new Map([[0, 0], [1, 32768], [2, 0xFFFF]]), // map<uint64, uint64>
125 f9: new Map([[1000.5, -50000], [100.5, 5000]]), // map<float, float>
126 f10: new Map([[-100.5, -50000], [0, 50000000]]), // map<double, double>
127 f11: new Map([["one", "two"], ["free", "four"]]), // map<string, string>
128 });
129 var decodedStruct = structEncodeDecode(mapFieldsStruct);
130 expect(decodedStruct.f0).toEqual(mapFieldsStruct.f0);
131 expect(decodedStruct.f1).toEqual(mapFieldsStruct.f1);
132 expect(decodedStruct.f2).toEqual(mapFieldsStruct.f2);
133 expect(decodedStruct.f3).toEqual(mapFieldsStruct.f3);
134 expect(decodedStruct.f4).toEqual(mapFieldsStruct.f4);
135 expect(decodedStruct.f5).toEqual(mapFieldsStruct.f5);
136 expect(decodedStruct.f6).toEqual(mapFieldsStruct.f6);
137 expect(decodedStruct.f7).toEqual(mapFieldsStruct.f7);
138 expect(decodedStruct.f8).toEqual(mapFieldsStruct.f8);
139 expect(decodedStruct.f9).toEqual(mapFieldsStruct.f9);
140 expect(decodedStruct.f10).toEqual(mapFieldsStruct.f10);
141 expect(decodedStruct.f11).toEqual(mapFieldsStruct.f11);
142 }
143
144 function testMapValueTypes() {
145 var mapFieldsStruct = new testStructs.MapValueTypes({
146 f0: new Map([["a", ["b", "c"]], ["d", ["e"]]]), // array<string>>
147 f1: new Map([["a", null], ["b", ["c", "d"]]]), // array<string>?>
148 f2: new Map([["a", [null]], ["b", [null, "d"]]]), // array<string?>>
149 f3: new Map([["a", ["1", "2"]], ["b", ["1", "2"]]]), // array<string,2>>
yzshen1 2014/10/16 20:22:47 nit: two spaces before //
hansmuller 2014/10/16 20:43:41 Done.
150 f4: new Map([["a", [["1"]]], ["b", [null]]]), // array<array<string, 1>?>
151 f5: new Map([["a", [["1", "2"]]]]), // array<array<string, 2>, 1>>
152 });
153 var decodedStruct = structEncodeDecode(mapFieldsStruct);
154 expect(decodedStruct.f0).toEqual(mapFieldsStruct.f0);
155 expect(decodedStruct.f1).toEqual(mapFieldsStruct.f1);
156 expect(decodedStruct.f2).toEqual(mapFieldsStruct.f2);
157 expect(decodedStruct.f3).toEqual(mapFieldsStruct.f3);
158 expect(decodedStruct.f4).toEqual(mapFieldsStruct.f4);
159 expect(decodedStruct.f5).toEqual(mapFieldsStruct.f5);
160 }
161
96 testConstructors(); 162 testConstructors();
97 testNoDefaultFieldValues(); 163 testNoDefaultFieldValues();
98 testDefaultFieldValues(); 164 testDefaultFieldValues();
99 testScopedConstants(); 165 testScopedConstants();
166 testMapKeyTypes();
167 testMapValueTypes();
100 this.result = "PASS"; 168 this.result = "PASS";
101 }); 169 });
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698