Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
test: add CodeHighlighter unit tests
  • Loading branch information
commit 9fae7709b4a5d8791fc71f8bafdf58badf401f74
View file Open in desktop
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);
Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot Sep 14, 2025

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 invokes android.graphics.Color.parseColor, but the project’s test configuration only includes plain JUnit/Mockito and no Robolectric or Android runtime stubs. When ./gradlew test executes, this call will throw RuntimeException: Method parseColor in android.graphics.Color not mocked before any assertions run (same for applyXmlTheme later in the file), causing the entire test task to fail. Consider moving these tests under androidTest, adding Robolectric, or mocking the color parsing so the tests can execute in a JVM environment.

Useful? React with 👍 / 👎.


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();
}
}
Loading

AltStyle によって変換されたページ (->オリジナル) /