-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformat_callback_test.go
More file actions
66 lines (56 loc) · 1.79 KB
/
Copy pathformat_callback_test.go
File metadata and controls
66 lines (56 loc) · 1.79 KB
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
package clicky
import (
"encoding/json"
"testing"
)
func TestFormatWithContextCallbacks(t *testing.T) {
t.Cleanup(ClearFormatCallbacks)
type callbackContext struct {
RequestID string
}
ctx := &callbackContext{RequestID: "req-42"}
var beforeSeen any
AddFormatCallback(FormatCallback{
BeforeFormat: func(callbackCtx any, manager *FormatManager, options FormatOptions, before any) any {
if manager == nil {
t.Fatal("expected manager")
}
gotCtx, ok := callbackCtx.(*callbackContext)
if !ok {
t.Fatalf("callback ctx type = %T", callbackCtx)
}
if gotCtx.RequestID != ctx.RequestID {
t.Fatalf("request id = %q, want %q", gotCtx.RequestID, ctx.RequestID)
}
payload, ok := before.(map[string]any)
if !ok {
t.Fatalf("before type = %T", before)
}
payload["request_id"] = gotCtx.RequestID
return payload
},
AfterFormat: func(callbackCtx any, manager *FormatManager, options FormatOptions, before any, out string) string {
beforeSeen = before
return out + "\n# callback"
},
})
output, err := FormatWithContext(ctx, map[string]any{"name": "Ada"}, FormatOptions{Format: "json"})
if err != nil {
t.Fatalf("FormatWithContext() error = %v", err)
}
var payload map[string]any
raw := output[:len(output)-len("\n# callback")]
if err := json.Unmarshal([]byte(raw), &payload); err != nil {
t.Fatalf("invalid json output: %v\n%s", err, output)
}
if payload["request_id"] != ctx.RequestID {
t.Fatalf("request_id = %#v, want %q", payload["request_id"], ctx.RequestID)
}
beforePayload, ok := beforeSeen.(map[string]any)
if !ok {
t.Fatalf("after callback before type = %T", beforeSeen)
}
if beforePayload["request_id"] != ctx.RequestID {
t.Fatalf("after callback before.request_id = %#v, want %q", beforePayload["request_id"], ctx.RequestID)
}
}