hybrid_test.js 3.4 KB

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