From be9b84288c3ceabe6d00a73ae6c01e7896b5976d Mon Sep 17 00:00:00 2001
From: Eli Bendersky
Date: Thu, 12 Sep 2019 11:30:07 -0700
Subject: [PATCH] Add a new example: testing
---
examples.txt | 1 +
examples/testing/main_test.go | 23 +--
examples/testing/main_test.sh | 18 ++
examples/testing/testing.hash | 2 +
public/command-line-arguments | 2 +-
public/index.html | 2 +
public/temporary-files-and-directories | 4 +-
public/testing | 239 +++++++++++++++++++++++++
8 files changed, 277 insertions(+), 14 deletions(-)
create mode 100644 examples/testing/main_test.sh
create mode 100644 examples/testing/testing.hash
create mode 100644 public/testing
diff --git a/examples.txt b/examples.txt
index 880802f..dd3c157 100644
--- a/examples.txt
+++ b/examples.txt
@@ -61,6 +61,7 @@ Line Filters
File Paths
Directories
Temporary Files and Directories
+Testing
Command-Line Arguments
Command-Line Flags
Command-Line Subcommands
diff --git a/examples/testing/main_test.go b/examples/testing/main_test.go
index 2fc5ecc..3bcdd6e 100644
--- a/examples/testing/main_test.go
+++ b/examples/testing/main_test.go
@@ -3,7 +3,7 @@
// provides the tools we need to write unit tests
// and the `go test` command runs tests.
-// For the same of demonstration, this code is in package
+// For the sake of demonstration, this code is in package
// `main`, but it could be any package. Testing code
// typically lives in the same package as the code it tests.
package main
@@ -29,12 +29,12 @@ func IntMin(a, b int) int {
// A test is created by writing a function with a name
// beginning with `Test`.
func TestIntMinBasic(t *testing.T) {
- result := IntMin(2, -2)
- if result != -2 {
+ ans := IntMin(2, -2)
+ if ans != -2 {
// `t.Error*` will report test failures but continue
// executing the test. `t.Fail*` will report test
// failures and stop the test immediately.
- t.Errorf("IntMin(2, -2) = %d; want -2", result)
+ t.Errorf("IntMin(2, -2) = %d; want -2", ans)
}
}
@@ -44,8 +44,8 @@ func TestIntMinBasic(t *testing.T) {
// walks over them and performs the test logic.
func TestIntMinTableDriven(t *testing.T) {
var tests = []struct {
- a, b int
- expected int
+ a, b int
+ want int
}{
{0, 1, 0},
{1, 0, 0},
@@ -56,12 +56,13 @@ func TestIntMinTableDriven(t *testing.T) {
for _, tt := range tests {
// t.Run enables running "subtests", one for each
- // table entry. These will be reported separately
+ // table entry. These are shown separately
// when executing `go test -v`.
- t.Run(fmt.Sprintf("%d,%d", tt.a, tt.b), func(t *testing.T) {
- result := IntMin(tt.a, tt.b)
- if result != tt.expected {
- t.Errorf("got %d, want %d", result, tt.expected)
+ testname := fmt.Sprintf("%d,%d", tt.a, tt.b)
+ t.Run(testname, func(t *testing.T) {
+ ans := IntMin(tt.a, tt.b)
+ if ans != tt.want {
+ t.Errorf("got %d, want %d", ans, tt.want)
}
})
}
diff --git a/examples/testing/main_test.sh b/examples/testing/main_test.sh
new file mode 100644
index 0000000..58e0615
--- /dev/null
+++ b/examples/testing/main_test.sh
@@ -0,0 +1,18 @@
+# Run all tests in the current project in verbose mode.
+$ go test -v
+== RUN TestIntMinBasic
+--- PASS: TestIntMinBasic (0.00s)
+=== RUN TestIntMinTableDriven
+=== RUN TestIntMinTableDriven/0,1
+=== RUN TestIntMinTableDriven/1,0
+=== RUN TestIntMinTableDriven/2,-2
+=== RUN TestIntMinTableDriven/0,-1
+=== RUN TestIntMinTableDriven/-1,0
+--- PASS: TestIntMinTableDriven (0.00s)
+ --- PASS: TestIntMinTableDriven/0,1 (0.00s)
+ --- PASS: TestIntMinTableDriven/1,0 (0.00s)
+ --- PASS: TestIntMinTableDriven/2,-2 (0.00s)
+ --- PASS: TestIntMinTableDriven/0,-1 (0.00s)
+ --- PASS: TestIntMinTableDriven/-1,0 (0.00s)
+PASS
+ok examples/testing 0.023s
diff --git a/examples/testing/testing.hash b/examples/testing/testing.hash
new file mode 100644
index 0000000..317487f
--- /dev/null
+++ b/examples/testing/testing.hash
@@ -0,0 +1,2 @@
+8f00c5178a33be2e92a853f14bfc3fbf0919cd97
+fyy7h1adGWr
diff --git a/public/command-line-arguments b/public/command-line-arguments
index a2aa9ff..593e93f 100644
--- a/public/command-line-arguments
+++ b/public/command-line-arguments
@@ -9,7 +9,7 @@
onkeydown = (e) => {
if (e.key == "ArrowLeft") {
- window.location.href = 'temporary-files-and-directories';
+ window.location.href = 'testing';
}
diff --git a/public/index.html b/public/index.html
index 74ff261..94549ee 100644
--- a/public/index.html
+++ b/public/index.html
@@ -149,6 +149,8 @@
Unit testing is an important part of writing
+principled Go programs. The testing package
+provides the tools we need to write unit tests
+and the go test command runs tests.
+
+
+
+
+
+
+
+
+
+
+
For the sake of demonstration, this code is in package
+main, but it could be any package. Testing code
+typically lives in the same package as the code it tests.
+
+
+
+
+
packagemain
+
+
+
+
+
+
+
+
+
+
+
+
import(
+ "fmt"
+ "testing"
+)
+
+
+
+
+
+
+
+
We’ll be testing this simple implementation of an
+integer minimum. Typically, the code we’re testing
+would be in a source file named something like
+intutils.go, and the test file for it would then
+be named intutils_test.go.
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.