testing project
This commit is contained in:
parent
d7f0e206ca
commit
bafba8a7b6
5
4-testing/go.mod
Normal file
5
4-testing/go.mod
Normal file
@ -0,0 +1,5 @@
|
||||
module example/user/testing
|
||||
|
||||
go 1.17
|
||||
|
||||
require github.com/google/go-cmp v0.5.6
|
4
4-testing/go.sum
Normal file
4
4-testing/go.sum
Normal file
@ -0,0 +1,4 @@
|
||||
github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ=
|
||||
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
|
||||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
14
4-testing/hello.go
Normal file
14
4-testing/hello.go
Normal file
@ -0,0 +1,14 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"example/user/testing/importpackage"
|
||||
|
||||
"github.com/google/go-cmp/cmp"
|
||||
)
|
||||
|
||||
func main() {
|
||||
fmt.Println(importpackage.ReverseRunes("!oG ,olleH"))
|
||||
fmt.Println(cmp.Diff("Hello World", "Hello Go"))
|
||||
}
|
12
4-testing/importpackage/reverse.go
Normal file
12
4-testing/importpackage/reverse.go
Normal file
@ -0,0 +1,12 @@
|
||||
// Package importpackage implements additional functions to manipulate UTF-8
|
||||
// encoded strings, beyond what is provided in the standard "strings" package.
|
||||
package importpackage
|
||||
|
||||
// ReverseRunes returns its argument string reversed rune-wise left to right.
|
||||
func ReverseRunes(s string) string {
|
||||
r := []rune(s)
|
||||
for i, j := 0, len(r)-1; i < len(r)/2; i, j = i+1, j-1 {
|
||||
r[i], r[j] = r[j], r[i]
|
||||
}
|
||||
return string(r)
|
||||
}
|
19
4-testing/importpackage/reverse_test.go
Normal file
19
4-testing/importpackage/reverse_test.go
Normal file
@ -0,0 +1,19 @@
|
||||
package importpackage
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestReverseRunes(t *testing.T) {
|
||||
cases := []struct {
|
||||
in, want string
|
||||
}{
|
||||
{"Hello, world", "dlrow ,olleH"},
|
||||
{"Hello, 世界", "界世 ,olleH"},
|
||||
{"", ""},
|
||||
}
|
||||
for _, c := range cases {
|
||||
got := ReverseRunes(c.in)
|
||||
if got != c.want {
|
||||
t.Errorf("ReverseRunes(%q) == %q, want %q", c.in, got, c.want)
|
||||
}
|
||||
}
|
||||
}
|
36
4-testing/readme.md
Normal file
36
4-testing/readme.md
Normal file
@ -0,0 +1,36 @@
|
||||
# Testing
|
||||
Go has a lightweight test framework composed of the go test command and the testing package.
|
||||
|
||||
Write a test by creating a file with a name ending in `_test.go` that contains functions named TestXXX with signature `func (t *testing.T)`. The test framework runs each such function; if the function calls a failure function such as t.Error or t.Fail, the test is considered to have failed.
|
||||
|
||||
```bash
|
||||
cp -r 3-importremote 4-testing
|
||||
# change importremote to testing
|
||||
nano go.mod
|
||||
nano hello.go
|
||||
# create test for reverse
|
||||
cd importpackage
|
||||
nano reverse_test.go
|
||||
> package morestrings
|
||||
>
|
||||
> import "testing"
|
||||
>
|
||||
> func TestReverseRunes(t *testing.T) {
|
||||
> cases := []struct {
|
||||
> in, want string
|
||||
> }{
|
||||
> {"Hello, world", "dlrow ,olleH"},
|
||||
> {"Hello, 世界", "界世 ,olleH"},
|
||||
> {"", ""},
|
||||
> }
|
||||
> for _, c := range cases {
|
||||
> got := ReverseRunes(c.in)
|
||||
> if got != c.want {
|
||||
> t.Errorf("ReverseRunes(%q) == %q, want %q", c.in, got, c.want)
|
||||
> }
|
||||
> }
|
||||
> }
|
||||
go build
|
||||
# run test code
|
||||
go test
|
||||
```
|
BIN
4-testing/testing
Executable file
BIN
4-testing/testing
Executable file
Binary file not shown.
Loading…
Reference in New Issue
Block a user