Skip to content

Commit

Permalink
Merge pull request #20 from czechboy0/hd/swift3
Browse files Browse the repository at this point in the history
updated for Swift 3
  • Loading branch information
czechboy0 committed Mar 29, 2016
2 parents 466056b + 1c21dbc commit 1ec335a
Show file tree
Hide file tree
Showing 24 changed files with 433 additions and 829 deletions.
2 changes: 1 addition & 1 deletion .swift-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
DEVELOPMENT-SNAPSHOT-2016-03-01-a
DEVELOPMENT-SNAPSHOT-2016-03-24-a
14 changes: 7 additions & 7 deletions Sources/Redbird/ClientSocket.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public enum SocketErrorType {
}

//see error codes: https://gist.github.com/gabrielfalcao/4216897
public struct SocketError : ErrorType, CustomStringConvertible {
public struct SocketError : ErrorProtocol, CustomStringConvertible {

public let type: SocketErrorType
public let number: Int32
Expand Down Expand Up @@ -129,15 +129,15 @@ class ClientSocket: Socket {
guard _hostInfo != nil else {
throw SocketError(.FailedToGetIPFromHostname(self.address))
}
let hostInfo = _hostInfo.memory
let hostInfo = _hostInfo.pointee
guard hostInfo.h_addrtype == AF_INET else {
throw SocketError(.FailedToGetIPFromHostname("No IPv4 address"))
}
guard hostInfo.h_addr_list != nil else {
throw SocketError(.FailedToGetIPFromHostname("List is empty"))
}

let addrStruct = sockadd_list_cast(hostInfo.h_addr_list)[0].memory
let addrStruct = sockadd_list_cast(hostInfo.h_addr_list)[0].pointee
return addrStruct
}

Expand Down Expand Up @@ -216,18 +216,18 @@ extension SocketReader {
lastReadCount = readChars.count

//append received chars before delimiter
totalBuffer.appendContentsOf(readChars)
totalBuffer.append(contentsOf: readChars)
}
}
}

extension ClientSocket: SocketReader {}

