|
| 1 | +//! # Runnables Tests |
| 2 | +//! |
| 3 | +//! Unit tests for the Swift runnables tree-sitter query file (`languages/swift/runnables.scm`). |
| 4 | +//! |
| 5 | +//! ## Overview |
| 6 | +//! |
| 7 | +//! The runnables query is used by Zed to identify test functions and test classes in Swift code. |
| 8 | +//! The tests validate that the query correctly captures: |
| 9 | +//! |
| 10 | +//! 1. **Swift Testing Framework** (modern Swift 6+ testing) |
| 11 | +//! - `@Suite` annotations on structs and classes |
| 12 | +//! - `@Test` annotations on top-level functions |
| 13 | +//! - `@Test` annotations on member functions within test suites |
| 14 | +//! |
| 15 | +//! 2. **XCTest Framework** (traditional Swift testing) |
| 16 | +//! - Classes that inherit from `XCTestCase` |
| 17 | +//! - Classes marked with `@XCTestCase` comment annotation (for indirect subclasses) |
| 18 | +//! - Test methods within XCTest classes (must start with `test` prefix) |
| 19 | +//! |
| 20 | +//! 3. **Quick/Nimble Framework** (BDD-style testing) |
| 21 | +//! - `QuickSpec` subclasses |
| 22 | +//! - `AsyncSpec` subclasses |
| 23 | +//! |
| 24 | +//! ## Running Tests |
| 25 | +//! |
| 26 | +//! ```bash |
| 27 | +//! # Run all tests |
| 28 | +//! cargo test --lib |
| 29 | +//! |
| 30 | +//! # Run only the runnables tests |
| 31 | +//! cargo test --lib runnables_test |
| 32 | +//! ``` |
| 33 | +//! |
| 34 | +//! ## Test Structure |
| 35 | +//! |
| 36 | +//! Each test: |
| 37 | +//! 1. Defines a Swift code snippet containing test code |
| 38 | +//! 2. Parses the code using tree-sitter-swift |
| 39 | +//! 3. Runs the runnables query against the parsed tree |
| 40 | +//! 4. Validates that the correct captures are returned with the appropriate tags |
| 41 | +//! |
| 42 | +//! ## Tags |
| 43 | +//! |
| 44 | +//! The query assigns tags to different types of test definitions: |
| 45 | +//! |
| 46 | +//! - `swift-testing-suite` - A struct/class with `@Suite` annotation |
| 47 | +//! - `swift-testing-bare-func` - A top-level function with `@Test` annotation |
| 48 | +//! - `swift-testing-member-func` - A member function with `@Test` annotation within a suite |
| 49 | +//! - `swift-xctest-class` - A class that inherits from `XCTestCase` or is marked with `@XCTestCase` comment |
| 50 | +//! - `swift-xctest-func` - A test method within an XCTest class |
| 51 | +//! - `swift-test-quick-spec` - A QuickSpec subclass |
| 52 | +//! - `swift-test-async-spec` - An AsyncSpec subclass |
| 53 | +//! |
| 54 | +//! ## Adding New Tests |
| 55 | +//! |
| 56 | +//! When adding support for new test frameworks or patterns: |
| 57 | +//! |
| 58 | +//! 1. Add a new test function in this module |
| 59 | +//! 2. Create a Swift code snippet that demonstrates the pattern |
| 60 | +//! 3. Use `get_captures()` to run the query |
| 61 | +//! 4. Assert that the expected tags and names are captured |
| 62 | +//! |
| 63 | +//! Example: |
| 64 | +//! |
| 65 | +//! ```rust |
| 66 | +//! #[test] |
| 67 | +//! fn test_new_framework() { |
| 68 | +//! let source = r#" |
| 69 | +//! // Your Swift test code here |
| 70 | +//! "#; |
| 71 | +//! |
| 72 | +//! let captures = get_captures(source, get_query()); |
| 73 | +//! |
| 74 | +//! assert!( |
| 75 | +//! captures |
| 76 | +//! .iter() |
| 77 | +//! .any(|(tag, class, func)| tag == "expected-tag" && class == "ExpectedClass"), |
| 78 | +//! "Expected to find the test pattern" |
| 79 | +//! ); |
| 80 | +//! } |
| 81 | +//! ``` |
| 82 | +
|
1 | 83 | #[cfg(test)] |
2 | 84 | mod tests { |
3 | 85 | use std::sync::OnceLock; |
|
0 commit comments