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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
mod common;
use anyhow::Result;
use common::*;
use serde::{Deserialize, Serialize};
use type_reflect::*;
#[derive(Reflect, Serialize, Deserialize)]
pub struct Rectangle {
width: f32,
height: f32,
}
#[derive(Reflect, Serialize, Deserialize)]
#[serde(tag = "_case", content = "data")]
pub enum Shape {
Circle { radius: f32 },
Square { side: f32 },
Rectangle(Rectangle),
ScaledRectangle(Rectangle, u32),
Null,
}
pub const SCOPE: &'static str = "test_adt";
#[test]
fn test_validation() -> Result<()> {
let output = init_path(SCOPE, "test_validation");
export_types!(
types: [ Shape, Rectangle ],
destinations: [(
output.ts_path(),
emitters: [
TypeScript(),
TSValidation(),
TSFormat(
tab_size: 2,
line_width: 80,
),
],
)]
)?;
output.write_jest(
"Shape, Rectangle, ShapeCase, ShapeCaseKey",
ts_string! {
describe("ADT Validation", ()=>{
it("Validates a Null variant: ShapeCaseKey.Null", ()=>{
expect(() => {
Shape.validate({_case: ShapeCaseKey.Null})
}).not.toThrow();
});
it("Validates a Null variant literal: 'Null'", ()=>{
expect(() => {
Shape.validate({_case: "Null"})
}).not.toThrow();
});
it("Validates a Circle variant: {_case: ShapeCaseKey.Circle, data: { radius: 1.7} }", ()=>{
expect(() => {
Shape.validate({
_case: ShapeCaseKey.Circle,
data: {
radius: 1.7
}
})
}).not.toThrow();
});
it("Validates a Rectangle variant: {_case: ShapeCaseKey.Rectangle, data: { width: 1, height: 2} }", ()=>{
expect(() => {
Shape.validate({
_case: ShapeCaseKey.Rectangle,
data: {
width: 1,
height: 2
}
})
}).not.toThrow();
});
it("Validates a ScaledRectangle variant: {_case: ShapeCaseKey.ScaledRectangle, data: [{ width: 1, height: 2}, 0.5] }", ()=>{
expect(() => {
Shape.validate({
_case: ShapeCaseKey.ScaledRectangle,
data: [
{
width: 1,
height: 2
},
0.5
]
})
}).not.toThrow();
});
it("Doesn't Validate an incorrect ScaledRectangle variant: {_case: ShapeCaseKey.Circle, data: [{ width: 1, height: 2}, 0.5] }", ()=>{
expect(() => {
Shape.validate({
_case: ShapeCaseKey.Circle,
data: [
{
width: 1,
height: 2
},
0.5
]
})
}).toThrow();
});
});
}
.as_str(),
)?;
output.run_ts()
}