| 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 |
|
| 15 |
goog.provide('goog.format.JsonPrettyPrinterTest'); |
| 16 |
goog.setTestOnly('goog.format.JsonPrettyPrinterTest'); |
| 17 |
|
| 18 |
goog.require('goog.format.JsonPrettyPrinter'); |
| 19 |
goog.require('goog.testing.jsunit'); |
| 20 |
|
| 21 |
var formatter; |
| 22 |
|
| 23 |
|
| 24 |
function setUp() { |
| 25 |
formatter = new goog.format.JsonPrettyPrinter(); |
| 26 |
} |
| 27 |
|
| 28 |
|
| 29 |
function testUndefined() { |
| 30 |
assertEquals('', formatter.format()); |
| 31 |
} |
| 32 |
|
| 33 |
|
| 34 |
function testNull() { |
| 35 |
assertEquals('', formatter.format(null)); |
| 36 |
} |
| 37 |
|
| 38 |
|
| 39 |
function testBoolean() { |
| 40 |
assertEquals('true', formatter.format(true)); |
| 41 |
} |
| 42 |
|
| 43 |
|
| 44 |
function testNumber() { |
| 45 |
assertEquals('1', formatter.format(1)); |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
function testEmptyString() { |
| 50 |
assertEquals('', formatter.format('')); |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
function testWhitespaceString() { |
| 55 |
assertEquals('', formatter.format(' ')); |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
function testString() { |
| 60 |
assertEquals('{}', formatter.format('{}')); |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
function testEmptyArray() { |
| 65 |
assertEquals('[]', formatter.format([])); |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
function testArrayOneElement() { |
| 70 |
assertEquals('[\n 1\n]', formatter.format([1])); |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
function testArrayMultipleElements() { |
| 75 |
assertEquals('[\n 1,\n 2,\n 3\n]', formatter.format([1, 2, 3])); |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
function testFunction() { |
| 80 |
assertEquals( |
| 81 |
'{\n "a": "1",\n "b": ""\n}', |
| 82 |
formatter.format({'a': '1', 'b': function() { return null; }})); |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
function testObject() { |
| 87 |
assertEquals('{}', formatter.format({})); |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
function testObjectMultipleProperties() { |
| 92 |
assertEquals( |
| 93 |
'{\n "a": null,\n "b": true,\n "c": 1,\n "d": "d",\n "e":' + |
| 94 |
' [\n 1,\n 2,\n 3\n ],\n "f": {\n "g": 1,\n "h": "h"\n' + |
| 95 |
' }\n}', |
| 96 |
formatter.format({ |
| 97 |
'a': null, |
| 98 |
'b': true, |
| 99 |
'c': 1, |
| 100 |
'd': 'd', |
| 101 |
'e': [1, 2, 3], |
| 102 |
'f': {'g': 1, 'h': 'h'} |
| 103 |
})); |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
function testSafeHtmlDelimiters() { |
| 108 |
var htmlFormatter = new goog.format.JsonPrettyPrinter( |
| 109 |
new goog.format.JsonPrettyPrinter.SafeHtmlDelimiters()); |
| 110 |
assertEquals( |
| 111 |
'{\n <span class="goog-jsonprettyprinter-propertyname">"' + |
| 112 |
'a<b"</span>: <span class="goog-jsonprettyprinter-propertyvalue' + |
| 113 |
'-string">">"</span>\n}', |
| 114 |
htmlFormatter.formatSafeHtml({'a<b': '>'}).getTypedStringValue()); |
| 115 |
} |