@Test public void formatTest1() { Formatter format = new Formatter(); String result = formatter.format("{0:.2f}", 3.1415926); Assert.assertEquals(format.format("%.2f", 3.1415926).toString(), result); format.close(); }
@Test public void otherExtractorError() { class TestClass { public Object a = null; } String result = formatter.format("Test {0.a.b}", new TestClass()); Assert.assertEquals("Test ", result); }
@Test public void extractorFieldNotFound() { class TestClass { public int a = 10; } String result = formatter.format("Test {0.a}, {0.b}", new TestClass()); Assert.assertEquals("Test 10, ", result); }
@Test public void extractorTest1() { class TestClass { public int a = 10; } class TestClassSon extends TestClass { public int b = 20; } String result = formatter.format("Test {0.a}, {0.b}", new TestClassSon()); assertEquals(result, "Test 10, 20"); }
@Test public void moreComplexExctractor() { class TestClassA { public int a = 10; } class TestClassB { TestClassA b = new TestClassA(); } String result = formatter.format("Test {0.b.a}", new TestClassB()); Assert.assertEquals("Test 10", result); }
@Test public void formatterIncorrectIndex3() { thrown.expectMessage("Index 5 is out of range"); formatter.format("test{5}", 1); }
@Test public void formatterIncorrectIndex2() { thrown.expectMessage("Incorrect number: blah"); formatter.format("test{blah.blah}", 1); }
@Test public void formatterNullArgs() { String result = formatter.format("test{0}test", null); Assert.assertEquals("testtest", result); }
@Test public void formatterNothingToDoHere() { String result = formatter.format("Test", 1, 2, 3); Assert.assertEquals(result, "Test"); }
@Test public void simpleFormat() { String result = formatter.format("Test {{ {0}, {1}, }} {{, {2} }}{{}}", 1, 2, 3); Assert.assertEquals(result, "Test { 1, 2, } {, 3 }{}"); }
@Test public void formatterFormatingOfNull() { thrown.expectMessage("Null can't be formatted"); formatter.format("{0:f}", (Object) null); }
@Test public void noFormatter() { Calendar c = Calendar.getInstance(); thrown.expectMessage("There is no good formatter for class " + c.getClass().getName()); String result = formatter.format("{0:04d}", c); }
@Test public void formatTest2() { String result = formatter.format("{0:04d}", new BigInteger("31")); Assert.assertEquals("0031", result); }
@Test public void formatterIncorrectIndex6() { thrown.expectMessage("Index 2 is out of range"); formatter.format("test{2.blah}", 1); }
@Test public void formatterAndNull() { String result = formatter.format("test{0}test{1}test{2}", null, null, null); assertEquals("testtesttest", result); }
@Test public void formatterIncorrectIndex7() { thrown.expectMessage("Incorrect number: +0"); formatter.format("{+0}", "1"); }
@Test public void incorrectPattern() { thrown.expectMessage("Incorrect pattern: \".2f\""); String result = formatter.format("{0:.2f}", new BigInteger("31")); }
@Test public void formatterEmptyFieldName() { thrown.expectMessage("Field of class can't have empty name"); formatter.format("{0.}", 1); }
@Test public void emptyPattern() { thrown.expectMessage("Pattern must be non empty string"); String result = formatter.format("{0:}", new BigInteger("31")); }
@Test public void formatterNullFormat() { thrown.expectMessage("Format musn't be null"); formatter.format(null, 1, 2, 3); }
@Test public void formatterNullBuffer() { thrown.expectMessage("Buffer musn't be null"); formatter.format((StringBuilder) null, "Test {0}", 1); }
@Test public void formatterBracketTest1() { thrown.expectMessage("Unexpected opened bracket"); String result = formatter.format("blah { Test", 1, 2, 3); }
@Test public void formatterBracketTest4() { thrown.expectMessage("Unexpected closed bracket"); String result = formatter.format("blah Test}", 1, 2, 3); }