Skip to content

Commit a506151

Browse files
author
Jules Kerssemakers
committed
Test NoteMapper helper with JUnit 5/Jupiter.
Needs some additional plugins, because Google isn't moving on JUnit 5 support see - Google tracking issue: android/android-test#224 - Android <-> JUnit 5 plugin https://github.com/mannodermaus/android-junit5 - StackOverflow answer about the plugin https://stackoverflow.com/questions/46161113/junit-5-for-android-testing
1 parent c9ec021 commit a506151

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed

app/build.gradle

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
apply plugin: 'com.android.application'
2+
apply plugin: 'de.mannodermaus.android-junit5'
23

34
android {
45
compileSdkVersion 33
@@ -51,6 +52,8 @@ dependencies {
5152
implementation 'com.google.android.material:material:1.8.0'
5253
implementation 'androidx.preference:preference:1.2.0'
5354
implementation "androidx.annotation:annotation:1.6.0"
55+
56+
testImplementation "org.junit.jupiter:junit-jupiter:5.9.2"
5457
}
5558

5659
// Not sure why this started happening all of a sudden, but the buidl was failing
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.nicobrailo.pianoli.melodies;
2+
3+
import org.junit.jupiter.api.Test;
4+
import org.junit.jupiter.params.ParameterizedTest;
5+
import org.junit.jupiter.params.provider.ValueSource;
6+
7+
import static com.nicobrailo.pianoli.melodies.NoteMapper.get_key_idx_from_note;
8+
import static org.junit.jupiter.api.Assertions.assertEquals;
9+
import static org.junit.jupiter.api.Assertions.fail;
10+
11+
class NoteMapperTest {
12+
13+
@Test
14+
public void noteRangeLimits() {
15+
assertEquals(0, get_key_idx_from_note("C1"),
16+
"lowest parsable note should be parsed");
17+
assertEquals(26, get_key_idx_from_note("B2"),
18+
"lowest parsable note should be parsed");
19+
20+
}
21+
22+
@ParameterizedTest
23+
@ValueSource(strings = {"C#1", "Db1", "D♭1"})
24+
public void fancySynonyms(String note) {
25+
assertEquals(1, get_key_idx_from_note(note),
26+
"all synonyms for the same note should work, including fancy symbols");
27+
}
28+
29+
@ParameterizedTest
30+
@ValueSource(strings = {"", " ", "fooooooo"})
31+
public void noNoteFallback(String notANote) {
32+
assertEquals(5, get_key_idx_from_note(notANote),
33+
"non-existing notes should fall back to the not-a-note special value");
34+
}
35+
36+
@Test
37+
public void nullSafe() {
38+
assertEquals(5, get_key_idx_from_note(null),
39+
"Notemapper should gracefully handle null");
40+
}
41+
}

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ buildscript {
88

99
dependencies {
1010
classpath 'com.android.tools.build:gradle:7.4.2'
11+
classpath "de.mannodermaus.gradle.plugins:android-junit5:1.9.3.0"
1112

1213
// NOTE: Do not place your application dependencies here; they belong
1314
// in the individual module build.gradle files

0 commit comments

Comments
 (0)