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