jsonprettyprinter_test.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // Copyright 2010 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.format.JsonPrettyPrinterTest');
  15. goog.setTestOnly('goog.format.JsonPrettyPrinterTest');
  16. goog.require('goog.format.JsonPrettyPrinter');
  17. goog.require('goog.testing.jsunit');
  18. var formatter;
  19. function setUp() {
  20. formatter = new goog.format.JsonPrettyPrinter();
  21. }
  22. function testUndefined() {
  23. assertEquals('', formatter.format());
  24. }
  25. function testNull() {
  26. assertEquals('', formatter.format(null));
  27. }
  28. function testBoolean() {
  29. assertEquals('true', formatter.format(true));
  30. }
  31. function testNumber() {
  32. assertEquals('1', formatter.format(1));
  33. }
  34. function testEmptyString() {
  35. assertEquals('', formatter.format(''));
  36. }
  37. function testWhitespaceString() {
  38. assertEquals('', formatter.format(' '));
  39. }
  40. function testString() {
  41. assertEquals('{}', formatter.format('{}'));
  42. }
  43. function testEmptyArray() {
  44. assertEquals('[]', formatter.format([]));
  45. }
  46. function testArrayOneElement() {
  47. assertEquals('[\n 1\n]', formatter.format([1]));
  48. }
  49. function testArrayMultipleElements() {
  50. assertEquals('[\n 1,\n 2,\n 3\n]', formatter.format([1, 2, 3]));
  51. }
  52. function testFunction() {
  53. assertEquals(
  54. '{\n "a": "1",\n "b": ""\n}',
  55. formatter.format({'a': '1', 'b': function() { return null; }}));
  56. }
  57. function testObject() {
  58. assertEquals('{}', formatter.format({}));
  59. }
  60. function testObjectMultipleProperties() {
  61. assertEquals(
  62. '{\n "a": null,\n "b": true,\n "c": 1,\n "d": "d",\n "e":' +
  63. ' [\n 1,\n 2,\n 3\n ],\n "f": {\n "g": 1,\n "h": "h"\n' +
  64. ' }\n}',
  65. formatter.format({
  66. 'a': null,
  67. 'b': true,
  68. 'c': 1,
  69. 'd': 'd',
  70. 'e': [1, 2, 3],
  71. 'f': {'g': 1, 'h': 'h'}
  72. }));
  73. }
  74. function testSafeHtmlDelimiters() {
  75. var htmlFormatter = new goog.format.JsonPrettyPrinter(
  76. new goog.format.JsonPrettyPrinter.SafeHtmlDelimiters());
  77. assertEquals(
  78. '{\n <span class="goog-jsonprettyprinter-propertyname">&quot;' +
  79. 'a&lt;b&quot;</span>: <span class="goog-jsonprettyprinter-propertyvalue' +
  80. '-string">&quot;&gt;&quot;</span>\n}',
  81. htmlFormatter.formatSafeHtml({'a<b': '>'}).getTypedStringValue());
  82. }