expectedfailures_test.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. // Copyright 2008 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.testing.ExpectedFailuresTest');
  15. goog.setTestOnly('goog.testing.ExpectedFailuresTest');
  16. goog.require('goog.debug.Logger');
  17. goog.require('goog.testing.ExpectedFailures');
  18. goog.require('goog.testing.JsUnitException');
  19. goog.require('goog.testing.TestCase');
  20. goog.require('goog.testing.jsunit');
  21. var count, expectedFailures, lastLevel, lastMessage;
  22. // Stub out the logger.
  23. goog.testing.ExpectedFailures.prototype.logger_.log = function(level, message) {
  24. lastLevel = level;
  25. lastMessage = message;
  26. count++;
  27. };
  28. function setUp() {
  29. // TODO(b/25875505): Fix unreported assertions (go/failonunreportedasserts).
  30. goog.testing.TestCase.getActiveTestCase().failOnUnreportedAsserts = false;
  31. expectedFailures = new goog.testing.ExpectedFailures();
  32. count = 0;
  33. lastLevel = lastMessage = '';
  34. }
  35. // Individual test methods.
  36. function testNoExpectedFailure() {
  37. expectedFailures.handleTearDown();
  38. }
  39. function testPreventExpectedFailure() {
  40. expectedFailures.expectFailureFor(true);
  41. expectedFailures.handleException(new goog.testing.JsUnitException('', ''));
  42. assertEquals('Should have logged a message', 1, count);
  43. assertEquals(
  44. 'Should have logged an info message', goog.debug.Logger.Level.INFO,
  45. lastLevel);
  46. assertContains(
  47. 'Should log a suppression message', 'Suppressing test failure',
  48. lastMessage);
  49. expectedFailures.handleTearDown();
  50. assertEquals('Should not have logged another message', 1, count);
  51. }
  52. function testDoNotPreventException() {
  53. var ex = 'exception';
  54. expectedFailures.expectFailureFor(false);
  55. var e = assertThrows('Should have rethrown exception', function() {
  56. expectedFailures.handleException(ex);
  57. });
  58. assertEquals('Should rethrow same exception', ex, e);
  59. }
  60. function testExpectedFailureDidNotOccur() {
  61. expectedFailures.expectFailureFor(true);
  62. expectedFailures.handleTearDown();
  63. assertEquals('Should have logged a message', 1, count);
  64. assertEquals(
  65. 'Should have logged a warning', goog.debug.Logger.Level.WARNING,
  66. lastLevel);
  67. assertContains(
  68. 'Should log a suppression message', 'Expected a test failure',
  69. lastMessage);
  70. }
  71. function testRun() {
  72. expectedFailures.expectFailureFor(true);
  73. expectedFailures.run(function() { fail('Expected failure'); });
  74. assertEquals('Should have logged a message', 1, count);
  75. assertEquals(
  76. 'Should have logged an info message', goog.debug.Logger.Level.INFO,
  77. lastLevel);
  78. assertContains(
  79. 'Should log a suppression message', 'Suppressing test failure',
  80. lastMessage);
  81. expectedFailures.handleTearDown();
  82. assertEquals('Should not have logged another message', 1, count);
  83. }
  84. function testRunStrict() {
  85. expectedFailures.expectFailureFor(true);
  86. var ex = assertThrows(function() {
  87. expectedFailures.run(function() {
  88. // Doesn't fail!
  89. });
  90. });
  91. assertContains(
  92. "Expected a test failure in 'testRunStrict' but the test passed.",
  93. ex.message);
  94. }
  95. function testRunLenient() {
  96. expectedFailures.expectFailureFor(true);
  97. expectedFailures.run(function() {
  98. // Doesn't fail!
  99. }, true);
  100. expectedFailures.handleTearDown();
  101. assertEquals('Should have logged a message', 1, count);
  102. assertEquals(
  103. 'Should have logged a warning', goog.debug.Logger.Level.WARNING,
  104. lastLevel);
  105. assertContains(
  106. 'Should log a suppression message', 'Expected a test failure',
  107. lastMessage);
  108. }