From a63e031ac00222668401195b1941659db1c59fc7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filip=20Dolni=CC=81k?= Date: Sun, 17 Jul 2016 18:33:02 +0200 Subject: [PATCH] Fix problem with testable frameworks when project name contains non alphanumeric characters. --- .../FileHeaderHandler.swift | 13 ++++++++++++- Tests/SourceFiles/Expected/TestableFrameworks.swift | 3 +++ Tests/features/generate_command.feature | 2 +- 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/Source/CuckooGeneratorFramework/FileHeaderHandler.swift b/Source/CuckooGeneratorFramework/FileHeaderHandler.swift index 50d2495..3cfe527 100644 --- a/Source/CuckooGeneratorFramework/FileHeaderHandler.swift +++ b/Source/CuckooGeneratorFramework/FileHeaderHandler.swift @@ -28,7 +28,7 @@ public struct FileHeaderHandler { // Add new line before code imports imports = [""] + imports } - return ["import Cuckoo"] + testableFrameworks.map { "@testable import \($0)" } + imports + return ["import Cuckoo"] + getTestableImports(testableFrameworks) + imports } private static func getRelativePath(absolutePath: String) -> String { @@ -81,4 +81,15 @@ public struct FileHeaderHandler { return "" } } + + private static func getTestableImports(testableFrameworks: [String]) -> [String] { + func replaceIllegalCharacters(char: UnicodeScalar) -> Character { + if NSCharacterSet.letterCharacterSet().longCharacterIsMember(char.value) || NSCharacterSet.decimalDigitCharacterSet().longCharacterIsMember(char.value) { + return Character(char) + } else { + return "_" + } + } + return testableFrameworks.map { String($0.unicodeScalars.map(replaceIllegalCharacters)) }.map { "@testable import \($0)" } + } } diff --git a/Tests/SourceFiles/Expected/TestableFrameworks.swift b/Tests/SourceFiles/Expected/TestableFrameworks.swift index 723fdba..a7c7ed4 100644 --- a/Tests/SourceFiles/Expected/TestableFrameworks.swift +++ b/Tests/SourceFiles/Expected/TestableFrameworks.swift @@ -9,6 +9,9 @@ import Cuckoo @testable import Cuckoo +@testable import A_b +@testable import A_c +@testable import A_d import Foundation diff --git a/Tests/features/generate_command.feature b/Tests/features/generate_command.feature index ac6b2ad..bdc2f5b 100644 --- a/Tests/features/generate_command.feature +++ b/Tests/features/generate_command.feature @@ -10,7 +10,7 @@ Feature: Generate command When I run `runcuckoo generate --no-timestamp ../SourceFiles/TestedClass.swift ../SourceFiles/TestedProtocol.swift` Then the file "../SourceFiles/Expected/GeneratedMocks.swift" should be equal to file "GeneratedMocks.swift" Scenario: with testableFrameworks - When I run `runcuckoo generate --no-timestamp --testable Cuckoo --output Actual.swift ../SourceFiles/TestedEmptyClass.swift` + When I run `runcuckoo generate --no-timestamp --testable "Cuckoo,A b,A-c,A.d" --output Actual.swift ../SourceFiles/TestedEmptyClass.swift` Then the file "../SourceFiles/Expected/TestableFrameworks.swift" should be equal to file "Actual.swift" Scenario: non existing input file When I run `runcuckoo generate non_existing_file.swift`