Skip to content

Commit

Permalink
test: Fixed 4 nondeterministic flaky tests by sorting collections to …
Browse files Browse the repository at this point in the history
…make them deterministic
  • Loading branch information
nikunjagarwal321 committed Nov 25, 2024
1 parent 5e519b7 commit 6496e3c
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
3 changes: 3 additions & 0 deletions src/test/java/spoon/test/imports/ImportTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
Expand Down Expand Up @@ -1877,6 +1878,7 @@ void staticImports_ofNestedTypes_shouldBeRecorded(CtModel model) {

// assert
List<CtImport> imports = mainType.getPosition().getCompilationUnit().getImports();
imports.sort(Comparator.comparing(importElement -> importElement.getReference().getSimpleName()));
assertThat(imports, hasSize(2));

CtImport import0 = imports.get(0);
Expand All @@ -1894,6 +1896,7 @@ void staticTypeAndMethodImport_importShouldAppearOnlyOnceIfTheirSimpleNamesAreEq
// assert
List<CtImport> imports = mainType.getPosition().getCompilationUnit().getImports();
assertThat(imports, hasSize(2));
imports.sort(Comparator.comparing(importElement -> importElement.getImportKind().toString()));

CtImport import0 = imports.get(0);
assertThat(import0.getImportKind(), is(CtImportKind.METHOD));
Expand Down
3 changes: 3 additions & 0 deletions src/test/java/spoon/test/position/PositionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import java.io.File;
import java.io.IOException;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;

Expand Down Expand Up @@ -806,6 +807,8 @@ public void testPositionOfCtImport() throws Exception {
AnnonymousClassNewIface.class);
String originSources = foo.getPosition().getCompilationUnit().getOriginalSourceCode();
List<CtImport> imports = foo.getPosition().getCompilationUnit().getImports();
// Sorting using position of import element so that they are sorted in the order they are imported
imports.sort(Comparator.comparing(importElement -> importElement.getPosition().getSourceStart()));
assertEquals(2, imports.size());
Iterator<CtImport> iter = imports.iterator();
{
Expand Down
8 changes: 6 additions & 2 deletions src/test/java/spoon/test/template/SubstitutionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,12 @@ public void testCreateTypeFromTemplate() {
assertNotNull(genEnum);
assertSame(genEnum, factory.Type().get("generated.GenEnum"));
assertEquals(2, genEnum.getEnumValues().size());
assertEquals("GOOD", genEnum.getEnumValues().get(0).getSimpleName());
assertEquals("BETTER", genEnum.getEnumValues().get(1).getSimpleName());
List<String> enumValueNames = genEnum.getEnumValues().stream()
.map(CtEnumValue::getSimpleName)
.sorted()
.toList();
assertEquals("GOOD", enumValueNames.get(1));
assertEquals("BETTER", enumValueNames.get(0));
}

@Test
Expand Down

0 comments on commit 6496e3c

Please sign in to comment.