|
| 1 | +// |
| 2 | +// ClosureCoverageTests.swift |
| 3 | +// SyntaxKitTests |
| 4 | +// |
| 5 | +// Created by Leo Dion. |
| 6 | +// Copyright © 2025 BrightDigit. |
| 7 | +// |
| 8 | +// Permission is hereby granted, free of charge, to any person |
| 9 | +// obtaining a copy of this software and associated documentation |
| 10 | +// files (the "Software"), to deal in the Software without |
| 11 | +// restriction, including without limitation the rights to use, |
| 12 | +// copy, modify, merge, publish, distribute, sublicense, and/or |
| 13 | +// sell copies of the Software, and to permit persons to whom the |
| 14 | +// Software is furnished to do so, subject to the following |
| 15 | +// conditions: |
| 16 | +// |
| 17 | +// The above copyright notice and this permission notice shall be |
| 18 | +// included in all copies or substantial portions of the Software. |
| 19 | +// |
| 20 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 21 | +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
| 22 | +// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 23 | +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
| 24 | +// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 25 | +// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 26 | +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 27 | +// OTHER DEALINGS IN THE SOFTWARE. |
| 28 | +// |
| 29 | + |
| 30 | +import SwiftSyntax |
| 31 | +import Testing |
| 32 | + |
| 33 | +@testable import SyntaxKit |
| 34 | + |
| 35 | +/// Test suite for improving code coverage of Closure-related functionality. |
| 36 | +/// |
| 37 | +/// This test suite focuses on testing edge cases and uncovered code paths |
| 38 | +/// in the Closure extension files to ensure comprehensive test coverage. |
| 39 | +internal final class ClosureCoverageTests { |
| 40 | + // MARK: - Closure+Body.swift Coverage Tests |
| 41 | + |
| 42 | + /// Tests the DeclSyntax case in buildBodyItem method. |
| 43 | + @Test("Build body item with DeclSyntax") |
| 44 | + internal func testBuildBodyItemWithDeclSyntax() { |
| 45 | + // Test the DeclSyntax case in buildBodyItem |
| 46 | + let closure = Closure(body: { |
| 47 | + Variable(.let, name: "test", equals: "value") |
| 48 | + }) |
| 49 | + |
| 50 | + let syntax = closure.syntax |
| 51 | + let description = syntax.description |
| 52 | + |
| 53 | + // Verify that the variable declaration is properly included |
| 54 | + #expect(description.contains("let test = \"value\"")) |
| 55 | + } |
| 56 | + |
| 57 | + /// Tests the StmtSyntax case in buildBodyItem method. |
| 58 | + @Test("Build body item with StmtSyntax") |
| 59 | + internal func testBuildBodyItemWithStmtSyntax() { |
| 60 | + // Test the StmtSyntax case in buildBodyItem |
| 61 | + let closure = Closure(body: { |
| 62 | + Return { |
| 63 | + VariableExp("value") |
| 64 | + } |
| 65 | + }) |
| 66 | + |
| 67 | + let syntax = closure.syntax |
| 68 | + let description = syntax.description |
| 69 | + |
| 70 | + // Verify that the return statement is properly included |
| 71 | + #expect(description.contains("return value")) |
| 72 | + } |
| 73 | + |
| 74 | + /// Tests the buildParameterExpressionItem method. |
| 75 | + @Test("Build parameter expression item") |
| 76 | + internal func testBuildParameterExpressionItem() { |
| 77 | + // Test the buildParameterExpressionItem method |
| 78 | + let paramExp = ParameterExp(name: "test", value: "value") |
| 79 | + let closure = Closure(body: { |
| 80 | + paramExp |
| 81 | + }) |
| 82 | + |
| 83 | + let syntax = closure.syntax |
| 84 | + let description = syntax.description |
| 85 | + |
| 86 | + // Verify that the parameter expression is properly included |
| 87 | + #expect(description.contains("value")) |
| 88 | + } |
| 89 | + |
| 90 | + /// Tests ParameterExp with ExprCodeBlock value. |
| 91 | + @Test("Build parameter expression item with ExprCodeBlock") |
| 92 | + internal func testBuildParameterExpressionItemWithExprCodeBlock() { |
| 93 | + // Test ParameterExp with ExprCodeBlock value |
| 94 | + let initBlock = Init("String") |
| 95 | + let paramExp = ParameterExp(name: "test", value: initBlock) |
| 96 | + let closure = Closure(body: { |
| 97 | + paramExp |
| 98 | + }) |
| 99 | + |
| 100 | + let syntax = closure.syntax |
| 101 | + let description = syntax.description |
| 102 | + |
| 103 | + // Verify that the parameter expression with ExprCodeBlock is properly included |
| 104 | + #expect(description.contains("String()")) |
| 105 | + } |
| 106 | + |
| 107 | + /// Tests ParameterExp with ExprSyntax value. |
| 108 | + @Test("Build parameter expression item with ExprSyntax") |
| 109 | + internal func testBuildParameterExpressionItemWithExprSyntax() { |
| 110 | + // Test ParameterExp with ExprSyntax value |
| 111 | + let initBlock = Init("String") |
| 112 | + let paramExp = ParameterExp(name: "test", value: initBlock) |
| 113 | + let closure = Closure(body: { |
| 114 | + paramExp |
| 115 | + }) |
| 116 | + |
| 117 | + let syntax = closure.syntax |
| 118 | + let description = syntax.description |
| 119 | + |
| 120 | + // Verify that the parameter expression with ExprSyntax is properly included |
| 121 | + #expect(description.contains("String()")) |
| 122 | + } |
| 123 | + |
| 124 | + /// Tests ParameterExp with parameter expression syntax. |
| 125 | + @Test("Build parameter expression item with param expr syntax") |
| 126 | + internal func testBuildParameterExpressionItemWithParamExprSyntax() { |
| 127 | + // Test ParameterExp with parameter expression syntax |
| 128 | + let paramExp = ParameterExp(name: "test", value: "value") |
| 129 | + let closure = Closure(body: { |
| 130 | + paramExp |
| 131 | + }) |
| 132 | + |
| 133 | + let syntax = closure.syntax |
| 134 | + let description = syntax.description |
| 135 | + |
| 136 | + // Verify that the parameter expression syntax is properly included |
| 137 | + #expect(description.contains("value")) |
| 138 | + } |
| 139 | + |
| 140 | + // MARK: - Closure+Signature.swift Coverage Tests |
| 141 | + |
| 142 | + /// Tests the buildParameterClause method. |
| 143 | + @Test("Build parameter clause") |
| 144 | + internal func testBuildParameterClause() { |
| 145 | + // Test the buildParameterClause method |
| 146 | + let closure = Closure( |
| 147 | + parameters: { |
| 148 | + ClosureParameter("param1", type: "String") |
| 149 | + ClosureParameter("param2", type: "Int") |
| 150 | + }, |
| 151 | + body: { |
| 152 | + Variable(.let, name: "result", equals: "value") |
| 153 | + } |
| 154 | + ) |
| 155 | + |
| 156 | + let syntax = closure.syntax |
| 157 | + let description = syntax.description |
| 158 | + |
| 159 | + // Verify that the parameter clause is properly included |
| 160 | + #expect(description.contains("param1: String")) |
| 161 | + #expect(description.contains("param2: Int")) |
| 162 | + } |
| 163 | + |
| 164 | + /// Tests the buildParameterSyntax method. |
| 165 | + @Test("Build parameter syntax") |
| 166 | + internal func testBuildParameterSyntax() { |
| 167 | + // Test the buildParameterSyntax method |
| 168 | + let closure = Closure( |
| 169 | + parameters: { |
| 170 | + ClosureParameter("param", type: "String") |
| 171 | + }, |
| 172 | + body: { |
| 173 | + Variable(.let, name: "result", equals: "value") |
| 174 | + } |
| 175 | + ) |
| 176 | + |
| 177 | + let syntax = closure.syntax |
| 178 | + let description = syntax.description |
| 179 | + |
| 180 | + // Verify that the parameter syntax is properly included |
| 181 | + #expect(description.contains("param: String")) |
| 182 | + } |
| 183 | + |
| 184 | + /// Tests buildParameterSyntax with empty name. |
| 185 | + @Test("Build parameter syntax with empty name") |
| 186 | + internal func testBuildParameterSyntaxWithEmptyName() { |
| 187 | + // Test buildParameterSyntax with empty name |
| 188 | + let closure = Closure( |
| 189 | + parameters: { |
| 190 | + ClosureParameter("", type: "String") |
| 191 | + }, |
| 192 | + body: { |
| 193 | + Variable(.let, name: "result", equals: "value") |
| 194 | + } |
| 195 | + ) |
| 196 | + |
| 197 | + let syntax = closure.syntax |
| 198 | + let description = syntax.description |
| 199 | + |
| 200 | + // Verify that the parameter syntax handles empty name properly |
| 201 | + #expect(description.contains("String")) |
| 202 | + } |
| 203 | + |
| 204 | + /// Tests the buildReturnClause method. |
| 205 | + @Test("Build return clause") |
| 206 | + internal func testBuildReturnClause() { |
| 207 | + // Test the buildReturnClause method |
| 208 | + let closure = Closure( |
| 209 | + returns: "String", |
| 210 | + body: { |
| 211 | + Variable(.let, name: "result", equals: "value") |
| 212 | + } |
| 213 | + ) |
| 214 | + |
| 215 | + let syntax = closure.syntax |
| 216 | + let description = syntax.description |
| 217 | + |
| 218 | + // Verify that the return clause is properly included |
| 219 | + #expect(description.contains("-> String")) |
| 220 | + } |
| 221 | +} |
0 commit comments