Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve tests #1

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 79 additions & 34 deletions deepcopy_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package deepcopy

import (
"fmt"
. "reflect"
"testing"
"testing/quick"
)

func ExampleAnything() {
func TestExampleAnything(t *testing.T) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an example test, so the method is named and implemented so that it shows up as an example in the godoc.
https://pkg.go.dev/github.com/barkimedes/go-deepcopy#pkg-examples

For more info on example tests: https://go.dev/blog/examples

tests := []interface{}{
`"Now cut that out!"`,
39,
Expand All @@ -27,41 +27,35 @@ func ExampleAnything() {

for _, expected := range tests {
actual := MustAnything(expected)
fmt.Println(actual)
}
// Output:
// "Now cut that out!"
// 39
// true
// false
// 2.14
// [Phil Harris Rochester van Jones Mary Livingstone Dennis Day]
// [Jell-O Grape-Nuts]
if !DeepEqual(expected, actual) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changing this would make the example not work anymore, so we can drop this suggestion.

t.Errorf("want '%v', got '%v'", expected, actual)
}
}
}

type Foo struct {
Foo *Foo
Bar int
}

func ExampleMap() {
func TestExampleMap(t *testing.T) {
Copy link
Owner

@barkimedes barkimedes May 14, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this example doesn't actually show up in the docs (I got rid of copying map as a public interface), we can rename this one, just take out "Example"

(Make it TestMap)

x := map[string]*Foo{
"foo": &Foo{Bar: 1},
"bar": &Foo{Bar: 2},
"foo": {Bar: 1},
"bar": {Bar: 2},
}

y := MustAnything(x).(map[string]*Foo)
for _, k := range []string{"foo", "bar"} { // to ensure consistent order
fmt.Printf("x[\"%v\"] = y[\"%v\"]: %v\n", k, k, x[k] == y[k])
fmt.Printf("x[\"%v\"].Foo = y[\"%v\"].Foo: %v\n", k, k, x[k].Foo == y[k].Foo)
fmt.Printf("x[\"%v\"].Bar = y[\"%v\"].Bar: %v\n", k, k, x[k].Bar == y[k].Bar)
}
// Output:
// x["foo"] = y["foo"]: false
// x["foo"].Foo = y["foo"].Foo: false
// x["foo"].Bar = y["foo"].Bar: true
// x["bar"] = y["bar"]: false
// x["bar"].Foo = y["bar"].Foo: false
// x["bar"].Bar = y["bar"].Bar: true
if x[k] == y[k] {
t.Errorf("x[\"%v\"] == y[\"%v\"]: want '%v' got '%v'", k, k, false, x[k] == y[k])
}
if x[k].Foo == y[k].Foo {
t.Errorf("x[\"%v\"].Foo == y[\"%v\"].Foo: want '%v' got '%v'", k, k, false, x[k].Foo == y[k].Foo)
}
if x[k].Bar != y[k].Bar {
t.Errorf("x[\"%v\"].Bar == y[\"%v\"].Bar: want '%v' got '%v'", k, k, true, x[k].Bar == y[k].Bar)
}
}
}

func TestInterface(t *testing.T) {
Expand All @@ -77,19 +71,22 @@ func TestInterface(t *testing.T) {
}
}

func ExampleAvoidInfiniteLoops() {
func TestExampleAvoidInfiniteLoops(t *testing.T) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, can just make it TestAvoidInfiniteLoops

x := &Foo{
Bar: 4,
}
x.Foo = x
y := MustAnything(x).(*Foo)
fmt.Printf("x == y: %v\n", x == y)
fmt.Printf("x == x.Foo: %v\n", x == x.Foo)
fmt.Printf("y == y.Foo: %v\n", y == y.Foo)
// Output:
// x == y: false
// x == x.Foo: true
// y == y.Foo: true

if x == y {
t.Errorf("x == y: want '%v' got '%v'", false, x == y)
}
if x != x.Foo {
t.Errorf("x == x.Foo: want '%v' got '%v'", true, x == x.Foo)
}
if y != y.Foo {
t.Errorf("y == y.Foo: want '%v' got '%v'", true, y == y.Foo)
}
}

func TestUnsupportedKind(t *testing.T) {
Expand Down Expand Up @@ -152,3 +149,51 @@ func TestMismatchedTypesFail(t *testing.T) {
}
}
}

type basicTypes struct {
B bool
I int
I8 int8
I16 int16
I32 int32
I64 int64
U uint
U8 uint8
U16 uint16
U32 uint32
U64 uint64
Uptr uintptr
F32 float32
F64 float64
C64 complex64
C128 complex128
}

type supportedTypesExceptPointers struct {
St basicTypes
A1 [4]string
A2 [4][4][4][4][4]string
A3 [4][]int
M1 map[string]string
M2 map[int][]string
M3 map[string][4]string
M4 map[complex128][][][][]complex64
M5 map[string]basicTypes
M6 map[basicTypes]int
Sl1 []string
Sl2 [][][][]complex128
Sl3 []basicTypes
Sl4 []byte
}

func TestMustAnythingSupportedTypesExceptPointers(t *testing.T) {
f := func(x supportedTypesExceptPointers) bool {
y := MustAnything(x)
return DeepEqual(x, y)
}
if err := quick.Check(f, &quick.Config{
MaxCount: 1000,
}); err != nil {
t.Error(err)
}
}