From 55804dc838ae49e37b072556537c8c62a9360e1c Mon Sep 17 00:00:00 2001 From: Ian McDowell Date: Tue, 13 Feb 2018 11:26:28 -0800 Subject: [PATCH] Added ls colors test --- OpenTermTests/ParserTests.swift | 74 +++++++++++++++++++++++++++++---- 1 file changed, 66 insertions(+), 8 deletions(-) diff --git a/OpenTermTests/ParserTests.swift b/OpenTermTests/ParserTests.swift index 832b9810..7b722d18 100644 --- a/OpenTermTests/ParserTests.swift +++ b/OpenTermTests/ParserTests.swift @@ -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: @@ -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() { @@ -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() { @@ -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 + } + }