Skip to content

Commit

Permalink
Test: Add tests for conflict.go
Browse files Browse the repository at this point in the history
  • Loading branch information
mkchoi212 committed May 19, 2018
1 parent 9dd4809 commit a2da952
Showing 1 changed file with 147 additions and 16 deletions.
163 changes: 147 additions & 16 deletions conflict/conflict_test.go
Original file line number Diff line number Diff line change
@@ -1,36 +1,167 @@
package conflict

import (
"reflect"
"testing"
)

func TestEqual(t *testing.T) {
c1, c2, c3 := Conflict{}, Conflict{}, Conflict{}
var dummyFile = struct {
lines []string
start int
middle int
end int
diff3 []int
}{
lines: []string{
"<<<<<<< Updated upstream:assets/README.md",
"brew tap mkchoi212/fac https://github.com/mkchoi212/fac.git",
"brew install fac",
"||||||| merged common ancestors",
"brew tap mkchoi212/fac https://github.com/parliament/fac.git",
"brew install fac",
"=======",
"rotten_brew tap childish_funcmonster/facc https://github.com/parliament/facc.git",
"rotten_brew install facc",
">>>>>>> Stashed changes:README.md",
"hello",
"world",
},
start: 0,
diff3: []int{3},
middle: 6,
end: 9,
}

func TestIdentifyStyle(t *testing.T) {
styles := []int{}

for _, line := range dummyFile.lines {
style := IdentifyStyle(line)
if style != text {
styles = append(styles, style)
}
}

expected := []int{start, diff3, separator, end}
if !(reflect.DeepEqual(styles, expected)) {
t.Errorf("IdentifyStyle failed: got %v, want %v", styles, expected)
}
}

func TestValidConflict(t *testing.T) {
invalid := Conflict{CurrentName: "foobar"}
if invalid.Valid() {
t.Errorf("Valid failed: got %t, want %t", true, false)
}

valid := Conflict{
File: &File{},
CurrentName: "foo",
ForeignName: "bar",
Middle: 4,
End: 8,
}
if !(valid.Valid()) {
t.Errorf("Valid failed: got %t, want %t", false, true)
}
}

func TestExtract(t *testing.T) {
c := Conflict{
Start: dummyFile.start,
Middle: dummyFile.middle,
End: dummyFile.end,
Diff3: dummyFile.diff3,
}

if err := c.Extract(dummyFile.lines); err != nil {
t.Errorf("Extract failed: could not parse file lines")
}

expectedCurrentName := "Updated"
if expectedCurrentName != c.CurrentName {
t.Errorf("Extract failed: got %s want %s", c.CurrentName, expectedCurrentName)
}

expectedForeignName := "Stashed"
if expectedForeignName != c.ForeignName {
t.Errorf("Extract failed: got %s want %s", c.ForeignName, expectedForeignName)
}

expectedIncoming := dummyFile.lines[dummyFile.middle+1 : dummyFile.end]
if !(reflect.DeepEqual(c.IncomingLines, expectedIncoming)) {
t.Errorf("Extract failed: got %v want %v", c.IncomingLines, expectedIncoming)
}

c1.FileName, c2.FileName, c3.FileName = "foobar", "foobar", "foobar"
c1.Start, c2.Start, c3.Start = 45, 45, 45
expectedPureIncoming := dummyFile.lines[dummyFile.start+1 : dummyFile.diff3[0]]
if !(reflect.DeepEqual(c.LocalPureLines, expectedPureIncoming)) {
t.Errorf("Extract failed: got %v want %v", c.LocalPureLines, expectedPureIncoming)
}
}

c1.AbsolutePath, c2.AbsolutePath = "/path/foobar", "/path/foobar"
c3.AbsolutePath = "/other/path/foobar"
func TestEqual(t *testing.T) {
foo := File{AbsolutePath: "/bin/foo"}
bar := File{AbsolutePath: "/bin/bar"}

c1, c2 := Conflict{Start: 45, File: &foo}, Conflict{Start: 45, File: &foo}
c3 := Conflict{Start: 45, File: &bar}

if c1.Equal(&c2) != true {
t.Errorf("%v and %v should be equal", c1, c2)
t.Errorf("Equal failed: got %t, want %t", false, true)
}

if c1.Equal(&c3) != false {
t.Errorf("%v and %v should NOT be equal", c1, c3)
t.Errorf("Equal failed: got %t, want %t", true, false)
}
}

func TestToggleDiff(t *testing.T) {
c1 := Conflict{}
c1.ToggleDiff()
if c1.DisplayDiff != true {
t.Errorf("%v should be toggled ON", c1)
func TestPaddingLines(t *testing.T) {
f := File{Lines: dummyFile.lines}
c := Conflict{
Start: dummyFile.start,
End: dummyFile.end,
File: &f,
}

c1.ToggleDiff()
if c1.DisplayDiff != false {
t.Errorf("%v should be toggled OFF", c1)
top, bottom := c.PaddingLines()
if len(top) != 0 || len(bottom) != 0 {
t.Errorf("PaddingLines failed: got %d, %d lines, want 0, 0 lines", len(top), len(bottom))
}

c.TopPeek--
c.BottomPeek++
top, bottom = c.PaddingLines()
expectedBottom := f.Lines[dummyFile.end+1]
if len(top) != 0 || reflect.DeepEqual(bottom, expectedBottom) {
t.Errorf("PaddingLines failed: got %v, want %v", bottom, expectedBottom)
}

}

func TestHighlight(t *testing.T) {
for _, test := range tests {
f := File{AbsolutePath: test.path, Name: test.path}

if err := f.Read(); err != nil && test.highlightable {
t.Error("conflict/Read failed")
}

conflicts, err := parseConflictsIn(f, test.markers)
if err != nil {
if !test.parsable {
continue
}
t.Error("conflict/parseConflicts failed")
}

for _, c := range conflicts {
if err := c.HighlightSyntax(); err != nil {
t.Errorf("HighlightSyntax failed: %s", err.Error())
}

if test.highlightable && reflect.DeepEqual(c.IncomingLines, c.ColoredIncomingLines) {
t.Errorf("HighlightSyntax failed: %s has not been highlighted", f.Name)
}
}
}
}

0 comments on commit a2da952

Please sign in to comment.