Skip to content
This repository has been archived by the owner on Feb 13, 2019. It is now read-only.

Commit

Permalink
Added ls colors test
Browse files Browse the repository at this point in the history
  • Loading branch information
ian-mcdowell committed Feb 13, 2018
1 parent 2e66292 commit 300b802
Showing 1 changed file with 66 additions and 8 deletions.
74 changes: 66 additions & 8 deletions OpenTermTests/ParserTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,16 +73,20 @@ class ParserTests: XCTestCase {
parser.parse(Parser.Code.endOfTransmission.rawValue.data(using: .utf8)!)
}

private var receivedString: String {
var receivedStr = ""
private func receivedString(withControlCharacters controlCharacters: Bool = true) -> NSAttributedString {
let receivedStr = NSMutableAttributedString()
for method in parserDelegate.receivedMethods {
switch method {
case .string(let str):
receivedStr += str.string
receivedStr.append(str)
case .newLine:
receivedStr += "\n"
if controlCharacters {
receivedStr.append(NSAttributedString.init(string: "\n"))
}
case .carriageReturn:
receivedStr += "\r"
if controlCharacters {
receivedStr.append(NSAttributedString.init(string: "\r"))
}
case .endTransmission:
break
default:
Expand All @@ -98,7 +102,7 @@ class ParserTests: XCTestCase {
send(str)
end()

XCTAssertEqual(str, receivedString, "Received string should equal sent string")
XCTAssertEqual(str, receivedString().string, "Received string should equal sent string")
}

func testTextWithNewLine() {
Expand All @@ -107,7 +111,7 @@ class ParserTests: XCTestCase {
send(str)
end()

XCTAssertEqual(str, receivedString, "Received string should equal sent string")
XCTAssertEqual(str, receivedString().string, "Received string should equal sent string")
}

func testSanitizedOutput() {
Expand All @@ -116,6 +120,60 @@ class ParserTests: XCTestCase {
send(str)
end()

XCTAssertEqual(receivedString, "~")
XCTAssertEqual(receivedString().string, "~")
}

func testLSColors() {
let esc = Parser.Code.escape.rawValue
// First line = normal output
let line1 = "cacert.pem\tctd.cpp\techoTest\tinput\tknown_hosts"
// Second line = bold / blue "lua"
let line2text = "lua"
let line2 = "\(esc)[1m\(esc)[34m\(line2text)\(esc)[39;49m\(esc)[0m"
// Third line = normal "path"
let line3 = "path"
// Fourth line = bold / green "test"
let line4text = "test"
let line4 = "\(esc)[1m\(esc)[32m\(line4text)\(esc)[39;49m\(esc)[0m"
// Fifth line = normal "test.tar.gz"
let line5 = "test.tar.gz"
// Sixth line = bold / purple "test2"
let line6text = "test2"
let line6 = "\(esc)[1m\(esc)[35m\(line6text)\(esc)[39;49m\(esc)[0m"

send([line1, line2, line3, line4, line5, line6].joined(separator: "\n"))
end()

let received = self.receivedString(withControlCharacters: false)

// Retrieve an attributed substring for each line
var currentPosition = 0
let rLine1 = received.attributedSubstring(from: NSRange.init(location: currentPosition, length: line1.count))
currentPosition += rLine1.length
let rLine2 = received.attributedSubstring(from: NSRange.init(location: currentPosition, length: line2text.count))
currentPosition += rLine2.length
let rLine3 = received.attributedSubstring(from: NSRange.init(location: currentPosition, length: line3.count))
currentPosition += rLine3.length
let rLine4 = received.attributedSubstring(from: NSRange.init(location: currentPosition, length: line4text.count))
currentPosition += rLine4.length
let rLine5 = received.attributedSubstring(from: NSRange.init(location: currentPosition, length: line5.count))
currentPosition += rLine5.length
let rLine6 = received.attributedSubstring(from: NSRange.init(location: currentPosition, length: line6text.count))
currentPosition += rLine6.length

// Make sure we got through the whole string
XCTAssertEqual(currentPosition, received.length)

// Make sure lines are equal to what we passed in
XCTAssertEqual(rLine1.string, line1)
XCTAssertEqual(rLine2.string, line2text)
XCTAssertEqual(rLine3.string, line3)
XCTAssertEqual(rLine4.string, line4text)
XCTAssertEqual(rLine5.string, line5)
XCTAssertEqual(rLine6.string, line6text)

// For lines with styles, make sure styles were applied
// TODO
}

}

0 comments on commit 300b802

Please sign in to comment.