forked from attic-labs/noms
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
3 changed files
with
190 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
// Copyright 2016 Attic Labs, Inc. All rights reserved. | ||
// Licensed under the Apache License, version 2.0: | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
package merge | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/attic-labs/noms/go/types" | ||
"github.com/attic-labs/testify/suite" | ||
) | ||
|
||
func TestThreeWaySetMerge(t *testing.T) { | ||
suite.Run(t, &ThreeWaySetMergeSuite{}) | ||
} | ||
|
||
type items []interface{} | ||
|
||
func (kv items) items() []interface{} { | ||
return kv | ||
} | ||
|
||
func (kv items) remove(k interface{}) items { | ||
out := make(items, 0, len(kv)) | ||
for i := 0; i < len(kv); i++ { | ||
switch k := k.(type) { | ||
default: | ||
if kv[i] == k { | ||
continue | ||
} | ||
case types.Value: | ||
if item, ok := kv[i].(types.Value); ok && k.Equals(item) { | ||
continue | ||
} | ||
} | ||
out = append(out, kv[i]) | ||
} | ||
return out | ||
} | ||
|
||
func (kv items) set(idx int, v interface{}) (out items) { | ||
copy(out, kv) | ||
out[idx] = v | ||
return | ||
} | ||
|
||
type ThreeWaySetMergeSuite struct { | ||
ThreeWayMergeSuite | ||
} | ||
|
||
func (s *ThreeWaySetMergeSuite) SetupSuite() { | ||
s.create = func(i seq) (val types.Value) { | ||
if i != nil { | ||
keyValues := valsToTypesValues(s.create, i.items()...) | ||
val = types.NewSet(keyValues...) | ||
} | ||
return | ||
} | ||
s.typeStr = "Set" | ||
} | ||
|
||
var ( | ||
flat = items{"a1", "a2", "a3", "a4"} | ||
flatA = items{"a1", "a2", "a5", "a6"} | ||
flatB = items{"a1", "a4", "a7", "a5"} | ||
flatM = items{"a1", "a5", "a6", "a7"} | ||
|
||
ss1 = items{} | ||
ss1a = items{"k1", flatA, items{"a", 0}} | ||
ss1b = items{"k1", items{"a", 0}, flatB} | ||
ss1Merged = items{"k1", items{"a", 0}, flatA, flatB} | ||
) | ||
|
||
func (s *ThreeWaySetMergeSuite) TestThreeWayMerge_DoNothing() { | ||
s.tryThreeWayMerge(nil, nil, flat, flat, nil) | ||
} | ||
|
||
func (s *ThreeWaySetMergeSuite) TestThreeWayMerge_Primitives() { | ||
s.tryThreeWayMerge(flatA, flatB, flat, flatM, nil) | ||
s.tryThreeWayMerge(flatB, flatA, flat, flatM, nil) | ||
} | ||
|
||
func (s *ThreeWaySetMergeSuite) TestThreeWayMerge_HandleEmpty() { | ||
s.tryThreeWayMerge(ss1a, ss1b, ss1, ss1Merged, nil) | ||
s.tryThreeWayMerge(ss1b, ss1a, ss1, ss1Merged, nil) | ||
} | ||
|
||
func (s *ThreeWaySetMergeSuite) TestThreeWayMerge_HandleNil() { | ||
s.tryThreeWayMerge(ss1a, ss1b, nil, ss1Merged, nil) | ||
s.tryThreeWayMerge(ss1b, ss1a, nil, ss1Merged, nil) | ||
} | ||
|
||
func (s *ThreeWaySetMergeSuite) TestThreeWayMerge_Refs() { | ||
vs := types.NewTestValueStore() | ||
|
||
strRef := vs.WriteValue(types.NewStruct("Foo", types.StructData{"life": types.Number(42)})) | ||
|
||
m := items{vs.WriteValue(s.create(flatA)), vs.WriteValue(s.create(flatB))} | ||
ma := items{"r1", vs.WriteValue(s.create(flatA))} | ||
mb := items{"r1", strRef, vs.WriteValue(s.create(flatA))} | ||
mMerged := items{"r1", strRef, vs.WriteValue(s.create(flatA))} | ||
vs.Flush() | ||
|
||
s.tryThreeWayMerge(ma, mb, m, mMerged, vs) | ||
s.tryThreeWayMerge(mb, ma, m, mMerged, vs) | ||
} | ||
|
||
func (s *ThreeWaySetMergeSuite) TestThreeWayMerge_ImmediateConflict() { | ||
s.tryThreeWayConflict(types.NewMap(), s.create(ss1b), s.create(ss1), "Cannot merge Map<, > with "+s.typeStr) | ||
s.tryThreeWayConflict(s.create(ss1b), types.NewMap(), s.create(ss1), "Cannot merge "+s.typeStr) | ||
} |