|
| 1 | +package org.schabi.newpipe.extractor.services.youtube; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +import static org.schabi.newpipe.extractor.services.youtube.YoutubeDescriptionHelper.runsToHtml; |
| 5 | + |
| 6 | +import org.junit.jupiter.api.Test; |
| 7 | +import org.schabi.newpipe.extractor.services.youtube.YoutubeDescriptionHelper.Run; |
| 8 | + |
| 9 | +import java.util.Comparator; |
| 10 | +import java.util.List; |
| 11 | +import java.util.function.Function; |
| 12 | +import java.util.stream.Collectors; |
| 13 | + |
| 14 | +public class YoutubeDescriptionHelperTest { |
| 15 | + |
| 16 | + private static void assertRunsToHtml(final String expectedHtml, |
| 17 | + final List<Run> openers, |
| 18 | + final List<Run> closers, |
| 19 | + final String content) { |
| 20 | + assertEquals( |
| 21 | + expectedHtml, |
| 22 | + runsToHtml( |
| 23 | + openers.stream() |
| 24 | + .sorted(Comparator.comparingInt(run -> run.pos)) |
| 25 | + .collect(Collectors.toList()), |
| 26 | + closers.stream() |
| 27 | + .sorted(Comparator.comparingInt(run -> run.pos)) |
| 28 | + .collect(Collectors.toList()), |
| 29 | + content |
| 30 | + ) |
| 31 | + ); |
| 32 | + } |
| 33 | + |
| 34 | + @Test |
| 35 | + public void testNoRuns() { |
| 36 | + assertRunsToHtml( |
| 37 | + "abc *a* _c_ <br> <br> <a href=\"#\">test</a> &", |
| 38 | + List.of(), |
| 39 | + List.of(), |
| 40 | + "abc *a* _c_ <br>\u00a0\n\u00a0<a href=\"#\">test</a> &" |
| 41 | + ); |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + public void testNormalRuns() { |
| 46 | + assertRunsToHtml( |
| 47 | + "<A>hel<B>lo </B>nic</A>e <C>test</C>", |
| 48 | + List.of(new Run("<A>", "</A>", 0), new Run("<B>", "</B>", 3), |
| 49 | + new Run("<C>", "</C>", 11)), |
| 50 | + List.of(new Run("<A>", "</A>", 9), new Run("<B>", "</B>", 6), |
| 51 | + new Run("<C>", "</C>", 15)), |
| 52 | + "hello nice test" |
| 53 | + ); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + public void testOverlappingRuns() { |
| 58 | + assertRunsToHtml( |
| 59 | + "01<A>23<B>45</B></A><B>67</B>89", |
| 60 | + List.of(new Run("<A>", "</A>", 2), new Run("<B>", "</B>", 4)), |
| 61 | + List.of(new Run("<A>", "</A>", 6), new Run("<B>", "</B>", 8)), |
| 62 | + "0123456789" |
| 63 | + ); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + public void testTransformingRuns() { |
| 68 | + final Function<String, String> tA = content -> "whatever"; |
| 69 | + final Function<String, String> tD |
| 70 | + = content -> Integer.parseInt(content) % 2 == 0 ? "even" : "odd"; |
| 71 | + |
| 72 | + assertRunsToHtml( |
| 73 | + "0<A>whatever</A><C>4</C>5<D>odd</D>89", |
| 74 | + List.of(new Run("<A>", "</A>", 1, tA), new Run("<B>", "</B>", 2), |
| 75 | + new Run("<C>", "</C>", 3), new Run("<D>", "</D>", 6, tD)), |
| 76 | + List.of(new Run("<A>", "</A>", 4, tA), new Run("<B>", "</B>", 3), |
| 77 | + new Run("<C>", "</C>", 5), new Run("<D>", "</D>", 8, tD)), |
| 78 | + "0123456789" |
| 79 | + ); |
| 80 | + } |
| 81 | +} |
0 commit comments