|
| 1 | +package fi.helsinki.cs.tmc.langs.qmake; |
| 2 | + |
| 3 | +import fi.helsinki.cs.tmc.langs.domain.RunResult; |
| 4 | +import fi.helsinki.cs.tmc.langs.domain.TestResult; |
| 5 | +import fi.helsinki.cs.tmc.langs.utils.TestUtils; |
| 6 | +import java.io.IOException; |
| 7 | +import java.io.PrintWriter; |
| 8 | +import java.nio.file.Path; |
| 9 | +import java.util.ArrayList; |
| 10 | +import java.util.Arrays; |
| 11 | +import java.util.List; |
| 12 | +import org.junit.Test; |
| 13 | +import static org.junit.Assert.*; |
| 14 | + |
| 15 | +public class QTestResultParserTest { |
| 16 | + |
| 17 | + private final QTestCase passing; |
| 18 | + private final QTestCase failing; |
| 19 | + private final QTestCase another; |
| 20 | + |
| 21 | + public QTestResultParserTest() { |
| 22 | + passing = new QTestCase("passing", true, "Passed", Arrays.asList(new String[]{"1"})); |
| 23 | + failing = new QTestCase("failing", false, "This test should've failed", Arrays.asList(new String[]{"2"})); |
| 24 | + another = new QTestCase("another", true, "Passed", Arrays.asList(new String[]{"3"})); |
| 25 | + } |
| 26 | + |
| 27 | + @Test(expected = IllegalStateException.class) |
| 28 | + public void testParsingWithNoTests() throws Exception { |
| 29 | + Path empty_test_output = TestUtils.initTempFileWithContent("empty_test_output", "xml", ""); |
| 30 | + QTestResultParser qtparser = new QTestResultParser(empty_test_output); |
| 31 | + assertTrue(qtparser.getTestResults().isEmpty()); |
| 32 | + } |
| 33 | + |
| 34 | + @Test |
| 35 | + public void testParsingWithSuccessfulTest() { |
| 36 | + List<QTestCase> pass = new ArrayList<>(); |
| 37 | + pass.add(passing); |
| 38 | + QTestResultParser qtparser = createResultParser(pass); |
| 39 | + List<TestResult> results = qtparser.getTestResults(); |
| 40 | + assertEquals("There should only be one test result", 1, results.size()); |
| 41 | + TestResult result = results.get(0); |
| 42 | + assertTrue("The test should be successful", result.isSuccessful()); |
| 43 | + assertEquals("The name of the test should be \"passing\"", |
| 44 | + "passing", |
| 45 | + result.getName()); |
| 46 | + assertEquals("The message should not contain any assertions", |
| 47 | + "", |
| 48 | + result.getMessage()); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + public void testParsingWithFailingTest() { |
| 53 | + List<QTestCase> fail = new ArrayList<>(); |
| 54 | + fail.add(failing); |
| 55 | + QTestResultParser qtparser = createResultParser(fail); |
| 56 | + List<TestResult> results = qtparser.getTestResults(); |
| 57 | + assertEquals("There should only be one test result", 1, results.size()); |
| 58 | + TestResult result = results.get(0); |
| 59 | + assertFalse("The test should be failing", result.isSuccessful()); |
| 60 | + assertEquals("The name of the test should be \"failing\"", "failing", |
| 61 | + result.getName()); |
| 62 | + assertEquals("The message should contain the failed assertion", |
| 63 | + "This test should've failed", |
| 64 | + result.getMessage()); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + public void testParsingWithPassingAndFailingTest() { |
| 69 | + List<QTestCase> failAndPass = new ArrayList<>(); |
| 70 | + failAndPass.add(failing); |
| 71 | + failAndPass.add(passing); |
| 72 | + QTestResultParser qtparser = createResultParser(failAndPass); |
| 73 | + List<TestResult> results = qtparser.getTestResults(); |
| 74 | + assertEquals("There should only be two test results", 2, results.size()); |
| 75 | + TestResult result = results.get(0); |
| 76 | + assertFalse("The test should be failing", result.isSuccessful()); |
| 77 | + assertEquals("The name of the test should be \"failing\"", "failing", |
| 78 | + result.getName()); |
| 79 | + assertEquals("The message should contain the failed assertion", |
| 80 | + "This test should've failed", |
| 81 | + result.getMessage()); |
| 82 | + result = results.get(1); |
| 83 | + assertTrue("The test should be passing", result.isSuccessful()); |
| 84 | + assertEquals("The name of the test should be \"passing\"", "passing", |
| 85 | + result.getName()); |
| 86 | + assertEquals("The message should not contain any assertions", |
| 87 | + "", |
| 88 | + result.getMessage()); |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + public void testTestRunWithOneFailingTestIsFailed() { |
| 93 | + List<QTestCase> failAndPass = new ArrayList<>(); |
| 94 | + failAndPass.add(failing); |
| 95 | + failAndPass.add(passing); |
| 96 | + QTestResultParser qtparser = createResultParser(failAndPass); |
| 97 | + RunResult result = qtparser.result(); |
| 98 | + assertEquals("Run result should be failed if one failing test", |
| 99 | + result.status, |
| 100 | + RunResult.Status.TESTS_FAILED); |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + public void testTestrunWithPassingTestsIsPassed() { |
| 105 | + List<QTestCase> allPass = new ArrayList<>(); |
| 106 | + allPass.add(passing); |
| 107 | + allPass.add(passing); |
| 108 | + QTestResultParser qtparser = createResultParser(allPass); |
| 109 | + RunResult result = qtparser.result(); |
| 110 | + assertEquals("Run result should be passed if all tests are passing", |
| 111 | + result.status, |
| 112 | + RunResult.Status.PASSED); |
| 113 | + } |
| 114 | + |
| 115 | + @Test |
| 116 | + public void testParsedPointsMapToCorrectTests() { |
| 117 | + List<QTestCase> allPass = new ArrayList<>(); |
| 118 | + allPass.add(passing); |
| 119 | + allPass.add(another); |
| 120 | + |
| 121 | + QTestResultParser qtparser = createResultParser(allPass); |
| 122 | + List<TestResult> results = qtparser.result().testResults; |
| 123 | + assertEquals(2, results.size()); |
| 124 | + assertEquals("Point for passing should be 1", |
| 125 | + "1", |
| 126 | + results.get(0).points.get(0)); |
| 127 | + assertEquals("Point for another should be 3", |
| 128 | + "3", |
| 129 | + results.get(1).points.get(0)); |
| 130 | + } |
| 131 | + |
| 132 | + private QTestResultParser createResultParser(List<QTestCase> testCases) { |
| 133 | + Path testPath = null; |
| 134 | + try { |
| 135 | + testPath = constructTestOutput(testCases); |
| 136 | + } catch (Exception e) { |
| 137 | + fail("Error creating or parsing output file: " + e.getMessage()); |
| 138 | + } |
| 139 | + return new QTestResultParser(testPath); |
| 140 | + } |
| 141 | + |
| 142 | + private Path constructTestOutput(List<QTestCase> testCases) throws IOException { |
| 143 | + Path tmp = TestUtils.initTempFileWithContent("test_output", "xml", ""); |
| 144 | + PrintWriter pw = new PrintWriter(tmp.toFile(), "UTF-8"); |
| 145 | + pw.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); |
| 146 | + pw.println("<TestCase name=\"test_runner\">"); |
| 147 | + for (QTestCase t : testCases) { |
| 148 | + pw.println("<TestFunction name=\"" + t.getName() + "\">"); |
| 149 | + for (String point : t.getPoints()) { |
| 150 | + pw.println("<Message type=\"qinfo\" file=\"\" line=\"0\">"); |
| 151 | + pw.println("<Description><![CDATA[TMC:" + t.getName() + "." + point + "]]></Description>"); |
| 152 | + pw.println("</Message>"); |
| 153 | + } |
| 154 | + |
| 155 | + if (t.getResult()) { |
| 156 | + pw.println("<Incident type=\"pass\" file=\"\" line=\"0\" />"); |
| 157 | + } else { |
| 158 | + pw.println("<Incident type=\"fail\" file=\"test_runner.cpp\" line=\"420\">"); |
| 159 | + pw.println(" <Description>" + t.getMessage() + "</Description>"); |
| 160 | + pw.println("</Incident>"); |
| 161 | + } |
| 162 | + |
| 163 | + pw.println("</TestFunction>"); |
| 164 | + } |
| 165 | + |
| 166 | + pw.println("</TestCase>"); |
| 167 | + |
| 168 | + pw.flush(); |
| 169 | + pw.close(); |
| 170 | + return tmp; |
| 171 | + } |
| 172 | + |
| 173 | +} |
0 commit comments