serializer_test.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. goog.provide('goog.protoTest');
  15. goog.setTestOnly('goog.protoTest');
  16. goog.require('goog.proto');
  17. goog.require('goog.testing.jsunit');
  18. var serialize = goog.proto.serialize;
  19. function testArraySerialize() {
  20. assertEquals('Empty array', serialize([]), '[]');
  21. assertEquals('Normal array', serialize([0, 1, 2]), '[0,1,2]');
  22. assertEquals('Empty start', serialize([, 1, 2]), '[null,1,2]');
  23. assertEquals('Empty start', serialize([, , , 3, 4]), '[null,null,null,3,4]');
  24. assertEquals('Empty middle', serialize([0, , 2]), '[0,null,2]');
  25. assertEquals('Empty middle', serialize([0, , , 3]), '[0,null,null,3]');
  26. assertEquals('Empty end', serialize(withLength([0, 1, 2], 4)), '[0,1,2]');
  27. assertEquals(
  28. 'Empty start, middle and end', serialize([, , 2, , 4, null]),
  29. '[null,null,2,null,4]');
  30. assertEquals('All elements empty', serialize(withLength([], 3)), '[]');
  31. assertEquals(
  32. 'Nested', serialize([, 1, [, 1, [, 1]]]), '[null,1,[null,1,[null,1]]]');
  33. }
  34. /**
  35. * Returns an array with the given elements and length.
  36. * @param {!Array<T>} elems The elements in the array.
  37. * @param {number} length The length.
  38. * @return {!Array<T>} The original 'elems' array with its length changed.
  39. * @template T
  40. */
  41. function withLength(elems, length) {
  42. elems.length = length;
  43. return elems;
  44. }