-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.go
More file actions
61 lines (55 loc) · 1.69 KB
/
Copy pathapp.go
File metadata and controls
61 lines (55 loc) · 1.69 KB
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
// Unit testing is an important part of writing So programs.
// The `so/testing` package provides the tools we need to write
// unit tests, and the `so test` command runs them.
// By convention, testing code lives in the `test` subdirectory of the
// package we are testing - that's where the `so test` command looks for it.
//
// Do not name the file with tests `main.go` - that file will be
// auto-generated when you run `so test` and will contain the `main`
// function that runs the tests.
//
// Do not use the `_test.go` suffix for test files.
package main
import (
"app"
"solod.dev/so/fmt"
"solod.dev/so/testing"
)
// A test is created by writing a function with a name
// beginning with `Test`.
func TestIntMinBasic(t *testing.T) {
ans := app.IntMin(2, -2)
if ans != -2 {
// t.Error reports a test failure but continue executing the test.
// t.Fatal reports a test failure and must be followed by a return
// statement to stop executing the test.
buf := fmt.NewBuffer(fmt.BufSize)
msg := fmt.Sprintf(buf, "IntMin(2, -2) = %d, want -2", ans)
t.Error(msg)
}
}
func TestIntMinTableDriven(t *testing.T) {
// Writing tests can be repetitive, so it's idiomatic to use
// a table-driven style, where test inputs and expected outputs
// are listed in a table and a single loop walks over them
// and performs the test logic.
type testCase struct {
a, b int
want int
}
var tests = []testCase{
{0, 1, 0},
{1, 0, 0},
{2, -2, -2},
{0, -1, -1},
{-1, 0, -1},
}
buf := fmt.NewBuffer(fmt.BufSize)
for _, tt := range tests {
ans := app.IntMin(tt.a, tt.b)
if ans != tt.want {
msg := fmt.Sprintf(buf, "IntMin(%d, %d) = %d, want %d", tt.a, tt.b, ans, tt.want)
t.Error(msg)
}
}
}