]>
Commit | Line | Data |
---|---|---|
6988a057 JH |
1 | package hotline |
2 | ||
625b0580 | 3 | import ( |
7cd900d6 | 4 | "encoding/hex" |
00d1ef67 | 5 | "github.com/stretchr/testify/assert" |
a6216dd8 | 6 | "log/slog" |
625b0580 | 7 | "os" |
625b0580 JH |
8 | "testing" |
9 | ) | |
10 | ||
a6216dd8 JH |
11 | func NewTestLogger() *slog.Logger { |
12 | return slog.New(slog.NewTextHandler(os.Stdout, nil)) | |
625b0580 JH |
13 | } |
14 | ||
7cd900d6 JH |
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 | ||
f8e4cd54 | 33 | // tranAssertEqual compares equality of transactions slices after stripping out the random transaction ID |
00d1ef67 JH |
34 | func tranAssertEqual(t *testing.T, tran1, tran2 []Transaction) bool { |
35 | var newT1 []Transaction | |
36 | var newT2 []Transaction | |
7cd900d6 | 37 | |
92a7e455 | 38 | for _, trans := range tran1 { |
153e2eac | 39 | trans.ID = [4]byte{0, 0, 0, 0} |
7cd900d6 JH |
40 | var fs []Field |
41 | for _, field := range trans.Fields { | |
f8e4cd54 JH |
42 | if field.ID == [2]byte{0x00, 0x6b} { // FieldRefNum |
43 | continue | |
44 | } | |
45 | if field.ID == [2]byte{0x00, 0x72} { // FieldChatID | |
7cd900d6 JH |
46 | continue |
47 | } | |
48 | fs = append(fs, field) | |
49 | } | |
50 | trans.Fields = fs | |
00d1ef67 JH |
51 | newT1 = append(newT1, trans) |
52 | } | |
625b0580 | 53 | |
92a7e455 | 54 | for _, trans := range tran2 { |
153e2eac | 55 | trans.ID = [4]byte{0, 0, 0, 0} |
7cd900d6 JH |
56 | var fs []Field |
57 | for _, field := range trans.Fields { | |
f8e4cd54 JH |
58 | if field.ID == [2]byte{0x00, 0x6b} { // FieldRefNum |
59 | continue | |
60 | } | |
61 | if field.ID == [2]byte{0x00, 0x72} { // FieldChatID | |
7cd900d6 JH |
62 | continue |
63 | } | |
64 | fs = append(fs, field) | |
65 | } | |
66 | trans.Fields = fs | |
00d1ef67 | 67 | newT2 = append(newT2, trans) |
00d1ef67 | 68 | } |
625b0580 | 69 | |
00d1ef67 | 70 | return assert.Equal(t, newT1, newT2) |
92a7e455 | 71 | } |