// Copyright (C) 2018 rameshvk. All rights reserved.
// Use of this source code is governed by a MIT-style license
// that can be found in the LICENSE file.

package refs_test

import (
	"github.com/dotchain/dot/changes"
	"github.com/dotchain/dot/changes/types"
	"github.com/dotchain/dot/refs"

	"reflect"
	"testing"
)

func TestInvalidRef(t *testing.T) {
	c := changes.Move{Offset: 2, Count: 2, Distance: 2}

	r, cx := refs.InvalidRef.Merge(c)
	if r != refs.InvalidRef || cx != nil {
		t.Error("InvalidRef failed merge", r, cx)
	}

	if !refs.InvalidRef.Equal(refs.InvalidRef) {
		t.Error("InvalidRef does not equal itself")
	}

	if refs.InvalidRef.Equal(refs.Path{5}) {
		t.Error("InvalidRef equals something else")
	}
}

func TestMerge(t *testing.T) {
	move1 := changes.Move{Offset: 0, Count: 1, Distance: 10}
	move2 := changes.Move{Offset: 10, Count: 1, Distance: 10}
	c := changes.ChangeSet{move1, move2}
	r := refs.Merge([]interface{}{4}, c)
	expected := &refs.MergeResult{P: []interface{}{3}, Unaffected: c}
	if r == nil || !reflect.DeepEqual(r, expected) {
		t.Error("Merge failed", r)
	}

	replace := changes.Replace{Before: changes.Nil, After: types.S8("ok")}
	replace1 := changes.PathChange{Path: []interface{}{4, "key"}, Change: replace}
	replace2 := changes.PathChange{Path: []interface{}{3}, Change: replace}
	c = changes.ChangeSet{replace1, replace2}
	r = refs.Merge([]interface{}{4}, c)
	expected = &refs.MergeResult{
		P:          []interface{}{4},
		Scoped:     changes.PathChange{Path: []interface{}{"key"}, Change: replace},
		Affected:   replace1,
		Unaffected: replace2,
	}
	if r == nil || !reflect.DeepEqual(r, expected) {
		t.Error("Merge failed", r.Affected)
	}

	move3 := changes.Move{Offset: 2, Count: 2, Distance: 2}
	move4 := changes.Move{Offset: 1, Count: 1, Distance: 1}
	c = changes.ChangeSet{
		changes.PathChange{Path: []interface{}{4}, Change: move1},
		changes.ChangeSet{
			changes.PathChange{Path: []interface{}{4}, Change: move2},
			changes.PathChange{Path: []interface{}{4}, Change: move3},
		},
		changes.ChangeSet{
			changes.PathChange{Path: []interface{}{4}, Change: move2},
			changes.PathChange{Path: []interface{}{4}, Change: move3},
		},
		changes.PathChange{Path: []interface{}{4}, Change: move4},
	}
	r = refs.Merge([]interface{}{4}, c)
	cx := changes.ChangeSet{
		changes.PathChange{Path: []interface{}{}, Change: move1},
		changes.PathChange{Path: []interface{}{}, Change: move2},
		changes.PathChange{Path: []interface{}{}, Change: move3},
		changes.PathChange{Path: []interface{}{}, Change: move2},
		changes.PathChange{Path: []interface{}{}, Change: move3},
		changes.PathChange{Path: []interface{}{}, Change: move4},
	}
	expected = &refs.MergeResult{
		P:      []interface{}{4},
		Scoped: cx,
		Affected: changes.ChangeSet{
			changes.PathChange{Path: []interface{}{4}, Change: move1},
			changes.PathChange{Path: []interface{}{4}, Change: move2},
			changes.PathChange{Path: []interface{}{4}, Change: move3},
			changes.PathChange{Path: []interface{}{4}, Change: move2},
			changes.PathChange{Path: []interface{}{4}, Change: move3},
			changes.PathChange{Path: []interface{}{4}, Change: move4},
		},
	}

	if r == nil || !reflect.DeepEqual(r, expected) {
		t.Error("Merge failed", r)
	}
}

Related articles

dot union

// Copyright (C) 2019 rameshvk. All rights reserved. // Use of this source code is governed by a MIT-style license // that can be found in the LICENSE file. package dotc import ( "io" "text/template" ) // Union has the type information of a union fo

dot generic

// Copyright (C) 2018 rameshvk. All rights reserved. // Use of this source code is governed by a MIT-style license // that can be found in the LICENSE file. package types import "github.com/dotchain/dot/changes" // Generic is a helper to build value a

dot ord

// Copyright (C) 2019 rameshvk. All rights reserved. // Use of this source code is governed by a MIT-style license // that can be found in the LICENSE file. package crdt import ( "math/big" "strconv" "strings" ) // NextOrd returns the next ordinal.

dot heading_test

// Copyright (C) 2019 rameshvk. All rights reserved. // Use of this source code is governed by a MIT-style license // that can be found in the LICENSE file. package data_test import ( "reflect" "testing" "github.com/dotchain/dot/changes" "github.c

dot typename

// Copyright (C) 2018 rameshvk. All rights reserved. // Use of this source code is governed by a MIT-style license // that can be found in the LICENSE file. package sjson import ( "errors" "reflect" "strconv" "strings" "time" ) func typeName(v re

dot go

github.com/etcd-io/bbolt v1.3.2 h1:RLRQ0TKLX7DlBRXAJHvbmXL17Q3KNnTBtZ9B6Qo+/Y0= github.com/etcd-io/bbolt v1.3.2/go.mod h1:ZF2nL25h33cCyBtcyWeZ2/I3HQOfTP+0PIEvHjkjCrw= github.com/google/go-cmp v0.2.0 h1:+dTQ8DZQJz0Mb/HjFlkptS1FeQ4cWSnN941F8aEG4SQ= github.

dot setattr_test

// Copyright (C) 2019 rameshvk. All rights reserved. // Use of this source code is governed by a MIT-style license // that can be found in the LICENSE file. package rich_test import ( "testing" "github.com/dotchain/dot/changes" "github.com/dotchain

dot poller_test

// Copyright (C) 2018 rameshvk. All rights reserved. // Use of this source code is governed by a MIT-style license // that can be found in the LICENSE file. package ops_test import ( "context" "errors" "testing" "time" "github.com/dotchain/dot/op

dot row

// Copyright (C) 2019 rameshvk. All rights reserved. // Use of this source code is governed by a MIT-style license // that can be found in the LICENSE file. package data import ( "github.com/dotchain/dot/changes" "github.com/dotchain/dot/changes/type

dot int_test

// Copyright (C) 2018 rameshvk. All rights reserved. // Use of this source code is governed by a MIT-style license // that can be found in the LICENSE file. package streams_test import ( "reflect" "testing" "github.com/dotchain/dot/changes" "githu