kythe.io@v0.0.68-0.20240422202219-7225dbc01741/kythe/go/util/metadata/metadata_test.go (about)

     1  /*
     2   * Copyright 2017 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 metadata
    18  
    19  import (
    20  	"bytes"
    21  	"encoding/json"
    22  	"strings"
    23  	"testing"
    24  
    25  	"kythe.io/kythe/go/util/compare"
    26  	"kythe.io/kythe/go/util/schema/edges"
    27  
    28  	"google.golang.org/protobuf/proto"
    29  
    30  	protopb "google.golang.org/protobuf/types/descriptorpb"
    31  	spb "kythe.io/kythe/proto/storage_go_proto"
    32  )
    33  
    34  func TestParse(t *testing.T) {
    35  	tests := []struct {
    36  		input string
    37  		want  Rules
    38  	}{
    39  		// Minimal value: Just a plain type tag.
    40  		{`{"type":"kythe0"}`, Rules{}},
    41  
    42  		// NOP values, multiple rules.
    43  		{`{"type":"kythe0","meta":[
    44               {"type":"nop"},
    45  						 {"type":"nop","begin":42,"end":99},
    46  						 {"type":"nop","semantic":"SET"}
    47            ]}`, Rules{
    48  			{},
    49  			{
    50  				Begin: 42,
    51  				End:   99,
    52  			},
    53  			{
    54  				Semantic: SemanticSet.Enum(),
    55  			},
    56  		}},
    57  
    58  		// Test vector from the C++ implementation.
    59  		{`{"type":"kythe0","meta":[{"type":"anchor_defines","begin":179,"end":182,
    60             "edge":"%/kythe/edge/generates",
    61             "vname":{
    62                   "signature":"gsig",
    63                   "corpus":"gcorp",
    64                   "path":"gpath",
    65                   "language":"glang",
    66                   "root":"groot"}
    67            }]}`, Rules{{
    68  			Begin:   179,
    69  			End:     182,
    70  			EdgeIn:  edges.DefinesBinding,
    71  			EdgeOut: "/kythe/edge/generates",
    72  			Reverse: true,
    73  			VName: &spb.VName{
    74  				Signature: "gsig",
    75  				Corpus:    "gcorp",
    76  				Path:      "gpath",
    77  				Language:  "glang",
    78  				Root:      "groot",
    79  			},
    80  		}}},
    81  	}
    82  	for _, test := range tests {
    83  		got, err := Parse(strings.NewReader(test.input))
    84  		if err != nil {
    85  			t.Errorf("Parse %q failed: %v", test.input, err)
    86  			continue
    87  		}
    88  
    89  		if diff := compare.ProtoDiff(test.want, got); diff != "" {
    90  			t.Errorf("Parse %q: %s", test.input, diff)
    91  		}
    92  	}
    93  }
    94  
    95  func TestRoundTrip(t *testing.T) {
    96  	tests := []Rules{
    97  		Rules{},
    98  		Rules{{}},
    99  		Rules{
   100  			{},
   101  			{Begin: 25, End: 37, EdgeOut: "blah"},
   102  		},
   103  		Rules{{
   104  			VName: &spb.VName{
   105  				Signature: "gsig",
   106  				Corpus:    "gcorp",
   107  				Path:      "gpath",
   108  				Language:  "glang",
   109  				Root:      "groot",
   110  			},
   111  			Reverse:  true,
   112  			EdgeIn:   edges.DefinesBinding,
   113  			EdgeOut:  edges.Generates,
   114  			Begin:    179,
   115  			End:      182,
   116  			Semantic: SemanticSet.Enum(),
   117  		}},
   118  	}
   119  	for _, test := range tests {
   120  		enc, err := json.Marshal(test)
   121  		if err != nil {
   122  			t.Errorf("Encoding %+v failed: %v", test, err)
   123  			continue
   124  		}
   125  
   126  		dec, err := Parse(bytes.NewReader(enc))
   127  		if err != nil {
   128  			t.Errorf("Decoding %q failed: %v", string(enc), err)
   129  			continue
   130  		}
   131  
   132  		if diff := compare.ProtoDiff(test, dec); diff != "" {
   133  			t.Errorf("Round-trip of %+v failed: %s", test, diff)
   134  		}
   135  	}
   136  }
   137  
   138  func TestGeneratedCodeInfo(t *testing.T) {
   139  	in := &protopb.GeneratedCodeInfo{
   140  		Annotation: []*protopb.GeneratedCodeInfo_Annotation{{
   141  			Path:       []int32{1, 2, 3, 4, 5},
   142  			SourceFile: proto.String("a"),
   143  			Begin:      proto.Int32(1),
   144  			End:        proto.Int32(100),
   145  			Semantic:   SemanticSet.Enum(),
   146  		}},
   147  	}
   148  	want := Rules{{
   149  		VName: &spb.VName{
   150  			Signature: "1.2.3.4.5",
   151  			Language:  "protobuf",
   152  			Path:      "a",
   153  		},
   154  		Reverse:  true,
   155  		EdgeIn:   edges.DefinesBinding,
   156  		EdgeOut:  edges.Generates,
   157  		Begin:    1,
   158  		End:      100,
   159  		Semantic: SemanticSet.Enum(),
   160  	}}
   161  	{
   162  		got := FromGeneratedCodeInfo(in, nil)
   163  		if diff := compare.ProtoDiff(got, want); diff != "" {
   164  			t.Errorf("FromGeneratedCodeInfo failed: %s", diff)
   165  		}
   166  	}
   167  	{
   168  		got := FromGeneratedCodeInfo(in, &spb.VName{
   169  			Corpus: "blargh",
   170  		})
   171  		want[0].VName.Corpus = "blargh"
   172  		if diff := compare.ProtoDiff(got, want); diff != "" {
   173  			t.Errorf("FromGeneratedCodeInfo failed: %s", diff)
   174  		}
   175  	}
   176  }