In-line WebvttSubtitleTest helper methods

testSubtitleEventTimes/IndicesHelper make assertions that only happen
to be the same because values in two different constants match up. It
seems much better to explicitly put the assertions in each test.

The other assert methods are just obscuring the underlying call to
Truth.

PiperOrigin-RevId: 309022044
This commit is contained in:
ibaker 2020-04-29 15:57:48 +01:00 committed by Oliver Woodman
parent 767b29f159
commit b74b4f3c35

View File

@ -83,63 +83,124 @@ public class WebvttSubtitleTest {
@Test @Test
public void simpleSubtitleEventTimes() { public void simpleSubtitleEventTimes() {
testSubtitleEventTimesHelper(simpleSubtitle); assertThat(simpleSubtitle.getEventTime(0)).isEqualTo(1_000_000);
assertThat(simpleSubtitle.getEventTime(1)).isEqualTo(2_000_000);
assertThat(simpleSubtitle.getEventTime(2)).isEqualTo(3_000_000);
assertThat(simpleSubtitle.getEventTime(3)).isEqualTo(4_000_000);
} }
@Test @Test
public void simpleSubtitleEventIndices() { public void simpleSubtitleEventIndices() {
testSubtitleEventIndicesHelper(simpleSubtitle); // Test first event
assertThat(simpleSubtitle.getNextEventTimeIndex(0)).isEqualTo(0);
assertThat(simpleSubtitle.getNextEventTimeIndex(500_000)).isEqualTo(0);
assertThat(simpleSubtitle.getNextEventTimeIndex(999_999)).isEqualTo(0);
// Test second event
assertThat(simpleSubtitle.getNextEventTimeIndex(1_000_000)).isEqualTo(1);
assertThat(simpleSubtitle.getNextEventTimeIndex(1_500_000)).isEqualTo(1);
assertThat(simpleSubtitle.getNextEventTimeIndex(1_999_999)).isEqualTo(1);
// Test third event
assertThat(simpleSubtitle.getNextEventTimeIndex(2_000_000)).isEqualTo(2);
assertThat(simpleSubtitle.getNextEventTimeIndex(2_500_000)).isEqualTo(2);
assertThat(simpleSubtitle.getNextEventTimeIndex(2_999_999)).isEqualTo(2);
// Test fourth event
assertThat(simpleSubtitle.getNextEventTimeIndex(3_000_000)).isEqualTo(3);
assertThat(simpleSubtitle.getNextEventTimeIndex(3_500_000)).isEqualTo(3);
assertThat(simpleSubtitle.getNextEventTimeIndex(3_999_999)).isEqualTo(3);
// Test null event (i.e. look for events after the last event)
assertThat(simpleSubtitle.getNextEventTimeIndex(4_000_000)).isEqualTo(INDEX_UNSET);
assertThat(simpleSubtitle.getNextEventTimeIndex(4_500_000)).isEqualTo(INDEX_UNSET);
assertThat(simpleSubtitle.getNextEventTimeIndex(MAX_VALUE)).isEqualTo(INDEX_UNSET);
} }
@Test @Test
public void simpleSubtitleText() { public void simpleSubtitleText() {
// Test before first subtitle // Test before first subtitle
assertSingleCueEmpty(simpleSubtitle.getCues(0)); assertThat(simpleSubtitle.getCues(0)).isEmpty();
assertSingleCueEmpty(simpleSubtitle.getCues(500_000)); assertThat(simpleSubtitle.getCues(500_000)).isEmpty();
assertSingleCueEmpty(simpleSubtitle.getCues(999_999)); assertThat(simpleSubtitle.getCues(999_999)).isEmpty();
// Test first subtitle // Test first subtitle
assertSingleCueTextEquals(FIRST_SUBTITLE_STRING, simpleSubtitle.getCues(1_000_000)); assertThat(getCueTexts(simpleSubtitle.getCues(1_000_000)))
assertSingleCueTextEquals(FIRST_SUBTITLE_STRING, simpleSubtitle.getCues(1_500_000)); .containsExactly(FIRST_SUBTITLE_STRING);
assertSingleCueTextEquals(FIRST_SUBTITLE_STRING, simpleSubtitle.getCues(1_999_999)); assertThat(getCueTexts(simpleSubtitle.getCues(1_500_000)))
.containsExactly(FIRST_SUBTITLE_STRING);
assertThat(getCueTexts(simpleSubtitle.getCues(1_999_999)))
.containsExactly(FIRST_SUBTITLE_STRING);
// Test after first subtitle, before second subtitle // Test after first subtitle, before second subtitle
assertSingleCueEmpty(simpleSubtitle.getCues(2_000_000)); assertThat(simpleSubtitle.getCues(2_000_000)).isEmpty();
assertSingleCueEmpty(simpleSubtitle.getCues(2_500_000)); assertThat(simpleSubtitle.getCues(2_500_000)).isEmpty();
assertSingleCueEmpty(simpleSubtitle.getCues(2_999_999)); assertThat(simpleSubtitle.getCues(2_999_999)).isEmpty();
// Test second subtitle // Test second subtitle
assertSingleCueTextEquals(SECOND_SUBTITLE_STRING, simpleSubtitle.getCues(3_000_000)); assertThat(getCueTexts(simpleSubtitle.getCues(3_000_000)))
assertSingleCueTextEquals(SECOND_SUBTITLE_STRING, simpleSubtitle.getCues(3_500_000)); .containsExactly(SECOND_SUBTITLE_STRING);
assertSingleCueTextEquals(SECOND_SUBTITLE_STRING, simpleSubtitle.getCues(3_999_999)); assertThat(getCueTexts(simpleSubtitle.getCues(3_500_000)))
.containsExactly(SECOND_SUBTITLE_STRING);
assertThat(getCueTexts(simpleSubtitle.getCues(3_999_999)))
.containsExactly(SECOND_SUBTITLE_STRING);
// Test after second subtitle // Test after second subtitle
assertSingleCueEmpty(simpleSubtitle.getCues(4_000_000)); assertThat(simpleSubtitle.getCues(4_000_000)).isEmpty();
assertSingleCueEmpty(simpleSubtitle.getCues(4_500_000)); assertThat(simpleSubtitle.getCues(4_500_000)).isEmpty();
assertSingleCueEmpty(simpleSubtitle.getCues(Long.MAX_VALUE)); assertThat(simpleSubtitle.getCues(Long.MAX_VALUE)).isEmpty();
} }
@Test @Test
public void overlappingSubtitleEventTimes() { public void overlappingSubtitleEventTimes() {
testSubtitleEventTimesHelper(overlappingSubtitle); assertThat(overlappingSubtitle.getEventTime(0)).isEqualTo(1_000_000);
assertThat(overlappingSubtitle.getEventTime(1)).isEqualTo(2_000_000);
assertThat(overlappingSubtitle.getEventTime(2)).isEqualTo(3_000_000);
assertThat(overlappingSubtitle.getEventTime(3)).isEqualTo(4_000_000);
} }
@Test @Test
public void overlappingSubtitleEventIndices() { public void overlappingSubtitleEventIndices() {
testSubtitleEventIndicesHelper(overlappingSubtitle); // Test first event
assertThat(overlappingSubtitle.getNextEventTimeIndex(0)).isEqualTo(0);
assertThat(overlappingSubtitle.getNextEventTimeIndex(500_000)).isEqualTo(0);
assertThat(overlappingSubtitle.getNextEventTimeIndex(999_999)).isEqualTo(0);
// Test second event
assertThat(overlappingSubtitle.getNextEventTimeIndex(1_000_000)).isEqualTo(1);
assertThat(overlappingSubtitle.getNextEventTimeIndex(1_500_000)).isEqualTo(1);
assertThat(overlappingSubtitle.getNextEventTimeIndex(1_999_999)).isEqualTo(1);
// Test third event
assertThat(overlappingSubtitle.getNextEventTimeIndex(2_000_000)).isEqualTo(2);
assertThat(overlappingSubtitle.getNextEventTimeIndex(2_500_000)).isEqualTo(2);
assertThat(overlappingSubtitle.getNextEventTimeIndex(2_999_999)).isEqualTo(2);
// Test fourth event
assertThat(overlappingSubtitle.getNextEventTimeIndex(3_000_000)).isEqualTo(3);
assertThat(overlappingSubtitle.getNextEventTimeIndex(3_500_000)).isEqualTo(3);
assertThat(overlappingSubtitle.getNextEventTimeIndex(3_999_999)).isEqualTo(3);
// Test null event (i.e. look for events after the last event)
assertThat(overlappingSubtitle.getNextEventTimeIndex(4_000_000)).isEqualTo(INDEX_UNSET);
assertThat(overlappingSubtitle.getNextEventTimeIndex(4_500_000)).isEqualTo(INDEX_UNSET);
assertThat(overlappingSubtitle.getNextEventTimeIndex(MAX_VALUE)).isEqualTo(INDEX_UNSET);
} }
@Test @Test
public void overlappingSubtitleText() { public void overlappingSubtitleText() {
// Test before first subtitle // Test before first subtitle
assertSingleCueEmpty(overlappingSubtitle.getCues(0)); assertThat(overlappingSubtitle.getCues(0)).isEmpty();
assertSingleCueEmpty(overlappingSubtitle.getCues(500_000)); assertThat(overlappingSubtitle.getCues(500_000)).isEmpty();
assertSingleCueEmpty(overlappingSubtitle.getCues(999_999)); assertThat(overlappingSubtitle.getCues(999_999)).isEmpty();
// Test first subtitle // Test first subtitle
assertSingleCueTextEquals(FIRST_SUBTITLE_STRING, overlappingSubtitle.getCues(1_000_000)); assertThat(getCueTexts(overlappingSubtitle.getCues(1_000_000)))
assertSingleCueTextEquals(FIRST_SUBTITLE_STRING, overlappingSubtitle.getCues(1_500_000)); .containsExactly(FIRST_SUBTITLE_STRING);
assertSingleCueTextEquals(FIRST_SUBTITLE_STRING, overlappingSubtitle.getCues(1_999_999)); assertThat(getCueTexts(overlappingSubtitle.getCues(1_500_000)))
.containsExactly(FIRST_SUBTITLE_STRING);
assertThat(getCueTexts(overlappingSubtitle.getCues(1_999_999)))
.containsExactly(FIRST_SUBTITLE_STRING);
// Test after first and second subtitle // Test after first and second subtitle
assertThat(getCueTexts(overlappingSubtitle.getCues(2_000_000))) assertThat(getCueTexts(overlappingSubtitle.getCues(2_000_000)))
@ -150,37 +211,69 @@ public class WebvttSubtitleTest {
.containsExactly(FIRST_SUBTITLE_STRING, SECOND_SUBTITLE_STRING); .containsExactly(FIRST_SUBTITLE_STRING, SECOND_SUBTITLE_STRING);
// Test second subtitle // Test second subtitle
assertSingleCueTextEquals(SECOND_SUBTITLE_STRING, overlappingSubtitle.getCues(3_000_000)); assertThat(getCueTexts(overlappingSubtitle.getCues(3_000_000)))
assertSingleCueTextEquals(SECOND_SUBTITLE_STRING, overlappingSubtitle.getCues(3_500_000)); .containsExactly(SECOND_SUBTITLE_STRING);
assertSingleCueTextEquals(SECOND_SUBTITLE_STRING, overlappingSubtitle.getCues(3_999_999)); assertThat(getCueTexts(overlappingSubtitle.getCues(3_500_000)))
.containsExactly(SECOND_SUBTITLE_STRING);
assertThat(getCueTexts(overlappingSubtitle.getCues(3_999_999)))
.containsExactly(SECOND_SUBTITLE_STRING);
// Test after second subtitle // Test after second subtitle
assertSingleCueEmpty(overlappingSubtitle.getCues(4_000_000)); assertThat(overlappingSubtitle.getCues(4_000_000)).isEmpty();
assertSingleCueEmpty(overlappingSubtitle.getCues(4_500_000)); assertThat(overlappingSubtitle.getCues(4_500_000)).isEmpty();
assertSingleCueEmpty(overlappingSubtitle.getCues(Long.MAX_VALUE)); assertThat(overlappingSubtitle.getCues(Long.MAX_VALUE)).isEmpty();
} }
@Test @Test
public void nestedSubtitleEventTimes() { public void nestedSubtitleEventTimes() {
testSubtitleEventTimesHelper(nestedSubtitle); assertThat(nestedSubtitle.getEventTime(0)).isEqualTo(1_000_000);
assertThat(nestedSubtitle.getEventTime(1)).isEqualTo(2_000_000);
assertThat(nestedSubtitle.getEventTime(2)).isEqualTo(3_000_000);
assertThat(nestedSubtitle.getEventTime(3)).isEqualTo(4_000_000);
} }
@Test @Test
public void nestedSubtitleEventIndices() { public void nestedSubtitleEventIndices() {
testSubtitleEventIndicesHelper(nestedSubtitle); // Test first event
assertThat(nestedSubtitle.getNextEventTimeIndex(0)).isEqualTo(0);
assertThat(nestedSubtitle.getNextEventTimeIndex(500_000)).isEqualTo(0);
assertThat(nestedSubtitle.getNextEventTimeIndex(999_999)).isEqualTo(0);
// Test second event
assertThat(nestedSubtitle.getNextEventTimeIndex(1_000_000)).isEqualTo(1);
assertThat(nestedSubtitle.getNextEventTimeIndex(1_500_000)).isEqualTo(1);
assertThat(nestedSubtitle.getNextEventTimeIndex(1_999_999)).isEqualTo(1);
// Test third event
assertThat(nestedSubtitle.getNextEventTimeIndex(2_000_000)).isEqualTo(2);
assertThat(nestedSubtitle.getNextEventTimeIndex(2_500_000)).isEqualTo(2);
assertThat(nestedSubtitle.getNextEventTimeIndex(2_999_999)).isEqualTo(2);
// Test fourth event
assertThat(nestedSubtitle.getNextEventTimeIndex(3_000_000)).isEqualTo(3);
assertThat(nestedSubtitle.getNextEventTimeIndex(3_500_000)).isEqualTo(3);
assertThat(nestedSubtitle.getNextEventTimeIndex(3_999_999)).isEqualTo(3);
// Test null event (i.e. look for events after the last event)
assertThat(nestedSubtitle.getNextEventTimeIndex(4_000_000)).isEqualTo(INDEX_UNSET);
assertThat(nestedSubtitle.getNextEventTimeIndex(4_500_000)).isEqualTo(INDEX_UNSET);
assertThat(nestedSubtitle.getNextEventTimeIndex(MAX_VALUE)).isEqualTo(INDEX_UNSET);
} }
@Test @Test
public void nestedSubtitleText() { public void nestedSubtitleText() {
// Test before first subtitle // Test before first subtitle
assertSingleCueEmpty(nestedSubtitle.getCues(0)); assertThat(nestedSubtitle.getCues(0)).isEmpty();
assertSingleCueEmpty(nestedSubtitle.getCues(500_000)); assertThat(nestedSubtitle.getCues(500_000)).isEmpty();
assertSingleCueEmpty(nestedSubtitle.getCues(999_999)); assertThat(nestedSubtitle.getCues(999_999)).isEmpty();
// Test first subtitle // Test first subtitle
assertSingleCueTextEquals(FIRST_SUBTITLE_STRING, nestedSubtitle.getCues(1_000_000)); assertThat(getCueTexts(nestedSubtitle.getCues(1_000_000)))
assertSingleCueTextEquals(FIRST_SUBTITLE_STRING, nestedSubtitle.getCues(1_500_000)); .containsExactly(FIRST_SUBTITLE_STRING);
assertSingleCueTextEquals(FIRST_SUBTITLE_STRING, nestedSubtitle.getCues(1_999_999)); assertThat(getCueTexts(nestedSubtitle.getCues(1_500_000)))
.containsExactly(FIRST_SUBTITLE_STRING);
assertThat(getCueTexts(nestedSubtitle.getCues(1_999_999)))
.containsExactly(FIRST_SUBTITLE_STRING);
// Test after first and second subtitle // Test after first and second subtitle
assertThat(getCueTexts(nestedSubtitle.getCues(2_000_000))) assertThat(getCueTexts(nestedSubtitle.getCues(2_000_000)))
@ -191,57 +284,17 @@ public class WebvttSubtitleTest {
.containsExactly(FIRST_SUBTITLE_STRING, SECOND_SUBTITLE_STRING); .containsExactly(FIRST_SUBTITLE_STRING, SECOND_SUBTITLE_STRING);
// Test first subtitle // Test first subtitle
assertSingleCueTextEquals(FIRST_SUBTITLE_STRING, nestedSubtitle.getCues(3_000_000)); assertThat(getCueTexts(nestedSubtitle.getCues(3_000_000)))
assertSingleCueTextEquals(FIRST_SUBTITLE_STRING, nestedSubtitle.getCues(3_500_000)); .containsExactly(FIRST_SUBTITLE_STRING);
assertSingleCueTextEquals(FIRST_SUBTITLE_STRING, nestedSubtitle.getCues(3_999_999)); assertThat(getCueTexts(nestedSubtitle.getCues(3_500_000)))
.containsExactly(FIRST_SUBTITLE_STRING);
assertThat(getCueTexts(nestedSubtitle.getCues(3_999_999)))
.containsExactly(FIRST_SUBTITLE_STRING);
// Test after second subtitle // Test after second subtitle
assertSingleCueEmpty(nestedSubtitle.getCues(4_000_000)); assertThat(nestedSubtitle.getCues(4_000_000)).isEmpty();
assertSingleCueEmpty(nestedSubtitle.getCues(4_500_000)); assertThat(nestedSubtitle.getCues(4_500_000)).isEmpty();
assertSingleCueEmpty(nestedSubtitle.getCues(Long.MAX_VALUE)); assertThat(nestedSubtitle.getCues(Long.MAX_VALUE)).isEmpty();
}
private void testSubtitleEventTimesHelper(WebvttSubtitle subtitle) {
assertThat(subtitle.getEventTime(0)).isEqualTo(1_000_000);
assertThat(subtitle.getEventTime(1)).isEqualTo(2_000_000);
assertThat(subtitle.getEventTime(2)).isEqualTo(3_000_000);
assertThat(subtitle.getEventTime(3)).isEqualTo(4_000_000);
}
private void testSubtitleEventIndicesHelper(WebvttSubtitle subtitle) {
// Test first event
assertThat(subtitle.getNextEventTimeIndex(0)).isEqualTo(0);
assertThat(subtitle.getNextEventTimeIndex(500_000)).isEqualTo(0);
assertThat(subtitle.getNextEventTimeIndex(999_999)).isEqualTo(0);
// Test second event
assertThat(subtitle.getNextEventTimeIndex(1_000_000)).isEqualTo(1);
assertThat(subtitle.getNextEventTimeIndex(1_500_000)).isEqualTo(1);
assertThat(subtitle.getNextEventTimeIndex(1_999_999)).isEqualTo(1);
// Test third event
assertThat(subtitle.getNextEventTimeIndex(2_000_000)).isEqualTo(2);
assertThat(subtitle.getNextEventTimeIndex(2_500_000)).isEqualTo(2);
assertThat(subtitle.getNextEventTimeIndex(2_999_999)).isEqualTo(2);
// Test fourth event
assertThat(subtitle.getNextEventTimeIndex(3_000_000)).isEqualTo(3);
assertThat(subtitle.getNextEventTimeIndex(3_500_000)).isEqualTo(3);
assertThat(subtitle.getNextEventTimeIndex(3_999_999)).isEqualTo(3);
// Test null event (i.e. look for events after the last event)
assertThat(subtitle.getNextEventTimeIndex(4_000_000)).isEqualTo(INDEX_UNSET);
assertThat(subtitle.getNextEventTimeIndex(4_500_000)).isEqualTo(INDEX_UNSET);
assertThat(subtitle.getNextEventTimeIndex(MAX_VALUE)).isEqualTo(INDEX_UNSET);
}
private void assertSingleCueEmpty(List<Cue> cues) {
assertThat(cues).isEmpty();
}
private void assertSingleCueTextEquals(String expected, List<Cue> cues) {
assertThat(cues).hasSize(1);
assertThat(cues.get(0).text.toString()).isEqualTo(expected);
} }
private static List<String> getCueTexts(List<Cue> cues) { private static List<String> getCueTexts(List<Cue> cues) {