Skip to content

Commit

Permalink
Merge pull request #64 from scribd/improve-generator-tests
Browse files Browse the repository at this point in the history
Improve generator tests
  • Loading branch information
trupin authored Sep 25, 2018
2 parents 5a00fba + edd412d commit 6223d96
Show file tree
Hide file tree
Showing 38 changed files with 889 additions and 891 deletions.
4 changes: 2 additions & 2 deletions Resources/dependency_resolver.stencil
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,9 @@ final class {{dependencyContainer.targetType.name}}DependencyContainer{{dependen

{% if registration.hasBuilder %}
let dependencies = {{registration.type.name}}DependencyContainer({% if registration.hasReferences %}injecting: self{% if registration.parameters %}, {% endif %}{% endif %}{% call ParametersPassIn registration.parameters %})
let value = {{registration.type}}(injecting: dependencies)
let value = {{registration.type.name}}{{registration.type.generics}}(injecting: dependencies)
{% else %}{# registration.hasBuilder #}
let value = {{registration.type}}({% if registration.hasDependencyContainer %}injecting: {{registration.type.name}}DependencyContainer{{registration.type.generics}}(){% endif %})
let value = {{registration.type.name}}{{registration.type.generics}}({% if registration.hasDependencyContainer %}injecting: {{registration.type.name}}DependencyContainer{{registration.type.generics}}(){% endif %})
{% endif %}{# registration.hasBuilder #}

{% else %}{# not registration.customRef #}
Expand Down
2 changes: 1 addition & 1 deletion Sample/API/Generated/Weaver.API.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/// This file is generated by Weaver 0.10.1
/// This file is generated by Weaver 0.10.2
/// DO NOT EDIT!
import Foundation
// MARK: - MovieAPI
Expand Down
2 changes: 1 addition & 1 deletion Sample/API/Generated/Weaver.ImageManager.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/// This file is generated by Weaver 0.10.1
/// This file is generated by Weaver 0.10.2
/// DO NOT EDIT!
import Foundation
import UIKit
Expand Down
2 changes: 1 addition & 1 deletion Sample/API/Generated/Weaver.MovieManager.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/// This file is generated by Weaver 0.10.1
/// This file is generated by Weaver 0.10.2
/// DO NOT EDIT!
import Foundation
// MARK: - MovieManager
Expand Down
2 changes: 1 addition & 1 deletion Sample/Sample/Generated/Weaver.AppDelegate.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/// This file is generated by Weaver 0.10.1
/// This file is generated by Weaver 0.10.2
/// DO NOT EDIT!
import API
import UIKit
Expand Down
2 changes: 1 addition & 1 deletion Sample/Sample/Generated/Weaver.HomeViewController.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/// This file is generated by Weaver 0.10.1
/// This file is generated by Weaver 0.10.2
/// DO NOT EDIT!
import API
import Foundation
Expand Down
2 changes: 1 addition & 1 deletion Sample/Sample/Generated/Weaver.MovieViewController.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/// This file is generated by Weaver 0.10.1
/// This file is generated by Weaver 0.10.2
/// DO NOT EDIT!
import API
import Foundation
Expand Down
2 changes: 1 addition & 1 deletion Sample/Sample/Generated/Weaver.PersonManager.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/// This file is generated by Weaver 0.10.1
/// This file is generated by Weaver 0.10.2
/// DO NOT EDIT!
import API
import Foundation
Expand Down
2 changes: 1 addition & 1 deletion Sample/Sample/Generated/Weaver.ReviewManager.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/// This file is generated by Weaver 0.10.1
/// This file is generated by Weaver 0.10.2
/// DO NOT EDIT!
import API
import Foundation
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/// This file is generated by Weaver 0.10.1
/// This file is generated by Weaver 0.10.2
/// DO NOT EDIT!
import Foundation
// MARK: - WSReviewViewController
Expand Down
195 changes: 195 additions & 0 deletions Tests/WeaverCodeGenTests/Generator/GeneratorTests.swift
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) }
}
}
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()
}
}
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
}
}
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
}
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> {
}
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
}
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) {
}
}
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) {
}
}
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
}
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
}
}
Loading

0 comments on commit 6223d96

Please sign in to comment.