Skip to content

Commit c8e5b76

Browse files
adding Test for PrimitiveDecoder
1 parent 884dd3b commit c8e5b76

File tree

3 files changed

+101
-13
lines changed

3 files changed

+101
-13
lines changed

src/main/java/dev/toonformat/jtoon/decoder/PrimitiveDecoder.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,7 @@ static Object parse(String value) {
7171

7272
// Check for leading zeros (treat as string, except for "0", "-0", "0.0", etc.)
7373
String trimmed = value.trim();
74-
if (trimmed.length() > 1
75-
&& trimmed.matches("^-?0+[0-7].*") //octal number
76-
&& !trimmed.matches("^-?0+(\\.0+)?([eE][+-]?\\d+)?$")) {
74+
if (trimmed.length() > 1 && trimmed.matches("^-?0+[0-7].*")) {
7775
return value;
7876
}
7977

src/main/java/dev/toonformat/jtoon/decoder/ValueDecoder.java

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -77,28 +77,26 @@ public static Object decode(String toon, DecodeOptions options) {
7777
return new LinkedHashMap<>();
7878
}
7979

80-
String content = depth == 0 ? line : line.substring(depth * context.options.indent());
81-
8280
// Handle standalone arrays: [2]:
83-
if (!content.isEmpty() && content.charAt(0) == '[') {
84-
return ArrayDecoder.parseArray(content, depth, context);
81+
if (!line.isEmpty() && line.charAt(0) == '[') {
82+
return ArrayDecoder.parseArray(line, depth, context);
8583
}
8684

8785
// Handle keyed arrays: items[2]{id,name}:
88-
Matcher keyedArray = KEYED_ARRAY_PATTERN.matcher(content);
86+
Matcher keyedArray = KEYED_ARRAY_PATTERN.matcher(line);
8987
if (keyedArray.matches()) {
90-
return KeyDecoder.parseKeyedArrayValue(keyedArray, content, depth, context);
88+
return KeyDecoder.parseKeyedArrayValue(keyedArray, line, depth, context);
9189
}
9290
// Handle key-value pairs: name: Ada
93-
int colonIdx = DecodeHelper.findUnquotedColon(content);
91+
int colonIdx = DecodeHelper.findUnquotedColon(line);
9492
if (colonIdx > 0) {
95-
String key = content.substring(0, colonIdx).trim();
96-
String value = content.substring(colonIdx + 1).trim();
93+
String key = line.substring(0, colonIdx).trim();
94+
String value = line.substring(colonIdx + 1).trim();
9795
return KeyDecoder.parseKeyValuePair(key, value, depth, depth == 0, context);
9896
}
9997

10098
// Bare scalar value
101-
return ObjectDecoder.parseBareScalarValue(content, depth, context);
99+
return ObjectDecoder.parseBareScalarValue(line, depth, context);
102100
}
103101

104102
/**

src/test/java/dev/toonformat/jtoon/decoder/PrimitiveDecoderTest.java

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,98 @@ void givenNegativeZeroDouble_whenParse_thenReturnsZeroLong() {
190190
assertEquals(0L, result);
191191
}
192192

193+
@Test
194+
void givenOctalNumber_whenParse_thenReturnsLong() {
195+
// Given
196+
String input = "07";
197+
198+
// When
199+
Object result = PrimitiveDecoder.parse(input);
200+
201+
// Then
202+
assertNotNull(result);
203+
assertEquals("07", result.toString());
204+
}
205+
206+
@Test
207+
void givenNumberWithLeadingZero_whenParse_thenReturnsLong() {
208+
// Given
209+
String input = "0.7";
210+
211+
// When
212+
Object result = PrimitiveDecoder.parse(input);
213+
214+
// Then
215+
assertNotNull(result);
216+
assertEquals("0.7", result.toString());
217+
}
218+
219+
@Test
220+
void givenNumberWithLeadingZeroOutsideTheOctalRange_whenParse_thenReturnsLong() {
221+
// Given
222+
String input = "0.9";
223+
224+
// When
225+
Object result = PrimitiveDecoder.parse(input);
226+
227+
// Then
228+
assertNotNull(result);
229+
assertEquals("0.9", result.toString());
230+
}
231+
232+
@Test
233+
void givenMinLongNumber_whenParse_thenReturnsLong() {
234+
// Given
235+
String input = String.valueOf(Long.MIN_VALUE);
236+
237+
// When
238+
Object result = PrimitiveDecoder.parse(input);
239+
240+
// Then
241+
assertNotNull(result);
242+
assertEquals("-9223372036854775808", result.toString());
243+
}
244+
245+
@Test
246+
void givenMaxLongNumber_whenParse_thenReturnsLong() {
247+
// Given
248+
String input = String.valueOf(Long.MAX_VALUE);
249+
250+
// When
251+
Object result = PrimitiveDecoder.parse(input);
252+
253+
// Then
254+
assertNotNull(result);
255+
assertEquals("9223372036854775807", result.toString());
256+
}
257+
258+
@Test
259+
void givenSmallerMinLongNumber_whenParse_thenReturnsLong() {
260+
// Given
261+
String input = String.valueOf(Long.MIN_VALUE -1);
262+
263+
// When
264+
Object result = PrimitiveDecoder.parse(input);
265+
266+
// Then
267+
assertNotNull(result);
268+
assertEquals("9223372036854775807", result.toString());
269+
}
270+
271+
@Test
272+
void givenBiggerMaxLongNumber_whenParse_thenReturnsLong() {
273+
// Given
274+
String input = String.valueOf(Long.MAX_VALUE +1);
275+
276+
// When
277+
Object result = PrimitiveDecoder.parse(input);
278+
279+
// Then
280+
assertNotNull(result);
281+
assertEquals("-9223372036854775808", result.toString());
282+
}
283+
284+
193285
@Test
194286
void givenInvalidNumber_whenParse_thenReturnsOriginalString() {
195287
// Given

0 commit comments

Comments
 (0)