formatter_test.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. // Copyright 2014 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.FormatterTest');
  15. goog.setTestOnly('goog.debug.FormatterTest');
  16. goog.require('goog.debug.HtmlFormatter');
  17. goog.require('goog.debug.LogRecord');
  18. goog.require('goog.debug.Logger');
  19. goog.require('goog.html.SafeHtml');
  20. goog.require('goog.testing.jsunit');
  21. var EXPECTED_RECORD_HTML_RE =
  22. '^prefix \\[.*?\\] \\[  .*?s\\] \\[loggerName\\] ' +
  23. '<span class="dbg-f">mess<br> &#160;age<br>Message:';
  24. var logRecord;
  25. function setUp() {
  26. logRecord = new goog.debug.LogRecord(
  27. goog.debug.Logger.Level.CONFIG, 'mess\n age', 'loggerName', 1, 100);
  28. // Exception needs to be present for exception text to get printed
  29. // by HtmlFormatter.
  30. logRecord.setException(new Error('exc\n eption'));
  31. }
  32. function testExposeException() {
  33. var expected = 'Message: message&quot;<br>' +
  34. 'Url: <a href="view-source:http://fileName&quot;" ' +
  35. 'target="_new">http://fileName&quot;</a><br>' +
  36. 'Line: lineNumber&quot;<br><br>' +
  37. 'Browser stack:<br>' +
  38. 'stack&quot;-&gt; [end]';
  39. var error = {
  40. message: 'message"',
  41. fileName: 'http://fileName"',
  42. lineNumber: 'lineNumber"',
  43. stack: 'stack"'
  44. };
  45. var actualHtml = goog.debug.HtmlFormatter.exposeExceptionAsHtml(error);
  46. var actual = goog.html.SafeHtml.unwrap(actualHtml);
  47. actual = actual.substring(0, expected.length);
  48. assertEquals(expected, actual);
  49. }
  50. function testHtmlFormatter_formatRecord() {
  51. var formatter = new goog.debug.HtmlFormatter('prefix');
  52. var actual = formatter.formatRecord(logRecord);
  53. assertRegExp(EXPECTED_RECORD_HTML_RE, actual);
  54. }
  55. function testHtmlFormatter_formatRecordAsHtml() {
  56. var formatter = new goog.debug.HtmlFormatter('prefix');
  57. var actual = formatter.formatRecordAsHtml(logRecord);
  58. assertRegExp(EXPECTED_RECORD_HTML_RE, goog.html.SafeHtml.unwrap(actual));
  59. }