Skip to content

Commit 901bcd1

Browse files
authored
Export useful variables from test runnables and create tasks for tests (#42)
Added tasks to enable test execution using runnable triangles. Supports both XCTest and Swift Testing libraries. Runnables now export useful variables, which enables users to write custom testing tasks (e.g. for xcode projects – outside the scope of this plugin). Note I'm not really clear on the reason for the underscore captures like @_name and @_tag-name in the existing code, so I've mostly left @_name in place and created @_tag-name ones. Let me know if I've done something wrong. I haven't done anything for Quick tests (not a Quick user, and it's not an Apple library) and have left those runnables as-is.
1 parent 4921a6f commit 901bcd1

File tree

2 files changed

+99
-51
lines changed

2 files changed

+99
-51
lines changed

languages/swift/runnables.scm

Lines changed: 68 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
;; @Suite struct TestSuite
1+
;; Tags are named according to which testing library the test uses:
2+
;; swift-testing-* = Swift Testing library
3+
;; swift-xctest-* = XCTest library
4+
;;
5+
;; While the tasks defined in this extension don't care which library is used,
6+
;; other tasks built by users might.
7+
8+
;; @Suite struct/class
29
(
310
(class_declaration
411
(modifiers
@@ -8,78 +15,102 @@
815
)
916
)
1017
)
11-
name: (type_identifier) @_name
12-
) @_swift-test-suite
13-
(#set! tag swift-test-suite)
18+
name: (type_identifier) @_name @SWIFT_TEST_CLASS
19+
) @_swift-testing-suite
20+
(#set! tag swift-testing-suite)
1421
)
1522

16-
;; @Test test func
23+
;; @Test top-level func
1724
(
18-
(function_declaration
19-
(modifiers
20-
(attribute
21-
(user_type
22-
(type_identifier) @run (#eq? @run "Test")
25+
(source_file
26+
(function_declaration
27+
(modifiers
28+
(attribute
29+
(user_type
30+
(type_identifier) @run (#eq? @run "Test")
31+
)
32+
)
2333
)
34+
name: (simple_identifier) @_name @SWIFT_TEST_FUNC
35+
) @_swift-testing-bare-func
36+
)
37+
(#set! tag swift-testing-bare-func)
38+
)
39+
40+
;; @Test within struct/class
41+
(
42+
(class_declaration
43+
name: (type_identifier) @_name @SWIFT_TEST_CLASS
44+
body: (class_body
45+
(function_declaration
46+
(modifiers
47+
(attribute
48+
(user_type
49+
(type_identifier) @run (#eq? @run "Test")
50+
)
51+
)
52+
)
53+
name: (simple_identifier) @_name @SWIFT_TEST_FUNC
2454
)
2555
)
26-
name: (simple_identifier) @_name
27-
) @_swift-test-test
28-
(#set! tag swift-test-test)
56+
) @_swift-testing-member-func
57+
(#set! tag swift-testing-member-func)
2958
)
3059

31-
;; QuickSpec subclass
60+
;; XCTestCase subclass
3261
(
3362
(class_declaration
34-
name: (type_identifier) @_name
63+
name: (type_identifier) @SWIFT_TEST_CLASS
3564
(inheritance_specifier
3665
inherits_from: (user_type
37-
(type_identifier) @run (#eq? @run "QuickSpec")
66+
(type_identifier) @run (#eq? @run "XCTestCase")
3867
)
3968
)
40-
) @_swift-test-quick-spec
41-
(#set! tag swift-test-quick-spec)
69+
) @_swift-xctest-class
70+
(#set! tag swift-xctest-class)
4271
)
4372

44-
;; AsyncSpec subclass
73+
;; Test function within XCTestCase
4574
(
4675
(class_declaration
47-
name: (type_identifier) @_name
76+
name: (type_identifier) @SWIFT_TEST_CLASS
4877
(inheritance_specifier
4978
inherits_from: (user_type
50-
(type_identifier) @run (#eq? @run "AsyncSpec")
79+
(type_identifier) @_superclass_name (#eq? @_superclass_name "XCTestCase")
5180
)
5281
)
53-
) @_swift-test-async-spec
54-
(#set! tag swift-test-async-spec)
82+
body: (class_body
83+
(function_declaration
84+
name: (simple_identifier) @_name @SWIFT_TEST_FUNC @run (#match? @run "^test")
85+
)
86+
)
87+
) @_swift-xctest-func
88+
(#set! tag swift-xctest-func)
5589
)
5690

57-
;; XCTestCase subclass
91+
92+
;; QuickSpec subclass
5893
(
5994
(class_declaration
6095
name: (type_identifier) @_name
6196
(inheritance_specifier
6297
inherits_from: (user_type
63-
(type_identifier) @run (#eq? @run "XCTestCase")
98+
(type_identifier) @run (#eq? @run "QuickSpec")
6499
)
65100
)
66-
) @_swift-test-test-case
67-
(#set! tag swift-test-test-case)
101+
) @_swift-test-quick-spec
102+
(#set! tag swift-test-quick-spec)
68103
)
69104

70-
;; Test function within XCTestCase
105+
;; AsyncSpec subclass
71106
(
72107
(class_declaration
108+
name: (type_identifier) @_name
73109
(inheritance_specifier
74110
inherits_from: (user_type
75-
(type_identifier) @test_class_name (#eq? @test_class_name "XCTestCase")
76-
)
77-
)
78-
body: (class_body
79-
(function_declaration
80-
name: (simple_identifier) @_name @run (#match? @run "^test")
111+
(type_identifier) @run (#eq? @run "AsyncSpec")
81112
)
82113
)
83-
) @_swift-test-func
84-
(#set! tag swift-test-func)
85-
)
114+
) @_swift-test-async-spec
115+
(#set! tag swift-test-async-spec)
116+
)

languages/swift/tasks.json

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,33 @@
11
[
2-
// These don't yet work: `swift test --filter` only supports Symbols (not filenames)
3-
// {
4-
// "label": "swift test",
5-
// "command": "swift",
6-
// "args": ["test", "--filter", "${ZED_STEM}"],
7-
// "tags": [
8-
// "swift-test-async-spec",
9-
// "swift-test-quick-spec",
10-
// "swift-test-func",
11-
// "swift-test-suite",
12-
// "swift-test-test-case",
13-
// "swift-test-test"
14-
// ]
15-
// }
2+
// The first component of a test specifier is the test target name. Since we don't
3+
// know that here, our filters match any leading word.
4+
//
5+
// Test specifiers can be listed with "swift test list".
6+
7+
{
8+
"label": "$ZED_CUSTOM_SWIFT_TEST_CLASS test",
9+
"command": "swift",
10+
"args": ["test", "--filter", "'^\\w+\\.$ZED_CUSTOM_SWIFT_TEST_CLASS/'"],
11+
"tags": ["swift-xctest-class", "swift-testing-suite"]
12+
},
13+
{
14+
"label": "$ZED_CUSTOM_SWIFT_TEST_CLASS.$ZED_CUSTOM_SWIFT_TEST_FUNC test",
15+
"command": "swift",
16+
"args": [
17+
"test",
18+
"--filter",
19+
"'^\\w+\\.$ZED_CUSTOM_SWIFT_TEST_CLASS/$ZED_CUSTOM_SWIFT_TEST_FUNC\\b'"
20+
],
21+
"tags": ["swift-xctest-func", "swift-testing-member-func"]
22+
},
23+
{
24+
"label": "$ZED_CUSTOM_SWIFT_TEST_FUNC test",
25+
"command": "swift",
26+
"args": [
27+
"test",
28+
"--filter",
29+
"'^\\w+\\.$ZED_CUSTOM_SWIFT_TEST_FUNC\\b'"
30+
],
31+
"tags": ["swift-testing-bare-func"]
32+
}
1633
]

0 commit comments

Comments
 (0)