| 1 |
// Copyright 2010 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.LogBufferTest'); |
| 16 |
goog.setTestOnly('goog.debug.LogBufferTest'); |
| 17 |
|
| 18 |
goog.require('goog.debug.LogBuffer'); |
| 19 |
goog.require('goog.debug.Logger'); |
| 20 |
goog.require('goog.testing.jsunit'); |
| 21 |
|
| 22 |
var DUMMY_LEVELS = [ |
| 23 |
goog.debug.Logger.Level.INFO, goog.debug.Logger.Level.WARNING, |
| 24 |
goog.debug.Logger.Level.SEVERE |
| 25 |
]; |
| 26 |
var DUMMY_MESSAGES = ['a', 'b', 'c']; |
| 27 |
var DUMMY_NAMES = ['X', 'Y', 'Z']; |
| 28 |
|
| 29 |
var buffer; |
| 30 |
var dummyIndex = 0; |
| 31 |
|
| 32 |
function setUp() { |
| 33 |
goog.debug.LogBuffer.CAPACITY = 4; |
| 34 |
goog.debug.LogBuffer.instance_ = null; |
| 35 |
buffer = goog.debug.LogBuffer.getInstance(); |
| 36 |
} |
| 37 |
|
| 38 |
function verifyRecord(expectedIndex, record) { |
| 39 |
var index = expectedIndex % DUMMY_MESSAGES.length; |
| 40 |
var message = DUMMY_MESSAGES[index]; |
| 41 |
var level = DUMMY_LEVELS[index]; |
| 42 |
var name = DUMMY_NAMES[index]; |
| 43 |
assertEquals( |
| 44 |
'Wrong level for record ' + expectedIndex, level, record.getLevel()); |
| 45 |
assertEquals( |
| 46 |
'Wrong message for record ' + expectedIndex, message, |
| 47 |
record.getMessage()); |
| 48 |
assertEquals( |
| 49 |
'Wrong name for record ' + expectedIndex, name, record.getLoggerName()); |
| 50 |
} |
| 51 |
|
| 52 |
function addAndVerifyRecord() { |
| 53 |
var index = dummyIndex % DUMMY_MESSAGES.length; |
| 54 |
var level = DUMMY_LEVELS[index]; |
| 55 |
var message = DUMMY_MESSAGES[index]; |
| 56 |
var name = DUMMY_NAMES[index]; |
| 57 |
var record = buffer.addRecord(level, message, name); |
| 58 |
verifyRecord(dummyIndex, record); |
| 59 |
dummyIndex++; |
| 60 |
} |
| 61 |
|
| 62 |
function addSomeRecords(howMany) { |
| 63 |
for (var i = 0; i < howMany; i++) { |
| 64 |
addAndVerifyRecord(); |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
function testAddRecord() { |
| 69 |
addSomeRecords(goog.debug.LogBuffer.CAPACITY * 3); |
| 70 |
} |
| 71 |
|
| 72 |
function testIsFull() { |
| 73 |
assertFalse('Should not be full.', buffer.isFull_); |
| 74 |
addSomeRecords(goog.debug.LogBuffer.CAPACITY * 1.5); |
| 75 |
assertTrue('Should be full.', buffer.isFull_); |
| 76 |
buffer.clear(); |
| 77 |
assertFalse('Should not be full after clear().', buffer.isFull_); |
| 78 |
addSomeRecords(goog.debug.LogBuffer.CAPACITY - 1); |
| 79 |
assertFalse('Should not be full but almost full.', buffer.isFull_); |
| 80 |
} |
| 81 |
|
| 82 |
function testForEachRecord() { |
| 83 |
// Test with it half full. |
| 84 |
var howMany1 = goog.debug.LogBuffer.CAPACITY / 2; |
| 85 |
addSomeRecords(howMany1); |
| 86 |
var counter1 = 0; |
| 87 |
buffer.forEachRecord(function(record) { verifyRecord(counter1++, record); }); |
| 88 |
assertEquals('Wrong number of records when half full.', howMany1, counter1); |
| 89 |
|
| 90 |
// Test with it full. |
| 91 |
var howMany2 = goog.debug.LogBuffer.CAPACITY; |
| 92 |
addSomeRecords(howMany2); |
| 93 |
var index = counter1; |
| 94 |
buffer.forEachRecord(function(record) { verifyRecord(index++, record); }); |
| 95 |
assertEquals( |
| 96 |
'Wrong number of records when full.', howMany1 + howMany2, index); |
| 97 |
} |