kythe.io@v0.0.68-0.20240422202219-7225dbc01741/kythe/go/util/markedsource/resolve_test.go (about) 1 /* 2 * Copyright 2023 The Kythe Authors. All rights reserved. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package markedsource 18 19 import ( 20 "strconv" 21 "testing" 22 23 "kythe.io/kythe/go/test/testutil" 24 "kythe.io/kythe/go/util/compare" 25 "kythe.io/kythe/go/util/kytheuri" 26 "kythe.io/kythe/go/util/schema/edges" 27 "kythe.io/kythe/go/util/schema/facts" 28 29 "google.golang.org/protobuf/encoding/prototext" 30 "google.golang.org/protobuf/proto" 31 32 cpb "kythe.io/kythe/proto/common_go_proto" 33 spb "kythe.io/kythe/proto/storage_go_proto" 34 ) 35 36 func TestResolve(t *testing.T) { 37 tests := []struct { 38 Entries []*spb.Entry 39 Ticket string 40 Expected string 41 }{ 42 { 43 []*spb.Entry{code(t, "kythe:#test", `kind: IDENTIFIER pre_text: "simple"`)}, 44 "kythe:#test", 45 `kind: IDENTIFIER pre_text: "simple"`, 46 }, 47 { 48 []*spb.Entry{ 49 code(t, "kythe:#test", `kind: BOX child: { kind: LOOKUP_BY_PARAM lookup_index: 0 }`), 50 edge(t, "kythe:#test", edges.ParamIndex(0), "kythe:#param"), 51 code(t, "kythe:#param", `kind: IDENTIFIER pre_text: "param"`), 52 }, 53 "kythe:#test", 54 `kind: BOX child: { kind: PARAMETER child: { kind: IDENTIFIER pre_text: "param" } }`, 55 }, 56 { 57 []*spb.Entry{ 58 code(t, "kythe:#test", `kind: BOX child: { kind: LOOKUP_BY_PARAM lookup_index: 0 }`), 59 edge(t, "kythe:#test", edges.ParamIndex(0), "kythe:#paramLevel0"), 60 code(t, "kythe:#paramLevel0", `kind: LOOKUP_BY_PARAM lookup_index: 1`), 61 edge(t, "kythe:#paramLevel0", edges.ParamIndex(1), "kythe:#paramLevel1"), 62 code(t, "kythe:#paramLevel1", `kind: IDENTIFIER pre_text: "deep"`), 63 }, 64 "kythe:#test", 65 `kind: BOX child: { kind: PARAMETER child: { kind: IDENTIFIER pre_text: "deep" } }`, 66 }, 67 { 68 []*spb.Entry{ 69 code(t, "kythe:#test", `kind: PARAMETER_LOOKUP_BY_PARAM post_child_text: ", "`), 70 edge(t, "kythe:#test", edges.ParamIndex(0), "kythe:#param0"), 71 edge(t, "kythe:#test", edges.ParamIndex(1), "kythe:#param1"), 72 edge(t, "kythe:#test", edges.ParamIndex(2), "kythe:#param2"), 73 code(t, "kythe:#param0", `kind: IDENTIFIER pre_text: "0"`), 74 code(t, "kythe:#param1", `kind: IDENTIFIER pre_text: "1"`), 75 code(t, "kythe:#param2", `kind: IDENTIFIER pre_text: "2"`), 76 }, 77 "kythe:#test", 78 `kind: PARAMETER post_child_text: ", " child: { kind: IDENTIFIER pre_text: "0" } child: { kind: IDENTIFIER pre_text: "1" } child: { kind: IDENTIFIER pre_text: "2" }`, 79 }, 80 } 81 82 for i, test := range tests { 83 t.Run(strconv.Itoa(i), func(t *testing.T) { 84 r, err := NewResolver(test.Entries) 85 testutil.Fatalf(t, "NewResolver: %v", err) 86 v := parseVName(t, test.Ticket) 87 found := r.Resolve(v) 88 expected := parseMarkedSource(t, test.Expected) 89 90 if diff := compare.ProtoDiff(expected, found); diff != "" { 91 t.Fatalf("(- expected; + found)\n%s", diff) 92 } 93 }) 94 } 95 } 96 97 func code(t testing.TB, src, code string) *spb.Entry { 98 t.Helper() 99 v := parseVName(t, src) 100 ms := parseMarkedSource(t, code) 101 rec, err := proto.Marshal(ms) 102 testutil.Fatalf(t, "proto.Marshal: %v", err) 103 return &spb.Entry{ 104 Source: v, 105 FactName: facts.Code, 106 FactValue: rec, 107 } 108 } 109 110 func edge(t testing.TB, src, kind, tgt string) *spb.Entry { 111 t.Helper() 112 return &spb.Entry{ 113 Source: parseVName(t, src), 114 EdgeKind: kind, 115 Target: parseVName(t, tgt), 116 } 117 } 118 119 func parseVName(t testing.TB, s string) *spb.VName { 120 t.Helper() 121 v, err := kytheuri.ToVName(s) 122 testutil.Fatalf(t, "kytheuri.ParseVName: %v", err) 123 return v 124 } 125 126 func parseMarkedSource(t testing.TB, s string) *cpb.MarkedSource { 127 t.Helper() 128 var ms cpb.MarkedSource 129 testutil.Fatalf(t, "prototext.Unmarshal: %v", prototext.Unmarshal([]byte(s), &ms)) 130 return &ms 131 } 132 133 func e(t testing.TB, s string) *spb.Entry { 134 t.Helper() 135 var e spb.Entry 136 testutil.Fatalf(t, "prototext.Unmarshal: %v", prototext.Unmarshal([]byte(s), &e)) 137 return &e 138 }