Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -220,10 +220,28 @@ private static class PrimitiveDelegate implements Delegate {
if (value instanceof Long) {
return CodeBlock.of("$LL", value);
}
if (value instanceof Float) {
if (value instanceof Float floatValue) {
if (Float.isNaN(floatValue)) {
return CodeBlock.of("$T.NaN", Float.class);
}
if (floatValue == Float.POSITIVE_INFINITY) {
return CodeBlock.of("$T.POSITIVE_INFINITY", Float.class);
}
if (floatValue == Float.NEGATIVE_INFINITY) {
return CodeBlock.of("$T.NEGATIVE_INFINITY", Float.class);
}
return CodeBlock.of("$LF", value);
}
if (value instanceof Double) {
if (value instanceof Double doubleValue) {
if (Double.isNaN(doubleValue)) {
return CodeBlock.of("$T.NaN", Double.class);
}
if (doubleValue == Double.POSITIVE_INFINITY) {
return CodeBlock.of("$T.POSITIVE_INFINITY", Double.class);
}
if (doubleValue == Double.NEGATIVE_INFINITY) {
return CodeBlock.of("$T.NEGATIVE_INFINITY", Double.class);
}
return CodeBlock.of("(double) $L", value);
}
if (value instanceof Character character) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,36 @@ void generateWhenDouble() {
assertThat(generateCode(0.2)).hasToString("(double) 0.2");
}

@Test
void generateWhenFloatNaN() {
assertThat(generateCode(Float.NaN)).hasToString("java.lang.Float.NaN");
}

@Test
void generateWhenFloatPositiveInfinity() {
assertThat(generateCode(Float.POSITIVE_INFINITY)).hasToString("java.lang.Float.POSITIVE_INFINITY");
}

@Test
void generateWhenFloatNegativeInfinity() {
assertThat(generateCode(Float.NEGATIVE_INFINITY)).hasToString("java.lang.Float.NEGATIVE_INFINITY");
}

@Test
void generateWhenDoubleNaN() {
assertThat(generateCode(Double.NaN)).hasToString("java.lang.Double.NaN");
}

@Test
void generateWhenDoublePositiveInfinity() {
assertThat(generateCode(Double.POSITIVE_INFINITY)).hasToString("java.lang.Double.POSITIVE_INFINITY");
}

@Test
void generateWhenDoubleNegativeInfinity() {
assertThat(generateCode(Double.NEGATIVE_INFINITY)).hasToString("java.lang.Double.NEGATIVE_INFINITY");
}

@Test
void generateWhenChar() {
assertThat(generateCode('a')).hasToString("'a'");
Expand Down