|
1 | 1 | import { describe, expect, test } from "vitest"; |
2 | | -import { deserialize, serializable, serialize } from "../src"; |
| 2 | +import { configureSerializer, deserialize, serializable, serialize } from "../src"; |
3 | 3 |
|
4 | 4 | describe("对象序列化", () => { |
5 | 5 | class A { |
@@ -69,6 +69,41 @@ describe("对象序列化", () => { |
69 | 69 | expect(deserialized.nestedInstance).toBeInstanceOf(A); |
70 | 70 | expect(deserialized.propString).toBe("@graphif/serializer"); |
71 | 71 | }); |
| 72 | + |
| 73 | + test("使用稳定类名而不是压缩后的运行时类名", () => { |
| 74 | + class a { |
| 75 | + static className = "StableClass"; |
| 76 | + |
| 77 | + @serializable |
| 78 | + value: string; |
| 79 | + |
| 80 | + constructor(value: string) { |
| 81 | + this.value = value; |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + const serialized = serialize(new a("value")); |
| 86 | + expect(serialized).toEqual({ _: "StableClass", value: "value" }); |
| 87 | + expect(deserialize(serialized)).toBeInstanceOf(a); |
| 88 | + }); |
| 89 | + |
| 90 | + test("配置的类名解析器会回退到运行时类名", () => { |
| 91 | + configureSerializer((class_) => class_.className!); |
| 92 | + |
| 93 | + class C { |
| 94 | + @serializable |
| 95 | + value: string; |
| 96 | + |
| 97 | + constructor(value: string) { |
| 98 | + this.value = value; |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + const serialized = serialize(new C("value")); |
| 103 | + expect(serialized).toEqual({ _: "C", value: "value" }); |
| 104 | + expect(deserialize(serialized)).toBeInstanceOf(C); |
| 105 | + }); |
| 106 | + |
72 | 107 | // 指针引用同一性的反序列化测试(数组与对象容器) |
73 | 108 | class B { |
74 | 109 | @serializable |
|
0 commit comments