forked from johannesboyne/gofakes3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util_test.go
64 lines (58 loc) · 1.44 KB
/
util_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
package gofakes3
import (
"strings"
"testing"
)
func TestParseClampedIntValid(t *testing.T) {
for _, tc := range []struct {
in string
dflt, min, max int64
out int64
}{
{in: "", dflt: 1, min: 0, max: 1, out: 1},
{in: "", dflt: 2, min: 0, max: 1, out: 1},
{in: "1", dflt: 2, min: 0, max: 100, out: 1},
{in: "1", dflt: 0, min: 2, max: 100, out: 2},
{in: "1000", dflt: 0, min: 2, max: 100, out: 100},
} {
t.Run("", func(t *testing.T) {
result, err := parseClampedInt(tc.in, tc.dflt, tc.min, tc.max)
if err != nil {
t.Fatal(err)
}
if result != tc.out {
t.Fatal(result, "!=", tc.out)
}
})
}
}
func TestReadAll(t *testing.T) {
t.Run("simple-read", func(t *testing.T) {
tt := TT{t}
b, err := ReadAll(strings.NewReader("test"), 4)
tt.OK(err)
if string(b) != "test" {
t.Fatal(string(b), "!=", "test")
}
})
t.Run("empty-input", func(t *testing.T) {
tt := TT{t}
b, err := ReadAll(strings.NewReader(""), 0)
tt.OK(err)
if string(b) != "" {
t.Fatal(string(b), "!=", "")
}
})
t.Run("size-too-large", func(t *testing.T) {
_, err := ReadAll(strings.NewReader("test"), 5)
if !HasErrorCode(err, ErrIncompleteBody) {
t.Fatal("expected ErrIncompleteBody, found", err)
}
})
t.Run("size-too-small", func(t *testing.T) {
_, err := ReadAll(strings.NewReader("test"), 3)
if !HasErrorCode(err, ErrIncompleteBody) {
t.Fatal("expected ErrIncompleteBody, found", err)
}
})
}