...

Source file src/dario.cat/mergo/issue52_test.go

Documentation: dario.cat/mergo

     1  package mergo_test
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  	"time"
     7  
     8  	"dario.cat/mergo"
     9  )
    10  
    11  type structWithTime struct {
    12  	Birth time.Time
    13  }
    14  
    15  type timeTransfomer struct {
    16  	overwrite bool
    17  }
    18  
    19  func (t timeTransfomer) Transformer(typ reflect.Type) func(dst, src reflect.Value) error {
    20  	if typ == reflect.TypeOf(time.Time{}) {
    21  		return func(dst, src reflect.Value) error {
    22  			if dst.CanSet() {
    23  				if t.overwrite {
    24  					isZero := src.MethodByName("IsZero")
    25  
    26  					result := isZero.Call([]reflect.Value{})
    27  					if !result[0].Bool() {
    28  						dst.Set(src)
    29  					}
    30  				} else {
    31  					isZero := dst.MethodByName("IsZero")
    32  
    33  					result := isZero.Call([]reflect.Value{})
    34  					if result[0].Bool() {
    35  						dst.Set(src)
    36  					}
    37  				}
    38  			}
    39  			return nil
    40  		}
    41  	}
    42  	return nil
    43  }
    44  
    45  func TestOverwriteZeroSrcTime(t *testing.T) {
    46  	now := time.Now()
    47  	dst := structWithTime{now}
    48  	src := structWithTime{}
    49  
    50  	if err := mergo.MergeWithOverwrite(&dst, src); err != nil {
    51  		t.FailNow()
    52  	}
    53  
    54  	if !dst.Birth.IsZero() {
    55  		t.Errorf("dst should have been overwritten: dst.Birth(%v) != now(%v)", dst.Birth, now)
    56  	}
    57  }
    58  
    59  func TestOverwriteZeroSrcTimeWithTransformer(t *testing.T) {
    60  	now := time.Now()
    61  	dst := structWithTime{now}
    62  	src := structWithTime{}
    63  
    64  	if err := mergo.MergeWithOverwrite(&dst, src, mergo.WithTransformers(timeTransfomer{true})); err != nil {
    65  		t.FailNow()
    66  	}
    67  
    68  	if dst.Birth.IsZero() {
    69  		t.Errorf("dst should not have been overwritten: dst.Birth(%v) != now(%v)", dst.Birth, now)
    70  	}
    71  }
    72  
    73  func TestOverwriteZeroDstTime(t *testing.T) {
    74  	now := time.Now()
    75  	dst := structWithTime{}
    76  	src := structWithTime{now}
    77  
    78  	if err := mergo.MergeWithOverwrite(&dst, src); err != nil {
    79  		t.FailNow()
    80  	}
    81  
    82  	if dst.Birth.IsZero() {
    83  		t.Errorf("dst should have been overwritten: dst.Birth(%v) != zero(%v)", dst.Birth, time.Time{})
    84  	}
    85  }
    86  
    87  func TestZeroDstTime(t *testing.T) {
    88  	now := time.Now()
    89  	dst := structWithTime{}
    90  	src := structWithTime{now}
    91  
    92  	if err := mergo.Merge(&dst, src); err != nil {
    93  		t.FailNow()
    94  	}
    95  
    96  	if !dst.Birth.IsZero() {
    97  		t.Errorf("dst should not have been overwritten: dst.Birth(%v) != zero(%v)", dst.Birth, time.Time{})
    98  	}
    99  }
   100  
   101  func TestZeroDstTimeWithTransformer(t *testing.T) {
   102  	now := time.Now()
   103  	dst := structWithTime{}
   104  	src := structWithTime{now}
   105  
   106  	if err := mergo.Merge(&dst, src, mergo.WithTransformers(timeTransfomer{})); err != nil {
   107  		t.FailNow()
   108  	}
   109  
   110  	if dst.Birth.IsZero() {
   111  		t.Errorf("dst should have been overwritten: dst.Birth(%v) != now(%v)", dst.Birth, now)
   112  	}
   113  }
   114  

View as plain text