console_test.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. // Copyright 2011 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.debug.ConsoleTest');
  15. goog.setTestOnly('goog.debug.ConsoleTest');
  16. goog.require('goog.debug.Console');
  17. goog.require('goog.debug.LogRecord');
  18. goog.require('goog.debug.Logger');
  19. goog.require('goog.testing.jsunit');
  20. goog.require('goog.testing.recordFunction');
  21. var debugConsole;
  22. var mockConsole;
  23. var loggerName1;
  24. var logRecord1;
  25. var loggerName2;
  26. var logRecord2;
  27. var loggerName3;
  28. var logRecord3;
  29. function setUp() {
  30. debugConsole = new goog.debug.Console();
  31. // Set up a recorder for mockConsole.log
  32. mockConsole = {log: goog.testing.recordFunction()};
  33. goog.debug.Console.console_ = mockConsole;
  34. // Test logger 1.
  35. loggerName1 = 'this.is.a.logger';
  36. logRecord1 = new goog.debug.LogRecord(
  37. goog.debug.Logger.Level.INFO, 'this is a statement', loggerName1);
  38. // Test logger 2.
  39. loggerName2 = 'name.of.logger';
  40. logRecord2 = new goog.debug.LogRecord(
  41. goog.debug.Logger.Level.WARNING, 'hey, this is a warning', loggerName2);
  42. // Test logger 3.
  43. loggerName3 = 'third.logger';
  44. logRecord3 = new goog.debug.LogRecord(
  45. goog.debug.Logger.Level.SEVERE, 'seriously, this statement is serious',
  46. loggerName3);
  47. }
  48. function testLoggingWithSimpleConsole() {
  49. // Make sure all messages use the log function.
  50. logAtAllLevels('test message');
  51. assertEquals(9, mockConsole.log.getCallCount());
  52. }
  53. function testLoggingWithInfoSupported() {
  54. // Make sure the log function is the default when only 'info' is available.
  55. mockConsole['info'] = goog.testing.recordFunction();
  56. logAtAllLevels('test message');
  57. assertEquals(1, mockConsole.info.getCallCount());
  58. assertEquals(8, mockConsole.log.getCallCount());
  59. }
  60. function testLoggingWithErrorSupported() {
  61. // Make sure the log function is the default when only 'error' is available.
  62. mockConsole['error'] = goog.testing.recordFunction();
  63. logAtAllLevels('test message');
  64. assertEquals(1, mockConsole.error.getCallCount());
  65. assertEquals(8, mockConsole.log.getCallCount());
  66. }
  67. function testLoggingWithWarningSupported() {
  68. // Make sure the log function is the default when only 'warn' is available.
  69. mockConsole['warn'] = goog.testing.recordFunction();
  70. logAtAllLevels('test message');
  71. assertEquals(1, mockConsole.warn.getCallCount());
  72. assertEquals(8, mockConsole.log.getCallCount());
  73. }
  74. function testLoggingWithDebugSupported() {
  75. // Make sure the log function is the default when only 'debug' is available.
  76. mockConsole['debug'] = goog.testing.recordFunction();
  77. logAtAllLevels('test message');
  78. assertEquals(6, mockConsole.debug.getCallCount());
  79. assertEquals(3, mockConsole.log.getCallCount());
  80. }
  81. function testLoggingWithEverythingSupported() {
  82. mockConsole['info'] = goog.testing.recordFunction();
  83. mockConsole['error'] = goog.testing.recordFunction();
  84. mockConsole['warn'] = goog.testing.recordFunction();
  85. mockConsole['debug'] = goog.testing.recordFunction();
  86. logAtAllLevels('test message');
  87. assertEquals(1, mockConsole.info.getCallCount());
  88. assertEquals(1, mockConsole.error.getCallCount());
  89. assertEquals(1, mockConsole.warn.getCallCount());
  90. assertEquals(6, mockConsole.debug.getCallCount());
  91. }
  92. function testAddLogRecordWithoutFilters() {
  93. // Make sure none are filtered.
  94. debugConsole.addLogRecord(logRecord1);
  95. assertEquals(1, mockConsole.log.getCallCount());
  96. debugConsole.addLogRecord(logRecord2);
  97. assertEquals(2, mockConsole.log.getCallCount());
  98. debugConsole.addLogRecord(logRecord3);
  99. assertEquals(3, mockConsole.log.getCallCount());
  100. }
  101. function testAddLogRecordWithOneFilter() {
  102. // Filter #2 and make sure the filtering is correct for all records.
  103. debugConsole.addFilter(loggerName2);
  104. debugConsole.addLogRecord(logRecord1);
  105. assertEquals(1, mockConsole.log.getCallCount());
  106. debugConsole.addLogRecord(logRecord2);
  107. assertEquals(1, mockConsole.log.getCallCount());
  108. debugConsole.addLogRecord(logRecord3);
  109. assertEquals(2, mockConsole.log.getCallCount());
  110. }
  111. function testAddLogRecordWithMoreThanOneFilter() {
  112. // Filter #1 and #3 and check.
  113. debugConsole.addFilter(loggerName1);
  114. debugConsole.addFilter(loggerName3);
  115. debugConsole.addLogRecord(logRecord1);
  116. assertEquals(0, mockConsole.log.getCallCount());
  117. debugConsole.addLogRecord(logRecord2);
  118. assertEquals(1, mockConsole.log.getCallCount());
  119. debugConsole.addLogRecord(logRecord3);
  120. assertEquals(1, mockConsole.log.getCallCount());
  121. }
  122. function testAddLogRecordWithAddAndRemoveFilter() {
  123. debugConsole.addFilter(loggerName1);
  124. debugConsole.addFilter(loggerName2);
  125. debugConsole.removeFilter(loggerName1);
  126. debugConsole.removeFilter(loggerName2);
  127. debugConsole.addLogRecord(logRecord1);
  128. assertEquals(1, mockConsole.log.getCallCount());
  129. debugConsole.addLogRecord(logRecord2);
  130. assertEquals(2, mockConsole.log.getCallCount());
  131. debugConsole.addLogRecord(logRecord3);
  132. assertEquals(3, mockConsole.log.getCallCount());
  133. }
  134. function testSetConsole() {
  135. var fakeConsole = {log: goog.testing.recordFunction()};
  136. logAtLevel(goog.debug.Logger.Level.INFO, 'test message 1');
  137. logAtAllLevels('test message 1');
  138. assertEquals(0, fakeConsole.log.getCallCount());
  139. goog.debug.Console.setConsole(fakeConsole);
  140. logAtLevel(goog.debug.Logger.Level.INFO, 'test message 2');
  141. assertEquals(1, fakeConsole.log.getCallCount());
  142. }
  143. /**
  144. * Logs the message at all log levels.
  145. * @param {string} message The message to log.
  146. */
  147. function logAtAllLevels(message) {
  148. logAtLevel(goog.debug.Logger.Level.SHOUT, message);
  149. logAtLevel(goog.debug.Logger.Level.SEVERE, message);
  150. logAtLevel(goog.debug.Logger.Level.WARNING, message);
  151. logAtLevel(goog.debug.Logger.Level.INFO, message);
  152. logAtLevel(goog.debug.Logger.Level.CONFIG, message);
  153. logAtLevel(goog.debug.Logger.Level.FINE, message);
  154. logAtLevel(goog.debug.Logger.Level.FINER, message);
  155. logAtLevel(goog.debug.Logger.Level.FINEST, message);
  156. logAtLevel(goog.debug.Logger.Level.ALL, message);
  157. }
  158. /**
  159. * Adds a log record to the debug console.
  160. * @param {!goog.debug.Logger.Level} level The level at which to log.
  161. * @param {string} message The message to log.
  162. */
  163. function logAtLevel(level, message) {
  164. debugConsole.addLogRecord(
  165. new goog.debug.LogRecord(level, message, loggerName1));
  166. }