summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorShulhan <ms@kilabit.info>2021-08-25 03:04:18 +0700
committerShulhan <ms@kilabit.info>2021-08-25 03:04:18 +0700
commitf6062e1cbfee252ba9c110568c21932f95560af5 (patch)
tree5eadda07ceaea9d83c93903fbfdda0ef30e544b3
parent6bff1e58db336276702db31cbbb0cff0ddc89cd7 (diff)
downloadpakakeh.go-f6062e1cbfee252ba9c110568c21932f95560af5.tar.xz
lib/reflect: make the IsNil tests to become an example
In this way we do test and provide an example at the same time. While at it, add another test cases for boolean, initialized slice, map, and errors.
-rw-r--r--lib/reflect/reflect_example_test.go72
-rw-r--r--lib/reflect/reflect_test.go62
2 files changed, 72 insertions, 62 deletions
diff --git a/lib/reflect/reflect_example_test.go b/lib/reflect/reflect_example_test.go
new file mode 100644
index 00000000..41df3727
--- /dev/null
+++ b/lib/reflect/reflect_example_test.go
@@ -0,0 +1,72 @@
+// Copyright 2020, Shulhan <ms@kilabit.info>. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package reflect
+
+import (
+ "errors"
+ "fmt"
+ "net/http"
+)
+
+type F func()
+
+type T struct{}
+
+func (t *T) J() bool {
+ return true
+}
+
+func ExampleIsNil() {
+ var (
+ aBoolean bool
+ aChannel chan int
+ aFunction F
+ aMap map[int]int
+ aPtr *T
+ aSlice []int
+ anInt int
+ emptyError error
+ fs http.FileSystem
+ )
+
+ cases := []struct {
+ v interface{}
+ }{
+ {}, // Unitialized.
+ {v: aBoolean},
+ {v: aChannel}, // Uninitialized channel.
+ {v: aFunction}, // Empty func type.
+ {v: aMap}, // Unitialized map.
+ {v: make(map[int]int)}, // Initialized map.
+ {v: aPtr}, // Uninitialized pointer to struct.
+ {v: &T{}}, // Initialized pointer to struct.
+ {v: aSlice}, // Unitialized slice.
+ {v: make([]int, 0)}, // Initialized slice.
+ {v: anInt},
+ {v: emptyError},
+ {v: errors.New("e")}, // Initialized error
+ {v: fs}, // Unitialized interface type to interface{}
+ }
+
+ for _, c := range cases {
+ fmt.Printf("%19T: v == nil is %5t, IsNil() is %5t\n", c.v, c.v == nil, IsNil(c.v))
+ }
+
+ //Output:
+ // <nil>: v == nil is true, IsNil() is true
+ // bool: v == nil is false, IsNil() is false
+ // chan int: v == nil is false, IsNil() is true
+ // reflect.F: v == nil is false, IsNil() is true
+ // map[int]int: v == nil is false, IsNil() is true
+ // map[int]int: v == nil is false, IsNil() is false
+ // *reflect.T: v == nil is false, IsNil() is true
+ // *reflect.T: v == nil is false, IsNil() is false
+ // []int: v == nil is false, IsNil() is true
+ // []int: v == nil is false, IsNil() is false
+ // int: v == nil is false, IsNil() is false
+ // <nil>: v == nil is true, IsNil() is true
+ // *errors.errorString: v == nil is false, IsNil() is false
+ // <nil>: v == nil is true, IsNil() is true
+}
diff --git a/lib/reflect/reflect_test.go b/lib/reflect/reflect_test.go
deleted file mode 100644
index 17a41127..00000000
--- a/lib/reflect/reflect_test.go
+++ /dev/null
@@ -1,62 +0,0 @@
-// Copyright 2020, Shulhan <ms@kilabit.info>. All rights reserved.
-// Use of this source code is governed by a BSD-style
-// license that can be found in the LICENSE file.
-
-package reflect
-
-import (
- "net/http"
- "testing"
-)
-
-type F func()
-
-type T struct{}
-
-func (t *T) J() bool {
- return true
-}
-
-func TestIsNil(t *testing.T) {
- var (
- aChannel chan int
- aFunction F
- aMap map[int]int
- aPtr *T
- aSlice []int
- fs http.FileSystem
- )
-
- cases := []struct {
- v interface{}
- exp bool
- }{{
- v: true,
- }, {
- v: aChannel,
- exp: true,
- }, {
- v: aFunction,
- exp: true,
- }, {
- v: aMap,
- exp: true,
- }, {
- v: aPtr,
- exp: true,
- }, {
- v: aSlice,
- exp: true,
- }, {
- v: fs,
- exp: true,
- }}
-
- for _, c := range cases {
- got := IsNil(c.v)
- if c.exp != got {
- t.Errorf("Expecting %v, got %v on %v(%T)", c.exp, got,
- c.v, c.v)
- }
- }
-}