Skip to content
This repository has been archived by the owner on Oct 18, 2024. It is now read-only.

Commit

Permalink
tests: revert to using JUnit4 runner but with Mockito
Browse files Browse the repository at this point in the history
  • Loading branch information
itsaky committed Aug 1, 2023
1 parent 1de48ed commit 7421e2c
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .idea/kotlinc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions android-tree-sitter/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,5 @@ dependencies {
testImplementation(libs.tests.google.truth)
testImplementation(libs.tests.junit)
testImplementation(libs.tests.robolectric)
testImplementation(libs.tests.mockito)
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import static com.google.common.truth.Truth.assertThat;
import static com.itsaky.androidide.treesitter.TestUtils.readString;

import android.text.TextUtils;
import com.itsaky.androidide.treesitter.aidl.TSLanguageAidl;
import com.itsaky.androidide.treesitter.java.TSLanguageJava;
import com.itsaky.androidide.treesitter.json.TSLanguageJson;
Expand All @@ -33,15 +34,38 @@
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.junit.runners.JUnit4;
import org.mockito.ArgumentMatchers;
import org.mockito.MockedStatic;
import org.mockito.Mockito;

@RunWith(RobolectricTestRunner.class)
@RunWith(JUnit4.class)
public class ParserTest extends TreeSitterTest {

private static String readResource(String... names) {
return readString(Paths.get("./src/test/resources", names));
private MockedStatic<TextUtils> mockedTextUtils;

@Before
public void setupMocks() {
mockedTextUtils = Mockito.mockStatic(TextUtils.class);
mockedTextUtils.when(() -> TextUtils.getTrimmedLength(ArgumentMatchers.anyString()))
.thenAnswer(invocation -> {
final var arguments = invocation.getArguments();
if (arguments == null || arguments.length != 1 || !(arguments[0] instanceof CharSequence)) {
throw new IllegalArgumentException();
}
return getTrimmedLength(((CharSequence) arguments[0]));
});
}

@After
public void releaseMocks() {
if (mockedTextUtils != null) {
mockedTextUtils.close();
}
}

@Test
Expand Down Expand Up @@ -219,9 +243,9 @@ public void testAIDLGrammar_interfaceDecl() {
assertThat(rootNode.hasErrors()).isFalse();

//noinspection DataFlowIssue
final var packages = substrings(execQueryGroupByCaptures(
"(package_declaration name: (_) @package)",
TSLanguageAidl.getInstance(), rootNode).get("package"), source);
final var packages = substrings(
execQueryGroupByCaptures("(package_declaration name: (_) @package)",
TSLanguageAidl.getInstance(), rootNode).get("package"), source);
assertThat(packages).containsExactly("com.itsaky.androidide.treesitter.test");

//noinspection DataFlowIssue
Expand Down Expand Up @@ -271,7 +295,8 @@ public void testAIDLGrammar_parcelableDecl() {
final var interfaces = substrings(
execQueryGroupByCaptures("(parcelable_declaration name: (_) @parcelable)",
TSLanguageAidl.getInstance(), rootNode).get("parcelable"), source);
assertThat(interfaces).containsExactly("SomethingDefinedSomewhere", "CanWeDefineAsManyAsWeWant", "SomethingParcelable");
assertThat(interfaces).containsExactly("SomethingDefinedSomewhere",
"CanWeDefineAsManyAsWeWant", "SomethingParcelable");

//noinspection DataFlowIssue
final var variables = substrings(
Expand Down Expand Up @@ -306,4 +331,24 @@ private static List<String> substrings(List<TSNode> nodes, String source) {
.map(node -> source.substring(node.getStartByte() / 2, node.getEndByte() / 2))
.collect(Collectors.toList());
}

private static int getTrimmedLength(CharSequence s) {
int len = s.length();

int start = 0;
while (start < len && s.charAt(start) <= ' ') {
start++;
}

int end = len;
while (end > start && s.charAt(end - 1) <= ' ') {
end--;
}

return end - start;
}

private static String readResource(String... names) {
return readString(Paths.get("./src/test/resources", names));
}
}
1 change: 1 addition & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ tests-google-truth = { module = "com.google.truth:truth", version = "1.1.5" }
tests-androidx-ext-junit = { module = "androidx.test.ext:junit", version = "1.1.5" }
tests-androidx-espresso-core = { module = "androidx.test.espresso:espresso-core", version = "3.5.1" }
tests-robolectric = { module = "org.robolectric:robolectric", version = "4.10.3" }
tests-mockito = { module = "org.mockito:mockito-core", version = "5.4.0" }

gradle-android = { module = "com.android.tools.build:gradle", version.ref = "agp" }

Expand Down

0 comments on commit 7421e2c

Please sign in to comment.