extension CollectionType where Generator.Element == CChar {
extension Collection where Iterator.Element == CChar {

func stringView() throws -> String {
let selfArray = Array(self) + [0]
guard let string = String.fromCString(selfArray) else {
guard let string = String(validatingUTF8: selfArray) else {
throw SocketError(.UnparsableChars(selfArray))
}
return string
Expand Down Expand Up @@ -256,7 +256,7 @@ class Data {
}

var characters: [CChar] {
var data = [CChar](count: self.capacity, repeatedValue: 0)
var data = [CChar](repeating: 0, count: self.capacity)
memcpy(&data, self.bytes, data.count)
return data
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/Redbird/Error.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// Copyright © 2016 Honza Dvorsky. All rights reserved.
//

public struct RespError: RespObject, ErrorType {
public struct RespError: RespObject, ErrorProtocol {
static var signature: String = "-"
public let respType: RespType = .Error

Expand Down
4 changes: 2 additions & 2 deletions Sources/Redbird/Errors.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// Copyright © 2016 Honza Dvorsky. All rights reserved.
//

public enum RedbirdError: ErrorType {
public enum RedbirdError: ErrorProtocol {
case ParsingGeneric(String)
case ParsingStringNotThisType(String, RespType?)
case SimpleStringInvalidInput(String)
Expand All @@ -23,5 +23,5 @@ public enum RedbirdError: ErrorType {
case WrongNativeTypeUnboxing(RespObject, String)
case UnexpectedReturnedObject(RespObject)
case PipelineNoCommandProvided
case FailedToCreateSocket(ErrorType)
case FailedToCreateSocket(ErrorProtocol)
}
12 changes: 6 additions & 6 deletions Sources/Redbird/Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,23 +47,23 @@ extension String {
func subwords(separator: Character = " ") -> [String] {
return self
.characters
.split(separator)
.split(separator: separator)
.map(String.init)
}

func stringWithDroppedFirstWord(separator: Character = " ", dropCount: Int = 1) -> String {
return self
.subwords(separator)
.dropFirst(dropCount)
.joinWithSeparator(String(separator))
.joined(separator: String(separator))
}

func hasPrefixStr(prefix: String) -> Bool {
return self.characters.startsWith(prefix.characters)
return self.characters.starts(with: prefix.characters)
}

func hasSuffixStr(suffix: String) -> Bool {
return self.characters.reverse().startsWith(suffix.characters.reverse())
return self.characters.reversed().starts(with: suffix.characters.reversed())
}

func containsCharacter(other: Character) -> Bool {
Expand All @@ -74,7 +74,7 @@ extension String {
return self.withCString { ptr in
let count = Int(strlen(ptr))
var idx = 0
var out = Array<CChar>(count: count, repeatedValue: 0)
var out = Array<CChar>(repeating: 0, count: count)
while idx < count { out[idx] = ptr[idx]; idx += 1 }
return out
}
Expand All @@ -91,7 +91,7 @@ extension String {
}
}

extension CollectionType where Generator.Element == CChar {
extension Collection where Iterator.Element == CChar {

/// Splits string around a delimiter, returns the first subarray
/// as the first return value (including the delimiter) and the rest
Expand Down
12 changes: 6 additions & 6 deletions Tests/LinuxMain.swift
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import XCTest
@testable import Redbirdtest
@testable import RedbirdTestSuite

XCTMain([
ConversionTests(),
FormattingTests(),
ParsingTests(),
RedbirdTests(),
StringTests()
testCase(ConversionTests.allTests),
testCase(FormattingTests.allTests),
testCase(ParsingTests.allTests),
testCase(RedbirdTests.allTests),
testCase(StringTests.allTests)
])
21 changes: 0 additions & 21 deletions Tests/Redbird/ConversionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,6 @@
import XCTest
@testable import Redbird

#if os(Linux)
extension ConversionTests: XCTestCaseProvider {
var allTests : [(String, () throws -> Void)] {
return [
("testArray_Ok", testArray_Ok),
("testArray_NullThrows", testArray_NullThrows),
("testMaybeArray_Ok", testMaybeArray_Ok),
("testMaybeArray_NullOk", testMaybeArray_NullOk),
("testSimpleString_Ok", testSimpleString_Ok),
("testBulkString_Ok", testBulkString_Ok),
("testString_NullThrows", testString_NullThrows),
("testMaybeString_Ok", testMaybeString_Ok),
("testMaybeString_NullOk", testMaybeString_NullOk),
("testInt_Ok", testInt_Ok),
("testBool_Ok", testBool_Ok),
("testError_Ok", testError_Ok)
]
}
}
#endif

class ConversionTests: XCTestCase {

func testArray_Ok() {
Expand Down
21 changes: 0 additions & 21 deletions Tests/Redbird/FormattingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,6 @@
import XCTest
@testable import Redbird

#if os(Linux)
extension FormattingTests: XCTestCaseProvider {
var allTests : [(String, () throws -> Void)] {
return [
("testInitialFormatter_Integer", testInitialFormatter_Integer),
("testError", testError),
("testSimpleString", testSimpleString),
("testInteger", testInteger),
("testBulkString_Normal", testBulkString_Normal),
("testBulkString_Empty", testBulkString_Empty),
("testBulkString_Null", testBulkString_Null),
("testArray_Normal", testArray_Normal),
("testArray_Empty", testArray_Empty),
("testArray_Null", testArray_Null),
("testArray_TwoStrings", testArray_TwoStrings),
("testArray_ArrayOfArrays", testArray_ArrayOfArrays)
]
}
}
#endif

class FormattingTests: XCTestCase {

func testInitialFormatter_Integer() {
Expand Down
23 changes: 0 additions & 23 deletions Tests/Redbird/ParsingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,29 +27,6 @@ class TestReader: SocketReader {
}
}

#if os(Linux)
extension ParsingTests: XCTestCaseProvider {
var allTests : [(String, () throws -> Void)] {
return [
("testParsingError_NothingReadYet", testParsingError_NothingReadYet),
("testParsingError_FirstReadChar", testParsingError_FirstReadChar),
("testParsingSimpleString", testParsingSimpleString),
("testParsingSimpleString_WithLeftover", testParsingSimpleString_WithLeftover),
("testParsingInteger", testParsingInteger),
("testParsingBulkString_Normal", testParsingBulkString_Normal),
("testParsingBulkString_Normal_WithLeftover", testParsingBulkString_Normal_WithLeftover),
("testParsingBulkString_Empty", testParsingBulkString_Empty),
("testParsingBulkString_Null", testParsingBulkString_Null),
("testParsingArray_Null", testParsingArray_Null),
("testParsingArray_Empty", testParsingArray_Empty),
("testParsingArray_Normal", testParsingArray_Normal),
("testParsingArray_TwoString", testParsingArray_TwoString),
("testParsingArray_ArrayOfArrays", testParsingArray_ArrayOfArrays)
]
}
}
#endif

class ParsingTests: XCTestCase {

func testParsingError_NothingReadYet() {
Expand Down
16 changes: 6 additions & 10 deletions Tests/Redbird/PerformanceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,18 @@ import XCTest
class PerformanceTests: XCTestCase {

func urlForFixture(name: String) -> NSURL {
let url: NSURL
//if we're running from CLI, use SPM_INSTALL_PATH to find fixtures, otherwise bundle
if let path = NSProcessInfo().environment["SPM_INSTALL_PATH"] {
url = NSURL(string: "file://\(path)/../Tests/Redbird/\(name).txt")!
} else {
url = NSBundle(forClass: PerformanceTests.classForCoder()).URLForResource(name, withExtension: "txt")!
}

let parent = (#file).componentsSeparated(by: "/").dropLast().joined(separator: "/")
let url = NSURL(string: "file://\(parent)/\(name).txt")!
print("Loading fixture from url \(url)")
return url
}

func testPerf_ParsingArray_Normal() {

let strUrl = urlForFixture("teststring")
let str = try! String(contentsOfURL: strUrl)
measureBlock {
let str = try! String(contentsOf: strUrl)
measure {
let reader = TestReader(content: str)
let (_, _) = try! InitialParser().parse([], reader: reader)
}
Expand All @@ -57,7 +53,7 @@ class PerformanceTests: XCTestCase {
]
let content: [RespObject] = Array(1..<100).map { _ in RespArray(content: subinput) }
let input = RespArray(content: content)
measureBlock {
measure {
_ = try! InitialFormatter().format(input)
}
}
Expand Down
19 changes: 0 additions & 19 deletions Tests/Redbird/RedbirdTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,25 +33,6 @@ func assertThrow(@autoclosure errorType: () -> RedbirdError, @noescape block: ()
}
}

#if os(Linux)
extension RedbirdTests: XCTestCaseProvider {
var allTests : [(String, () throws -> Void)] {
return [
("testServersideKilledSocket_Reconnected", testServersideKilledSocket_Reconnected),
("testServersideTimeout", testServersideTimeout),
("testSimpleString_Ping", testSimpleString_Ping),
("testError_UnknownCommand", testError_UnknownCommand),
("testBulkString_SetGet", testBulkString_SetGet),
("testPipelining_PingSetGetUnknownPing", testPipelining_PingSetGetUnknownPing),
("testCommandReconnectFailsOnFailed", testCommandReconnectFailsOnFailed),
("testPipelineReconnectFailsOnFailed", testPipelineReconnectFailsOnFailed),
("testCommandReconnectSucceedsTheSecondTime", testCommandReconnectSucceedsTheSecondTime),
("testPipelineReconnectSucceedsTheSecondTime", testPipelineReconnectSucceedsTheSecondTime)
]
}
}
#endif

class RedbirdTests: XCTestCase {

func live(@noescape block: (client: Redbird) throws -> ()) {
Expand Down
23 changes: 0 additions & 23 deletions Tests/Redbird/StringTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,6 @@
import XCTest
@testable import Redbird

#if os(Linux)
extension StringTests: XCTestCaseProvider {
var allTests : [(String, () throws -> Void)] {
return [
("testStrippingTrailingTerminator", testStrippingTrailingTerminator),
("testStrippingSignature", testStrippingSignature),
("testStrippingSignatureAndTrailingTerminator", testStrippingSignatureAndTrailingTerminator),
("testWrappingTrailingTerminator", testWrappingTrailingTerminator),
("testWrappingSignature", testWrappingSignature),
("testWrappingSignatureAndTrailingTerminator", testWrappingSignatureAndTrailingTerminator),
("testHasPrefix", testHasPrefix),
("testHasSuffix", testHasSuffix),
("testContainsCharacter", testContainsCharacter),
("testCCharArrayView", testCCharArrayView),
("testSplitAround_NotFound", testSplitAround_NotFound),
("testSplitAround_Middle", testSplitAround_Middle),
("testSplitAround_Start", testSplitAround_Start),
("testSplitAround_End", testSplitAround_End)
]
}
}
#endif

class StringTests: XCTestCase {

func testStrippingTrailingTerminator() {
Expand Down
Loading

0 comments on commit 1ec335a

Please sign in to comment.