View Javadoc
1   /*
2    * The MIT License (MIT)
3    *
4    * Copyright (c) 2013-2026 The Coveralls Maven Plugin Project Contributors:
5    *     https://github.com/hazendaz/coveralls-maven-plugin/graphs/contributors
6    *
7    * Permission is hereby granted, free of charge, to any person obtaining a copy
8    * of this software and associated documentation files (the "Software"), to deal
9    * in the Software without restriction, including without limitation the rights
10   * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11   * copies of the Software, and to permit persons to whom the Software is
12   * furnished to do so, subject to the following conditions:
13   *
14   * The above copyright notice and this permission notice shall be included in
15   * all copies or substantial portions of the Software.
16   *
17   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23   * THE SOFTWARE.
24   */
25  package org.eluder.coveralls.maven.plugin.parser;
26  
27  import java.io.File;
28  import java.io.IOException;
29  import java.util.Arrays;
30  import java.util.List;
31  
32  import org.assertj.core.api.Assertions;
33  import org.eluder.coveralls.maven.plugin.CoverageFixture;
34  import org.eluder.coveralls.maven.plugin.CoverageParser;
35  import org.eluder.coveralls.maven.plugin.ProcessingException;
36  import org.eluder.coveralls.maven.plugin.domain.Source;
37  import org.eluder.coveralls.maven.plugin.source.SourceCallback;
38  import org.eluder.coveralls.maven.plugin.source.SourceLoader;
39  import org.eluder.coveralls.maven.plugin.util.TestIoUtil;
40  import org.junit.jupiter.api.Test;
41  import org.mockito.ArgumentCaptor;
42  import org.mockito.Mockito;
43  
44  /**
45   * The Class SagaParserTest.
46   */
47  class SagaParserTest extends AbstractCoverageParserTest {
48  
49      @Override
50      protected CoverageParser createCoverageParser(final File coverageFile, final SourceLoader sourceLoader) {
51          return new SagaParser(coverageFile, sourceLoader);
52      }
53  
54      @Override
55      protected List<String> getCoverageResources() {
56          return Arrays.asList("saga.xml");
57      }
58  
59      @Override
60      protected List<List<String>> getCoverageFixture() {
61          return CoverageFixture.JAVASCRIPT_FILES;
62      }
63  
64      /**
65       * Parse coverage with branch coverage lines including covered and missed branches.
66       *
67       * @throws ProcessingException
68       *             the processing exception
69       * @throws IOException
70       *             Signals that an I/O exception has occurred.
71       */
72      @Test
73      void parseCoverageWithBranches() throws ProcessingException, IOException {
74          final var content = TestIoUtil.readFileContent(TestIoUtil.getFile("BranchScript.js"));
75          final var sourceLoader = Mockito.mock(SourceLoader.class);
76          Mockito.when(sourceLoader.load("BranchScript.js")).thenAnswer(
77                  invocation -> new Source("BranchScript.js", content, TestIoUtil.getSha512DigestHex(content)));
78  
79          final SourceCallback callback = Mockito.mock(SourceCallback.class);
80  
81          final var parser = new SagaParser(TestIoUtil.getFile("saga-branches.xml"), sourceLoader);
82          parser.parse(callback);
83  
84          final ArgumentCaptor<Source> captor = ArgumentCaptor.forClass(Source.class);
85          Mockito.verify(callback).onSource(captor.capture());
86  
87          final var source = captor.getValue();
88          Assertions.assertThat(source.getName()).isEqualTo("BranchScript.js");
89  
90          // Line 2: covered with 3 covered branches and 1 missed branch (75% (3/4))
91          // Line 3: missed with 0 covered and 2 missed branches (0% (0/2))
92          // Line 4: branch=true but no condition-coverage attribute (null case - returns early)
93          final var branches = source.getBranchesList();
94          // Line 2 contributes 3 covered branches and 1 missed branch
95          // Line 3 contributes 2 missed branches
96          // Line 4 contributes nothing (null condition-coverage → early return)
97          final var coveredCount = branches.stream().filter(b -> b.getHits() > 0).count();
98          final var missedCount = branches.stream().filter(b -> b.getHits() == 0).count();
99          Assertions.assertThat(coveredCount).isEqualTo(3);
100         Assertions.assertThat(missedCount).isEqualTo(3);
101     }
102 
103 }