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

Commit

Permalink
Fixed parser tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ian-mcdowell committed Feb 13, 2018
1 parent 0abe5dc commit d09ebcd
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
17 changes: 6 additions & 11 deletions OpenTerm/Util/Parsing & Formatting/Parser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,7 @@ class Parser {
private var pendingString = NSMutableAttributedString()

func parse(_ data: Data) {
let didEnd = self.decodeUTF8(fromData: data, buffer: &dataBuffer)
if didEnd {
self.delegate?.parserDidEndTransmission(self)
}
self.decodeUTF8(fromData: data, buffer: &dataBuffer)
}

func reset() {
Expand All @@ -132,11 +129,11 @@ class Parser {
pendingString = NSMutableAttributedString()
}

private func decodeUTF8(fromData data: Data, buffer: inout Data) -> Bool {
private func decodeUTF8(fromData data: Data, buffer: inout Data) {
let data = buffer + data

// Parse what we can from the previous leftover and the new data.
let (leftover, didEnd) = self.decodeUTF8(fromData: data)
let leftover = self.decodeUTF8(fromData: data)

// There are two reasons we could get leftover data:
// - An invalid character was found in the middle of the string
Expand All @@ -151,16 +148,14 @@ class Parser {
} else {
buffer = Data()
}

return didEnd
}

/// Decode UTF-8 string from the given data.
/// This is a custom implementation that decodes what characters it can then returns whatever it can't,
/// which is necessary since data can come in arbitrarily-sized chunks of bytes, with characters split
/// across multiple chunks.
/// The first time decoding fails, all of the rest of the data will be returned.
private func decodeUTF8(fromData data: Data) -> (remaining: Data, didEnd: Bool) {
private func decodeUTF8(fromData data: Data) -> Data {
let byteArray = [UInt8](data)

var utf8Decoder = UTF8()
Expand All @@ -177,8 +172,7 @@ class Parser {
}
}

let remaining = Data.init(bytes: byteArray.suffix(from: decodedByteCount))
return (remaining, didEnd)
return Data.init(bytes: byteArray.suffix(from: decodedByteCount))
}

/// This method is called for each UTF-8 character that is received.
Expand All @@ -205,6 +199,7 @@ class Parser {
switch code {
case .endOfText, .endOfTransmission:
// Ended transmission, return immediately.
self.delegate?.parserDidEndTransmission(self)
return true
case .escape:
self.state = .escape
Expand Down
11 changes: 11 additions & 0 deletions OpenTermTests/ParserTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,17 +68,25 @@ class ParserTests: XCTestCase {
parser.parse(text.data(using: .utf8)!)
}

private func end() {
// Must send end of transmission when we are done, since that will flush the pending text out of the parser
parser.parse(Parser.Code.endOfTransmission.rawValue.data(using: .utf8)!)
}

func testBasicText() {
let str = "hello world"

send(str)
end()

// Each character should be received in a message
var receivedStr = ""
for method in parserDelegate.receivedMethods {
switch method {
case .string(let str):
receivedStr += str.string
case .endTransmission:
break
default:
XCTFail("Unexpected method called on parser delegate")
}
Expand All @@ -91,6 +99,7 @@ class ParserTests: XCTestCase {
let str = "hello\nworld"

send(str)
end()

var receivedStr = ""
for method in parserDelegate.receivedMethods {
Expand All @@ -99,6 +108,8 @@ class ParserTests: XCTestCase {
receivedStr += str.string
case .newLine:
receivedStr += "\n"
case .endTransmission:
break
default:
XCTFail("Unexpected method called on parser delegate")
}
Expand Down

0 comments on commit d09ebcd

Please sign in to comment.