logbuffer_test.js 3.1 KB

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