github.com/hasnat/dolt/go@v0.0.0-20210628190320-9eb5d843fbb7/libraries/doltcore/doltdb/commit_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  	"testing"
    19  
    20  	"github.com/dolthub/dolt/go/libraries/utils/test"
    21  	"github.com/dolthub/dolt/go/store/hash"
    22  )
    23  
    24  func TestCommitRegex(t *testing.T) {
    25  	for i := 0; i < 32; i++ {
    26  		data := test.RandomData(hash.ByteLen)
    27  
    28  		var hashVal hash.Hash
    29  		copy(hashVal[:], data)
    30  
    31  		hashStr := hashVal.String()
    32  
    33  		if !hashRegex.MatchString(hashStr) {
    34  			t.Error(hashStr, ": random hash failed to match hash regex.")
    35  		}
    36  	}
    37  }
    38  
    39  func TestNewCommitSpec(t *testing.T) {
    40  	tests := []struct {
    41  		inputStr        string
    42  		expectedRefStr  string
    43  		expecteASpecStr string
    44  		expectErr       bool
    45  	}{
    46  		{"master", "master", "", false},
    47  		{"refs/heads/master", "refs/heads/master", "", false},
    48  		{"head", "head", "", false},
    49  		{"head", "head", "", false},
    50  		{"head^~2", "head", "^~2", false},
    51  		{"00000000000000000000000000000000", "00000000000000000000000000000000", "", false},
    52  		{"head", "head", "", true},
    53  	}
    54  
    55  	for _, test := range tests {
    56  		cs, err := NewCommitSpec(test.inputStr)
    57  
    58  		if err != nil {
    59  			if !test.expectErr {
    60  				t.Error(test.inputStr, "Error didn't match expected.  Errored: ", err != nil)
    61  			}
    62  		} else if cs.baseSpec != test.expectedRefStr {
    63  			t.Error(test.inputStr, "expected name:", test.expectedRefStr, "actual name:", cs.baseSpec)
    64  		} else if cs.aSpec.SpecStr != test.expecteASpecStr {
    65  			t.Error(test.inputStr, "expected ancestor spec:", test.expecteASpecStr, "actual ancestor spec:", cs.aSpec.SpecStr)
    66  		}
    67  	}
    68  }