@Test public void testStringList() throws Exception { Object input = Arrays.asList("foo", "bar", "wiz"); List<String> converted = Type.STRING_LIST.convert(input, null); assertEquals(input, converted); assertNotSame(input, converted); assertThat(Type.STRING_LIST.flatten(input)).isEmpty(); }
@Test public void testNonStringList() throws Exception { try { Type.STRING_LIST.convert(3, "blah"); fail(); } catch (Type.ConversionException e) { assertThat(e).hasMessage("expected value of type 'list(string)' for blah, but got 3 (int)"); } }
// Ensure that types are reported correctly. @Test public void testTypeErrorMessage() throws Exception { try { Type.STRING_LIST.convert("[(1,2), 3, 4]", "myexpr", null); fail(); } catch (Type.ConversionException e) { assertThat(e) .hasMessage( "expected value of type 'list(string)' for myexpr, " + "but got \"[(1,2), 3, 4]\" (string)"); } }
@Test public void testStringListBadElements() throws Exception { Object input = Arrays.<Object>asList("foo", "bar", 1); try { Type.STRING_LIST.convert(input, "argument quux"); fail(); } catch (Type.ConversionException e) { assertThat(e) .hasMessage( "expected value of type 'string' for element 2 of argument quux, but got 1 (int)"); } }
@Test public void testTagConversion() throws Exception { assertSameContents(Sets.newHashSet("attribute"), Type.BOOLEAN.toTagSet(true, "attribute")); assertSameContents(Sets.newHashSet("noattribute"), Type.BOOLEAN.toTagSet(false, "attribute")); assertSameContents( Sets.newHashSet("whiskey"), Type.STRING.toTagSet("whiskey", "preferred_cocktail")); assertSameContents( Sets.newHashSet("cheddar", "ementaler", "gruyere"), Type.STRING_LIST.toTagSet( Lists.newArrayList("cheddar", "ementaler", "gruyere"), "cheeses")); }