github.com/hasnat/dolt/go@v0.0.0-20210628190320-9eb5d843fbb7/store/merge/three_way_list_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  // This file incorporates work covered by the following copyright and
    16  // permission notice:
    17  //
    18  // Copyright 2016 Attic Labs, Inc. All rights reserved.
    19  // Licensed under the Apache License, version 2.0:
    20  // http://www.apache.org/licenses/LICENSE-2.0
    21  
    22  package merge
    23  
    24  import (
    25  	"context"
    26  	"testing"
    27  
    28  	"github.com/stretchr/testify/suite"
    29  
    30  	"github.com/dolthub/dolt/go/store/types"
    31  )
    32  
    33  func TestThreeWayListMerge(t *testing.T) {
    34  	suite.Run(t, &ThreeWayListMergeSuite{})
    35  }
    36  
    37  type ThreeWayListMergeSuite struct {
    38  	ThreeWayMergeSuite
    39  }
    40  
    41  func (s *ThreeWayListMergeSuite) SetupSuite() {
    42  	s.create = func(i seq) (types.Value, error) {
    43  		if i != nil {
    44  			items, err := valsToTypesValues(s.create, i.items()...)
    45  
    46  			if err != nil {
    47  				return nil, err
    48  			}
    49  
    50  			val, err := types.NewList(context.Background(), s.vs, items...)
    51  
    52  			if err != nil {
    53  				return nil, err
    54  			}
    55  
    56  			return val, nil
    57  		}
    58  
    59  		return nil, nil
    60  	}
    61  	s.typeStr = "List"
    62  }
    63  
    64  var p = items{"a", "b", "c", "d", "e"}
    65  
    66  func (s *ThreeWayListMergeSuite) TestThreeWayMerge_DoNothing() {
    67  	s.tryThreeWayMerge(nil, nil, p, p)
    68  }
    69  
    70  func (s *ThreeWayListMergeSuite) TestThreeWayMerge_NoLengthChange() {
    71  	a := items{"a", 1, "c", "d", "e"}
    72  	b := items{"a", "b", "c", 2, "e"}
    73  	m := items{"a", 1, "c", 2, "e"}
    74  	s.tryThreeWayMerge(a, b, p, m)
    75  	s.tryThreeWayMerge(b, a, p, m)
    76  }
    77  
    78  func (s *ThreeWayListMergeSuite) TestThreeWayMerge_HandleEmpty() {
    79  	s.tryThreeWayMerge(p, items{}, items{}, p)
    80  	s.tryThreeWayMerge(items{}, p, items{}, p)
    81  	s.tryThreeWayMerge(p, p, items{}, p)
    82  }
    83  
    84  func (s *ThreeWayListMergeSuite) TestThreeWayMerge_HandleNil() {
    85  	s.tryThreeWayMerge(p, items{}, nil, p)
    86  }
    87  
    88  func (s *ThreeWayListMergeSuite) TestThreeWayMerge_MakeLonger() {
    89  	a := items{"a", 1, 2, "c", "d", "e"}
    90  	b := items{"a", "b", "c", 3, "e"}
    91  	m := items{"a", 1, 2, "c", 3, "e"}
    92  	s.tryThreeWayMerge(a, b, p, m)
    93  	s.tryThreeWayMerge(b, a, p, m)
    94  
    95  }
    96  
    97  func (s *ThreeWayListMergeSuite) TestThreeWayMerge_MakeShorter() {
    98  	a := items{"a", "c", "d", "e"}
    99  	b := items{"a", "b", "c", 3, "e"}
   100  	m := items{"a", "c", 3, "e"}
   101  	s.tryThreeWayMerge(a, b, p, m)
   102  	s.tryThreeWayMerge(b, a, p, m)
   103  }
   104  
   105  func (s *ThreeWayListMergeSuite) TestThreeWayMerge_BothSidesRemove() {
   106  	a := items{"a", "c", "d", "e"}
   107  	b := items{"a", "b", "c", "e"}
   108  	m := items{"a", "c", "e"}
   109  	s.tryThreeWayMerge(a, b, p, m)
   110  	s.tryThreeWayMerge(b, a, p, m)
   111  }
   112  
   113  func (s *ThreeWayListMergeSuite) TestThreeWayMerge_OverlapSameRemoveNoInsert() {
   114  	a := items{"a", "d", "e"}
   115  	b := items{"a", "d", "e"}
   116  	m := items{"a", "d", "e"}
   117  	s.tryThreeWayMerge(a, b, p, m)
   118  	s.tryThreeWayMerge(b, a, p, m)
   119  }
   120  
   121  func (s *ThreeWayListMergeSuite) TestThreeWayMerge_OverlapSameRemoveSameInsert() {
   122  	a := items{"a", 1, 2, 3, "d", "e"}
   123  	b := items{"a", 1, 2, 3, "d", "e"}
   124  	m := items{"a", 1, 2, 3, "d", "e"}
   125  	s.tryThreeWayMerge(a, b, p, m)
   126  	s.tryThreeWayMerge(b, a, p, m)
   127  }
   128  
   129  func (s *ThreeWayListMergeSuite) TestThreeWayMerge_RemoveUpToOtherSideInsertionPoint() {
   130  	a := items{"a", 1, 2, "c", "d", "e"}
   131  	b := items{"a", "b", 3, "c", "d", "e"}
   132  	m := items{"a", 1, 2, 3, "c", "d", "e"}
   133  	s.tryThreeWayMerge(a, b, p, m)
   134  	s.tryThreeWayMerge(b, a, p, m)
   135  }
   136  
   137  func (s *ThreeWayListMergeSuite) TestThreeWayMerge_ConflictingAppends() {
   138  	a := append(p, 1)
   139  	b := append(p, 2)
   140  	s.tryThreeWayConflict(mustValue(s.create(a)), mustValue(s.create(b)), mustValue(s.create(p)), "Overlapping splices: 0 elements removed at 5; adding 1 elements")
   141  	s.tryThreeWayConflict(mustValue(s.create(b)), mustValue(s.create(a)), mustValue(s.create(p)), "Overlapping splices: 0 elements removed at 5; adding 1 elements")
   142  }
   143  
   144  func (s *ThreeWayListMergeSuite) TestThreeWayMerge_OverlappingRemoves() {
   145  	a := p[:4]
   146  	b := p[:3]
   147  	s.tryThreeWayConflict(mustValue(s.create(a)), mustValue(s.create(b)), mustValue(s.create(p)), "Overlapping splices: 1 elements removed at 4")
   148  	s.tryThreeWayConflict(mustValue(s.create(b)), mustValue(s.create(a)), mustValue(s.create(p)), "Overlapping splices: 2 elements removed at 3")
   149  }
   150  
   151  func (s *ThreeWayListMergeSuite) TestThreeWayMerge_SameRemoveAddPrefix() {
   152  	a := items{"a", "b", "c", 1}
   153  	b := items{"a", "b", "c", 1, 2}
   154  	s.tryThreeWayConflict(mustValue(s.create(a)), mustValue(s.create(b)), mustValue(s.create(p)), "Overlapping splices: 2 elements removed at 3; adding 1 elements")
   155  	s.tryThreeWayConflict(mustValue(s.create(b)), mustValue(s.create(a)), mustValue(s.create(p)), "Overlapping splices: 2 elements removed at 3; adding 2 elements")
   156  }
   157  
   158  func (s *ThreeWayListMergeSuite) TestThreeWayMerge_RemoveSupersetAddPrefix() {
   159  	a := items{"a", "b", "c", 1, 2}
   160  	b := items{"a", "b", "c", "d", 1}
   161  	s.tryThreeWayConflict(mustValue(s.create(a)), mustValue(s.create(b)), mustValue(s.create(p)), "Overlapping splices: 2 elements removed at 3; adding 2 elements")
   162  	s.tryThreeWayConflict(mustValue(s.create(b)), mustValue(s.create(a)), mustValue(s.create(p)), "Overlapping splices: 1 elements removed at 4; adding 1 elements")
   163  }
   164  
   165  func (s *ThreeWayListMergeSuite) TestThreeWayMerge_RemoveOtherSideInsertionPoint() {
   166  	a := items{"a", "c", "d", "e"}
   167  	b := items{"a", 1, "b", "c", "d", "e"}
   168  	s.tryThreeWayConflict(mustValue(s.create(a)), mustValue(s.create(b)), mustValue(s.create(p)), "Overlapping splices: 1 elements removed at 1; adding 0 elements")
   169  	s.tryThreeWayConflict(mustValue(s.create(b)), mustValue(s.create(a)), mustValue(s.create(p)), "Overlapping splices: 0 elements removed at 1; adding 1 elements")
   170  }