-
Notifications
You must be signed in to change notification settings - Fork 25
/
example_function_test.go
66 lines (52 loc) · 1.57 KB
/
example_function_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
// Copyright 2019 Gregory Petrosyan <gregory.petrosyan@gmail.com>
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
package rapid_test
import (
"fmt"
"strconv"
"testing"
"pgregory.net/rapid"
)
// ParseDate parses dates in the YYYY-MM-DD format.
func ParseDate(s string) (int, int, int, error) {
if len(s) != 10 {
return 0, 0, 0, fmt.Errorf("%q has wrong length: %v instead of 10", s, len(s))
}
if s[4] != '-' || s[7] != '-' {
return 0, 0, 0, fmt.Errorf("'-' separators expected in %q", s)
}
y, err := strconv.Atoi(s[0:4])
if err != nil {
return 0, 0, 0, fmt.Errorf("failed to parse year: %v", err)
}
m, err := strconv.Atoi(s[6:7])
if err != nil {
return 0, 0, 0, fmt.Errorf("failed to parse month: %v", err)
}
d, err := strconv.Atoi(s[8:10])
if err != nil {
return 0, 0, 0, fmt.Errorf("failed to parse day: %v", err)
}
return y, m, d, nil
}
func testParseDate(t *rapid.T) {
y := rapid.IntRange(0, 9999).Draw(t, "y")
m := rapid.IntRange(1, 12).Draw(t, "m")
d := rapid.IntRange(1, 31).Draw(t, "d")
s := fmt.Sprintf("%04d-%02d-%02d", y, m, d)
y_, m_, d_, err := ParseDate(s)
if err != nil {
t.Fatalf("failed to parse date %q: %v", s, err)
}
if y_ != y || m_ != m || d_ != d {
t.Fatalf("got back wrong date: (%d, %d, %d)", y_, m_, d_)
}
}
// Rename to TestParseDate(t *testing.T) to make an actual (failing) test.
func ExampleCheck_parseDate() {
var t *testing.T
rapid.Check(t, testParseDate)
}