diff --git a/4-testing/go.mod b/4-testing/go.mod new file mode 100644 index 0000000..7dfef65 --- /dev/null +++ b/4-testing/go.mod @@ -0,0 +1,5 @@ +module example/user/testing + +go 1.17 + +require github.com/google/go-cmp v0.5.6 diff --git a/4-testing/go.sum b/4-testing/go.sum new file mode 100644 index 0000000..03e1a9c --- /dev/null +++ b/4-testing/go.sum @@ -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= diff --git a/4-testing/hello.go b/4-testing/hello.go new file mode 100644 index 0000000..c2db773 --- /dev/null +++ b/4-testing/hello.go @@ -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")) +} \ No newline at end of file diff --git a/4-testing/importpackage/reverse.go b/4-testing/importpackage/reverse.go new file mode 100644 index 0000000..743724a --- /dev/null +++ b/4-testing/importpackage/reverse.go @@ -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) +} \ No newline at end of file diff --git a/4-testing/importpackage/reverse_test.go b/4-testing/importpackage/reverse_test.go new file mode 100644 index 0000000..7d4edd4 --- /dev/null +++ b/4-testing/importpackage/reverse_test.go @@ -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) + } + } +} \ No newline at end of file diff --git a/4-testing/readme.md b/4-testing/readme.md new file mode 100644 index 0000000..4f50beb --- /dev/null +++ b/4-testing/readme.md @@ -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 +``` \ No newline at end of file diff --git a/4-testing/testing b/4-testing/testing new file mode 100755 index 0000000..ac40159 Binary files /dev/null and b/4-testing/testing differ