-
Notifications
You must be signed in to change notification settings - Fork 163
/
Copy pathwriteSerialization.spec.ts
100 lines (95 loc) · 2.43 KB
/
writeSerialization.spec.ts
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
import { CompilerContext } from "../../context/context";
import {
getAllocation,
resolveAllocations,
} from "../../storage/resolveAllocation";
import {
getAllTypes,
getType,
resolveDescriptors,
} from "../../types/resolveDescriptors";
import { WriterContext } from "../Writer";
import { writeParser, writeSerializer } from "./writeSerialization";
import { writeStdlib } from "./writeStdlib";
import { openContext, parseModules } from "../../context/store";
import { writeAccessors } from "./writeAccessors";
import { getParser } from "../../grammar";
import { getAstFactory } from "../../ast/ast-helpers";
import type { Source } from "../../imports/source";
const code = `
primitive Int;
primitive Bool;
primitive Builder;
primitive Cell;
primitive Slice;
primitive Address;
struct A {
a: Int;
b: Int;
c: Int?;
d: Bool;
e: Bool?;
f: Int;
g: Int;
}
struct B {
a: Int;
b: Int;
c: Int?;
d: Bool;
e: Bool?;
f: Int;
g: Int;
}
struct C {
a: Cell;
b: Cell?;
c: Slice?;
d: Slice?;
e: Bool;
f: Int;
g: Int;
h: Address;
}
`;
describe("writeSerialization", () => {
for (const s of ["A", "B", "C"]) {
it("should write serializer for " + s, () => {
const ast = getAstFactory();
const sources: Source[] = [
{ code, path: "<unknown>", origin: "user" },
];
let ctx = openContext(
new CompilerContext(),
sources,
[],
parseModules(sources, getParser(ast)),
);
ctx = resolveDescriptors(ctx, ast);
ctx = resolveAllocations(ctx);
const wCtx = new WriterContext(ctx, s);
writeStdlib(wCtx);
writeSerializer(
getType(ctx, s).name,
false,
getAllocation(ctx, s),
"user",
wCtx,
);
for (const t of getAllTypes(ctx)) {
if (t.kind === "contract" || t.kind === "struct") {
writeAccessors(t, "user", wCtx);
}
}
writeParser(
getType(ctx, s).name,
false,
"with-opcode",
getAllocation(ctx, s),
wCtx,
);
const extracted = wCtx.extract(true);
expect(extracted).toMatchSnapshot();
});
}
});