| 1 |
// Copyright 2007 The Closure Library Authors. All Rights Reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS-IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
goog.provide('goog.protoTest'); |
| 16 |
goog.setTestOnly('goog.protoTest'); |
| 17 |
|
| 18 |
goog.require('goog.proto'); |
| 19 |
goog.require('goog.testing.jsunit'); |
| 20 |
|
| 21 |
var serialize = goog.proto.serialize; |
| 22 |
|
| 23 |
function testArraySerialize() { |
| 24 |
assertEquals('Empty array', serialize([]), '[]'); |
| 25 |
|
| 26 |
assertEquals('Normal array', serialize([0, 1, 2]), '[0,1,2]'); |
| 27 |
assertEquals('Empty start', serialize([, 1, 2]), '[null,1,2]'); |
| 28 |
assertEquals('Empty start', serialize([, , , 3, 4]), '[null,null,null,3,4]'); |
| 29 |
assertEquals('Empty middle', serialize([0, , 2]), '[0,null,2]'); |
| 30 |
assertEquals('Empty middle', serialize([0, , , 3]), '[0,null,null,3]'); |
| 31 |
assertEquals('Empty end', serialize(withLength([0, 1, 2], 4)), '[0,1,2]'); |
| 32 |
assertEquals( |
| 33 |
'Empty start, middle and end', serialize([, , 2, , 4, null]), |
| 34 |
'[null,null,2,null,4]'); |
| 35 |
assertEquals('All elements empty', serialize(withLength([], 3)), '[]'); |
| 36 |
assertEquals( |
| 37 |
'Nested', serialize([, 1, [, 1, [, 1]]]), '[null,1,[null,1,[null,1]]]'); |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Returns an array with the given elements and length. |
| 42 |
* @param {!Array<T>} elems The elements in the array. |
| 43 |
* @param {number} length The length. |
| 44 |
* @return {!Array<T>} The original 'elems' array with its length changed. |
| 45 |
* @template T |
| 46 |
*/ |
| 47 |
function withLength(elems, length) { |
| 48 |
elems.length = length; |
| 49 |
return elems; |
| 50 |
} |