|
20 | 20 | import static com.google.common.truth.Truth.assertThat; |
21 | 21 | import static com.itsaky.androidide.treesitter.TestUtils.readString; |
22 | 22 |
|
| 23 | +import android.text.TextUtils; |
23 | 24 | import com.itsaky.androidide.treesitter.aidl.TSLanguageAidl; |
24 | 25 | import com.itsaky.androidide.treesitter.java.TSLanguageJava; |
25 | 26 | import com.itsaky.androidide.treesitter.json.TSLanguageJson; |
|
33 | 34 | import java.util.List; |
34 | 35 | import java.util.Map; |
35 | 36 | import java.util.stream.Collectors; |
| 37 | +import org.junit.After; |
| 38 | +import org.junit.Before; |
36 | 39 | import org.junit.Test; |
37 | 40 | import org.junit.runner.RunWith; |
38 | | -import org.robolectric.RobolectricTestRunner; |
| 41 | +import org.junit.runners.JUnit4; |
| 42 | +import org.mockito.ArgumentMatchers; |
| 43 | +import org.mockito.MockedStatic; |
| 44 | +import org.mockito.Mockito; |
39 | 45 |
|
40 | | -@RunWith(RobolectricTestRunner.class) |
| 46 | +@RunWith(JUnit4.class) |
41 | 47 | public class ParserTest extends TreeSitterTest { |
42 | 48 |
|
43 | | - private static String readResource(String... names) { |
44 | | - return readString(Paths.get("./src/test/resources", names)); |
| 49 | + private MockedStatic<TextUtils> mockedTextUtils; |
| 50 | + |
| 51 | + @Before |
| 52 | + public void setupMocks() { |
| 53 | + mockedTextUtils = Mockito.mockStatic(TextUtils.class); |
| 54 | + mockedTextUtils.when(() -> TextUtils.getTrimmedLength(ArgumentMatchers.anyString())) |
| 55 | + .thenAnswer(invocation -> { |
| 56 | + final var arguments = invocation.getArguments(); |
| 57 | + if (arguments == null || arguments.length != 1 || !(arguments[0] instanceof CharSequence)) { |
| 58 | + throw new IllegalArgumentException(); |
| 59 | + } |
| 60 | + return getTrimmedLength(((CharSequence) arguments[0])); |
| 61 | + }); |
| 62 | + } |
| 63 | + |
| 64 | + @After |
| 65 | + public void releaseMocks() { |
| 66 | + if (mockedTextUtils != null) { |
| 67 | + mockedTextUtils.close(); |
| 68 | + } |
45 | 69 | } |
46 | 70 |
|
47 | 71 | @Test |
@@ -219,9 +243,9 @@ public void testAIDLGrammar_interfaceDecl() { |
219 | 243 | assertThat(rootNode.hasErrors()).isFalse(); |
220 | 244 |
|
221 | 245 | //noinspection DataFlowIssue |
222 | | - final var packages = substrings(execQueryGroupByCaptures( |
223 | | - "(package_declaration name: (_) @package)", |
224 | | - TSLanguageAidl.getInstance(), rootNode).get("package"), source); |
| 246 | + final var packages = substrings( |
| 247 | + execQueryGroupByCaptures("(package_declaration name: (_) @package)", |
| 248 | + TSLanguageAidl.getInstance(), rootNode).get("package"), source); |
225 | 249 | assertThat(packages).containsExactly("com.itsaky.androidide.treesitter.test"); |
226 | 250 |
|
227 | 251 | //noinspection DataFlowIssue |
@@ -271,7 +295,8 @@ public void testAIDLGrammar_parcelableDecl() { |
271 | 295 | final var interfaces = substrings( |
272 | 296 | execQueryGroupByCaptures("(parcelable_declaration name: (_) @parcelable)", |
273 | 297 | TSLanguageAidl.getInstance(), rootNode).get("parcelable"), source); |
274 | | - assertThat(interfaces).containsExactly("SomethingDefinedSomewhere", "CanWeDefineAsManyAsWeWant", "SomethingParcelable"); |
| 298 | + assertThat(interfaces).containsExactly("SomethingDefinedSomewhere", |
| 299 | + "CanWeDefineAsManyAsWeWant", "SomethingParcelable"); |
275 | 300 |
|
276 | 301 | //noinspection DataFlowIssue |
277 | 302 | final var variables = substrings( |
@@ -306,4 +331,24 @@ private static List<String> substrings(List<TSNode> nodes, String source) { |
306 | 331 | .map(node -> source.substring(node.getStartByte() / 2, node.getEndByte() / 2)) |
307 | 332 | .collect(Collectors.toList()); |
308 | 333 | } |
| 334 | + |
| 335 | + private static int getTrimmedLength(CharSequence s) { |
| 336 | + int len = s.length(); |
| 337 | + |
| 338 | + int start = 0; |
| 339 | + while (start < len && s.charAt(start) <= ' ') { |
| 340 | + start++; |
| 341 | + } |
| 342 | + |
| 343 | + int end = len; |
| 344 | + while (end > start && s.charAt(end - 1) <= ' ') { |
| 345 | + end--; |
| 346 | + } |
| 347 | + |
| 348 | + return end - start; |
| 349 | + } |
| 350 | + |
| 351 | + private static String readResource(String... names) { |
| 352 | + return readString(Paths.get("./src/test/resources", names)); |
| 353 | + } |
309 | 354 | } |
0 commit comments