This repository was archived by the owner on Apr 23, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbyte_test.go
78 lines (70 loc) · 1.63 KB
/
byte_test.go
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
67
68
69
70
71
72
73
74
75
76
77
78
// Copyright 2018 Romano de Souza. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package coalesce_test
import (
"reflect"
"testing"
"github.com/romanodesouza/coalesce"
)
func TestByte(t *testing.T) {
tests := []struct {
name string
in []*byte
want *byte
}{
{
name: "it should return the first non-nil value",
in: []*byte{nil, ptrbyte(byte('a')), ptrbyte(byte('b'))},
want: ptrbyte('a'),
},
{
name: "it should return nil if a non-nil value has not been found",
in: []*byte{nil, nil, nil},
want: nil,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := coalesce.Byte(tt.in...)
if tt.want != nil && *got != *tt.want {
t.Errorf("got %v; want %v", *got, *tt.want)
}
if tt.want == nil && got != nil {
t.Errorf("got %v; want %v", *got, tt.want)
}
})
}
}
func TestByteSlice(t *testing.T) {
tests := []struct {
name string
in [][]byte
want []byte
}{
{
name: "it should return the first non-nil value",
in: [][]byte{nil, []byte{byte('a')}, []byte{byte('b')}},
want: []byte{byte('a')},
},
{
name: "it should return nil if a non-nil value has not been found",
in: [][]byte{nil, nil, nil},
want: nil,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := coalesce.ByteSlice(tt.in...)
if tt.want != nil && !reflect.DeepEqual(got, tt.want) {
t.Errorf("got %v; want %v", got, tt.want)
}
if tt.want == nil && got != nil {
t.Errorf("got %v; want %v", got, tt.want)
}
})
}
}
func ptrbyte(b byte) *byte {
return &b
}