github.com/hedzr/evendeep@v0.4.8/00_testcase_for_test.go (about)

     1  package evendeep
     2  
     3  import (
     4  	"fmt"
     5  	"reflect"
     6  	"testing"
     7  
     8  	"github.com/hedzr/log"
     9  
    10  	"github.com/hedzr/evendeep/dbglog"
    11  	"github.com/hedzr/evendeep/diff"
    12  	"github.com/hedzr/evendeep/flags/cms"
    13  	"github.com/hedzr/evendeep/internal/tool"
    14  	"github.com/hedzr/evendeep/typ"
    15  
    16  	"gopkg.in/hedzr/errors.v3"
    17  )
    18  
    19  func HelperAssertYes(t *testing.T, b bool, expect, got typ.Any) { //nolint:thelper
    20  	if !b {
    21  		t.Fatalf("expecting %v but got %v", expect, got)
    22  	}
    23  }
    24  
    25  func TestNewForTest(t *testing.T) {
    26  	copier := New( // nolint:ineffassign
    27  		WithValueConverters(&toDurationConverter{}),
    28  		WithValueCopiers(&toDurationConverter{}),
    29  
    30  		WithCloneStyle(),
    31  		WithCopyStyle(),
    32  
    33  		WithCopyStrategyOpt,
    34  		WithMergeStrategyOpt,
    35  		WithStrategiesReset(),
    36  		WithStrategies(cms.SliceMerge, cms.MapMerge),
    37  
    38  		WithAutoExpandStructOpt,
    39  		WithAutoNewForStructFieldOpt,
    40  		WithCopyUnexportedFieldOpt,
    41  		WithCopyFunctionResultToTargetOpt,
    42  		WithPassSourceToTargetFunctionOpt,
    43  
    44  		WithSyncAdvancingOpt,
    45  
    46  		WithTryApplyConverterAtFirstOpt,
    47  		WithByNameStrategyOpt,
    48  		WithByOrdinalStrategyOpt,
    49  
    50  		WithIgnoreNamesReset(),
    51  		WithIgnoreNames("Bugs*", "Test*"),
    52  
    53  		WithStructTagName("copy"),
    54  
    55  		WithoutPanic(),
    56  
    57  		WithStringMarshaller(nil),
    58  	)
    59  
    60  	var a = 1
    61  	var b int
    62  	if err := copier.CopyTo(a, &b); err != nil {
    63  		t.Error("bad")
    64  	}
    65  }
    66  
    67  // NewForTest creates a new copier with most common options.
    68  func NewForTest() DeepCopier {
    69  	var copier DeepCopier
    70  
    71  	lazyInitRoutines()
    72  	var c1 = newCopier()
    73  	WithStrategies(cms.SliceMerge, cms.MapMerge)(c1)
    74  	if c1.flags.IsAnyFlagsOK(cms.ByOrdinal, cms.SliceMerge, cms.MapMerge, cms.OmitIfEmpty, cms.Default) == false {
    75  		log.Panicf("except flag set with optional values but not matched, 1")
    76  	}
    77  	c1 = newDeepCopier()
    78  	WithStrategies(cms.SliceCopyAppend, cms.MapCopy)(c1)
    79  	if c1.flags.IsAnyFlagsOK(cms.ByOrdinal, cms.SliceCopyAppend, cms.MapCopy, cms.OmitIfEmpty, cms.Default) == false {
    80  		log.Panicf("except flag set with optional values but not matched, 2")
    81  	}
    82  	c1 = newCloner()
    83  	WithStrategies(cms.SliceCopy)(c1)
    84  	if c1.flags.IsAnyFlagsOK(cms.ByOrdinal, cms.SliceCopy, cms.MapCopy, cms.OmitIfEmpty, cms.Default) == false {
    85  		log.Panicf("except flag set with optional values but not matched, 3")
    86  	}
    87  
    88  	copier = NewFlatDeepCopier(
    89  		WithStrategies(cms.SliceMerge, cms.MapMerge),
    90  		WithValueConverters(&toDurationConverter{}),
    91  		WithValueCopiers(&toDurationConverter{}),
    92  		WithCloneStyle(),
    93  		WithCopyStyle(),
    94  		WithAutoExpandStructOpt,
    95  		WithCopyStrategyOpt,
    96  		WithStrategiesReset(),
    97  		WithMergeStrategyOpt,
    98  		WithCopyUnexportedField(true),
    99  		WithCopyFunctionResultToTarget(true),
   100  		WithIgnoreNamesReset(),
   101  		WithIgnoreNames("Bugs*", "Test*"),
   102  	)
   103  
   104  	return copier
   105  }
   106  
   107  //
   108  
   109  //
   110  
   111  //
   112  
   113  //
   114  
   115  //
   116  
   117  //
   118  
   119  // Verifier _
   120  type Verifier func(src, dst, expect typ.Any, e error) (err error)
   121  
   122  // TestCase _
   123  type TestCase struct {
   124  	Description string  // description of what test is checking
   125  	Src, Dst    typ.Any //
   126  	Expect      typ.Any // expected output
   127  	Opts        []Opt
   128  	Verifier    Verifier
   129  }
   130  
   131  // NewTestCases _
   132  func NewTestCases(c ...TestCase) []TestCase {
   133  	return c
   134  }
   135  
   136  // NewTestCase _
   137  func NewTestCase(
   138  	description string, // description of what test is checking
   139  	src, dst typ.Any, //
   140  	expect typ.Any, // expected output
   141  	opts []Opt,
   142  	verifier Verifier,
   143  ) TestCase {
   144  	return TestCase{
   145  		Description: description, Src: src, Dst: dst,
   146  		Expect: expect, Opts: opts, Verifier: verifier,
   147  	}
   148  }
   149  
   150  // ExtrasOpt for TestCase
   151  type ExtrasOpt func(tc *TestCase)
   152  
   153  // RunTestCasesWith _
   154  func RunTestCasesWith(tc *TestCase) (desc string, helperSubtest func(t *testing.T)) {
   155  	desc = tc.Description
   156  	helperSubtest = func(t *testing.T) { //nolint:thelper
   157  		c := NewFlatDeepCopier(tc.Opts...)
   158  
   159  		err := c.CopyTo(&tc.Src, &tc.Dst)
   160  
   161  		verifier := tc.Verifier
   162  		if verifier == nil {
   163  			verifier = DoTestCasesVerifier(t)
   164  		}
   165  
   166  		// t.Logf("\nexpect: %+v\n   got: %+v.", tc.expect, tc.dst)
   167  		if err = verifier(tc.Src, tc.Dst, tc.Expect, err); err == nil {
   168  			return
   169  		}
   170  
   171  		t.Fatalf("%s FAILED, %+v", tc.Description, err)
   172  	}
   173  	return
   174  }
   175  
   176  // DefaultDeepCopyTestRunner _
   177  func DefaultDeepCopyTestRunner(ix int, tc TestCase, opts ...Opt) func(t *testing.T) {
   178  	return func(t *testing.T) {
   179  		c := NewFlatDeepCopier(append(opts, tc.Opts...)...)
   180  
   181  		dbglog.Log("- Case %3d: %v", ix, tc.Description)
   182  		err := c.CopyTo(&tc.Src, &tc.Dst)
   183  
   184  		verifier := tc.Verifier
   185  		if verifier == nil {
   186  			verifier = DoTestCasesVerifier(t)
   187  		}
   188  
   189  		// t.Logf("\nexpect: %+v\n   got: %+v.", tc.expect, tc.dst)
   190  		if err = verifier(tc.Src, tc.Dst, tc.Expect, err); err == nil {
   191  			log.Printf("%3d. test passed", ix)
   192  			return
   193  		}
   194  
   195  		log.Errorf("%3d. Error: %v", ix, err)
   196  		t.Fatalf("%3d. %s FAILED, %+v", ix, tc.Description, err)
   197  	}
   198  }
   199  
   200  // runTestCases _
   201  func runTestCases(t *testing.T, cases ...TestCase) {
   202  	for ix, tc := range cases {
   203  		if !t.Run(fmt.Sprintf("%3d. %s", ix, tc.Description),
   204  			DefaultDeepCopyTestRunner(ix, tc)) {
   205  			break
   206  		}
   207  	}
   208  }
   209  
   210  // runTestCasesWithOpts _
   211  func runTestCasesWithOpts(t *testing.T, cases []TestCase, opts ...Opt) {
   212  	for ix, tc := range cases {
   213  		if !t.Run(fmt.Sprintf("%3d. %s", ix, tc.Description), DefaultDeepCopyTestRunner(ix, tc, opts...)) {
   214  			break
   215  		}
   216  	}
   217  }
   218  
   219  func DoTestCasesVerifier(t *testing.T) Verifier {
   220  	return func(src, dst, expect typ.Any, e error) (err error) {
   221  		a, b := reflect.ValueOf(dst), reflect.ValueOf(expect)
   222  		aa, _ := tool.Rdecode(a)
   223  		bb, _ := tool.Rdecode(b)
   224  		av, bv := aa.Interface(), bb.Interface()
   225  		log.Printf("\nexpect: %+v (%v | %v)\n   got: %+v (%v | %v)\n   err: %v",
   226  			bv, tool.Typfmtv(&bb), aa.Type(), av, tool.Typfmtv(&aa), bb.Type(), e)
   227  
   228  		dif, equal := diff.New(expect, dst,
   229  			diff.WithSliceOrderedComparison(false),
   230  			diff.WithStripPointerAtFirst(true),
   231  		)
   232  		if !equal {
   233  			fmt.Println(dif)
   234  			err = errors.New("diff.PrettyDiff identified its not equal:\ndifferent:\n%v", dif).WithErrors(e)
   235  			return
   236  		}
   237  
   238  		// if !reflect.DeepEqual(av, bv) {
   239  		//	err = errors.New("reflect.DeepEqual identified its not equal")
   240  		// }
   241  		err = e
   242  		return
   243  	}
   244  }