|
7 | 7 |
|
8 | 8 | import static org.junit.jupiter.api.Assertions.assertEquals; |
9 | 9 | import static org.junit.jupiter.api.Assertions.assertThrows; |
| 10 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
10 | 11 |
|
11 | 12 | import tools.jackson.core.JsonGenerator; |
| 13 | +import tools.jackson.databind.JsonNode; |
12 | 14 | import tools.jackson.databind.ObjectMapper; |
13 | 15 |
|
14 | 16 | public class TomlGeneratorTest extends TomlMapperTestBase { |
@@ -48,6 +50,61 @@ public void floats() { |
48 | 50 | assertEquals("abc = 1.23\n", w.toString()); |
49 | 51 | } |
50 | 52 |
|
| 53 | + // [dataformats-text#696]: non-finite floats must be written as the TOML |
| 54 | + // tokens `nan` / `inf` / `-inf` (not Java's `NaN` / `Infinity`) so the |
| 55 | + // writer's own output can be parsed back. |
| 56 | + @Test |
| 57 | + public void nonFiniteDoubles() throws IOException { |
| 58 | + assertEquals("abc = nan\n", _writeDouble(Double.NaN)); |
| 59 | + assertEquals("abc = inf\n", _writeDouble(Double.POSITIVE_INFINITY)); |
| 60 | + assertEquals("abc = -inf\n", _writeDouble(Double.NEGATIVE_INFINITY)); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + public void nonFiniteFloats() throws IOException { |
| 65 | + assertEquals("abc = nan\n", _writeFloat(Float.NaN)); |
| 66 | + assertEquals("abc = inf\n", _writeFloat(Float.POSITIVE_INFINITY)); |
| 67 | + assertEquals("abc = -inf\n", _writeFloat(Float.NEGATIVE_INFINITY)); |
| 68 | + } |
| 69 | + |
| 70 | + // Written non-finite values must round-trip through the same mapper. |
| 71 | + @Test |
| 72 | + public void nonFiniteRoundTrip() throws IOException { |
| 73 | + TomlMapper mapper = newTomlMapper(); |
| 74 | + for (double d : new double[] { |
| 75 | + Double.NaN, Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY }) { |
| 76 | + String toml = mapper.writeValueAsString(java.util.Collections.singletonMap("x", d)); |
| 77 | + JsonNode node = mapper.readTree(toml).get("x"); |
| 78 | + if (Double.isNaN(d)) { |
| 79 | + assertTrue(Double.isNaN(node.doubleValue()), "expected NaN from: " + toml); |
| 80 | + } else { |
| 81 | + assertEquals(d, node.doubleValue(), 0.0, "round-trip failed for: " + toml); |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + private String _writeDouble(double d) throws IOException { |
| 87 | + StringWriter w = new StringWriter(); |
| 88 | + try (JsonGenerator generator = newTomlMapper().createGenerator(w)) { |
| 89 | + generator.writeStartObject(); |
| 90 | + generator.writeName("abc"); |
| 91 | + generator.writeNumber(d); |
| 92 | + generator.writeEndObject(); |
| 93 | + } |
| 94 | + return w.toString(); |
| 95 | + } |
| 96 | + |
| 97 | + private String _writeFloat(float f) throws IOException { |
| 98 | + StringWriter w = new StringWriter(); |
| 99 | + try (JsonGenerator generator = newTomlMapper().createGenerator(w)) { |
| 100 | + generator.writeStartObject(); |
| 101 | + generator.writeName("abc"); |
| 102 | + generator.writeNumber(f); |
| 103 | + generator.writeEndObject(); |
| 104 | + } |
| 105 | + return w.toString(); |
| 106 | + } |
| 107 | + |
51 | 108 | @Test |
52 | 109 | public void stringNormal() { |
53 | 110 | StringWriter w = new StringWriter(); |
|
0 commit comments