(t *testing.T)
| 7 | ) |
| 8 | |
| 9 | func TestMarshalDeterministic_Map(t *testing.T) { |
| 10 | // 创建一个 map,多次序列化应该产生相同的结果 |
| 11 | m := map[string]any{ |
| 12 | "zebra": 1, |
| 13 | "apple": 2, |
| 14 | "mango": 3, |
| 15 | } |
| 16 | |
| 17 | // 多次序列化,确保结果一致 |
| 18 | var results []string |
| 19 | for range 10 { |
| 20 | data, err := MarshalDeterministic(m) |
| 21 | if err != nil { |
| 22 | t.Fatalf("MarshalDeterministic failed: %v", err) |
| 23 | } |
| 24 | results = append(results, string(data)) |
| 25 | } |
| 26 | |
| 27 | // 所有结果应该相同 |
| 28 | expected := results[0] |
| 29 | for i, result := range results { |
| 30 | if result != expected { |
| 31 | t.Errorf("Iteration %d produced different result:\nexpected: %s\ngot: %s", i, expected, result) |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | // 验证 key 顺序是字典序 |
| 36 | expectedJSON := `{"apple":2,"mango":3,"zebra":1}` |
| 37 | if results[0] != expectedJSON { |
| 38 | t.Errorf("Expected ordered JSON:\nexpected: %s\ngot: %s", expectedJSON, results[0]) |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | func TestMarshalDeterministic_NestedMap(t *testing.T) { |
| 43 | m := map[string]any{ |
nothing calls this directly
no test coverage detected