Skip to content

Commit

Permalink
Tests: Cover all cases for file.go
Browse files Browse the repository at this point in the history
  • Loading branch information
mkchoi212 committed May 20, 2018
1 parent ddf05f7 commit 7d32441
Show file tree
Hide file tree
Showing 7 changed files with 181 additions and 133 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ dist
*.out

# Dummy conflict code
conflict/testdata/output.swift
conflict/testdata/.test_output

### Code ###
# Visual Studio Code - https://code.visualstudio.com/
Expand Down
2 changes: 1 addition & 1 deletion conflict/conflict_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ func TestHighlight(t *testing.T) {

conflicts, err := parseConflictsIn(f, test.markers)
if err != nil {
if !test.parsable {
if !test.shouldPass {
continue
}
t.Error("conflict/parseConflicts failed")
Expand Down
92 changes: 46 additions & 46 deletions conflict/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,59 +2,59 @@ package conflict

import (
"reflect"
"strings"
"testing"
)

var conflictFile = struct {
readPath string
writePath string
markers []int
lineNum int
}{
readPath: "testdata/CircularCrownSelector.swift",
writePath: "testdata/output.swift",
markers: []int{14, 22, 30, 38},
lineNum: 39,
}

func TestRead(t *testing.T) {
f := File{AbsolutePath: conflictFile.readPath}
if err := f.Read(); err != nil {
t.Error("Read failed: could not read file")
}

if len(f.Lines) != conflictFile.lineNum {
t.Errorf("Read failed: got %d lines, wanted %d lines", len(f.Lines), conflictFile.lineNum)
for _, test := range tests {
f := File{AbsolutePath: test.path}
if err := f.Read(); err != nil {
t.Error("Read failed: could not read file")
}

if len(f.Lines) != test.numLines {
t.Errorf("Read failed: got %d lines, wanted %d lines", len(f.Lines), test.numLines)
}
}
}

func TestWriteChanges(t *testing.T) {
f := File{AbsolutePath: conflictFile.readPath}
if err := f.Read(); err != nil {
t.Error("WriteChanges/Read failed")
}

conflicts, err := parseConflictsIn(f, conflictFile.markers)
if err != nil {
t.Error("WriteChanges/parseConflicts failed")
}

f.Conflicts = conflicts
targetConflict := &f.Conflicts[0]
targetConflict.Choice = Local

f.AbsolutePath = conflictFile.writePath
if err := f.WriteChanges(); err != nil {
t.Errorf("WriteChages failed: %s", err.Error())
}

expected := f.Lines[11:22]
f.Lines = nil
if err := f.Read(); err != nil {
t.Error("WriteChanges/Read failed")
}

output := f.Lines[11:]
if reflect.DeepEqual(output, expected) {
for _, test := range tests {
f := File{AbsolutePath: test.path}
if err := f.Read(); err != nil {
t.Fatalf("WriteChanges/Read failed")
}

conflicts, err := parseConflictsIn(f, test.markers)
if err != nil {
if !test.shouldPass {
continue
}
t.Fatal("WriteChanges/parseConflicts failed")
}

for i := range test.resolveDecision {
conflicts[i].Choice = test.resolveDecision[i]
}
f.Conflicts = conflicts

f.AbsolutePath = "testdata/.test_output"
if err := f.WriteChanges(); err != nil {
t.Errorf("WriteChages failed: %s", err.Error())
}

f.Lines = nil
if err := f.Read(); err != nil {
t.Error("WriteChanges/Read failed")
}

for i := range f.Lines {
f.Lines[i] = strings.TrimSuffix(f.Lines[i], "\n")
}

if !(reflect.DeepEqual(f.Lines, test.resolved)) {
t.Errorf("WriteChanges failed: got %v, want %v", f.Lines, test.resolved)
}
}
}
6 changes: 3 additions & 3 deletions conflict/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func TestParseGitInfo(t *testing.T) {
output = append(output, lineNum)
}

if !(reflect.DeepEqual(output, test.markers)) && test.parsable {
if !(reflect.DeepEqual(output, test.markers)) && test.shouldPass {
t.Errorf("parseGitInfo failed: got %v, want %v", output, test.markers)
}
}
Expand All @@ -31,7 +31,7 @@ func TestParseConflictsIn(t *testing.T) {
}

_, err := parseConflictsIn(f, test.markers)
if err != nil && test.parsable {
if err != nil && test.shouldPass {
t.Errorf("parseConflicts failed: %s", err.Error())
}
}
Expand All @@ -52,7 +52,7 @@ func TestFind(t *testing.T) {

for _, f := range files {
for _, test := range tests {
if f.AbsolutePath == test.path && test.parsable {
if f.AbsolutePath == test.path && test.shouldPass {
if len(f.Conflicts) != test.numConflicts {
t.Errorf("Find failed: got %d conflicts in %s, want %d",
len(f.Conflicts), test.path, test.numConflicts)
Expand Down
150 changes: 119 additions & 31 deletions conflict/test.go
Original file line number Diff line number Diff line change
@@ -1,46 +1,64 @@
package conflict

type test struct {
path string
diffCheck []string
markers []int
numConflicts int
path string
diffCheck []string

markers []int
resolved []string
resolveDecision []int

numConflicts int
numLines int

highlightable bool
parsable bool
shouldPass bool
}

var tests = []test{
{
path: "testdata/assets/README.md",
diffCheck: readmeDiffCheck,
markers: []int{20, 22, 24, 26, 32, 35, 38, 41},
numConflicts: 2,
highlightable: true,
parsable: true,
path: "testdata/assets/README.md",
diffCheck: readmeDiffCheck,
markers: []int{20, 22, 24, 26, 32, 35, 38, 41},
resolved: readmeResolved,
resolveDecision: []int{Local},
numConflicts: 2,
numLines: 43,
highlightable: true,
shouldPass: true,
},
{
path: "testdata/CircularCrownSelector.swift",
diffCheck: ccDiffCheck,
markers: []int{14, 22, 30, 38},
numConflicts: 1,
highlightable: true,
parsable: true,
path: "testdata/CircularCrownSelector.swift",
diffCheck: ccDiffCheck,
markers: []int{14, 22, 30, 38},
resolved: ccResolved,
resolveDecision: []int{Incoming},
numConflicts: 1,
numLines: 39,
highlightable: true,
shouldPass: true,
},
{
path: "testdata/lorem_ipsum",
diffCheck: loremDiffCheck,
markers: []int{3, 6, 9},
numConflicts: 1,
highlightable: false,
parsable: true,
path: "testdata/lorem_ipsum",
diffCheck: loremDiffCheck,
markers: []int{3, 6, 9},
resolved: loremResolved,
resolveDecision: []int{Local},
numConflicts: 1,
numLines: 11,
highlightable: false,
shouldPass: true,
},
{
path: "testdata/lorem_ipsum",
diffCheck: loremDiffCheck,
markers: []int{3, 9},
numConflicts: 0,
highlightable: false,
parsable: false,
path: "testdata/lorem_ipsum",
diffCheck: loremDiffCheck,
markers: []int{3, 9},
resolved: nil,
resolveDecision: nil,
numConflicts: 0,
numLines: 11,
highlightable: false,
shouldPass: false,
},
}

Expand Down Expand Up @@ -94,7 +112,77 @@ var readmeDiffCheck = []string{
"assets/README.md:41: leftover conflict marker",
"assets/README.md:41: trailing whitespace.",
"+>>>>>>> Stashed changes:README.md",
"assets/README.md:46: trailing whitespace.",
"+> **Please note facc does NOT support diff3 merge conflict outputs yet!**",
}

var readmeResolved = []string{
"<p align=\"center\">",
"<img src=\"./assets/header.png\">",
"<p align=\"center\">",
"Easy-to-get CUI for fixing git conflicts",
"<br>",
"<br>",
"</p>",
"</p>",
"<br>",
"",
"I never really liked any of the `synthesizers` out there so I made a simple program that does simple things… in a simple fashion.",
"",
"![](./assets/overview.png)",
"",
"## 👷 Installation",
"",
"Execute:",
"",
"```bash",
"$ go get github.com/mkchoi212/fac",
"```",
"",
"Or using [Homerotten_brew](https://rotten_brew.sh)",
"",
"```bash",
"<<<<<<< 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",
"```",
"",
}

var ccResolved = []string{
"private func generateInitials() -> [String] {",
" let randomString = UUID().uuidString",
" let str = randomString.replacingOccurrences(of: \"-\", with: \"\")",
"",
" let abbrev = stride(from: 0, to: 18, by: 2).map { i -> String in",
" let start = str.index(str.startIndex, offsetBy: i)",
" let end = str.index(str.startIndex, offsetBy: i + 2)",
" return String(str[start..<end])",
" }",
"",
" return abbrev",
"}",
"",
"",
"private func randomColor() -> UIColor{",
" let red = CGFloat(arc4random()) ",
" let green = CGFloat(arc4random()) ",
" let blue = CGFloat(arc4random()) ",
" return UIColor(red:red, green: green, blue: blue, alpha: 1.0)",
"}",
"",
}

var loremResolved = []string{
"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse placerat malesuada egestas.",
"Cras nunc lectus, pharetra ut pharetra ac, imperdiet sit amet sem.",
"Sed feugiat odio odio, at malesuada justo dictum ut.",
"Fusce sit amet efficitur ante. Maecenas consequat mollis laoreet. ",
"Morbi volutpat libero justo, quis aliquam elit consectetur in. ",
"Nulla nec molestie massa, a lacinia sapien.",
}
10 changes: 5 additions & 5 deletions conflict/testdata/CircularCrownSelector.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ private func generateInitials() -> [String] {
let str = randomString.replacingOccurrences(of: "-", with: "")

let abbrev = stride(from: 0, to: 18, by: 2).map { i -> String in
let start = str.index(str.startIndex, offsetBy: i)
let end = str.index(str.startIndex, offsetBy: i + 2)
return String(str[start..<end])
let start = str.index(str.startIndex, offsetBy: i)
let end = str.index(str.startIndex, offsetBy: i + 2)
return String(str[start..<end])
}

return abbrev
}

Expand All @@ -34,5 +34,5 @@ private func randomColor() -> UIColor{
let green = CGFloat(arc4random())
let blue = CGFloat(arc4random())
return UIColor(red:red, green: green, blue: blue, alpha: 1.0)
}
}
>>>>>>> Stashed changes
Loading

0 comments on commit 7d32441

Please sign in to comment.