Skip to content

Commit

Permalink
Add tests for summary.go
Browse files Browse the repository at this point in the history
  • Loading branch information
mkchoi212 committed May 12, 2018
1 parent a04cfa0 commit 2230b51
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 14 deletions.
2 changes: 1 addition & 1 deletion conflict/conflict.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func (c *Conflict) PaddingLines() (topPadding, bottomPadding []string) {
}

// In finds `Conflict`s that are from the provided file name
func In(conflicts []Conflict, fname string) (res []Conflict) {
func In(fname string, conflicts []Conflict) (res []Conflict) {
for _, c := range conflicts {
if c.AbsolutePath == fname && c.Choice != 0 {
res = append(res, c)
Expand Down
6 changes: 4 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,10 @@ func main() {

g.Close()

for fname := range conflict.FileLines {
if err := FinalizeChanges(fname); err != nil {
for absPath := range conflict.FileLines {
targetConflicts := conflict.In(absPath, all)
finalLines := FinalizeChanges(targetConflicts, conflict.FileLines[absPath])
if err = writeChanges(absPath, finalLines); err != nil {
fmt.Println(color.Red(color.Underline, "%s\n", err))
}
}
Expand Down
15 changes: 4 additions & 11 deletions summary.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,20 +74,16 @@ func writeChanges(absPath string, lines []string) (err error) {
return
}

// FinalizeChanges writes the changes the user selected to file
func FinalizeChanges(absPath string) (err error) {
targetConflicts := conflict.In(all, absPath)

// FinalizeChanges constructs final lines of the file with conflicts removed
func FinalizeChanges(conflicts []conflict.Conflict, fileLines []string) []string {
var replacementLines []string
fileLines := conflict.FileLines[absPath]

for _, c := range targetConflicts {
for _, c := range conflicts {
if c.Choice == Local {
replacementLines = append([]string{}, c.LocalPureLines...)
} else {
replacementLines = append([]string{}, c.IncomingLines...)
}

i := 0
for ; i < len(replacementLines); i++ {
fileLines[c.Start+i-1] = replacementLines[i]
Expand All @@ -97,8 +93,5 @@ func FinalizeChanges(absPath string) (err error) {
}
}

if err = writeChanges(absPath, fileLines); err != nil {
return
}
return
return fileLines
}
45 changes: 45 additions & 0 deletions summary_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package main

import (
"strings"
"testing"

"github.com/mkchoi212/fac/conflict"
)

func TestFinalizeChanges(t *testing.T) {
c := conflict.Conflict{}
c.Choice = Local
c.Start = 4
c.End = 10
c.LocalPureLines = []string{
"$ go get github.com/mkchoi212/fac\n",
}

dummyLines := []string{
"## 👷 Installation\n",
"Execute:\n",
"```bash\n",
"<<<<<<< Updated upstream:assets/README.md\n",
"$ go get github.com/mkchoi212/fac\n",
"||||||| merged common ancestors\n",
"$ go get github.com/parliament/fac\n",
"=======\n",
"$ go get github.com/parliament/facc\n",
">>>>>>> Stashed changes:README.md\n",
"```\n",
}

output := strings.Join(FinalizeChanges([]conflict.Conflict{c}, dummyLines), "")
expected := strings.Join([]string{
"## 👷 Installation\n",
"Execute:\n",
"```bash\n",
"$ go get github.com/mkchoi212/fac\n",
"```\n",
}, "")

if output != expected {
t.Errorf("FinalizeChanges was incorrect: got \n%s, want \n%s", output, expected)
}
}

0 comments on commit 2230b51

Please sign in to comment.