blob: 5096869962696c96dff9d1840b4d19c8ad6b6f15 [file] [log] [blame]
Feng Xiaoe841bac2015-12-11 17:09:20 -08001// Protocol Buffers - Google's data interchange format
2// Copyright 2008 Google Inc. All rights reserved.
3// https://developers.google.com/protocol-buffers/
4//
5// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions are
7// met:
8//
9// * Redistributions of source code must retain the above copyright
10// notice, this list of conditions and the following disclaimer.
11// * Redistributions in binary form must reproduce the above
12// copyright notice, this list of conditions and the following disclaimer
13// in the documentation and/or other materials provided with the
14// distribution.
15// * Neither the name of Google Inc. nor the names of its
16// contributors may be used to endorse or promote products derived from
17// this software without specific prior written permission.
18//
19// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
31goog.setTestOnly();
32
33goog.require('goog.testing.asserts');
Josh Habermane9f31ee2016-02-04 10:29:27 -080034
35// CommonJS-LoadFromFile: google-protobuf
Feng Xiaoe841bac2015-12-11 17:09:20 -080036goog.require('jspb.debug');
Josh Habermane9f31ee2016-02-04 10:29:27 -080037
38// CommonJS-LoadFromFile: test_pb
Feng Xiaoe841bac2015-12-11 17:09:20 -080039goog.require('proto.jspb.test.HasExtensions');
40goog.require('proto.jspb.test.IsExtension');
Adam Cozzette0894e072018-11-09 11:28:22 -080041goog.require('proto.jspb.test.MapValueMessageNoBinary');
Feng Xiaoe841bac2015-12-11 17:09:20 -080042goog.require('proto.jspb.test.Simple1');
Adam Cozzette0894e072018-11-09 11:28:22 -080043goog.require('proto.jspb.test.TestMapFieldsNoBinary');
Feng Xiaoe841bac2015-12-11 17:09:20 -080044
45
Adam Cozzette92a7e772017-12-01 10:05:10 -080046// CommonJS-LoadFromFile: testbinary_pb
47goog.require('proto.jspb.test.TestAllTypes');
Jisi Liu3b3c8ab2016-03-30 11:39:59 -070048
Feng Xiaoe841bac2015-12-11 17:09:20 -080049describe('debugTest', function() {
50 it('testSimple1', function() {
51 if (COMPILED) {
52 return;
53 }
54 var message = new proto.jspb.test.Simple1();
55 message.setAString('foo');
56 assertObjectEquals({
57 $name: 'proto.jspb.test.Simple1',
58 'aString': 'foo',
59 'aRepeatedStringList': []
60 }, jspb.debug.dump(message));
61
62 message.setABoolean(true);
63 message.setARepeatedStringList(['1', '2']);
64
65 assertObjectEquals({
66 $name: 'proto.jspb.test.Simple1',
67 'aString': 'foo',
68 'aRepeatedStringList': ['1', '2'],
69 'aBoolean': true
70 }, jspb.debug.dump(message));
71
Bo Yangcc8ca5b2016-09-19 13:45:07 -070072 message.clearAString();
Feng Xiaoe841bac2015-12-11 17:09:20 -080073
74 assertObjectEquals({
75 $name: 'proto.jspb.test.Simple1',
76 'aRepeatedStringList': ['1', '2'],
77 'aBoolean': true
78 }, jspb.debug.dump(message));
79 });
80
Adam Cozzette92a7e772017-12-01 10:05:10 -080081 it('testBytes', function() {
Adam Cozzettee372df52017-12-04 15:08:10 -080082 if (COMPILED || typeof Uint8Array == 'undefined') {
Adam Cozzette92a7e772017-12-01 10:05:10 -080083 return;
84 }
85 var message = new proto.jspb.test.TestAllTypes();
86 var bytes = new Uint8Array(4);
87 message.setOptionalBytes(bytes);
88 assertEquals(jspb.debug.dump(message)['optionalBytes'], bytes);
89 });
Feng Xiaoe841bac2015-12-11 17:09:20 -080090
91 it('testExtensions', function() {
92 if (COMPILED) {
93 return;
94 }
95 var extension = new proto.jspb.test.IsExtension();
96 extension.setExt1('ext1field');
97 var extendable = new proto.jspb.test.HasExtensions();
98 extendable.setStr1('v1');
99 extendable.setStr2('v2');
100 extendable.setStr3('v3');
101 extendable.setExtension(proto.jspb.test.IsExtension.extField, extension);
102
103 assertObjectEquals({
104 '$name': 'proto.jspb.test.HasExtensions',
105 'str1': 'v1',
106 'str2': 'v2',
107 'str3': 'v3',
108 '$extensions': {
109 'extField': {
110 '$name': 'proto.jspb.test.IsExtension',
111 'ext1': 'ext1field'
112 },
113 'repeatedSimpleList': []
114 }
115 }, jspb.debug.dump(extendable));
116 });
117
Adam Cozzette0894e072018-11-09 11:28:22 -0800118 it('testMapsBasicTypes', function() {
119 if (COMPILED) {
120 return;
121 }
122
123 var message = new proto.jspb.test.TestMapFieldsNoBinary();
124 message.getMapBoolStringMap().set(true, 'bool_string_value1');
125 message.getMapBoolStringMap().set(false, 'bool_string_value2');
126 message.getMapStringInt32Map().set('key', 111);
127
128 assertObjectEquals({
129 '$name': 'proto.jspb.test.TestMapFieldsNoBinary',
130 'mapBoolStringMap': {
131 true: 'bool_string_value1',
132 false: 'bool_string_value2'
133 },
134 'mapInt32StringMap': {},
135 'mapInt64StringMap': {},
136 'mapStringBoolMap': {},
137 'mapStringDoubleMap': {},
138 'mapStringEnumMap': {},
139 'mapStringInt32Map': {
140 'key': 111
141 },
142 'mapStringInt64Map': {},
143 'mapStringMsgMap': {},
144 'mapStringStringMap': {},
145 'mapStringTestmapfieldsMap': {}
146 }, jspb.debug.dump(message));
147 });
148
149 it('testMapsMessageValues', function() {
150 if (COMPILED) {
151 return;
152 }
153
154 var value1 = new proto.jspb.test.MapValueMessageNoBinary();
155 value1.setFoo(1111);
156 var value2 = new proto.jspb.test.MapValueMessageNoBinary();
157 value2.setFoo(2222);
158
159 var message = new proto.jspb.test.TestMapFieldsNoBinary();
160 message.getMapStringMsgMap().set('key1', value1);
161 message.getMapStringMsgMap().set('key2', value2);
162
163 assertObjectEquals({
164 '$name': 'proto.jspb.test.TestMapFieldsNoBinary',
165 'mapBoolStringMap': {},
166 'mapInt32StringMap': {},
167 'mapInt64StringMap': {},
168 'mapStringBoolMap': {},
169 'mapStringDoubleMap': {},
170 'mapStringEnumMap': {},
171 'mapStringInt32Map': {},
172 'mapStringInt64Map': {},
173 'mapStringMsgMap': {
174 'key1': {
175 '$name': 'proto.jspb.test.MapValueMessageNoBinary',
176 'foo': 1111
177 },
178 'key2': {
179 '$name': 'proto.jspb.test.MapValueMessageNoBinary',
180 'foo': 2222
181 }
182 },
183 'mapStringStringMap': {},
184 'mapStringTestmapfieldsMap': {}
185 }, jspb.debug.dump(message));
186 });
187
Feng Xiaoe841bac2015-12-11 17:09:20 -0800188});