Skip to content

Commit e3f53c1

Browse files
author
Glen Takahashi
committed
Add immutables test
1 parent 534d94a commit e3f53c1

File tree

6 files changed

+98
-0
lines changed

6 files changed

+98
-0
lines changed

typescript-generator-core/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@
5050
<version>4.11</version>
5151
<scope>test</scope>
5252
</dependency>
53+
<dependency>
54+
<groupId>org.immutables</groupId>
55+
<artifactId>value</artifactId>
56+
<version>2.5.3</version>
57+
<scope>test</scope>
58+
</dependency>
5359
<dependency>
5460
<groupId>com.google.code.findbugs</groupId>
5561
<artifactId>annotations</artifactId>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package cz.habarta.typescript.generator;
2+
3+
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
4+
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
5+
import org.immutables.value.Value;
6+
7+
@Value.Immutable
8+
@JsonSerialize(as = ImmutableCircle.class)
9+
@JsonDeserialize(as = ImmutableCircle.class)
10+
public interface Circle extends Shape {
11+
double radius();
12+
13+
final class Builder extends ImmutableCircle.Builder {}
14+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
2+
package cz.habarta.typescript.generator;
3+
4+
import org.junit.Assert;
5+
import org.junit.Test;
6+
7+
public class ImmutablesTest {
8+
9+
@Test
10+
public void testImmutables() {
11+
final Settings settings = TestUtils.settings();
12+
final String output = new TypeScriptGenerator(settings).generateTypeScript(Input.from(Shape.class));
13+
final String expected = (
14+
"\n" +
15+
"interface Shape {\n" +
16+
" kind: 'square' | 'rectangle' | 'circle';\n" +
17+
"}\n" +
18+
"\n" +
19+
"interface Square extends Shape {\n" +
20+
" kind: 'square';\n" +
21+
" size: number;\n" +
22+
"}\n" +
23+
"\n" +
24+
"interface Rectangle extends Shape {\n" +
25+
" kind: 'rectangle';\n" +
26+
" width: number;\n" +
27+
" height: number;\n" +
28+
"}\n" +
29+
"\n" +
30+
"interface Circle extends Shape {\n" +
31+
" kind: 'circle';\n" +
32+
" radius: number;\n" +
33+
"}\n" +
34+
"\n" +
35+
"type ShapeUnion = Square | Rectangle | Circle;\n" +
36+
""
37+
).replace('\'', '"');
38+
Assert.assertEquals(expected, output);
39+
}
40+
41+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package cz.habarta.typescript.generator;
2+
3+
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
4+
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
5+
import org.immutables.value.Value;
6+
7+
@Value.Immutable
8+
@JsonSerialize(as = ImmutableRectangle.class)
9+
@JsonDeserialize(as = ImmutableRectangle.class)
10+
public abstract class Rectangle implements Shape {
11+
public abstract double width();
12+
public abstract double height();
13+
14+
public static Rectangle.Builder builder() {
15+
return new Rectangle.Builder();
16+
}
17+
18+
public static final class Builder extends ImmutableRectangle.Builder {}
19+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package cz.habarta.typescript.generator;
2+
3+
import com.fasterxml.jackson.annotation.JsonSubTypes;
4+
import com.fasterxml.jackson.annotation.JsonTypeInfo;
5+
6+
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "kind")
7+
@JsonSubTypes({
8+
@JsonSubTypes.Type(value = Square.class, name = "square"),
9+
@JsonSubTypes.Type(value = Rectangle.class, name = "rectangle"),
10+
@JsonSubTypes.Type(value = Circle.class, name = "circle"),
11+
})
12+
public interface Shape {
13+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package cz.habarta.typescript.generator;
2+
3+
public class Square implements Shape {
4+
public double size;
5+
}

0 commit comments

Comments
 (0)