How to use targets method of JSONProject class

Best Mockingbird code snippet using JSONProject.targets

Run Mockingbird automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Project.swift

Source: Project.swift Github

copy
1import Foundation
2import XcodeProj
3
4public enum Project {
5  case xcode(_ xcodeProj: XcodeProj)
6  case json(_ jsonProject: JSONProject)
7  
8  public func targets(named name: String) -> [TargetType] {
9    switch self {
10    case .xcode(let xcodeProj):
11      return xcodeProj.pbxproj.targets(named: name).map({ .pbxTarget($0) })
12    case .json(let jsonProject):
13      return jsonProject.targets(named: name).map({ .describedTarget($0) })
14    }
15  }
16}
17
Full Screen

TargetDescriptionTests.swift

Source: TargetDescriptionTests.swift Github

copy
1//
2//  TargetDescriptionTests.swift
3//  MockingbirdTests
4//
5//  Created by Kiel Gillard on 22/7/21.
6//
7
8import XCTest
9@testable import MockingbirdGenerator
10
11class ProjectDescriptionDecodingTests: XCTestCase {
12  private func assertDecodedTarget(_ decodedTarget: TargetDescription, isEqualTo expectedTarget: TargetDescription) {
13    XCTAssertEqual(decodedTarget.name, expectedTarget.name)
14    XCTAssertEqual(decodedTarget.c99name, expectedTarget.c99name)
15    XCTAssertEqual(decodedTarget.path, expectedTarget.path)
16    XCTAssertEqual(decodedTarget.sources, expectedTarget.sources)
17    XCTAssertEqual(decodedTarget.dependencies, expectedTarget.dependencies)
18  }
19  
20  enum TestProjectDescription: String {
21    case swiftPackageManager = "spm-project-description"
22    case generic = "generic-project-description"
23    var name: String { return rawValue }
24    
25    struct LoadingError: LocalizedError {
26      let errorDescription: String?
27    }
28  }
29  
30  private func loadJSONProjectDescription(_ projectDescription: TestProjectDescription) throws -> Data {
31    let testBundle = Bundle(for: type(of: self))
32    guard let filePath = testBundle.path(forResource: projectDescription.name,
33                                         ofType: "json") else {
34      throw TestProjectDescription.LoadingError(
35        errorDescription: "No JSON project description named '\(name)'")
36    }
37    
38    guard let json = try? String(contentsOfFile: filePath) else {
39      throw TestProjectDescription.LoadingError(
40        errorDescription: "Unable to load JSON project description")
41    }
42    
43    guard let jsonData = json.data(using: .utf8) else {
44      throw TestProjectDescription.LoadingError(
45        errorDescription: "Failed to encode JSON project description to UTF-8")
46    }
47    
48    return jsonData
49  }
50  
51  func testParseSPMDescription() throws {
52    let json = try loadJSONProjectDescription(.swiftPackageManager)
53    let description = try JSONDecoder().decode(ProjectDescription.self, from: json)
54    
55    XCTAssertEqual(description.targets.count, 3)
56    
57    let expectedTestTarget = TargetDescription(name: "FeatureTargetTests",
58                                               c99name: "FeatureTargetTests",
59                                               type: "test",
60                                               path: "Tests/FeatureTargetTests",
61                                               sources: [
62                                                "ModelTests.swift",
63                                                "ControllerTests.swift",
64                                                "ViewTests.swift"
65                                               ],
66                                               dependencies: [
67                                                "FeatureTarget"
68                                               ])
69    
70    if let testTarget = description.targets.first(where: { $0.type == expectedTestTarget.type }) {
71      assertDecodedTarget(testTarget, isEqualTo: expectedTestTarget)
72    } else {
73      XCTFail("Did not decode test target.")
74    }
75    
76    let expectedLibraryTarget = TargetDescription(name: "FeatureTarget",
77                                                  c99name: "FeatureTarget",
78                                                  type: "library",
79                                                  path: "Sources/FeatureTarget",
80                                                  sources: [
81                                                    "Models/Things.swift",
82                                                    "Controllers/MasterViewController.swift",
83                                                    "Controllers/DetailViewController.swift",
84                                                    "Views/DetailView.swift"
85                                                  ], dependencies: [
86                                                    
87                                                  ])
88    
89    if let libraryTarget = description.targets.first(where: { $0.name == expectedLibraryTarget.name }) {
90      assertDecodedTarget(libraryTarget, isEqualTo: expectedLibraryTarget)
91    } else {
92      XCTFail("Did not decode \(expectedLibraryTarget.name) target.")
93    }
94    
95    let expectedEmptyTarget = TargetDescription(name: "EmptyTarget",
96                                                c99name: "EmptyTarget",
97                                                type: "library",
98                                                path: "Sources/EmptyTarget",
99                                                sources: [],
100                                                dependencies: [])
101    
102    if let emptyTarget = description.targets.first(where: { $0.name == expectedEmptyTarget.name }) {
103      assertDecodedTarget(emptyTarget, isEqualTo: expectedEmptyTarget)
104    } else {
105      XCTFail("Did not decode \(expectedEmptyTarget.name) target.")
106    }
107  }
108  
109  func testParseGenericDescription() throws {
110    let json = try loadJSONProjectDescription(.generic)
111    let description = try JSONDecoder().decode(ProjectDescription.self, from: json)
112    
113    XCTAssertEqual(description.targets.count, 3)
114    
115    let expectedTestTarget = TargetDescription(name: "FeatureTargetTests",
116                                               c99name: "FeatureTargetTests",
117                                               type: "test",
118                                               path: "Tests/FeatureTargetTests",
119                                               sources: [
120                                                "ModelTests.swift",
121                                                "ControllerTests.swift",
122                                                "ViewTests.swift"
123                                               ],
124                                               dependencies: [
125                                                "FeatureTarget"
126                                               ])
127    
128    if let testTarget = description.targets.first(where: { $0.type == expectedTestTarget.type }) {
129      assertDecodedTarget(testTarget, isEqualTo: expectedTestTarget)
130    } else {
131      XCTFail("Did not decode test target.")
132    }
133    
134    let expectedLibraryTarget = TargetDescription(name: "FeatureTarget",
135                                                  c99name: "FeatureTarget",
136                                                  type: "library",
137                                                  path: "Sources/FeatureTarget",
138                                                  sources: [
139                                                    "Models/Things.swift",
140                                                    "Controllers/MasterViewController.swift",
141                                                    "Controllers/DetailViewController.swift",
142                                                    "Views/DetailView.swift"
143                                                  ], dependencies: [
144                                                    
145                                                  ])
146    
147    if let libraryTarget = description.targets.first(where: { $0.name == expectedLibraryTarget.name }) {
148      assertDecodedTarget(libraryTarget, isEqualTo: expectedLibraryTarget)
149    } else {
150      XCTFail("Did not decode \(expectedLibraryTarget.name) target.")
151    }
152    
153    let expectedEmptyTarget = TargetDescription(name: "EmptyTarget",
154                                                c99name: "EmptyTarget",
155                                                type: "library",
156                                                path: "Sources/EmptyTarget",
157                                                sources: [],
158                                                dependencies: [])
159    
160    if let emptyTarget = description.targets.first(where: { $0.name == expectedEmptyTarget.name }) {
161      assertDecodedTarget(emptyTarget, isEqualTo: expectedEmptyTarget)
162    } else {
163      XCTFail("Did not decode \(expectedEmptyTarget.name) target.")
164    }
165  }
166}
167
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Most used method in JSONProject

Trigger targets code on LambdaTest Cloud Grid

Execute automation tests with targets on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)