Mercurial > games > semicongine
comparison tests/test_storage.nim @ 1470:63364723d460
fix: make de-/serialization work with all kinds of weird object configurtions
| author | sam <sam@basx.dev> |
|---|---|
| date | Wed, 02 Apr 2025 00:49:02 +0700 |
| parents | cbca94a95736 |
| children | 1f58458b7ef7 |
comparison
equal
deleted
inserted
replaced
| 1469:c69bb7c58cf2 | 1470:63364723d460 |
|---|---|
| 12 | 12 |
| 13 # save and load custom | 13 # save and load custom |
| 14 store(storage, KEY, TEST_VALUE) | 14 store(storage, KEY, TEST_VALUE) |
| 15 assert storage.load(KEY, 0) == TEST_VALUE | 15 assert storage.load(KEY, 0) == TEST_VALUE |
| 16 | 16 |
| 17 type ID = distinct int | |
| 18 proc `==`(a, b: ID): bool = | |
| 19 `==`(int(a), int(b)) | |
| 20 | |
| 17 proc testWorldAPI() = | 21 proc testWorldAPI() = |
| 18 type Obj1 = object | 22 type Obj1 = object |
| 19 value: int | 23 value: int |
| 24 id: ID | |
| 20 | 25 |
| 21 type Obj2 = object | 26 type Obj2 = object |
| 22 a: string | 27 a: string |
| 23 b: Obj1 | 28 b: Obj1 |
| 24 c: seq[int] | 29 c: seq[int] |
| 25 d: array[3, Obj1] | 30 d: array[3, Obj1] |
| 26 e: bool | 31 e: bool |
| 27 | 32 |
| 28 assert listWorlds().len == 0 | 33 assert listWorlds().len == 0 |
| 29 | 34 |
| 30 const obj1 = Obj1(value: 42) | 35 const obj1 = Obj1(value: 42, id: ID(1)) |
| 31 "testWorld".storeWorld(obj1) | 36 "testWorld".storeWorld(obj1) |
| 32 assert listWorlds() == @["testWorld"] | 37 assert listWorlds() == @["testWorld"] |
| 33 assert loadWorld[Obj1]("testWorld") == obj1 | 38 assert loadWorld[Obj1]("testWorld") == obj1 |
| 34 | 39 |
| 35 const obj2 = Obj2( | 40 const obj2 = Obj2( |
| 36 a: "Hello world", | 41 a: "Hello world", |
| 37 b: Obj1(value: 20), | 42 b: Obj1(value: 20, id: ID(20)), |
| 38 c: @[1, 2, 3, 4], | 43 c: @[1, 2, 3, 4], |
| 39 d: [Obj1(value: 1), Obj1(value: 2), Obj1(value: 3)], | 44 d: [ |
| 45 Obj1(value: 1, id: ID(11)), Obj1(value: 2, id: ID(22)), Obj1(value: 3, id: ID(33)) | |
| 46 ], | |
| 40 e: true, | 47 e: true, |
| 41 ) | 48 ) |
| 42 "testWorld".storeWorld(obj2) | 49 "testWorld".storeWorld(obj2) |
| 43 assert listWorlds() == @["testWorld"] | 50 assert listWorlds() == @["testWorld"] |
| 44 assert loadWorld[Obj2]("testWorld") == obj2 | 51 assert loadWorld[Obj2]("testWorld") == obj2 |
