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

     1  /*
     2   * Copyright 2016 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 schema
    18  
    19  import (
    20  	"testing"
    21  
    22  	"kythe.io/kythe/go/util/schema/facts"
    23  	"kythe.io/kythe/go/util/schema/nodes"
    24  
    25  	"github.com/golang/protobuf/proto"
    26  
    27  	scpb "kythe.io/kythe/proto/schema_go_proto"
    28  	spb "kythe.io/kythe/proto/storage_go_proto"
    29  )
    30  
    31  func TestNodeToEntry(t *testing.T) {
    32  	n := &Node{
    33  		VName: &spb.VName{Signature: "a"},
    34  		Kind:  nodes.Anchor,
    35  		Facts: Facts{
    36  			facts.AnchorStart: "25",
    37  			facts.AnchorEnd:   "37",
    38  			facts.Text:        "apple",
    39  		},
    40  	}
    41  	entries := n.ToEntries()
    42  	t.Logf("Checking entries for node: %+v", n)
    43  
    44  	check := func(key, value string) {
    45  		for _, entry := range entries {
    46  			if entry.FactName == key && string(entry.FactValue) == value {
    47  				return
    48  			}
    49  		}
    50  		t.Errorf("Missing entry for %q, wanted %q", key, value)
    51  	}
    52  	check(facts.NodeKind, nodes.Anchor)
    53  	check(facts.AnchorStart, "25")
    54  	check(facts.AnchorEnd, "37")
    55  	check(facts.Text, "apple")
    56  	check(facts.TextEncoding, facts.DefaultTextEncoding)
    57  }
    58  
    59  func TestEdgeToEntry(t *testing.T) {
    60  	src := &spb.VName{Signature: "source"}
    61  	tgt := &spb.VName{Signature: "target"}
    62  	e := &Edge{
    63  		Source: src,
    64  		Target: tgt,
    65  		Kind:   "childof",
    66  	}
    67  	got := e.ToEntry()
    68  	want := &spb.Entry{
    69  		Source:   src,
    70  		Target:   tgt,
    71  		EdgeKind: "childof",
    72  		FactName: "/",
    73  	}
    74  	if !proto.Equal(got, want) {
    75  		t.Errorf("ToEdge(%+v):\n--- got\n%s\n--- want\n%s", e, proto.MarshalTextString(got), proto.MarshalTextString(want))
    76  	}
    77  }
    78  
    79  func TestNodeKindEnum(t *testing.T) {
    80  	known := []struct {
    81  		Name string
    82  		Enum scpb.NodeKind
    83  	}{
    84  		{"anchor", scpb.NodeKind_ANCHOR},
    85  		{"record", scpb.NodeKind_RECORD},
    86  	}
    87  
    88  	for _, k := range known {
    89  		if found := NodeKind(k.Name); found != k.Enum {
    90  			t.Errorf("NodeKind(%q) == %v; expected: %v", k.Name, found, k.Enum)
    91  		}
    92  	}
    93  }
    94  
    95  func TestFactNameEnum(t *testing.T) {
    96  	known := []struct {
    97  		Name string
    98  		Enum scpb.FactName
    99  	}{
   100  		{"/kythe/loc/start", scpb.FactName_LOC_START},
   101  		{"/kythe/node/kind", scpb.FactName_NODE_KIND},
   102  		{"/kythe/subkind", scpb.FactName_SUBKIND},
   103  	}
   104  
   105  	for _, k := range known {
   106  		if found := FactName(k.Name); found != k.Enum {
   107  			t.Errorf("FactName(%q) == %v; expected: %v", k.Name, found, k.Enum)
   108  		}
   109  	}
   110  }
   111  
   112  func TestEdgeKindEnum(t *testing.T) {
   113  	known := []struct {
   114  		Name string
   115  		Enum scpb.EdgeKind
   116  	}{
   117  		{"/kythe/edge/annotatedby", scpb.EdgeKind_ANNOTATED_BY},
   118  		{"/kythe/edge/childof", scpb.EdgeKind_CHILD_OF},
   119  		{"/kythe/edge/ref", scpb.EdgeKind_REF},
   120  		{"/kythe/edge/ref/implicit", scpb.EdgeKind_REF_IMPLICIT},
   121  	}
   122  
   123  	for _, k := range known {
   124  		if found := EdgeKind(k.Name); found != k.Enum {
   125  			t.Errorf("EdgeKind(%q) == %v; expected: %v", k.Name, found, k.Enum)
   126  		}
   127  	}
   128  }
   129  
   130  func TestSubkindEnum(t *testing.T) {
   131  	known := []struct {
   132  		Name string
   133  		Enum scpb.Subkind
   134  	}{
   135  		{"class", scpb.Subkind_CLASS},
   136  		{"implicit", scpb.Subkind_IMPLICIT},
   137  		{"enumClass", scpb.Subkind_ENUM_CLASS},
   138  	}
   139  
   140  	for _, k := range known {
   141  		if found := Subkind(k.Name); found != k.Enum {
   142  			t.Errorf("Subkind(%q) == %v; expected: %v", k.Name, found, k.Enum)
   143  		}
   144  	}
   145  }
   146  
   147  func TestEdgeKindStrings(t *testing.T) {
   148  	for _, i := range proto.EnumValueMap("kythe.proto.schema.EdgeKind") {
   149  		enum := scpb.EdgeKind(i)
   150  		if enum == scpb.EdgeKind_UNKNOWN_EDGE_KIND {
   151  			continue
   152  		}
   153  		str := EdgeKindString(enum)
   154  		if str == "" {
   155  			t.Errorf("Missing string value for %v", enum)
   156  		} else if found := EdgeKind(str); found != enum {
   157  			t.Errorf("Mismatched EdgeKind: found: %v; expected: %v", found, enum)
   158  		}
   159  	}
   160  }
   161  
   162  func TestNodeKindStrings(t *testing.T) {
   163  	for _, i := range proto.EnumValueMap("kythe.proto.schema.NodeKind") {
   164  		enum := scpb.NodeKind(i)
   165  		if enum == scpb.NodeKind_UNKNOWN_NODE_KIND {
   166  			continue
   167  		}
   168  		str := NodeKindString(enum)
   169  		if str == "" {
   170  			t.Errorf("Missing string value for %v", enum)
   171  		} else if found := NodeKind(str); found != enum {
   172  			t.Errorf("Mismatched NodeKind: found: %v; expected: %v", found, enum)
   173  		}
   174  	}
   175  }
   176  
   177  func TestFactNameStrings(t *testing.T) {
   178  	for _, i := range proto.EnumValueMap("kythe.proto.schema.FactName") {
   179  		enum := scpb.FactName(i)
   180  		if enum == scpb.FactName_UNKNOWN_FACT_NAME {
   181  			continue
   182  		}
   183  		str := FactNameString(enum)
   184  		if str == "" {
   185  			t.Errorf("Missing string value for %v", enum)
   186  		} else if found := FactName(str); found != enum {
   187  			t.Errorf("Mismatched FactName: found: %v; expected: %v", found, enum)
   188  		}
   189  	}
   190  }
   191  
   192  func TestSubkindStrings(t *testing.T) {
   193  	for _, i := range proto.EnumValueMap("kythe.proto.schema.Subkind") {
   194  		enum := scpb.Subkind(i)
   195  		if enum == scpb.Subkind_UNKNOWN_SUBKIND {
   196  			continue
   197  		}
   198  		str := SubkindString(enum)
   199  		if str == "" {
   200  			t.Errorf("Missing string value for %v", enum)
   201  		} else if found := Subkind(str); found != enum {
   202  			t.Errorf("Mismatched Subkind: found: %v; expected: %v", found, enum)
   203  		}
   204  	}
   205  }