github.com/golang/dep@v0.5.4/gps/source_cache_bolt_encode_test.go (about) 1 // Copyright 2017 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package gps 6 7 import ( 8 "bytes" 9 "testing" 10 "time" 11 12 "github.com/golang/protobuf/proto" 13 ) 14 15 func TestPropertiesFromCache(t *testing.T) { 16 for _, test := range []struct { 17 name string 18 ip ProjectRoot 19 pp ProjectProperties 20 }{ 21 {"defaultBranch", 22 "root", ProjectProperties{"", newDefaultBranch("test")}}, 23 {"branch", 24 "root", ProjectProperties{"source", NewBranch("test")}}, 25 {"semver", 26 "root", ProjectProperties{"", testSemverConstraint(t, "^1.0.0")}}, 27 {"rev", 28 "root", ProjectProperties{"source", Revision("test")}}, 29 {"any", 30 "root", ProjectProperties{"source", Any()}}, 31 } { 32 t.Run(test.name, func(t *testing.T) { 33 var buf projectPropertiesMsgs 34 buf.copyFrom(test.ip, test.pp) 35 v, err := proto.Marshal(&buf.pp) 36 if err != nil { 37 t.Fatal(err) 38 } 39 40 if err := proto.Unmarshal(v, &buf.pp); err != nil { 41 t.Fatal(err) 42 } else { 43 ip, pp, err := propertiesFromCache(&buf.pp) 44 if err != nil { 45 t.Fatal(err) 46 } 47 if ip != test.ip { 48 t.Errorf("decoded unexpected ProjectRoot:\n\t(GOT): %#v\n\t(WNT): %#v", ip, test.ip) 49 } 50 if pp.Source != test.pp.Source { 51 t.Errorf("decoded unexpected ProjectRoot.Source:\n\t(GOT): %s\n\t (WNT): %s", pp.Source, test.pp.Source) 52 } 53 if !pp.Constraint.identical(test.pp.Constraint) { 54 t.Errorf("decoded non-identical ProjectRoot.Constraint:\n\t(GOT): %#v\n\t(WNT): %#v", pp.Constraint, test.pp.Constraint) 55 } 56 } 57 }) 58 } 59 } 60 61 func TestCacheTimestampedKey(t *testing.T) { 62 pre := byte('p') 63 for _, test := range []struct { 64 ts time.Time 65 suffix []byte 66 }{ 67 {time.Unix(0, 0), []byte{0, 0, 0, 0, 0, 0, 0, 0}}, 68 {time.Unix(100, 0), []byte{0, 0, 0, 0, 0, 0, 0, 100}}, 69 {time.Unix(255, 0), []byte{0, 0, 0, 0, 0, 0, 0, 255}}, 70 {time.Unix(1+1<<8+1<<16+1<<24, 0), []byte{0, 0, 0, 0, 1, 1, 1, 1}}, 71 {time.Unix(255<<48, 0), []byte{0, 255, 0, 0, 0, 0, 0, 0}}, 72 } { 73 b := cacheTimestampedKey(pre, test.ts) 74 if !bytes.Equal(b, append([]byte{pre}, test.suffix...)) { 75 t.Errorf("unexpected suffix:\n\t(GOT):%v\n\t(WNT):%v", b[4:], test.suffix) 76 } 77 } 78 }