github.com/cs3org/reva/v2@v2.27.7/pkg/storage/utils/indexer/reflect_test.go (about) 1 // Copyright 2018-2022 CERN 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 // 15 // In applying this license, CERN does not waive the privileges and immunities 16 // granted to it by virtue of its status as an Intergovernmental Organization 17 // or submit itself to any jurisdiction. 18 19 package indexer 20 21 import ( 22 "fmt" 23 "testing" 24 25 "github.com/cs3org/reva/v2/pkg/storage/utils/indexer/option" 26 ) 27 28 func Test_getTypeFQN(t *testing.T) { 29 type someT struct{} 30 31 type args struct { 32 t interface{} 33 } 34 tests := []struct { 35 name string 36 args args 37 want string 38 }{ 39 {name: "ByValue", args: args{&someT{}}, want: "github.com.cs3org.reva.v2.pkg.storage.utils.indexer.someT"}, 40 {name: "ByRef", args: args{someT{}}, want: "github.com.cs3org.reva.v2.pkg.storage.utils.indexer.someT"}, 41 } 42 for _, tt := range tests { 43 t.Run(tt.name, func(t *testing.T) { 44 if got := getTypeFQN(tt.args.t); got != tt.want { 45 t.Errorf("getTypeFQN() = %v, want %v", got, tt.want) 46 } 47 }) 48 } 49 } 50 51 func Test_valueOf(t *testing.T) { 52 type nestedDeeplyT struct { 53 Val string 54 } 55 type nestedT struct { 56 Deeply nestedDeeplyT 57 } 58 type someT struct { 59 val string 60 Nested nestedT 61 } 62 type args struct { 63 v interface{} 64 indexBy option.IndexBy 65 } 66 tests := []struct { 67 name string 68 args args 69 want string 70 }{ 71 {name: "ByValue", args: args{v: someT{val: "hello"}, indexBy: option.IndexByField("val")}, want: "hello"}, 72 {name: "ByRef", args: args{v: &someT{val: "hello"}, indexBy: option.IndexByField("val")}, want: "hello"}, 73 {name: "nested", args: args{v: &someT{Nested: nestedT{Deeply: nestedDeeplyT{Val: "nestedHello"}}}, indexBy: option.IndexByField("Nested.Deeply.Val")}, want: "nestedHello"}, 74 {name: "using a indexFunc", args: args{v: &someT{Nested: nestedT{Deeply: nestedDeeplyT{Val: "nestedHello"}}}, indexBy: option.IndexByFunc{ 75 Name: "neestedDeeplyVal", 76 Func: func(i interface{}) (string, error) { 77 t, ok := i.(*someT) 78 if !ok { 79 return "", fmt.Errorf("booo") 80 } 81 return t.Nested.Deeply.Val, nil 82 }, 83 }}, want: "nestedHello"}, 84 } 85 for _, tt := range tests { 86 t.Run(tt.name, func(t *testing.T) { 87 if got, err := valueOf(tt.args.v, tt.args.indexBy); got != tt.want || err != nil { 88 t.Errorf("valueOf() = %v, want %v", got, tt.want) 89 } 90 }) 91 } 92 }