debug_test.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // Copyright 2008 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.debugTest');
  15. goog.setTestOnly('goog.debugTest');
  16. goog.require('goog.debug');
  17. goog.require('goog.structs.Set');
  18. goog.require('goog.testing.jsunit');
  19. function testMakeWhitespaceVisible() {
  20. assertEquals(
  21. 'Hello[_][_]World![r][n]\n' +
  22. '[r][n]\n' +
  23. '[f][f]I[_]am[t][t]here![r][n]\n',
  24. goog.debug.makeWhitespaceVisible(
  25. 'Hello World!\r\n\r\n\f\fI am\t\there!\r\n'));
  26. }
  27. function testGetFunctionName() {
  28. // Trivial resolver that matches just a few names: a static function, a
  29. // constructor, and a member function.
  30. var resolver = function(f) {
  31. if (f === goog.debug.getFunctionName) {
  32. return 'goog.debug.getFunctionName';
  33. } else if (f === goog.structs.Set) {
  34. return 'goog.structs.Set';
  35. } else if (f === goog.structs.Set.prototype.getCount) {
  36. return 'goog.structs.Set.getCount';
  37. } else {
  38. return null;
  39. }
  40. };
  41. goog.debug.setFunctionResolver(resolver);
  42. assertEquals(
  43. 'goog.debug.getFunctionName',
  44. goog.debug.getFunctionName(goog.debug.getFunctionName));
  45. assertEquals(
  46. 'goog.structs.Set', goog.debug.getFunctionName(goog.structs.Set));
  47. var set = new goog.structs.Set();
  48. assertEquals(
  49. 'goog.structs.Set.getCount', goog.debug.getFunctionName(set.getCount));
  50. // This function is matched by the fallback heuristic.
  51. assertEquals(
  52. 'testGetFunctionName', goog.debug.getFunctionName(testGetFunctionName));
  53. goog.debug.setFunctionResolver(null);
  54. }
  55. /**
  56. * Asserts that a substring can be found in a specified text string.
  57. *
  58. * @param {string} substring The substring to search for.
  59. * @param {string} text The text string to search within.
  60. */
  61. function assertContainsSubstring(substring, text) {
  62. assertNotEquals(
  63. 'Could not find "' + substring + '" in "' + text + '"', -1,
  64. text.search(substring));
  65. }
  66. function testDeepExpose() {
  67. var a = {};
  68. var b = {};
  69. var c = {};
  70. a.ancestor = a;
  71. a.otherObject = b;
  72. a.otherObjectAgain = b;
  73. b.nextLevel = c;
  74. // Add Uid to a before deepExpose.
  75. var aUid = goog.getUid(a);
  76. var deepExpose = goog.debug.deepExpose(a);
  77. assertContainsSubstring(
  78. 'ancestor = ... reference loop detected .id=' + aUid + '. ...',
  79. deepExpose);
  80. assertContainsSubstring('otherObjectAgain = {', deepExpose);
  81. // Make sure we've reset Uids after the deepExpose call.
  82. assert(goog.hasUid(a));
  83. assertFalse(goog.hasUid(b));
  84. assertFalse(goog.hasUid(c));
  85. }