|
| 1 | +package org.scijava.widget; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertArrayEquals; |
| 4 | +import static org.junit.Assert.assertEquals; |
| 5 | +import static org.junit.Assert.assertTrue; |
| 6 | + |
| 7 | +import java.util.Arrays; |
| 8 | +import java.util.Collection; |
| 9 | +import java.util.HashSet; |
| 10 | +import java.util.List; |
| 11 | +import java.util.Set; |
| 12 | +import java.util.stream.Collectors; |
| 13 | +import java.util.stream.IntStream; |
| 14 | + |
| 15 | +import org.junit.Test; |
| 16 | +import org.junit.experimental.runners.Enclosed; |
| 17 | +import org.junit.runner.RunWith; |
| 18 | +import org.junit.runners.Parameterized; |
| 19 | +import org.junit.runners.Parameterized.Parameter; |
| 20 | +import org.junit.runners.Parameterized.Parameters; |
| 21 | + |
| 22 | +@RunWith(Enclosed.class) |
| 23 | +public class WidgetStyleTest { |
| 24 | + |
| 25 | + @RunWith(Parameterized.class) |
| 26 | + public static class TestIsStyle { |
| 27 | + |
| 28 | + static String[] styleStrings = { "foo, bar, someThing", " FOO, BAR, SOMEthing ", "foo ", " bar", |
| 29 | + "trash, sOmEtHiNg", null }; |
| 30 | + |
| 31 | + static String[] stylesToTest = { "foo", "bar", "someThing", null }; |
| 32 | + |
| 33 | + static boolean[][] stylesToHave = { // foo, bar, someThing |
| 34 | + new boolean[] { true, true, true, false }, new boolean[] { true, true, true, false }, |
| 35 | + new boolean[] { true, false, false, false }, new boolean[] { false, true, false, false }, |
| 36 | + new boolean[] { false, false, true, false }, new boolean[] { false, false, false, true } }; |
| 37 | + |
| 38 | + @Parameters(name = "{0}") |
| 39 | + public static List<Object[]> params() { |
| 40 | + return IntStream.range(0, styleStrings.length) |
| 41 | + .mapToObj(i -> new Object[] { styleStrings[i], stylesToHave[i] }).collect(Collectors.toList()); |
| 42 | + } |
| 43 | + |
| 44 | + @Parameter |
| 45 | + public String styleString; |
| 46 | + |
| 47 | + @Parameter(1) |
| 48 | + public boolean[] targetStyles; |
| 49 | + |
| 50 | + @Test |
| 51 | + public void testSimpleStyles() { |
| 52 | + for (int i = 0; i < stylesToTest.length; i++) { |
| 53 | + assertEquals("style: " + stylesToTest[i], targetStyles[i], |
| 54 | + WidgetStyle.isStyle(styleString, stylesToTest[i])); |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + public static class TestStyleModifiers { |
| 60 | + @Test |
| 61 | + public void testStyleModifiers() { |
| 62 | + String style = "open, extensions:tiff/tif/jpeg/jpg"; |
| 63 | + Set<String> extensions = new HashSet<>(Arrays.asList(WidgetStyle.getStyleModifiers(style, "extensions"))); |
| 64 | + Set<String> expected = new HashSet<>(Arrays.asList("tiff", "jpg", "jpeg", "tif")); |
| 65 | + assertEquals(expected, extensions); |
| 66 | + } |
| 67 | + } |
| 68 | +} |
0 commit comments