github.com/hasnat/dolt/go@v0.0.0-20210628190320-9eb5d843fbb7/libraries/doltcore/doltdb/anscestor_spec_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 doltdb
    16  
    17  import (
    18  	"reflect"
    19  	"testing"
    20  )
    21  
    22  func TestParseInstructions(t *testing.T) {
    23  	tests := []struct {
    24  		inputStr  string
    25  		expected  []int
    26  		expectErr bool
    27  	}{
    28  		{"", []int{}, false},
    29  		{"^", []int{0}, false},
    30  		{"^1", []int{0}, false},
    31  		{"~", []int{0}, false},
    32  		{"~1", []int{0}, false},
    33  		{"^10", []int{9}, false},
    34  		{"~3", []int{0, 0, 0}, false},
    35  		{"^^", []int{0, 0}, false},
    36  		{"^2~3^5", []int{1, 0, 0, 0, 4}, false},
    37  		{"invalid", nil, true},
    38  	}
    39  
    40  	for _, test := range tests {
    41  		actual, actualErr := parseInstructions(test.inputStr)
    42  
    43  		if actualErr != nil {
    44  			if !test.expectErr {
    45  				t.Error(test.inputStr, "- unexpected err")
    46  			}
    47  		} else if !reflect.DeepEqual(test.expected, actual) {
    48  			t.Error("Error parsing", test.inputStr)
    49  		}
    50  	}
    51  }
    52  
    53  func TestSplitAncestorSpec(t *testing.T) {
    54  	tests := []struct {
    55  		inputStr         string
    56  		expectedCSpecStr string
    57  		expectedASpecStr string
    58  		expectErr        bool
    59  	}{
    60  		{"master", "master", "", false},
    61  		{"MASTER^1", "MASTER", "^1", false},
    62  		{"head~3^^", "head", "~3^^", false},
    63  		{"HEAD~3^^", "HEAD", "~3^^", false},
    64  		{"branch^invalid", "", "", true},
    65  	}
    66  
    67  	for _, test := range tests {
    68  		actualCSpecStr, actualASpec, actualErr := SplitAncestorSpec(test.inputStr)
    69  
    70  		if actualErr != nil {
    71  			if !test.expectErr {
    72  				t.Error(test.inputStr, "- unexpected err")
    73  			}
    74  		} else if actualCSpecStr != test.expectedCSpecStr {
    75  			t.Error(test.inputStr, "- actual commit spcec:", actualCSpecStr, "expected commit spec:", test.expectedCSpecStr)
    76  		} else if actualASpec.SpecStr != test.expectedASpecStr {
    77  			t.Error(test.inputStr, "- actual commit spcec:", actualCSpecStr, "expected commit spec:", test.expectedCSpecStr)
    78  		}
    79  	}
    80  }