Skip to content

Commit

Permalink
issues/153: JSON deserialization changes the id of a Test Impact Anal…
Browse files Browse the repository at this point in the history
…ysis (#154)
  • Loading branch information
fmck3516 authored Apr 16, 2024
1 parent 55c1943 commit f19b312
Show file tree
Hide file tree
Showing 7 changed files with 850 additions and 1,223 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,10 @@ ClassFile getById(int id) {

String toJson() {
StringBuilder result = new StringBuilder();
var classFilesAsList = new ArrayList<>(classFiles);
result.append("{" + lineSeparator());
for (int i = 0; i < classFiles.size(); i++) {
result.append("\t\t\"%s\": ".formatted(idsByClassFile.get(classFilesAsList.get(i))));
result.append(classFilesAsList.get(i).toJson());
result.append("\t\t\"%s\": ".formatted(i));
result.append(classFilesById.get(i).toJson());
if (i < classFiles.size() - 1) {
result.append("," + lineSeparator());
}
Expand Down
12 changes: 11 additions & 1 deletion skippy-core/src/main/java/io/skippy/core/Tokenizer.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ final class Tokenizer {
private final int tail;

Tokenizer(String input) {
this.stream = input.replaceAll("\\s+","").toCharArray();
this.stream = input.replaceAll("[\\s&&[^ ]]", "").toCharArray();
this.head = 0;
this.tail = stream.length;
}
Expand All @@ -47,13 +47,15 @@ String asString() {
}

void skip(char c) {
skipLeadingWhitespaces();
if (head == tail || stream[head] != c) {
throw new IllegalStateException("Can't skip over '%s' in residual characters '%s'.".formatted(c, asString()));
}
head++;
}

String next() {
skipLeadingWhitespaces();
if (peek('{')) {
head++;
return "{";
Expand Down Expand Up @@ -83,26 +85,34 @@ String next() {
}

boolean peek(char c) {
skipLeadingWhitespaces();
if (head == tail) {
return false;
}
return stream[head] == c;
}

boolean peekDigit() {
skipLeadingWhitespaces();
if (head == tail) {
return false;
}
return isDigit(stream[head]);
}

void skipIfNext(char c) {
skipLeadingWhitespaces();
if (head == tail) {
return;
}
if (stream[head] == c) {
head++;
}
}
private void skipLeadingWhitespaces() {
while (head != tail && stream[head] == ' ') {
head++;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ void testParse() {
assertEquals("ZT0GoiWG8Az5TevH9/JwBg==", classFile.getHash());
}


@ParameterizedTest
@CsvSource(value = {
"LeftPadder.class:com.example.LeftPadder",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package io.skippy.core;

import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

import java.io.IOException;
Expand All @@ -25,14 +24,18 @@
import java.nio.file.Files;
import java.nio.file.Paths;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class TestImpactAnalysisParsePerformanceTest {

@Test
@Disabled
void testParse() throws URISyntaxException, IOException {
var jsonFile = Paths.get(getClass().getResource("test-impact-analysis.json").toURI());
TestImpactAnalysis.parse(Files.readString(jsonFile, StandardCharsets.UTF_8));
var testImpactAnalysis = TestImpactAnalysis.parse(Files.readString(jsonFile, StandardCharsets.UTF_8));
Profiler.printResults();
assertEquals("ACB10843996699388C4DD7A841D42BD9", testImpactAnalysis.getId());
assertEquals(2510, testImpactAnalysis.getClassFileContainer().getClassFiles().size());
assertEquals(400, testImpactAnalysis.getAnalyzedTests().size());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -158,12 +158,13 @@ void testToJsonTwoTestsFourClasses() {
void testParseNoClassesNoTests() {
var testImpactAnalysis = TestImpactAnalysis.parse("""
{
"id": "00000000000000000000000000000000",
"id": "F8D85DB143EC3F06FAD5D0E0C730E1E9",
"classes": {},
"tests": []
}
""");

assertEquals("F8D85DB143EC3F06FAD5D0E0C730E1E9", testImpactAnalysis.getId());
assertEquals(emptyList(), testImpactAnalysis.getAnalyzedTests());
assertEquals(emptySet(), testImpactAnalysis.getClassFileContainer().getClassFiles());
}
Expand All @@ -172,7 +173,7 @@ void testParseNoClassesNoTests() {
void testParseOneTestOneClass() {
var testImpactAnalysis = TestImpactAnalysis.parse("""
{
"id": "00000000000000000000000000000000",
"id": "D013368C0DD441D819DEA78640F4EC1A",
"classes": {
"0": {
"name": "com.example.FooTest",
Expand All @@ -191,6 +192,8 @@ void testParseOneTestOneClass() {
}
""");

assertEquals("D013368C0DD441D819DEA78640F4EC1A", testImpactAnalysis.getId());

var classFiles = new ArrayList<>(testImpactAnalysis.getClassFileContainer().getClassFiles());
assertEquals(1, classFiles.size());
assertEquals("com.example.FooTest", classFiles.get(0).getClassName());
Expand All @@ -206,7 +209,7 @@ void testParseOneTestOneClass() {
void testParseTwoTestsFourClasses() {
var testImpactAnalysis = TestImpactAnalysis.parse("""
{
"id": "00000000000000000000000000000000",
"id": "40F512FD02B1EEBF932622C51AFB5268",
"classes": {
"0": {
"name": "com.example.Class1",
Expand Down Expand Up @@ -248,6 +251,8 @@ void testParseTwoTestsFourClasses() {
}
""");

assertEquals("40F512FD02B1EEBF932622C51AFB5268", testImpactAnalysis.getId());

var classFiles = new ArrayList<>(testImpactAnalysis.getClassFileContainer().getClassFiles());
assertEquals(4, classFiles.size());
assertEquals("com.example.Class1", classFiles.get(0).getClassName());
Expand Down
24 changes: 17 additions & 7 deletions skippy-core/src/test/java/io/skippy/core/TokenizerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ public class TokenizerTest {

@ParameterizedTest
@CsvSource(value = {
"ab",
" ab"
"[{",
" [{"
}, delimiter = ':')
void testSkip(String stream) {
var tokenizer = new Tokenizer(stream);
tokenizer.skip('a');
assertEquals("b", tokenizer.asString());
tokenizer.skip('[');
assertEquals("{", tokenizer.asString());
}

@Test
Expand All @@ -45,9 +45,9 @@ void testInvalidSkip() {

@Test
void testSkipConsumesLeadingWhitespaces() {
var tokenizer = new Tokenizer(" ab");
tokenizer.skip('a');
assertEquals("b", tokenizer.asString());
var tokenizer = new Tokenizer(" [ foo");
tokenizer.skip('[');
assertEquals(" foo", tokenizer.asString());
}

@ParameterizedTest
Expand All @@ -73,4 +73,14 @@ void testPeek(String stream, char search, boolean expected) {
assertEquals(expected, tokenizer.peek(search));
}

@Test
void testTokenWithWhitespace() {
var tokenizer = new Tokenizer("""
"name": "foo bar"
""");
assertEquals("name", tokenizer.next());
tokenizer.skip(':');
assertEquals("foo bar", tokenizer.next());
}

}
Loading

0 comments on commit f19b312

Please sign in to comment.