processor_test.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // Copyright 2012 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.json.processorTest');
  15. goog.setTestOnly('goog.json.processorTest');
  16. goog.require('goog.json.EvalJsonProcessor');
  17. goog.require('goog.json.NativeJsonProcessor');
  18. goog.require('goog.testing.jsunit');
  19. goog.require('goog.userAgent');
  20. var SUPPORTS_NATIVE_JSON = false;
  21. function setUpPage() {
  22. SUPPORTS_NATIVE_JSON = goog.global['JSON'] &&
  23. !(goog.userAgent.GECKO && !goog.userAgent.isVersionOrHigher('5.0'));
  24. }
  25. var REPLACER = function(k, v) {
  26. return !!k ? v + 'd' : v;
  27. };
  28. var REVIVER = function(k, v) {
  29. return !!k ? v.substring(0, v.length - 1) : v;
  30. };
  31. // Just sanity check parsing and stringifying.
  32. // Thorough tests are in json_test.html.
  33. function testJsParser() {
  34. var json = '{"a":1,"b":{"c":2}}';
  35. runParsingTest(new goog.json.EvalJsonProcessor(), json, json);
  36. }
  37. function testNativeParser() {
  38. if (!SUPPORTS_NATIVE_JSON) {
  39. return;
  40. }
  41. var json = '{"a":1,"b":{"c":2}}';
  42. runParsingTest(new goog.json.NativeJsonProcessor(), json, json);
  43. }
  44. function testJsParser_withReplacer() {
  45. runParsingTest(
  46. new goog.json.EvalJsonProcessor(REPLACER), '{"a":"foo","b":"goo"}',
  47. '{"a":"food","b":"good"}');
  48. }
  49. function testNativeParser_withReplacer() {
  50. if (!SUPPORTS_NATIVE_JSON) {
  51. return;
  52. }
  53. runParsingTest(
  54. new goog.json.NativeJsonProcessor(REPLACER), '{"a":"foo","b":"goo"}',
  55. '{"a":"food","b":"good"}');
  56. }
  57. function testNativeParser_withReviver() {
  58. if (!SUPPORTS_NATIVE_JSON) {
  59. return;
  60. }
  61. var json = '{"a":"fod","b":"god"}';
  62. runParsingTest(
  63. new goog.json.NativeJsonProcessor(REPLACER, REVIVER), json, json);
  64. }
  65. function testUnsafeJsParser() {
  66. var json = '{"a":1,"b":{"c":2}}';
  67. runParsingTest(new goog.json.EvalJsonProcessor(null, true), json, json);
  68. }
  69. function testUnsafeJsParser_withReplacer() {
  70. runParsingTest(
  71. new goog.json.EvalJsonProcessor(REPLACER, true), '{"a":"foo","b":"goo"}',
  72. '{"a":"food","b":"good"}');
  73. }
  74. function runParsingTest(parser, input, expected) {
  75. assertEquals(expected, parser.stringify(parser.parse(input)));
  76. }