| 1 |
// Copyright 2013 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 |
/** |
| 16 |
* @fileoverview Unit tests for goog.json.hybrid. |
| 17 |
* @author [email protected] (Nathan Naze) |
| 18 |
*/ |
| 19 |
|
| 20 |
goog.provide('goog.json.hybridTest'); |
| 21 |
|
| 22 |
goog.require('goog.json'); |
| 23 |
goog.require('goog.json.hybrid'); |
| 24 |
goog.require('goog.testing.PropertyReplacer'); |
| 25 |
goog.require('goog.testing.jsunit'); |
| 26 |
goog.require('goog.testing.recordFunction'); |
| 27 |
goog.require('goog.userAgent'); |
| 28 |
|
| 29 |
goog.setTestOnly('goog.json.hybridTest'); |
| 30 |
|
| 31 |
|
| 32 |
var propertyReplacer = new goog.testing.PropertyReplacer(); |
| 33 |
|
| 34 |
var jsonParse; |
| 35 |
var jsonStringify; |
| 36 |
var googJsonParse; |
| 37 |
var googJsonSerialize; |
| 38 |
|
| 39 |
function isIe7() { |
| 40 |
return goog.userAgent.IE && !goog.userAgent.isVersionOrHigher('8'); |
| 41 |
} |
| 42 |
|
| 43 |
function setUp() { |
| 44 |
googJsonParse = goog.testing.recordFunction(goog.json.parse); |
| 45 |
googJsonSerialize = goog.testing.recordFunction(goog.json.serialize); |
| 46 |
|
| 47 |
propertyReplacer.set(goog.json, 'parse', googJsonParse); |
| 48 |
propertyReplacer.set(goog.json, 'serialize', googJsonSerialize); |
| 49 |
|
| 50 |
jsonParse = |
| 51 |
goog.testing.recordFunction(goog.global.JSON && goog.global.JSON.parse); |
| 52 |
jsonStringify = goog.testing.recordFunction( |
| 53 |
goog.global.JSON && goog.global.JSON.stringify); |
| 54 |
|
| 55 |
if (goog.global.JSON) { |
| 56 |
propertyReplacer.set(goog.global.JSON, 'parse', jsonParse); |
| 57 |
propertyReplacer.set(goog.global.JSON, 'stringify', jsonStringify); |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
function tearDown() { |
| 62 |
propertyReplacer.reset(); |
| 63 |
} |
| 64 |
|
| 65 |
function parseJson() { |
| 66 |
var obj = goog.json.hybrid.parse('{"a": 2}'); |
| 67 |
assertObjectEquals({'a': 2}, obj); |
| 68 |
} |
| 69 |
|
| 70 |
function serializeJson() { |
| 71 |
var str = goog.json.hybrid.stringify({b: 2}); |
| 72 |
assertEquals('{"b":2}', str); |
| 73 |
} |
| 74 |
|
| 75 |
function testParseNativeJsonPresent() { |
| 76 |
// No native JSON in IE7 |
| 77 |
if (isIe7()) { |
| 78 |
return; |
| 79 |
} |
| 80 |
|
| 81 |
parseJson(); |
| 82 |
assertEquals(1, jsonParse.getCallCount()); |
| 83 |
assertEquals(0, googJsonParse.getCallCount()); |
| 84 |
} |
| 85 |
|
| 86 |
function testStringifyNativeJsonPresent() { |
| 87 |
// No native JSON in IE7 |
| 88 |
if (isIe7()) { |
| 89 |
return; |
| 90 |
} |
| 91 |
|
| 92 |
serializeJson(); |
| 93 |
|
| 94 |
assertEquals(1, jsonStringify.getCallCount()); |
| 95 |
assertEquals(0, googJsonSerialize.getCallCount()); |
| 96 |
} |
| 97 |
|
| 98 |
function testParseNativeJsonAbsent() { |
| 99 |
propertyReplacer.set(goog.global, 'JSON', null); |
| 100 |
|
| 101 |
parseJson(); |
| 102 |
|
| 103 |
assertEquals(0, jsonParse.getCallCount()); |
| 104 |
assertEquals(0, jsonStringify.getCallCount()); |
| 105 |
assertEquals(1, googJsonParse.getCallCount()); |
| 106 |
} |
| 107 |
|
| 108 |
function testStringifyNativeJsonAbsent() { |
| 109 |
propertyReplacer.set(goog.global, 'JSON', null); |
| 110 |
|
| 111 |
serializeJson(); |
| 112 |
|
| 113 |
assertEquals(0, jsonStringify.getCallCount()); |
| 114 |
assertEquals(1, googJsonSerialize.getCallCount()); |
| 115 |
} |
| 116 |
|
| 117 |
function testParseCurrentBrowserParse() { |
| 118 |
parseJson(); |
| 119 |
assertEquals(isIe7() ? 0 : 1, jsonParse.getCallCount()); |
| 120 |
assertEquals(isIe7() ? 1 : 0, googJsonParse.getCallCount()); |
| 121 |
} |
| 122 |
|
| 123 |
function testParseCurrentBrowserStringify() { |
| 124 |
serializeJson(); |
| 125 |
assertEquals(isIe7() ? 0 : 1, jsonStringify.getCallCount()); |
| 126 |
assertEquals(isIe7() ? 1 : 0, googJsonSerialize.getCallCount()); |
| 127 |
} |