コード例 #1
0
        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
            }
        }
コード例 #2
0
        public void TestFormatReplaceNoOptimizationNoOffsets()
        {
            SimpleFormatter fmt = SimpleFormatter.Compile(
                "Arguments {0} and {1}");
            StringBuilder result = new StringBuilder("previous:");

            assertEquals(
                "",
                "Arguments previous: and frog",
                fmt.FormatAndReplace(result, null, result.ToString(), "frog").ToString());
        }
コード例 #3
0
        public void TestFormatReplaceOptimizationNoOffsets()
        {
            SimpleFormatter fmt    = SimpleFormatter.Compile("{2}, {0}, {1} and {3}");
            StringBuilder   result = new StringBuilder("original");

            assertEquals(
                "format",
                "original, freddy, frog and by",
                fmt.FormatAndReplace(
                    result,
                    null,
                    "freddy", "frog", result.ToString(), "by").ToString());
        }
コード例 #4
0
        public void TestFormatReplaceNoOptimizationLeadingArgumentUsedTwice()
        {
            SimpleFormatter fmt = SimpleFormatter.Compile(
                "{2}, {0}, {1} and {3} {2}");
            StringBuilder result = new StringBuilder("original");

            int[] offsets = new int[4];
            assertEquals(
                "",
                "original, freddy, frog and by original",
                fmt.FormatAndReplace(
                    result,
                    offsets,
                    "freddy", "frog", result.ToString(), "by").ToString());
            int[] expectedOffsets = { 10, 18, 30, 27 };
            verifyOffsets(expectedOffsets, offsets);
        }
コード例 #5
0
        public void TestFormatReplaceNoOptimizationLeadingText()
        {
            SimpleFormatter fmt = SimpleFormatter.Compile("boo {2}, {0}, {1} and {3}");

            int[]         offsets = new int[4];
            StringBuilder result  = new StringBuilder("original");

            assertEquals(
                "format",
                "boo original, freddy, frog and by",
                fmt.FormatAndReplace(
                    result,
                    offsets,
                    "freddy", "frog", result.ToString(), "by").ToString());

            int[] expectedOffsets = { 14, 22, 4, 31 };
            verifyOffsets(expectedOffsets, offsets);
        }
コード例 #6
0
        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());
        }