-
-
Notifications
You must be signed in to change notification settings - Fork 2
test: add CodeHighlighter unit tests #178
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
Closed
MihaiCristianCondrea
wants to merge
1
commit into
main
from
codex/add-unit-tests-for-codehighlighter
Closed
Changes from all commits
Commits
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
60 changes: 60 additions & 0 deletions
60
app/src/test/java/com/d4rk/androidtutorials/java/utils/CodeHighlighterTest.java
This file contains hidden or 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,60 @@ | ||
| package com.d4rk.androidtutorials.java.utils; | ||
|
|
||
| import static org.junit.Assert.assertEquals; | ||
| import static org.mockito.ArgumentMatchers.any; | ||
| import static org.mockito.ArgumentMatchers.anyInt; | ||
| import static org.mockito.Mockito.doAnswer; | ||
| import static org.mockito.Mockito.mock; | ||
| import static org.mockito.Mockito.verify; | ||
|
|
||
| import com.amrdeveloper.codeview.CodeView; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.regex.Pattern; | ||
|
|
||
| import org.junit.Test; | ||
|
|
||
| /** | ||
| * Tests for {@link CodeHighlighter} ensuring themes register syntax patterns and refresh highlighting. | ||
| */ | ||
| public class CodeHighlighterTest { | ||
|
|
||
| @Test | ||
| public void applyJavaTheme_addsPatternsAndReHighlights() { | ||
| CodeView codeView = mock(CodeView.class); | ||
| List<Pattern> patterns = new ArrayList<>(); | ||
| List<Integer> colors = new ArrayList<>(); | ||
|
|
||
| doAnswer(invocation -> { | ||
| patterns.add(invocation.getArgument(0)); | ||
| colors.add(invocation.getArgument(1)); | ||
| return null; | ||
| }).when(codeView).addSyntaxPattern(any(Pattern.class), anyInt()); | ||
|
|
||
| CodeHighlighter.applyJavaTheme(codeView); | ||
|
|
||
| assertEquals(5, patterns.size()); | ||
| assertEquals(5, colors.size()); | ||
| verify(codeView).reHighlightSyntax(); | ||
| } | ||
|
|
||
| @Test | ||
| public void applyXmlTheme_addsPatternsAndReHighlights() { | ||
| CodeView codeView = mock(CodeView.class); | ||
| List<Pattern> patterns = new ArrayList<>(); | ||
| List<Integer> colors = new ArrayList<>(); | ||
|
|
||
| doAnswer(invocation -> { | ||
| patterns.add(invocation.getArgument(0)); | ||
| colors.add(invocation.getArgument(1)); | ||
| return null; | ||
| }).when(codeView).addSyntaxPattern(any(Pattern.class), anyInt()); | ||
|
|
||
| CodeHighlighter.applyXmlTheme(codeView); | ||
|
|
||
| assertEquals(4, patterns.size()); | ||
| assertEquals(4, colors.size()); | ||
| verify(codeView).reHighlightSyntax(); | ||
| } | ||
| } | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[P1] Guard unit tests against Android Color dependency
Running
CodeHighlighter.applyJavaTheme(codeView)in a local unit test invokesandroid.graphics.Color.parseColor, but the project’s test configuration only includes plain JUnit/Mockito and no Robolectric or Android runtime stubs. When./gradlew testexecutes, this call will throwRuntimeException: Method parseColor in android.graphics.Color not mockedbefore any assertions run (same forapplyXmlThemelater in the file), causing the entire test task to fail. Consider moving these tests underandroidTest, adding Robolectric, or mocking the color parsing so the tests can execute in a JVM environment.Useful? React with 👍 / 👎.