-
Notifications
You must be signed in to change notification settings - Fork 41
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implemented LineRange-class and improved overall code-coverage #30
Merged
uhafner
merged 3 commits into
jenkinsci:main
from
hm-jtreichel:implement_line_range_class_and_improve_coverage
Sep 28, 2023
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
package edu.hm.hafner.coverage; | ||
|
||
import java.util.NavigableSet; | ||
import java.util.Objects; | ||
import java.util.Set; | ||
import java.util.SortedSet; | ||
import java.util.TreeSet; | ||
|
||
/** | ||
* Represents a range of consecutive lines within a file ({@link FileNode}). | ||
* | ||
* @author Jannik Treichel | ||
*/ | ||
public class LineRange implements Comparable<LineRange> { | ||
|
||
private final int start; | ||
private final int end; | ||
|
||
/** | ||
* Returns a set of LineRanges based on a given sorted set of line numbers. | ||
* | ||
* @param lines the set of line numbers to generate ranges from | ||
* @return a navigable set of LineRanges | ||
*/ | ||
public static NavigableSet<LineRange> getRangesFromSortedLines(final SortedSet<Integer> lines) { | ||
NavigableSet<LineRange> lineRanges = new TreeSet<>(); | ||
|
||
if (lines.isEmpty()) { | ||
return lineRanges; | ||
} | ||
|
||
int currentStart = lines.first(); | ||
int currentEnd = lines.first(); | ||
|
||
for (int line : lines) { | ||
if (line - currentEnd > 1) { | ||
lineRanges.add(new LineRange(currentStart, currentEnd)); | ||
currentStart = line; | ||
} | ||
currentEnd = line; | ||
} | ||
lineRanges.add(new LineRange(currentStart, currentEnd)); | ||
|
||
return lineRanges; | ||
} | ||
|
||
/** | ||
* Returns a set of LineRanges based on a given unsorted set of line numbers. | ||
* | ||
* @param lines the set of line numbers to generate ranges from | ||
* @return a navigable set of LineRanges | ||
*/ | ||
public static NavigableSet<LineRange> getRangesFromUnsortedLines(final Set<Integer> lines) { | ||
return getRangesFromSortedLines(new TreeSet<>(lines)); | ||
} | ||
|
||
/** | ||
* Constructs a new LineRange instance where the start- and end- | ||
* values are equal. | ||
* | ||
* @param startAndEnd the start- and end-value of the range | ||
*/ | ||
public LineRange(final int startAndEnd) { | ||
this(startAndEnd, startAndEnd); | ||
} | ||
|
||
/** | ||
* Constructs a new LineRange instance with a specified start- | ||
* and end-values. | ||
* | ||
* @param start the first line in the range | ||
* @param end the last line in the range | ||
* @throws IllegalArgumentException if start is less than or equal | ||
* to zero, or if end is less than start | ||
*/ | ||
public LineRange(final int start, final int end) { | ||
if (start <= 0) { | ||
throw new IllegalArgumentException( | ||
String.format("A LineRange can only contain positive values! Start-value <%s> is not possible.", start)); | ||
} | ||
if (end < start) { | ||
throw new IllegalArgumentException( | ||
String.format("The end <%s> of the LineRange cannot be smaller than the start <%s>!", end, start)); | ||
} | ||
this.start = start; | ||
this.end = end; | ||
} | ||
|
||
public int getStart() { | ||
return start; | ||
} | ||
|
||
public int getEnd() { | ||
return end; | ||
} | ||
|
||
@Override | ||
public boolean equals(final Object o) { | ||
if (this == o) { | ||
return true; | ||
} | ||
if (o == null || getClass() != o.getClass()) { | ||
return false; | ||
} | ||
LineRange lineRange = (LineRange) o; | ||
return start == lineRange.start && end == lineRange.end; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(start, end); | ||
} | ||
|
||
@Override | ||
public int compareTo(final LineRange o) { | ||
return Integer.compare(this.start, o.start); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return String.format("<LineRange [%s-%s]>", start, end); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
src/test/java/edu/hm/hafner/coverage/CoverageParserTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package edu.hm.hafner.coverage; | ||
import org.junit.jupiter.api.Test; | ||
import edu.hm.hafner.util.SecureXmlParserFactory.ParsingException; | ||
import static edu.hm.hafner.coverage.assertions.Assertions.*; | ||
/** | ||
* Test-class to provide tests for protected (static) methods of abstract class | ||
* {@link CoverageParser}. | ||
* | ||
* @author Jannik Treichel | ||
*/ | ||
class CoverageParserTest { | ||
|
||
@Test | ||
void shouldCreateEofException() { | ||
var parsingException = CoverageParser.createEofException(); | ||
|
||
assertThat(parsingException) | ||
.isInstanceOf(ParsingException.class) | ||
.hasMessage("Unexpected end of file"); | ||
} | ||
|
||
@Test | ||
void shouldReturnZeroOnInvalidStringParsing() { | ||
assertThat(CoverageParser.parseInteger("NO_NUMBER")) | ||
.isEqualTo(0); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check notice
Code scanning / CodeQL
Unused classes and interfaces Note test