From 7e455717d6fbb23a2b37a9d14eedef67b0241e29 Mon Sep 17 00:00:00 2001 From: "Mike JS. Choi" Date: Fri, 18 May 2018 09:27:17 -0500 Subject: [PATCH] Test: Add tests for file.go --- .gitignore | 3 + conflict/file_test.go | 60 +++++++++++++ conflict/testdata/output.swift | 152 --------------------------------- 3 files changed, 63 insertions(+), 152 deletions(-) create mode 100644 conflict/file_test.go delete mode 100644 conflict/testdata/output.swift diff --git a/.gitignore b/.gitignore index f4470fb..a9f1d6a 100644 --- a/.gitignore +++ b/.gitignore @@ -16,6 +16,9 @@ dist # Output of the go coverage tool, specifically when used with LiteIDE *.out +# Dummy conflict code +conflict/testdata/output.swift + ### Code ### # Visual Studio Code - https://code.visualstudio.com/ .settings/ diff --git a/conflict/file_test.go b/conflict/file_test.go new file mode 100644 index 0000000..5b9c806 --- /dev/null +++ b/conflict/file_test.go @@ -0,0 +1,60 @@ +package conflict + +import ( + "reflect" + "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) + } +} + +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) { + } +} diff --git a/conflict/testdata/output.swift b/conflict/testdata/output.swift deleted file mode 100644 index c7a05df..0000000 --- a/conflict/testdata/output.swift +++ /dev/null @@ -1,152 +0,0 @@ -// -// CrownSelectorInterfaceController.swift -// Circular Crown Selector WatchKit Extension -// -// Created by Mike Choi on 11/24/17. -// Copyright © 2017 Mike Choi. All rights reserved. -// - -import WatchKit - -class CrownSelectorInterfaceController: WKInterfaceController, WKCrownDelegate { - @IBOutlet var circle1: WKInterfaceGroup! - @IBOutlet var circle2: WKInterfaceGroup! - @IBOutlet var circle3: WKInterfaceGroup! - @IBOutlet var circle4: WKInterfaceGroup! - @IBOutlet var circle5: WKInterfaceGroup! - @IBOutlet var circle6: WKInterfaceGroup! - @IBOutlet var circle7: WKInterfaceGroup! - @IBOutlet var circle8: WKInterfaceGroup! - @IBOutlet var circle9: WKInterfaceGroup! - @IBOutlet var circle10: WKInterfaceGroup! - @IBOutlet var circle11: WKInterfaceGroup! - @IBOutlet var circle12: WKInterfaceGroup! - @IBOutlet var currentLabel: WKInterfaceLabel! - var circles : [WKInterfaceGroup]! - - var idx: Int! - - var deltaBuildUp: Double! - let sensitivity = 0.2 - var abbrev : [String]! - var fontColors : [UIColor] = [] - - /// #6E4000 - let activeFontColor = #colorLiteral(red: 0.431372549, green: 0.2509803922, blue: 0, alpha: 1) - /// #FF9403 - let activeColor = #colorLiteral(red: 1, green: 0.5803921569, blue: 0.01176470588, alpha: 1) - /// #262628 - let inactiveColor = #colorLiteral(red: 0.1490196078, green: 0.1490196078, blue: 0.1568627451, alpha: 1) - - override func awake(withContext context: Any?) { - super.awake(withContext: context) - - if let abbrev = context as? [String] { - self.abbrev = abbrev - } else { - self.abbrev = generateInitials() - } - abbrev = fill(abbrev, with: "●") - - circles = [c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, c11, c12] - - _ = zip(circles, abbrev).map { (tup) -> Void in - let (cir, str) = tup - let fontColor = randomColor() - fontColors.append(fontColor) - cir.setBackgroundColor(inactiveColor) - return cir.setBackgroundImage(stringToImage(str, color: fontColor)) - } - c1.setBackgroundColor(activeColor) - } - - override func willActivate() { - // Make `crownSequncer` responsive - crownSequencer.delegate = self - crownSequencer.focus() - deltaBuildUp = 0 - idx = 0 - setActive(0) - } - - func fill(_ arr: [String], with str: String) -> [String] { - let diff = 12 - arr.count - let filledArray = arr + Array(repeating: str, count: diff) - return filledArray - } - - // Set group at idx active by changing color attributes - func setActive(_ idx: Int) { - circles[idx].setBackgroundColor(activeColor) - circles[idx].setBackgroundImage(stringToImage(abbrev[idx], color: activeFontColor)) - currentLabel.setText(abbrev[idx]) - } - - func setInActive(_ idx: Int) { - circles[idx].setBackgroundColor(inactiveColor) - circles[idx].setBackgroundImage(stringToImage(abbrev[idx], color: fontColors[idx])) - } - - // MARK: WKCrownDelegate - func crownDidRotate(_ crownSequencer: WKCrownSequencer?, rotationalDelta: Double) { - // Only act on crown rotation if `deltaBuildUp` is greater than sensitivity - // for smoother / controllable scrolling experience - deltaBuildUp = deltaBuildUp.sign != rotationalDelta.sign ? 0 : deltaBuildUp - deltaBuildUp = deltaBuildUp + rotationalDelta - - if abs(deltaBuildUp) < sensitivity { - return - } - - setInActive(idx) - - idx = rotationalDelta > 0 ? idx + 1 : idx - 1; - idx = idx % 12 - if idx < 0 { - idx = 12 + idx - } - - setActive(idx) - deltaBuildUp = 0.0 - } - - // MARK: Helper Functions - private func stringToImage(_ str: String, color: UIColor) -> UIImage? { - let imageSize = CGSize(width: 23, height: 23) - UIGraphicsBeginImageContextWithOptions(imageSize, false, 0) - UIColor.clear.set() - let rect = CGRect(origin: CGPoint.zero, size: imageSize) - UIRectFill(rect) - - let style = NSMutableParagraphStyle() - style.alignment = .center - (str as NSString).draw(in: rect, withAttributes: [NSAttributedStringKey.font: UIFont.systemFont(ofSize: 13), - NSAttributedStringKey.paragraphStyle: style, - NSAttributedStringKey.foregroundColor: color, - NSAttributedStringKey.baselineOffset: -3.0]) - - let image = UIGraphicsGetImageFromCurrentImageContext() - UIGraphicsEndImageContext() - return image - } - - 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.. UIColor { - let hue = ( CGFloat(arc4random() % 256) / 256.0 ) // 0.0 to 1.0 - let saturation = ( CGFloat(arc4random() % 128) / 256.0 ) + 0.5 // 0.5 to 1.0, away from white - let brightness = ( CGFloat(arc4random() % 128) / 256.0 ) + 0.7 // 0.7 to 1.0, away from black - return UIColor(hue: hue, saturation: saturation, brightness: brightness, alpha: 1) - } -}