github.com/10XDev/rclone@v1.52.3-0.20200626220027-16af9ab76b2a/lib/structs/structs_test.go (about) 1 package structs 2 3 import ( 4 "fmt" 5 "net/http" 6 "testing" 7 8 "github.com/stretchr/testify/assert" 9 ) 10 11 // returns the "%p" representation of the thing passed in 12 func ptr(p interface{}) string { 13 return fmt.Sprintf("%p", p) 14 } 15 16 func TestSetDefaults(t *testing.T) { 17 old := http.DefaultTransport.(*http.Transport) 18 newT := new(http.Transport) 19 SetDefaults(newT, old) 20 // Can't use assert.Equal or reflect.DeepEqual for this as it has functions in 21 // Check functions by comparing the "%p" representations of them 22 assert.Equal(t, ptr(old.Proxy), ptr(newT.Proxy), "when checking .Proxy") 23 assert.Equal(t, ptr(old.DialContext), ptr(newT.DialContext), "when checking .DialContext") 24 // Check the other public fields 25 assert.Equal(t, ptr(old.Dial), ptr(newT.Dial), "when checking .Dial") 26 assert.Equal(t, ptr(old.DialTLS), ptr(newT.DialTLS), "when checking .DialTLS") 27 assert.Equal(t, old.TLSClientConfig, newT.TLSClientConfig, "when checking .TLSClientConfig") 28 assert.Equal(t, old.TLSHandshakeTimeout, newT.TLSHandshakeTimeout, "when checking .TLSHandshakeTimeout") 29 assert.Equal(t, old.DisableKeepAlives, newT.DisableKeepAlives, "when checking .DisableKeepAlives") 30 assert.Equal(t, old.DisableCompression, newT.DisableCompression, "when checking .DisableCompression") 31 assert.Equal(t, old.MaxIdleConns, newT.MaxIdleConns, "when checking .MaxIdleConns") 32 assert.Equal(t, old.MaxIdleConnsPerHost, newT.MaxIdleConnsPerHost, "when checking .MaxIdleConnsPerHost") 33 assert.Equal(t, old.IdleConnTimeout, newT.IdleConnTimeout, "when checking .IdleConnTimeout") 34 assert.Equal(t, old.ResponseHeaderTimeout, newT.ResponseHeaderTimeout, "when checking .ResponseHeaderTimeout") 35 assert.Equal(t, old.ExpectContinueTimeout, newT.ExpectContinueTimeout, "when checking .ExpectContinueTimeout") 36 assert.Equal(t, old.TLSNextProto, newT.TLSNextProto, "when checking .TLSNextProto") 37 assert.Equal(t, old.MaxResponseHeaderBytes, newT.MaxResponseHeaderBytes, "when checking .MaxResponseHeaderBytes") 38 } 39 40 type aType struct { 41 Matching string 42 OnlyA string 43 MatchingInt int 44 DifferentType string 45 } 46 47 type bType struct { 48 Matching string 49 OnlyB string 50 MatchingInt int 51 DifferentType int 52 Unused string 53 } 54 55 func TestSetFrom(t *testing.T) { 56 a := aType{ 57 Matching: "a", 58 OnlyA: "onlyA", 59 MatchingInt: 1, 60 DifferentType: "suprise", 61 } 62 63 b := bType{ 64 Matching: "b", 65 OnlyB: "onlyB", 66 MatchingInt: 2, 67 DifferentType: 7, 68 Unused: "Ha", 69 } 70 bBefore := b 71 72 SetFrom(&a, &b) 73 74 assert.Equal(t, aType{ 75 Matching: "b", 76 OnlyA: "onlyA", 77 MatchingInt: 2, 78 DifferentType: "suprise", 79 }, a) 80 81 assert.Equal(t, bBefore, b) 82 } 83 84 func TestSetFromReversed(t *testing.T) { 85 a := aType{ 86 Matching: "a", 87 OnlyA: "onlyA", 88 MatchingInt: 1, 89 DifferentType: "suprise", 90 } 91 aBefore := a 92 93 b := bType{ 94 Matching: "b", 95 OnlyB: "onlyB", 96 MatchingInt: 2, 97 DifferentType: 7, 98 Unused: "Ha", 99 } 100 101 SetFrom(&b, &a) 102 103 assert.Equal(t, bType{ 104 Matching: "a", 105 OnlyB: "onlyB", 106 MatchingInt: 1, 107 DifferentType: 7, 108 Unused: "Ha", 109 }, b) 110 111 assert.Equal(t, aBefore, a) 112 }