]>
Commit | Line | Data |
---|---|---|
1 | package hotline | |
2 | ||
3 | import ( | |
4 | "encoding/hex" | |
5 | "github.com/stretchr/testify/assert" | |
6 | "log/slog" | |
7 | "os" | |
8 | "testing" | |
9 | ) | |
10 | ||
11 | func NewTestLogger() *slog.Logger { | |
12 | return slog.New(slog.NewTextHandler(os.Stdout, nil)) | |
13 | } | |
14 | ||
15 | // assertTransferBytesEqual takes a string with a hexdump in the same format that `hexdump -C` produces and compares with | |
16 | // a hexdump for the bytes in got, after stripping the create/modify timestamps. | |
17 | // I don't love this, but as git does not preserve file create/modify timestamps, we either need to fully mock the | |
18 | // filesystem interactions or work around in this way. | |
19 | // TODO: figure out a better solution | |
20 | func assertTransferBytesEqual(t *testing.T, wantHexDump string, got []byte) bool { | |
21 | if wantHexDump == "" { | |
22 | return true | |
23 | } | |
24 | ||
25 | var clean []byte | |
26 | clean = append(clean, got[:92]...) // keep the first 92 bytes | |
27 | clean = append(clean, make([]byte, 16)...) // replace the next 16 bytes for create/modify timestamps | |
28 | clean = append(clean, got[108:]...) // keep the rest | |
29 | ||
30 | return assert.Equal(t, wantHexDump, hex.Dump(clean)) | |
31 | } | |
32 | ||
33 | // tranAssertEqual compares equality of transactions slices after stripping out the random transaction ID | |
34 | func tranAssertEqual(t *testing.T, tran1, tran2 []Transaction) bool { | |
35 | var newT1 []Transaction | |
36 | var newT2 []Transaction | |
37 | ||
38 | for _, trans := range tran1 { | |
39 | trans.ID = [4]byte{0, 0, 0, 0} | |
40 | var fs []Field | |
41 | for _, field := range trans.Fields { | |
42 | if field.ID == [2]byte{0x00, 0x6b} { // FieldRefNum | |
43 | continue | |
44 | } | |
45 | if field.ID == [2]byte{0x00, 0x72} { // FieldChatID | |
46 | continue | |
47 | } | |
48 | trans.Fields = append(trans.Fields, field) | |
49 | } | |
50 | trans.Fields = fs | |
51 | newT1 = append(newT1, trans) | |
52 | } | |
53 | ||
54 | for _, trans := range tran2 { | |
55 | trans.ID = [4]byte{0, 0, 0, 0} | |
56 | var fs []Field | |
57 | for _, field := range trans.Fields { | |
58 | if field.ID == [2]byte{0x00, 0x6b} { // FieldRefNum | |
59 | continue | |
60 | } | |
61 | if field.ID == [2]byte{0x00, 0x72} { // FieldChatID | |
62 | continue | |
63 | } | |
64 | trans.Fields = append(trans.Fields, field) | |
65 | } | |
66 | trans.Fields = fs | |
67 | newT2 = append(newT2, trans) | |
68 | } | |
69 | ||
70 | return assert.Equal(t, newT1, newT2) | |
71 | } |