v.io/jiri@v0.0.0-20160715023856-abfb8b131290/profiles/versions_test.go (about) 1 // Copyright 2015 The Vanadium 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 profiles_test 6 7 import ( 8 "testing" 9 10 "v.io/jiri/profiles" 11 ) 12 13 func cmp(a, b []string) bool { 14 if len(a) != len(b) { 15 return false 16 } 17 for i, v := range a { 18 if v != b[i] { 19 return false 20 } 21 } 22 return true 23 } 24 25 func TestVersionInfo(t *testing.T) { 26 type spec struct { 27 v string 28 } 29 vi := profiles.NewVersionInfo("test", map[string]interface{}{ 30 "3": "3x", 31 "5": "5x", 32 "4": "4x", 33 "6": &spec{"6"}, 34 }, "3") 35 36 if got, want := vi.Supported(), []string{"6", "5", "4", "3"}; !cmp(got, want) { 37 t.Errorf("got %v, want %v", got, want) 38 } 39 if got, want := vi.Default(), "3"; got != want { 40 t.Errorf("got %v, want %v", got, want) 41 } 42 43 var data string 44 if err := vi.Lookup("4", &data); err != nil { 45 t.Fatal(err) 46 } 47 if got, want := data, "4x"; got != want { 48 t.Errorf("got %v, want %v", got, want) 49 } 50 51 var bad bool 52 if err := vi.Lookup("4", &bad); err == nil || err.Error() != "mismatched types: string not assignable to *bool" { 53 t.Errorf("missing or wrong error: %v", err) 54 } 55 56 var s spec 57 if err := vi.Lookup("6", &s); err != nil { 58 t.Fatal(err) 59 } 60 if got, want := s.v, "6"; got != want { 61 t.Errorf("got %v, want %v", got, want) 62 } 63 64 ver, err := vi.Select("") 65 if err != nil { 66 t.Fatal(err) 67 } 68 if got, want := ver, "3"; got != want { 69 t.Errorf("got %v, want %v", got, want) 70 } 71 ver, err = vi.Select("5") 72 if err != nil { 73 t.Fatal(err) 74 } 75 if got, want := ver, "5"; got != want { 76 t.Errorf("got %v, want %v", got, want) 77 } 78 79 if ver, err := vi.Select("2"); ver != "" || err.Error() != "unsupported version: \"2\" for 6 5 4 3*" { 80 t.Errorf("failed to detect unsupported version: %q", err) 81 } 82 }