Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 package org.chromium.testing.local; | |
| 6 | |
| 7 import org.junit.Assert; | |
| 8 import org.junit.Test; | |
| 9 import org.junit.runner.Description; | |
| 10 import org.junit.runner.RunWith; | |
| 11 import org.junit.runners.BlockJUnit4ClassRunner; | |
| 12 | |
| 13 import java.io.ByteArrayOutputStream; | |
| 14 import java.io.PrintStream; | |
| 15 | |
| 16 import java.util.Comparator; | |
| 17 import java.util.HashSet; | |
| 18 import java.util.Set; | |
| 19 import java.util.TreeSet; | |
| 20 | |
| 21 /** | |
| 22 * Unit tests for GtestLogger. | |
| 23 */ | |
| 24 @RunWith(BlockJUnit4ClassRunner.class) | |
| 25 public class GtestLoggerTest { | |
|
nyquist
2014/09/19 22:21:45
Thanks! I think this is helpful, since in fact oth
| |
| 26 | |
| 27 @Test | |
| 28 public void testTestStarted() { | |
| 29 ByteArrayOutputStream actual = new ByteArrayOutputStream(); | |
|
nyquist
2014/09/19 22:21:45
Optional: This is fine with me, but maybe just use
jbudorick
2014/09/20 00:11:29
discussed offline, sticking with this impl.
| |
| 30 GtestLogger loggerUnderTest = new GtestLogger(new PrintStream(actual)); | |
| 31 loggerUnderTest.testStarted( | |
| 32 Description.createTestDescription(GtestLoggerTest.class, "testTe stStarted")); | |
| 33 Assert.assertEquals( | |
| 34 "[ RUN ] org.chromium.testing.local.GtestLoggerTest.testTes tStarted\n", | |
| 35 actual.toString()); | |
| 36 } | |
| 37 | |
| 38 @Test | |
| 39 public void testTestFinishedPassed() { | |
| 40 ByteArrayOutputStream actual = new ByteArrayOutputStream(); | |
|
nyquist
2014/09/19 22:21:45
Optional: This seems to be repeated. Could you put
jbudorick
2014/09/20 00:11:29
I thought about this too. I was on the fence, as l
nyquist
2014/09/22 23:32:09
I see.
Now that I see it, I do agree it loses a bi
jbudorick
2014/09/22 23:53:01
Reverting to non-@Before version.
| |
| 41 GtestLogger loggerUnderTest = new GtestLogger(new PrintStream(actual)); | |
| 42 loggerUnderTest.testFinished( | |
| 43 Description.createTestDescription(GtestLoggerTest.class, "testTe stFinishedPassed"), | |
| 44 true, 123); | |
| 45 Assert.assertEquals( | |
| 46 "[ OK ] org.chromium.testing.local.GtestLoggerTest.testTes tFinishedPassed" + | |
| 47 " (123 ms)\n", | |
| 48 actual.toString()); | |
| 49 } | |
| 50 | |
| 51 @Test | |
| 52 public void testTestFinishedFailed() { | |
| 53 ByteArrayOutputStream actual = new ByteArrayOutputStream(); | |
| 54 GtestLogger loggerUnderTest = new GtestLogger(new PrintStream(actual)); | |
| 55 loggerUnderTest.testFinished( | |
| 56 Description.createTestDescription(GtestLoggerTest.class, "testTe stFinishedPassed"), | |
| 57 false, 123); | |
| 58 Assert.assertEquals( | |
| 59 "[ FAILED ] org.chromium.testing.local.GtestLoggerTest.testTes tFinishedPassed" + | |
| 60 " (123 ms)\n", | |
| 61 actual.toString()); | |
| 62 } | |
| 63 | |
| 64 @Test | |
| 65 public void testTestCaseStarted() { | |
| 66 ByteArrayOutputStream actual = new ByteArrayOutputStream(); | |
| 67 GtestLogger loggerUnderTest = new GtestLogger(new PrintStream(actual)); | |
| 68 loggerUnderTest.testCaseStarted( | |
| 69 Description.createSuiteDescription(GtestLoggerTest.class), 456); | |
| 70 Assert.assertEquals( | |
| 71 "[----------] Run 456 test cases from org.chromium.testing.local .GtestLoggerTest\n", | |
| 72 actual.toString()); | |
| 73 } | |
| 74 | |
| 75 @Test | |
| 76 public void testTestCaseFinished() { | |
| 77 ByteArrayOutputStream actual = new ByteArrayOutputStream(); | |
| 78 GtestLogger loggerUnderTest = new GtestLogger(new PrintStream(actual)); | |
| 79 loggerUnderTest.testCaseFinished( | |
| 80 Description.createSuiteDescription(GtestLoggerTest.class), 456, 123); | |
| 81 Assert.assertEquals( | |
| 82 "[----------] Run 456 test cases from org.chromium.testing.local .GtestLoggerTest" + | |
| 83 " (123 ms)\n\n", | |
| 84 actual.toString()); | |
| 85 } | |
| 86 | |
| 87 @Test | |
| 88 public void testTestRunStarted() { | |
| 89 ByteArrayOutputStream actual = new ByteArrayOutputStream(); | |
| 90 GtestLogger loggerUnderTest = new GtestLogger(new PrintStream(actual)); | |
| 91 loggerUnderTest.testRunStarted(1234); | |
| 92 Assert.assertEquals( | |
| 93 "[==========] Running 1234 tests.\n" + | |
| 94 "[----------] Global test environment set-up.\n\n", | |
| 95 actual.toString()); | |
| 96 } | |
| 97 | |
| 98 @Test | |
| 99 public void testTestRunFinishedNoFailures() { | |
| 100 ByteArrayOutputStream actual = new ByteArrayOutputStream(); | |
| 101 GtestLogger loggerUnderTest = new GtestLogger(new PrintStream(actual)); | |
| 102 loggerUnderTest.testRunFinished(1234, new HashSet<Description>(), 4321); | |
| 103 Assert.assertEquals( | |
| 104 "[----------] Global test environment tear-down.\n" + | |
| 105 "[==========] 1234 tests ran. (4321 ms total)\n" + | |
| 106 "[ PASSED ] 1234 tests.\n", | |
| 107 actual.toString()); | |
| 108 } | |
| 109 | |
| 110 @Test | |
| 111 public void testTestRunFinishedWithFailures() { | |
| 112 ByteArrayOutputStream actual = new ByteArrayOutputStream(); | |
| 113 GtestLogger loggerUnderTest = new GtestLogger(new PrintStream(actual)); | |
| 114 | |
| 115 Set<Description> failures = new TreeSet<Description>(new DescriptionComp arator()); | |
| 116 failures.add(Description.createTestDescription( | |
| 117 "GtestLoggerTest", "testTestRunFinishedNoFailures")); | |
| 118 failures.add(Description.createTestDescription( | |
| 119 "GtestLoggerTest", "testTestRunFinishedWithFailures")); | |
| 120 | |
| 121 loggerUnderTest.testRunFinished(1232, failures, 4312); | |
| 122 Assert.assertEquals( | |
| 123 "[----------] Global test environment tear-down.\n" + | |
| 124 "[==========] 1234 tests ran. (4312 ms total)\n" + | |
| 125 "[ PASSED ] 1232 tests.\n" + | |
| 126 "[ FAILED ] 2 tests.\n" + | |
| 127 "[ FAILED ] GtestLoggerTest.testTestRunFinishedNoFailures\n" + | |
| 128 "[ FAILED ] GtestLoggerTest.testTestRunFinishedWithFailures\n" + | |
| 129 "\n", | |
| 130 actual.toString()); | |
| 131 } | |
| 132 | |
| 133 private static class DescriptionComparator implements Comparator<Description > { | |
| 134 | |
| 135 private static String toGtestStyleString(Description d) { | |
| 136 return d.getClassName() + "." + d.getMethodName(); | |
| 137 } | |
| 138 | |
| 139 @Override | |
| 140 public int compare(Description o1, Description o2) { | |
| 141 return toGtestStyleString(o1).compareTo(toGtestStyleString(o2)); | |
| 142 } | |
| 143 | |
| 144 @Override | |
| 145 public boolean equals(Object obj) { | |
| 146 return obj.getClass() == this.getClass(); | |
| 147 } | |
| 148 } | |
| 149 } | |
| 150 | |
| OLD | NEW |