Skip to content

Commit ee70e26

Browse files
committed
Remove hacky cast extension
Additionally, tee output for testing. It's nicer to see the build and test result altogether in Vim.
1 parent d8218c4 commit ee70e26

File tree

6 files changed

+36
-47
lines changed

6 files changed

+36
-47
lines changed

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,8 @@ test: debug
110110
-Xlinker $(BUILD_DIR)/lib$(PLUGIN_NAME).dylib \
111111
-Xlinker $(BUILD_DIR)/lib$(PLUGIN_NAME)VimAsync.dylib \
112112
-Xlinker $(BUILD_DIR)/lib$(PLUGIN_NAME)Vim.dylib \
113-
-Xswiftc -target -Xswiftc $(TRIPPLE)
114-
@swift test --skip-build | tee $(LAST_LOG)
113+
-Xswiftc -target -Xswiftc $(TRIPPLE) | tee $(LAST_LOG)
114+
@swift test --skip-build 2>&1 | tee -a $(LAST_LOG)
115115

116116

117117
.PHONY: test_generate

Sources/Vim/Vim.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ public struct Vim {
99
return Current()
1010
}
1111

12-
/// Run a vim command
12+
/// Run a Vim command.
13+
/// :help command
1314
@discardableResult public static func command(_ cmd: String) throws -> VimValue {
1415
var value: VimValue?
1516
cmd.withCString { cStr in
@@ -25,6 +26,7 @@ public struct Vim {
2526
}
2627

2728
/// Evaluate an expression
29+
/// :help eval
2830
@discardableResult public static func eval(_ cmd: String) throws -> VimValue {
2931
var value: VimValue?
3032
cmd.withCString { cStr in

Sources/Vim/VimExtensions.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,15 @@ extension Vim {
4949
}
5050

5151
public static func get(_ variable: String) -> Bool {
52-
return (try? eval(variable))?.asBool() ?? false
52+
return Bool(try? eval(variable)) ?? false
5353
}
5454

5555
public static func get(_ variable: String) -> Int {
56-
return (try? eval(variable))?.asInt() ?? 0
56+
return Int(try? eval(variable)) ?? 0
5757
}
5858

5959
public static func get(_ variable: String) -> String {
60-
return (try? eval(variable))?.asString() ?? ""
60+
return String(try? eval(variable)) ?? ""
6161
}
6262

6363
/// Returns the 0-based current line and 0-based current column

Sources/Vim/VimValue.swift

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
11
import VimInterface
22

3+
// Converting values from Vim.
4+
// VimValue is a `View` into the state of Vim.
5+
// Design:
6+
// - Be fast
7+
// - Be as idomatic as possible
8+
//
9+
// In the standard library, types do not dynamically cast to Any.
10+
//
11+
312
extension Int {
413
public init?(_ value: VimValue) {
514
// Generally, eval results are returned as strings
615
// Perhaps there is a better way to express this.
7-
if let strValue = value.asString(),
16+
if let strValue = String(value),
817
let intValue = Int(strValue) {
918
self.init(intValue)
1019
} else {
@@ -82,34 +91,12 @@ public final class VimValue {
8291
}
8392
}
8493

85-
/// Casting extensions
86-
extension VimValue {
87-
public func asString() -> String? {
88-
return String(self)
89-
}
90-
91-
public func asInt() -> Int? {
92-
return Int(self)
93-
}
94-
95-
public func asBool() -> Bool? {
96-
return Bool(self)
97-
}
98-
99-
public func asList() -> VimList? {
100-
return VimList(self)
101-
}
102-
103-
public func asDictionary() -> VimDictionary? {
104-
return VimDictionary(self)
105-
}
106-
}
10794

10895
// A Dictionary
10996
public final class VimDictionary {
11097
private let value: VimValue
11198

112-
init?(_ value: VimValue) {
99+
public init?(_ value: VimValue) {
113100
self.value = value
114101
}
115102

@@ -118,14 +105,14 @@ public final class VimDictionary {
118105
}
119106

120107
public var keys: VimList {
121-
guard let list = VimValue(reference: swiftvim_dict_keys(value.reference)).asList() else {
108+
guard let list = VimList(VimValue(reference: swiftvim_dict_keys(value.reference))) else {
122109
fatalError("Can't get keys")
123110
}
124111
return list
125112
}
126113

127114
public var values: VimList {
128-
guard let list = VimValue(reference: swiftvim_dict_values(value.reference)).asList() else {
115+
guard let list = VimList(VimValue(reference: swiftvim_dict_values(value.reference))) else {
129116
fatalError("Can't get values")
130117
}
131118
return list

Tests/VimAsyncTests/VimAsyncTest.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class VimAsyncTests: XCTestCase {
2626
fatalError("Fail.")
2727
}
2828
XCTAssertNotNil(result)
29-
XCTAssertNil(result.asString())
29+
XCTAssertNil(String(result))
3030
swiftvim_finalize()
3131
}
3232
}

Tests/VimTests/VimTests.swift

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -34,45 +34,45 @@ class VimValueTests: XCTestCase {
3434
func testEvalString() {
3535
swiftvim_initialize()
3636
let result = try! Vim.eval("VALUE")
37-
XCTAssertEqual(result.asString(), "VALUE")
37+
XCTAssertEqual(String(result), "VALUE")
3838
swiftvim_finalize()
3939
}
4040

4141
func testCommandNone() {
4242
swiftvim_initialize()
4343
let result = try! Vim.command("VALUE")
44-
XCTAssertNil(result.asString())
44+
XCTAssertNil(String(result))
4545
swiftvim_finalize()
4646
}
4747

4848
func testEvalInt() {
4949
swiftvim_initialize()
5050
mutateRuntime("eval", lambda: "lambda value : int(value)")
5151
let result = try! Vim.eval("2")
52-
XCTAssertEqual(result.asInt(), 2)
52+
XCTAssertEqual(Int(result), 2)
5353
swiftvim_finalize()
5454
}
5555

5656
func testEvalList() {
5757
swiftvim_initialize()
5858
mutateRuntime("eval", lambda: "lambda value : [1, 2]")
5959
let result = try! Vim.eval("")
60-
let list = result.asList()!
60+
let list = VimList(result)!
6161
XCTAssertEqual(list.count, 2)
62-
XCTAssertEqual(list[0].asInt(), 1)
63-
XCTAssertEqual(list[1].asInt(), 2)
62+
XCTAssertEqual(Int(list[0]), 1)
63+
XCTAssertEqual(Int(list[1]), 2)
6464
list[1] = list[0]
65-
XCTAssertEqual(list[1].asInt(), 1)
65+
XCTAssertEqual(Int(list[1]), 1)
6666
swiftvim_finalize()
6767
}
6868

6969
func testListCollectionUsage() {
7070
swiftvim_initialize()
7171
mutateRuntime("eval", lambda: "lambda value : [1, 2]")
7272
let result = try! Vim.eval("")
73-
let list = result.asList()!
73+
let list = VimList(result)!
7474
/// Smoke test we can do collectiony things.
75-
let incremented = list.map { $0.asInt()! + 1 }
75+
let incremented = list.map { Int($0)! + 1 }
7676
XCTAssertEqual(incremented[1], 3)
7777
swiftvim_finalize()
7878
}
@@ -81,11 +81,11 @@ class VimValueTests: XCTestCase {
8181
swiftvim_initialize()
8282
mutateRuntime("eval", lambda: "lambda value : dict(a=42, b='a')")
8383
let result = try! Vim.eval("")
84-
let dict = result.asDictionary()!
84+
let dict = VimDictionary(result)!
8585
let aVal = dict["a"]!
86-
XCTAssertEqual(aVal.asInt()!, 42)
86+
XCTAssertEqual(Int(aVal)!, 42)
8787
let nonVal = dict["s"]
88-
XCTAssertNil(nonVal?.asInt())
88+
XCTAssertNil(Int(nonVal))
8989

9090
swiftvim_finalize()
9191
}
@@ -94,7 +94,7 @@ class VimValueTests: XCTestCase {
9494
swiftvim_initialize()
9595
mutateRuntime("eval", lambda: "lambda value : dict(a=42, b='a')")
9696
let result = try! Vim.eval("")
97-
let dict = result.asDictionary()!
97+
let dict = VimDictionary(result)!
9898
XCTAssertEqual(dict.keys.count, 2)
9999
XCTAssertEqual(dict.values.count, 2)
100100
swiftvim_finalize()
@@ -131,7 +131,7 @@ class VimValueTests: XCTestCase {
131131
let cb = "Example.invoke('\(address)', 'arga')"
132132
print("Callback:", cb)
133133
mutateRuntime("eval", lambda: "lambda value : \(cb)")
134-
try? Vim.eval("")
134+
_ = try? Vim.eval("")
135135
XCTAssertTrue(called)
136136
swiftvim_finalize()
137137
}

0 commit comments

Comments
 (0)