github.com/dolthub/dolt/go@v0.40.5-0.20240520175717-68db7794bea6/libraries/doltcore/ref/ref_test.go (about)

     1  // Copyright 2019 Dolthub, Inc.
     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  package ref
    16  
    17  import (
    18  	"encoding/json"
    19  	"testing"
    20  )
    21  
    22  const defaultBranch = "main"
    23  
    24  type TestMarshalStruct struct {
    25  	Test MarshalableRef `json:"test"`
    26  }
    27  
    28  func TestJsonMarshalAndUnmarshal(t *testing.T) {
    29  	tests := []struct {
    30  		dr  DoltRef
    31  		str string
    32  	}{
    33  		{
    34  			NewBranchRef(defaultBranch),
    35  			`{"test":"refs/heads/main"}`,
    36  		},
    37  		{
    38  			NewRemoteRef("origin", defaultBranch),
    39  			`{"test":"refs/remotes/origin/main"}`,
    40  		},
    41  		{
    42  			NewInternalRef("create"),
    43  			`{"test":"refs/internal/create"}`,
    44  		},
    45  		{
    46  			NewWorkspaceRef("newworkspace"),
    47  			`{"test":"refs/workspaces/newworkspace"}`,
    48  		},
    49  	}
    50  
    51  	for _, test := range tests {
    52  		tms := TestMarshalStruct{MarshalableRef{test.dr}}
    53  		data, err := json.Marshal(tms)
    54  		actual := string(data)
    55  
    56  		if err != nil {
    57  			t.Error(err)
    58  		} else if test.str != actual {
    59  			t.Error(actual, "!=", test.str)
    60  		}
    61  	}
    62  
    63  	for _, test := range tests {
    64  		var tms TestMarshalStruct
    65  		err := json.Unmarshal([]byte(test.str), &tms)
    66  
    67  		if err != nil {
    68  			t.Error(err)
    69  		} else if !Equals(test.dr, tms.Test.Ref) {
    70  			t.Error(tms.Test, "!=", test.dr)
    71  		}
    72  	}
    73  }
    74  
    75  func TestEqualsStr(t *testing.T) {
    76  	om, _ := NewRemoteRefFromPathStr("origin/main")
    77  	rom, _ := NewRemoteRefFromPathStr("refs/remotes/origin/main")
    78  	tests := []struct {
    79  		dr       DoltRef
    80  		cmp      string
    81  		expected bool
    82  	}{
    83  		{
    84  			NewBranchRef(defaultBranch),
    85  			"refs/heads/main",
    86  			true,
    87  		},
    88  		{
    89  			NewBranchRef("refs/heads/main"),
    90  			"refs/heads/main",
    91  			true,
    92  		},
    93  		{
    94  			NewBranchRef(defaultBranch),
    95  			"refs/heads/notmain",
    96  			false,
    97  		},
    98  		{
    99  			NewBranchRef(defaultBranch),
   100  			"refs/remotes/origin/main",
   101  			false,
   102  		},
   103  		{
   104  			NewRemoteRef("origin", defaultBranch),
   105  			"refs/remotes/origin/main",
   106  			true,
   107  		},
   108  		{
   109  			om,
   110  			"refs/remotes/origin/main",
   111  			true,
   112  		},
   113  		{
   114  			rom,
   115  			"refs/remotes/origin/main",
   116  			true,
   117  		},
   118  		{
   119  			NewRemoteRef("origin", defaultBranch),
   120  			"refs/remotes/borigin/main",
   121  			false,
   122  		},
   123  		{
   124  			NewRemoteRef("origin", defaultBranch),
   125  			"refs/remotes/origin/notmain",
   126  			false,
   127  		},
   128  		{
   129  			NewRemoteRef("origin", defaultBranch),
   130  			"refs/notavalidtype/origin/notmain",
   131  			false,
   132  		},
   133  		{
   134  			NewInternalRef("create"),
   135  			"refs/internal/create",
   136  			true,
   137  		},
   138  		{
   139  			NewInternalRef("refs/internal/create"),
   140  			"refs/internal/create",
   141  			true,
   142  		},
   143  		{
   144  			NewWorkspaceRef("newworkspace"),
   145  			"refs/workspaces/newworkspace",
   146  			true,
   147  		},
   148  		{
   149  			NewWorkspaceRef("refs/workspaces/newworkspace"),
   150  			"refs/workspaces/newworkspace",
   151  			true,
   152  		},
   153  		{
   154  			NewWorkspaceRef("newworkspace"),
   155  			"refs/workspaces/notnewworkspace",
   156  			false,
   157  		},
   158  		{
   159  			NewWorkspaceRef("newworkspace"),
   160  			"refs/remotes/origin/newworkspace",
   161  			false,
   162  		},
   163  	}
   164  
   165  	for _, test := range tests {
   166  		actual := EqualsStr(test.dr, test.cmp)
   167  
   168  		if actual != test.expected {
   169  			t.Error("for input:", test.cmp, "error comparing", test.dr, "to", test.cmp)
   170  		}
   171  	}
   172  }