Showing the changes to example_function_test.go (1 of 36 files). Show all
Unified · Split
| @@ -0,0 +1,57 @@ | |||
| 1 | // This Source Code Form is subject to the terms of the Mozilla Public | ||
| 2 | // License, v. 2.0. If a copy of the MPL was not distributed with this | ||
| 3 | // file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
| 4 | |||
| 5 | package rapid_test | ||
| 6 | |||
| 7 | import ( | ||
| 8 | "fmt" | ||
| 9 | "strconv" | ||
| 10 | "testing" | ||
| 11 | |||
| 12 | "github.com/flyingmutant/rapid" | ||
| 13 | ) | ||
| 14 | |||
| 15 | // ParseDate parses dates in the YYYY-MM-DD format. | ||
| 16 | func ParseDate(s string) (int, int, int, error) { | ||
| 17 | if len(s) != 10 { | ||
| 18 | return 0, 0, 0, fmt.Errorf("%q has wrong length: %v instead of 10", s, len(s)) | ||
| 19 | } | ||
| 20 | |||
| 21 | if s[4] != '-' || s[7] != '-' { | ||
| 22 | return 0, 0, 0, fmt.Errorf("'-' separators expected in %q", s) | ||
| 23 | } | ||
| 24 | |||
| 25 | y, err := strconv.Atoi(s[0:4]) | ||
| 26 | if err != nil { | ||
| 27 | return 0, 0, 0, fmt.Errorf("failed to parse year: %v", err) | ||
| 28 | } | ||
| 29 | |||
| 30 | m, err := strconv.Atoi(s[6:7]) | ||
| 31 | if err != nil { | ||
| 32 | return 0, 0, 0, fmt.Errorf("failed to parse month: %v", err) | ||
| 33 | } | ||
| 34 | |||
| 35 | d, err := strconv.Atoi(s[8:10]) | ||
| 36 | if err != nil { | ||
| 37 | return 0, 0, 0, fmt.Errorf("failed to parse day: %v", err) | ||
| 38 | } | ||
| 39 | |||
| 40 | return y, m, d, nil | ||
| 41 | } | ||
| 42 | |||
| 43 | // Prefix Example_parseDate with Test to make an actual (failing) test. | ||
| 44 | func Example_parseDate(t *testing.T) { | ||
| 45 | rapid.Check(t, func(t *rapid.T, y int, m int, d int) { | ||
| 46 | s := fmt.Sprintf("%04d-%02d-%02d", y, m, d) | ||
| 47 | |||
| 48 | y_, m_, d_, err := ParseDate(s) | ||
| 49 | if err != nil { | ||
| 50 | t.Fatalf("failed to parse date %q: %v", s, err) | ||
| 51 | } | ||
| 52 | |||
| 53 | if y_ != y || m_ != m || d_ != d { | ||
| 54 | t.Fatalf("got back wrong date: (%d, %d, %d)", y_, m_, d_) | ||
| 55 | } | ||
| 56 | }, rapid.IntsRange(0, 9999), rapid.IntsRange(1, 12), rapid.IntsRange(1, 31)) | ||
| 57 | } | ||