From f6062e1cbfee252ba9c110568c21932f95560af5 Mon Sep 17 00:00:00 2001 From: Shulhan Date: Wed, 25 Aug 2021 03:04:18 +0700 Subject: 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. --- lib/reflect/reflect_example_test.go | 72 +++++++++++++++++++++++++++++++++++++ lib/reflect/reflect_test.go | 62 -------------------------------- 2 files changed, 72 insertions(+), 62 deletions(-) create mode 100644 lib/reflect/reflect_example_test.go delete mode 100644 lib/reflect/reflect_test.go 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 . 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: + // : 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 + // : v == nil is true, IsNil() is true + // *errors.errorString: v == nil is false, IsNil() is false + // : 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 . 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) - } - } -} -- cgit v1.3