1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30 package com.jcabi.log;
31
32 import org.hamcrest.MatcherAssert;
33 import org.hamcrest.Matchers;
34 import org.junit.jupiter.api.Test;
35
36
37
38
39
40 final class PreFormatterTest {
41
42
43
44
45 @Test
46 void decoratesArguments() {
47 final PreFormatter pre = new PreFormatter(
48 "%[com.jcabi.log.DecorMocker]-5.2f and %1$+.6f",
49 1.0d
50 );
51 MatcherAssert.assertThat(
52 pre.getFormat(),
53 Matchers.equalTo("%-5.2f and %1$+.6f")
54 );
55 MatcherAssert.assertThat(
56 pre.getArguments()[0],
57 Matchers.instanceOf(DecorMocker.class)
58 );
59 }
60
61
62
63
64 @Test
65 void formatsEvenWithMissedDecors() {
66 final PreFormatter pre =
67 new PreFormatter("ouch: %[missed]s", "test");
68 MatcherAssert.assertThat(
69 pre.getFormat(),
70 Matchers.equalTo("ouch: %s")
71 );
72 MatcherAssert.assertThat(
73 pre.getArguments()[0],
74 Matchers.instanceOf(String.class)
75 );
76 }
77
78
79
80
81 @Test
82 void formatsWithDirectlyProvidedDecors() {
83 final DecorMocker decor = new DecorMocker("a");
84 final PreFormatter pre = new PreFormatter("test: %s", decor);
85 MatcherAssert.assertThat(
86 pre.getArguments()[0],
87 Matchers.equalTo(decor)
88 );
89 }
90
91
92
93
94 @Test
95 void handleNewLineSpecifier() {
96 final String fmt = "%s%n%s";
97 final Object[] args = {"new", "line"};
98 final PreFormatter pre = new PreFormatter(fmt, args);
99 MatcherAssert.assertThat(
100 pre.getFormat(),
101 Matchers.is(fmt)
102 );
103 MatcherAssert.assertThat(
104 pre.getArguments(),
105 Matchers.is(args)
106 );
107 }
108
109
110
111
112 @Test
113 void handlePercentSpecifier() {
114 final String fmt = "%s%%";
115 final Object[] args = {"percent: "};
116 final PreFormatter pre = new PreFormatter(fmt, args);
117 MatcherAssert.assertThat(
118 pre.getFormat(),
119 Matchers.is(fmt)
120 );
121 MatcherAssert.assertThat(
122 pre.getArguments(),
123 Matchers.is(args)
124 );
125 }
126
127 }