From a15fbbf4353e795ed1c59ceb88fe2359918e4c16 Mon Sep 17 00:00:00 2001 From: Tae Won Ha Date: Tue, 18 Aug 2020 23:41:33 +0200 Subject: [PATCH] Re-add RxPack tests --- Cartfile | 3 - RxPack/Package.swift | 8 +- .../Tests/RxPackTests/RxMsgpackRpcTests.swift | 85 +++++++++++++++ .../Tests/RxPackTests/RxNeovimApiTests.swift | 101 ++++++++++++++++++ .../RxPackTests/RxPackTests.swift | 0 .../RxPackTests/XCTestManifests.swift | 9 ++ .../xcshareddata/swiftpm/Package.resolved | 11 +- 7 files changed, 208 insertions(+), 9 deletions(-) create mode 100644 RxPack/Tests/RxPackTests/RxMsgpackRpcTests.swift create mode 100644 RxPack/Tests/RxPackTests/RxNeovimApiTests.swift rename {RxPack/Tests => RxPackOld/RxMsgpackRpcTests}/RxPackTests/RxPackTests.swift (100%) create mode 100644 RxPackOld/RxMsgpackRpcTests/RxPackTests/XCTestManifests.swift diff --git a/Cartfile b/Cartfile index a2237c44..34af6315 100644 --- a/Cartfile +++ b/Cartfile @@ -4,6 +4,3 @@ github "sparkle-project/Sparkle" == 1.23.0 # included directly github "sindresorhus/github-markdown-css" == 4.0.0 - -# for testing only -github "Quick/Nimble" == 8.1.1 diff --git a/RxPack/Package.swift b/RxPack/Package.swift index a642c828..1324a305 100644 --- a/RxPack/Package.swift +++ b/RxPack/Package.swift @@ -6,15 +6,13 @@ let package = Package( name: "RxPack", platforms: [.macOS(.v10_13)], products: [ - .library( - name: "RxPack", - targets: ["RxPack"] - ), + .library(name: "RxPack", targets: ["RxPack"]), ], dependencies: [ .package(url: "https://github.com/ReactiveX/RxSwift", .upToNextMinor(from: "5.1.1")), .package(name: "MessagePack", url: "https://github.com/a2/MessagePack.swift", .upToNextMinor(from: "4.0.0")), .package(name: "Socket", url: "https://github.com/IBM-Swift/BlueSocket", .upToNextMinor(from: "1.0.52")), + .package(url: "https://github.com/Quick/Nimble", .upToNextMinor(from: "8.1.1")), ], targets: [ .target( @@ -23,7 +21,7 @@ let package = Package( ), .testTarget( name: "RxPackTests", - dependencies: ["RxPack"] + dependencies: ["RxPack", "Nimble"] ), ] ) diff --git a/RxPack/Tests/RxPackTests/RxMsgpackRpcTests.swift b/RxPack/Tests/RxPackTests/RxMsgpackRpcTests.swift new file mode 100644 index 00000000..2967c786 --- /dev/null +++ b/RxPack/Tests/RxPackTests/RxMsgpackRpcTests.swift @@ -0,0 +1,85 @@ +/** + * Tae Won Ha - http://taewon.de - @hataewon + * See LICENSE + */ + +import RxSwift +import XCTest + +@testable import RxPack + +class RxMsgpackRpcTests: XCTestCase { + private var connection: RxMsgpackRpc! + private let disposeBag = DisposeBag() + + override func setUp() { + super.setUp() + + // $ NVIM_LISTEN_ADDRESS=/tmp/nvim.sock nvim --headless $SOMEFILE + self.connection = RxMsgpackRpc() + self.connection.stream + .subscribe(onNext: { msg in + switch msg { + case let .notification(method, params): + print("NOTIFICATION: \(method): array of \(params.count) elements") + case let .error(value, msg): + print("ERROR: \(msg) with \(value)") + default: + print("???") + } + }, onError: { print("ERROR: \($0)") }) + .disposed(by: self.disposeBag) + + _ = self.connection.run(at: "/tmp/nvim.sock") + .andThen(self.connection.request( + method: "nvim_ui_attach", + params: [.int(40), .int(40), .map([:])], + expectsReturnValue: true + )) + .syncValue() + } + + override func tearDown() { + super.tearDown() + try? self.connection + .request( + method: "nvim_command", params: [.string("q!")], + expectsReturnValue: false + ) + .asCompletable() + .wait() + try? self.connection.stop().wait() + } + + func testExample() { + let disposeBag = DisposeBag() + + let lineCount = self.connection + .request( + method: "nvim_buf_line_count", + params: [.int(0)], + expectsReturnValue: true + ) + .syncValue() + print(lineCount ?? "???") + + let formatter = DateFormatter() + formatter.dateFormat = "mm:ss.SSS" + for i in 0 ... 100 { + let date = Date() + connection + .request( + method: "nvim_command_output", + params: [.string("echo '\(i) \(formatter.string(from: date))'")], + expectsReturnValue: true + ) + .subscribe( + onSuccess: { response in print(response) }, + onError: { error in print(error) } + ) + .disposed(by: disposeBag) + } + + sleep(1) + } +} diff --git a/RxPack/Tests/RxPackTests/RxNeovimApiTests.swift b/RxPack/Tests/RxPackTests/RxNeovimApiTests.swift new file mode 100644 index 00000000..8208918f --- /dev/null +++ b/RxPack/Tests/RxPackTests/RxNeovimApiTests.swift @@ -0,0 +1,101 @@ +/** + * Tae Won Ha - http://taewon.de - @hataewon + * See LICENSE + */ + +import MessagePack +import RxSwift +import XCTest + +@testable import RxPack + + +class NvimMsgPackTests: XCTestCase { + var nvim = RxNeovimApi() + let disposeBag = DisposeBag() + + override func setUp() { + super.setUp() + + // $ NVIM_LISTEN_ADDRESS=/tmp/nvim.sock nvim $SOME_FILES + try? self.nvim.run(at: "/tmp/nvim.sock").wait() + } + + override func tearDown() { + super.tearDown() + try? self.nvim + .command(command: "q!") + .wait() + try? self.nvim.stop().wait() + } + + func testSth() { + let colorNames = [ + "Normal", // color and background-color + "Directory", // a + "StatusLine", // code background and foreground + "NonText", // hr and block quote border + "Question", // blockquote foreground + ] + + typealias HlResult = [String: RxNeovimApi.Value] + typealias ColorNameHlResultTuple = (colorName: String, hlResult: HlResult) + typealias ColorNameObservableTuple = (colorName: String, observable: Observable) + + Observable + .from(colorNames.map { colorName -> ColorNameObservableTuple in + ( + colorName: colorName, + observable: self.nvim + .getHlByName(name: colorName, rgb: true) + .asObservable() + ) + }) + .flatMap { tuple -> Observable<(String, HlResult)> in + Observable.zip(Observable.just(tuple.colorName), tuple.observable) + } + .subscribe(onNext: { (tuple: ColorNameHlResultTuple) in + print(tuple) + }) + .disposed(by: self.disposeBag) + +// Observable +// .concat(colorNames.map { colorName in +// self.nvim +// .getHlByName(name: colorName, rgb: true) +// .asObservable() +// }) +// .enumerated() +// .subscribe(onNext: { dict in print(dict) }) +// .disposed(by: self.disposeBag) + +// self.nvim +// .getHlByName(name: "Normal", rgb: true) +// .subscribe(onSuccess: { dict in +// guard let f = dict["foreground"]?.uint64Value, +// let b = dict["background"]?.uint64Value else { return } +// print(String(format: "%06X %06X", f, b)) +// }, onError: { err in print(err) }) +// .disposed(by: self.disposeBag) + + sleep(1) + } + + func testExample() { + let formatter = DateFormatter() + formatter.dateStyle = .short + formatter.timeStyle = .full + let now = Date() + let dispose = DisposeBag() + for i in 0 ... 5 { + self.nvim + .command( + command: "echo '\(formatter.string(from: now)) \(i)'" + ) + .subscribe(onCompleted: { print("\(i) handled") }) + .disposed(by: dispose) + } + + sleep(1) + } +} diff --git a/RxPack/Tests/RxPackTests/RxPackTests.swift b/RxPackOld/RxMsgpackRpcTests/RxPackTests/RxPackTests.swift similarity index 100% rename from RxPack/Tests/RxPackTests/RxPackTests.swift rename to RxPackOld/RxMsgpackRpcTests/RxPackTests/RxPackTests.swift diff --git a/RxPackOld/RxMsgpackRpcTests/RxPackTests/XCTestManifests.swift b/RxPackOld/RxMsgpackRpcTests/RxPackTests/XCTestManifests.swift new file mode 100644 index 00000000..e46a3893 --- /dev/null +++ b/RxPackOld/RxMsgpackRpcTests/RxPackTests/XCTestManifests.swift @@ -0,0 +1,9 @@ +import XCTest + +#if !canImport(ObjectiveC) +public func allTests() -> [XCTestCaseEntry] { + return [ + testCase(RxPackTests.allTests), + ] +} +#endif diff --git a/VimR.xcworkspace/xcshareddata/swiftpm/Package.resolved b/VimR.xcworkspace/xcshareddata/swiftpm/Package.resolved index f0447967..d51a5e9c 100644 --- a/VimR.xcworkspace/xcshareddata/swiftpm/Package.resolved +++ b/VimR.xcworkspace/xcshareddata/swiftpm/Package.resolved @@ -51,10 +51,19 @@ "repositoryURL": "https://github.com/qvacua/neovim", "state": { "branch": null, - "revision": "13be312e380e7f93878486c8851a605a4e7af0eb", + "revision": "246da2521cdf3720b11a5045720d2ae1497a9268", "version": "0.1.0-types" } }, + { + "package": "Nimble", + "repositoryURL": "https://github.com/Quick/Nimble", + "state": { + "branch": null, + "revision": "2b1809051b4a65c1d7f5233331daa24572cd7fca", + "version": "8.1.1" + } + }, { "package": "PureLayout", "repositoryURL": "https://github.com/PureLayout/PureLayout",