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.aspects.aj;
31
32 import org.hamcrest.MatcherAssert;
33 import org.hamcrest.Matchers;
34 import org.junit.jupiter.api.Test;
35
36
37
38
39
40
41 final class MnemosTest {
42
43
44
45
46 private static final transient String EMPTY_ARRAY = "[]";
47
48 @Test
49 void buildsTextFromObject() {
50 final Object[][] pairs = {
51 new Object[] {1, "1"},
52 new Object[] {1.43f, "1.43"},
53 new Object[] {"\u20ac-plain", "'\u20ac-plain'"},
54 new Object[] {"test ", "'test '"},
55 new Object[] {null, "NULL"},
56 new Object[] {new String[0], MnemosTest.EMPTY_ARRAY},
57 new Object[] {new String[] {"abc", "x"}, "['abc', 'x']"},
58 new Object[] {new Object[] {null, 5}, "[NULL, 5]"},
59 };
60 this.validateText(pairs);
61 }
62
63 @Test
64 void handlesToxicObjectsGracefully() {
65 MatcherAssert.assertThat(
66 Mnemos.toText(
67 new Object() {
68 @Override
69 public String toString() {
70 throw new IllegalArgumentException("boom");
71 }
72 }, true, false
73 ),
74 Matchers.equalTo(
75
76 "[com.jcabi.aspects.aj.MnemosTest$1 thrown java.lang.IllegalArgumentException(boom)]"
77 )
78 );
79 }
80
81 @Test
82 void buildsTextFromIntArray() {
83 final Object[][] pairs = {
84 new Object[] {new int[0], MnemosTest.EMPTY_ARRAY},
85 new Object[] {new int[] {1}, "[1]"},
86 new Object[] {new int[] {1, 2, 3}, "[1, 2, 3]"},
87 };
88 this.validateText(pairs);
89 }
90
91 @Test
92 void buildsTextFromLongArray() {
93 final Object[][] pairs = {
94 new Object[] {new long[0], MnemosTest.EMPTY_ARRAY},
95 new Object[] {new long[] {2L}, "[2]"},
96 new Object[] {new long[] {2L, 3L, 4L}, "[2, 3, 4]"},
97 };
98 this.validateText(pairs);
99 }
100
101 @Test
102 void buildsTextFromFloatArray() {
103 final Object[][] pairs = {
104 new Object[] {new float[0], MnemosTest.EMPTY_ARRAY},
105 new Object[] {new float[] {1.01f}, "[1.01]"},
106 new Object[] {
107 new float[] {1.01f, 2.02f, 3.03f},
108 "[1.01, 2.02, 3.03]",
109 },
110 };
111 this.validateText(pairs);
112 }
113
114 @Test
115 void buildsTextFromDoubleArray() {
116 final Object[][] pairs = {
117 new Object[] {new double[0], MnemosTest.EMPTY_ARRAY},
118 new Object[] {new double[] {2.01}, "[2.01]"},
119 new Object[] {
120 new double[] {2.01, 2.02, 2.03},
121 "[2.01, 2.02, 2.03]",
122 },
123 };
124 this.validateText(pairs);
125 }
126
127 @Test
128 void buildsTextFromCharArray() {
129 final Object[][] pairs = {
130 new Object[] {new char[0], MnemosTest.EMPTY_ARRAY},
131 new Object[] {new char[] {'a'}, "[a]"},
132 new Object[] {new char[] {'a', 'b', 'c'}, "[a, b, c]"},
133 };
134 this.validateText(pairs);
135 }
136
137 @Test
138 void buildsTextFromBooleanArray() {
139 final Object[][] pairs = {
140 new Object[] {new boolean[0], MnemosTest.EMPTY_ARRAY},
141 new Object[] {new boolean[] {true}, "[true]"},
142 new Object[] {
143 new boolean[] {true, false, false},
144 "[true, false, false]",
145 },
146 };
147 this.validateText(pairs);
148 }
149
150
151
152
153
154 private void validateText(final Object[]... pairs) {
155 for (final Object[] pair : pairs) {
156 MatcherAssert.assertThat(
157 Mnemos.toText(pair[0], false, false),
158 Matchers.equalTo(pair[1].toString())
159 );
160 }
161 }
162 }