-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #64 from scribd/improve-generator-tests
Improve generator tests
- Loading branch information
Showing
38 changed files
with
889 additions
and
891 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
Sample/Sample/Generated/Weaver.WSReviewViewController+Injectable.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
195 changes: 195 additions & 0 deletions
195
Tests/WeaverCodeGenTests/Generator/GeneratorTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,195 @@ | ||
// | ||
// GeneratorTests.swift | ||
// WeaverCodeGenTests | ||
// | ||
// Created by Théophane Rupin on 3/4/18. | ||
// | ||
|
||
import Foundation | ||
import XCTest | ||
import SourceKittenFramework | ||
import PathKit | ||
|
||
@testable import WeaverCodeGen | ||
|
||
final class GeneratorTests: XCTestCase { | ||
|
||
let templatePath = Path(#file).parent() + Path("../../Resources/dependency_resolver.stencil") | ||
|
||
func test_no_annotation() { | ||
do { | ||
let actual = try actualOutput() | ||
XCTAssertNil(actual) | ||
} catch { | ||
XCTFail("Unexpected error \(error)") | ||
} | ||
} | ||
|
||
func test_empty_type_registration() { | ||
do { | ||
try performTest() | ||
} catch { | ||
XCTFail("Unexpected error \(error)") | ||
} | ||
} | ||
|
||
func test_isolated_type_registration() { | ||
do { | ||
try performTest() | ||
} catch { | ||
XCTFail("Unexpected error \(error)") | ||
} | ||
} | ||
|
||
func test_customRef_registration() { | ||
do { | ||
try performTest() | ||
} catch { | ||
XCTFail("Unexpected error \(error)") | ||
} | ||
} | ||
|
||
func test_embedded_injectable_type() { | ||
do { | ||
try performTest() | ||
} catch { | ||
XCTFail("Unexpected error \(error)") | ||
} | ||
} | ||
|
||
func test_optional_type_registration() { | ||
do { | ||
try performTest() | ||
} catch { | ||
XCTFail("Unexpected error \(error)") | ||
} | ||
} | ||
|
||
func test_public_type() { | ||
do { | ||
try performTest() | ||
} catch { | ||
XCTFail("Unexpected error \(error)") | ||
} | ||
} | ||
|
||
func test_ignored_types() { | ||
do { | ||
try performTest() | ||
} catch { | ||
XCTFail("Unexpected error \(error)") | ||
} | ||
} | ||
|
||
func test_internal_registration_in_public_type() { | ||
do { | ||
try performTest() | ||
} catch { | ||
XCTFail("Unexpected error \(error)") | ||
} | ||
} | ||
|
||
func test_generic_type_registration() { | ||
do { | ||
try performTest() | ||
} catch { | ||
XCTFail("Unexpected error \(error)") | ||
} | ||
} | ||
|
||
func test_injectable_type_with_indirect_references() { | ||
do { | ||
try performTest() | ||
} catch { | ||
XCTFail("Unexpected error \(error)") | ||
} | ||
} | ||
|
||
func test_registrations_only() { | ||
do { | ||
try performTest() | ||
} catch { | ||
XCTFail("Unexpected error \(error)") | ||
} | ||
} | ||
|
||
func test_root_type() { | ||
do { | ||
try performTest() | ||
} catch { | ||
XCTFail("Unexpected error \(error)") | ||
} | ||
} | ||
} | ||
|
||
// MARK: - Utils | ||
|
||
private extension GeneratorTests { | ||
|
||
func actualOutput(_ function: StringLiteralType = #function) throws -> String? { | ||
let fileName = function.replacingOccurrences(of: "()", with: "") | ||
let path = Path(#file).parent() + Path("Input/\(fileName).swift") | ||
|
||
if !path.exists { | ||
try path.write("\n") | ||
} | ||
|
||
guard let file = File(path: path.string) else { | ||
XCTFail("Could not find file at path \(path.string)") | ||
return nil | ||
} | ||
|
||
let templatePath = Path(#file).parent() + Path("../../../Resources/dependency_resolver.stencil") | ||
|
||
let lexer = Lexer(file, fileName: "test.swift") | ||
let tokens = try lexer.tokenize() | ||
let parser = Parser(tokens, fileName: "test.swift") | ||
let ast = try parser.parse() | ||
let dependencyGraph = try Linker(syntaxTrees: [ast]).dependencyGraph | ||
|
||
let generator = try Generator(dependencyGraph: dependencyGraph, template: templatePath) | ||
|
||
guard let (_ , actual) = try generator.generate().first else { | ||
return nil | ||
} | ||
|
||
return actual.flatMap { $0 + "\n" } | ||
} | ||
|
||
func expectedOutput(actual: String?, _ function: StringLiteralType = #function) throws -> String { | ||
let fileName = function.replacingOccurrences(of: "()", with: "") | ||
let path = Path(#file).parent() + Path("Output/Weaver.\(fileName).swift") | ||
|
||
if let actual = actual, !path.exists { | ||
try path.write(actual) | ||
} | ||
|
||
return try path.read() | ||
} | ||
|
||
func exportDiff(actual: String, expected: String, _ function: StringLiteralType = #function) throws { | ||
|
||
guard actual != expected else { return } | ||
|
||
let dirPath = Path("/tmp/weaver_tests/\(GeneratorTests.self)") | ||
let function = function.replacingOccurrences(of: "()", with: "") | ||
let actualFilePath = dirPath + Path("\(function)_actual.swift") | ||
let expectedFilePath = dirPath + Path("\(function)_expected.swift") | ||
|
||
try actualFilePath.write(actual) | ||
try expectedFilePath.write(expected) | ||
|
||
print("Execute the following to check the diffs:") | ||
print("\n") | ||
print("diffchecker \(actualFilePath.string) \(expectedFilePath.string)") | ||
print("\n") | ||
} | ||
|
||
func performTest(_ function: StringLiteralType = #function) throws { | ||
let actual = try actualOutput(function) | ||
let expected = try expectedOutput(actual: actual, function) | ||
|
||
XCTAssertEqual(actual, expected) | ||
try actual.flatMap { try exportDiff(actual: $0, expected: expected, function) } | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
Tests/WeaverCodeGenTests/Generator/Input/test_customRef_registration.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
protocol FooProtocolTest4 { | ||
} | ||
|
||
final class FuuTest4: FuuProtocolTest4 { | ||
} | ||
|
||
protocol FuuProtocolTest4 { | ||
} | ||
|
||
final class FooTest4: FooProtocolTest4 { | ||
// weaver: fuu = FuuTest4 <- FuuProtocolTest4 | ||
// weaver: fuu.customRef = true | ||
} | ||
|
||
extension FooTest4DependencyResolver { | ||
|
||
func fuuCustomRef() -> FuuProtocolTest4 { | ||
return FuuTest4() | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
Tests/WeaverCodeGenTests/Generator/Input/test_embedded_injectable_type.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
protocol FiiProtocolTest5 { | ||
} | ||
|
||
final class FiiTest5: FiiProtocolTest5 { | ||
} | ||
|
||
final class FooTest5 { | ||
// weaver: fii = FiiTest5 | ||
|
||
final class FuuTest5 { | ||
// weaver: fii = FiiTest5 <- FiiProtocolTest5 | ||
// weaver: fii.scope = .container | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
Tests/WeaverCodeGenTests/Generator/Input/test_empty_type_registration.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
typealias LogEngineTest1 = String | ||
|
||
final class LoggerTest1 { | ||
// weaver: logEngine = LogEngineTest1 | ||
|
||
init(injecting: LoggerTest1DependencyResolver) {} | ||
} | ||
|
||
final class ManagerTest1 { | ||
// weaver: logger = LoggerTest1 | ||
} |
6 changes: 6 additions & 0 deletions
6
Tests/WeaverCodeGenTests/Generator/Input/test_generic_type_registration.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
final class FooTest10 { | ||
// weaver: fuu = FuuTest10<String> | ||
} | ||
|
||
final class FuuTest10<T> { | ||
} |
7 changes: 7 additions & 0 deletions
7
Tests/WeaverCodeGenTests/Generator/Input/test_ignored_types.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
final class FooTest8 { | ||
// weaver: fuu <= UInt | ||
} | ||
|
||
class FeeTest8 { | ||
// This class is ignored | ||
} |
32 changes: 32 additions & 0 deletions
32
Tests/WeaverCodeGenTests/Generator/Input/test_injectable_type_with_indirect_references.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
final class FuuTest11 { | ||
} | ||
|
||
final class FooTest11 { | ||
// weaver: fuu = FuuTest11 | ||
// weaver: fuu.scope = .container | ||
|
||
// weaver: faa = FaaTest11 | ||
} | ||
|
||
final class FaaTest11 { | ||
// weaver: fee = FeeTest11 | ||
// weaver: fee.scope = .transient | ||
|
||
init(injecting _: FaaTest11DependencyResolver) { | ||
} | ||
} | ||
|
||
final class FeeTest11 { | ||
// weaver: fii = FiiTest11 | ||
// weaver: fii.scope = .transient | ||
|
||
init(injecting _: FeeTest11DependencyResolver) { | ||
} | ||
} | ||
|
||
final class FiiTest11 { | ||
// weaver: fuu <- FuuTest11 | ||
|
||
init(injecting _: FiiTest11DependencyResolver) { | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
Tests/WeaverCodeGenTests/Generator/Input/test_internal_registration_in_public_type.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
final class FooTest9 { | ||
// weaver: fuu <= String | ||
|
||
init(injecting _: FooTest9DependencyResolver) { | ||
} | ||
} | ||
public final class FuuTest9 { | ||
// weaver: fuu <= String | ||
|
||
// weaver: foo = FooTest9 | ||
|
||
init(injecting _: FuuTest9DependencyResolver) { | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
Tests/WeaverCodeGenTests/Generator/Input/test_isolated_type_registration.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
final class FuuTest3 { | ||
} | ||
|
||
protocol FaaTest3 { | ||
} | ||
|
||
final class FooTest3 { | ||
// weaver: self.isIsolated = true | ||
|
||
// weaver: fuu = FuuTest3 | ||
|
||
// weaver: faa <- FaaTest3 | ||
} |
8 changes: 8 additions & 0 deletions
8
Tests/WeaverCodeGenTests/Generator/Input/test_no_annotation.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
final class FooTest1 { | ||
|
||
let fuu: String | ||
|
||
init(fuu: String) { | ||
self.fuu = fuu | ||
} | ||
} |
Oops, something went wrong.