public void TestWithArguments() { SimpleFormatter fmt = SimpleFormatter.Compile( "Templates {2}{1} and {4} are out of order."); assertEquals( "getArgumentLimit", 5, fmt.ArgumentLimit); assertEquals( "toString", "Templates {2}{1} and {4} are out of order.", fmt.ToString()); int[] offsets = new int[6]; assertEquals( "format", "123456: Templates frogtommy and {0} are out of order.", fmt.FormatAndAppend( new StringBuilder("123456: "), offsets, "freddy", "tommy", "frog", "leg", "{0}").ToString()); int[] expectedOffsets = { -1, 22, 18, -1, 32, -1 }; verifyOffsets(expectedOffsets, offsets); }
public void TestTooFewArgumentValues() { SimpleFormatter fmt = SimpleFormatter.Compile( "Templates {2}{1} and {4} are out of order."); try { fmt.Format("freddy", "tommy", "frog", "leg"); fail("Expected IllegalArgumentException"); } catch (ArgumentException e) { // Expected } try { fmt.FormatAndAppend( new StringBuilder(), null, "freddy", "tommy", "frog", "leg"); fail("Expected IllegalArgumentException"); } catch (ArgumentException e) { // Expected } try { fmt.FormatAndReplace( new StringBuilder(), null, "freddy", "tommy", "frog", "leg"); fail("Expected IllegalArgumentException"); } catch (ArgumentException e) { // Expected } }
public void TestWithNoArguments() { SimpleFormatter fmt = SimpleFormatter.Compile("This doesn''t have templates '{0}"); assertEquals( "getArgumentLimit", 0, fmt.ArgumentLimit); assertEquals( "format", "This doesn't have templates {0}", fmt.Format("unused")); assertEquals( "format with values=null", "This doesn't have templates {0}", fmt.Format((ICharSequence[])null)); assertEquals( "toString", "This doesn't have templates {0}", fmt.ToString()); int[] offsets = new int[1]; assertEquals( "formatAndAppend", "This doesn't have templates {0}", fmt.FormatAndAppend(new StringBuilder(), offsets).ToString()); assertEquals( "offsets[0]", -1, offsets[0]); assertEquals( "formatAndAppend with values=null", "This doesn't have templates {0}", fmt.FormatAndAppend(new StringBuilder(), null, (ICharSequence[])null).ToString()); assertEquals( "formatAndReplace with values=null", "This doesn't have templates {0}", fmt.FormatAndReplace(new StringBuilder(), null, (ICharSequence[])null).ToString()); }
public void TestFormatUseAppendToAsArgument() { SimpleFormatter fmt = SimpleFormatter.Compile( "Arguments {0} and {1}"); StringBuilder appendTo = new StringBuilder("previous:"); try { fmt.FormatAndAppend(appendTo, null, appendTo.AsCharSequence(), "frog".AsCharSequence()); fail("IllegalArgumentException expected."); } catch (ArgumentException e) { // expected. } }