kythe.io@v0.0.68-0.20240422202219-7225dbc01741/kythe/go/util/schema/edges/edges_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 edges
    18  
    19  import (
    20  	"testing"
    21  
    22  	"kythe.io/kythe/go/test/testutil"
    23  )
    24  
    25  func TestParseOrdinal(t *testing.T) {
    26  	type ordinalTest struct { // fields exported for the comparator
    27  		Input, Kind string
    28  		Ordinal     int
    29  		HasOrdinal  bool
    30  	}
    31  	tests := []ordinalTest{
    32  		{"/kythe/edge/defines", "/kythe/edge/defines", 0, false},
    33  		{"/kythe/edge/kind.here", "/kythe/edge/kind.here", 0, false},
    34  		{"/kythe/edge/kind1", "/kythe/edge/kind1", 0, false},
    35  		{"kind.-1", "kind.-1", 0, false},
    36  
    37  		{"kind.3", "kind", 3, true},
    38  		{"/kythe/edge/param.0", "/kythe/edge/param", 0, true},
    39  		{"/kythe/edge/param.1", "/kythe/edge/param", 1, true},
    40  		{"%/kythe/edge/param.1", "%/kythe/edge/param", 1, true},
    41  		{"/kythe/edge/kind.1930", "/kythe/edge/kind", 1930, true},
    42  	}
    43  
    44  	for _, test := range tests {
    45  		kind, ord, ok := ParseOrdinal(test.Input)
    46  		if err := testutil.DeepEqual(test, ordinalTest{
    47  			Input:      test.Input,
    48  			Kind:       kind,
    49  			Ordinal:    ord,
    50  			HasOrdinal: ok,
    51  		}); err != nil {
    52  			t.Errorf("ParseOrdinal(%q): %v", test.Input, err)
    53  		}
    54  	}
    55  }
    56  
    57  func TestCanonical(t *testing.T) {
    58  	tests := []struct {
    59  		input, want string
    60  	}{
    61  		{"", ""},
    62  		{"/kythe/edge/childof", "/kythe/edge/childof"},
    63  		{"%/kythe/edge/defines/binding", "/kythe/edge/defines/binding"},
    64  	}
    65  	for _, test := range tests {
    66  		if got := Canonical(test.input); got != test.want {
    67  			t.Errorf("Canonical(%q): got %q, want %q", test.input, got, test.want)
    68  		}
    69  	}
    70  }
    71  
    72  func TestDirections(t *testing.T) {
    73  	tests := []struct {
    74  		input     string
    75  		isForward bool
    76  	}{
    77  		{"/kythe/edge/defines", true},
    78  		{"%/kythe/edge/defines", false},
    79  	}
    80  	for _, test := range tests {
    81  		if got := IsForward(test.input); got != test.isForward {
    82  			t.Errorf("IsForward(%q): got %v, want %v", test.input, got, test.isForward)
    83  		}
    84  		if got := IsReverse(test.input); got == test.isForward {
    85  			t.Errorf("IsReverse(%q): got %v, want %v", test.input, got, !test.isForward)
    86  		}
    87  	}
    88  }
    89  
    90  func TestMirroring(t *testing.T) {
    91  	tests := []struct {
    92  		input, want string
    93  	}{
    94  		{"/kythe/edge/a", "%/kythe/edge/a"},
    95  		{"%/kythe/edge/b", "/kythe/edge/b"},
    96  	}
    97  	for _, test := range tests {
    98  		got := Mirror(test.input)
    99  		if got != test.want {
   100  			t.Errorf("Mirror(%q): got %q, want %q", test.input, got, test.want)
   101  		}
   102  		if rev := Mirror(got); rev != test.input {
   103  			t.Errorf("Mirror(Mirror(%q)): got %q, want %q", test.input, rev, test.input)
   104  		}
   105  	}
   106  }
   107  
   108  func TestIsVariant(t *testing.T) {
   109  	tests := []struct {
   110  		x, y string
   111  		want bool
   112  	}{
   113  		{"/kythe/edge/a", "/kythe/edge/a", true},
   114  		{"/kythe/edge/a", "/kythe/edge/b", false},
   115  		{"/kythe/edge/a/b", "/kythe/edge/a", true},
   116  		{"/kythe/edge/a/c", "/kythe/edge/a/b", false},
   117  		{"%/kythe/edge/fwd", "%/kythe/edge/fwd", true},
   118  		{"%/kythe/edge/fwd/sub", "%/kythe/edge/fwd", true},
   119  		{"%/kythe/edge/fwd/sub/more", "%/kythe/edge/fwd", true},
   120  	}
   121  	for _, test := range tests {
   122  		if got := IsVariant(test.x, test.y); got != test.want {
   123  			t.Errorf("IsVariant(%q, %q): got %v, want %v", test.x, test.y, got, test.want)
   124  		}
   125  		if got := IsVariant(Mirror(test.x), Mirror(test.y)); got != test.want {
   126  			t.Errorf("IsVariant(!%q, !%q): got %v, want %v", test.x, test.y, got, test.want)
   127  		}
   128  	}
   129  }
   130  
   131  func TestParamIndex(t *testing.T) {
   132  	tests := []string{"param.0", "param.1", "param.2", "param.3"}
   133  	for i, test := range tests {
   134  		want := "/kythe/edge/" + test
   135  		if got := ParamIndex(i); got != want {
   136  			t.Errorf("ParamIndex(%d): got %q, want %q", i, got, want)
   137  		}
   138  	}
   139  }