-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Generate boilerplate code for XCTest on linux using AST #164
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
f25ad8a
AST generation for test modules
aciidgh 36b9d46
Add module name and import path for ast gen
aciidgh 9682c09
Move ASTs to a single dir
aciidgh 82855d4
Add parse AST method
aciidgh 50ee3d1
local commit
aciidgh c214c62
First attempt at ast parser
aciidgh 341b76c
working on Linux
aciidgh b0a8e8a
Fix quote issue
aciidgh 34c5fa9
Comment disabled testcases causing inbalanced AST
aciidgh 0d4eca3
Quick refactors
aciidgh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,91 @@ | ||
/* | ||
This source file is part of the Swift.org open source project | ||
|
||
Copyright 2015 - 2016 Apple Inc. and the Swift project authors | ||
Licensed under Apache License v2.0 with Runtime Library Exception | ||
|
||
See http://swift.org/LICENSE.txt for license information | ||
See http://swift.org/CONTRIBUTORS.txt for Swift project authors | ||
*/ | ||
|
||
import Utility | ||
import POSIX | ||
import func libc.fclose | ||
|
||
public func generate(testModules: [TestModule], prefix: String) throws { | ||
|
||
let testManifestFolder = Path.join(prefix, "XCTestGen") | ||
try mkdir(testManifestFolder) | ||
|
||
for module in testModules { | ||
let path = Path.join(testManifestFolder, "\(module.name)-XCTestManifest.swift") | ||
try writeXCTestManifest(module, path: path) | ||
} | ||
|
||
let main = Path.join(testManifestFolder, "XCTestMain.swift") | ||
try writeXCTestMain(testModules, path: main) | ||
} | ||
|
||
private func writeXCTestManifest(module: TestModule, path: String) throws { | ||
|
||
let file = try fopen(path, mode: .Write) | ||
defer { | ||
fclose(file) | ||
} | ||
|
||
//imports | ||
try fputs("import XCTest\n", file) | ||
try fputs("\n", file) | ||
|
||
try fputs("#if os(Linux)\n", file) | ||
|
||
//for each class | ||
try module.classes.sort { $0.name < $1.name }.forEach { moduleClass in | ||
try fputs("extension \(moduleClass.name) {\n", file) | ||
try fputs(" static var allTests : [(String, \(moduleClass.name) -> () throws -> Void)] {\n", file) | ||
try fputs(" return [\n", file) | ||
|
||
try moduleClass.testMethods.sort().forEach { | ||
let methodName = $0[$0.startIndex..<$0.endIndex.advancedBy(-2)] | ||
try fputs(" (\"\(methodName)\", \(methodName)),\n", file) | ||
} | ||
|
||
try fputs(" ]\n", file) | ||
try fputs(" }\n", file) | ||
try fputs("}\n", file) | ||
} | ||
|
||
try fputs("#endif\n\n", file) | ||
} | ||
|
||
private func writeXCTestMain(testModules: [TestModule], path: String) throws { | ||
|
||
//don't write anything if no classes are available | ||
guard testModules.count > 0 else { return } | ||
|
||
let file = try fopen(path, mode: .Write) | ||
defer { | ||
fclose(file) | ||
} | ||
|
||
//imports | ||
try fputs("import XCTest\n", file) | ||
try testModules.flatMap { $0.name }.sort().forEach { | ||
try fputs("@testable import \($0)\n", file) | ||
} | ||
try fputs("\n", file) | ||
|
||
try fputs("XCTMain([\n", file) | ||
|
||
//for each class | ||
for module in testModules { | ||
try module | ||
.classes | ||
.sort { $0.name < $1.name } | ||
.forEach { moduleClass in | ||
try fputs(" testCase(\(module.name).\(moduleClass.name).allTests),\n", file) | ||
} | ||
} | ||
|
||
try fputs("])\n\n", file) | ||
} |
This file contains hidden or 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,35 @@ | ||
/* | ||
This source file is part of the Swift.org open source project | ||
|
||
Copyright 2015 - 2016 Apple Inc. and the Swift project authors | ||
Licensed under Apache License v2.0 with Runtime Library Exception | ||
|
||
See http://swift.org/LICENSE.txt for license information | ||
See http://swift.org/CONTRIBUTORS.txt for Swift project authors | ||
*/ | ||
|
||
import Utility | ||
|
||
public struct TestModule { | ||
struct Class { | ||
let name: String | ||
let testMethods: [String] | ||
} | ||
let name: String | ||
let classes: [Class] | ||
} | ||
|
||
public func parseAST(dir: String) throws -> [TestModule] { | ||
var testModules: [TestModule] = [] | ||
|
||
try walk(dir, recursively: false).filter{$0.isFile}.forEach { file in | ||
let fp = File(path: file) | ||
let astString = try fp.enumerate().reduce("") { $0 + $1 } | ||
let fileName = file.basename | ||
let moduleName = fileName[fileName.startIndex..<fileName.endIndex.advancedBy(-4)] | ||
print("Processing \(moduleName) AST") | ||
testModules += [parseASTString(astString, module: moduleName)] | ||
} | ||
|
||
return testModules | ||
} |
This file contains hidden or 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,108 @@ | ||
/* | ||
This source file is part of the Swift.org open source project | ||
|
||
Copyright 2015 - 2016 Apple Inc. and the Swift project authors | ||
Licensed under Apache License v2.0 with Runtime Library Exception | ||
|
||
See http://swift.org/LICENSE.txt for license information | ||
See http://swift.org/CONTRIBUTORS.txt for Swift project authors | ||
*/ | ||
|
||
func parseASTString(astString: String, module: String) -> TestModule { | ||
let sourceNodes = parseASTString(astString) | ||
var classes: [TestModule.Class] = [] | ||
for source in sourceNodes { | ||
for node in source.nodes { | ||
guard case let .Class(isXCTestCaseSubClass) = node.type where isXCTestCaseSubClass else { continue } | ||
var testMethods: [String] = [] | ||
for classNode in node.nodes { | ||
guard case let .Fn(signature) = classNode.type else { continue } | ||
if classNode.name.hasPrefix("test") && signature == "(\(node.name)) -> () -> ()" { | ||
testMethods += [classNode.name] | ||
} | ||
} | ||
classes += [TestModule.Class(name: node.name, testMethods: testMethods)] | ||
} | ||
} | ||
return TestModule(name: module, classes: classes) | ||
} | ||
|
||
|
||
private class Node { | ||
enum NodeType { | ||
case Class(isXCTestCaseSubClass: Bool) | ||
case Fn(signature: String) // would be like : `(ClassName) -> () -> ()` | ||
case Unknown | ||
} | ||
var contents: String = "" { | ||
didSet { | ||
guard let index = contents.characters.indexOf(" ") else { | ||
return | ||
} | ||
let decl = contents[contents.startIndex..<index] | ||
name = contents.substringBetween("\"") ?? "" | ||
if decl == "class_decl" { | ||
type = .Class(isXCTestCaseSubClass: contents.hasSuffix("XCTestCase")) | ||
} else if decl == "func_decl", let signature = contents.substringBetween("'") { | ||
type = .Fn(signature: signature) | ||
} | ||
} | ||
} | ||
var nodes: [Node] = [] | ||
var type: NodeType = .Unknown | ||
var name: String = "" | ||
} | ||
|
||
private func parseASTString(astString: String) -> [Node] { | ||
var stack = Array<Node>() | ||
var data = "" | ||
var quoteStarted = false | ||
var quoteChar: Character? = nil | ||
var sources: [Node] = [] | ||
|
||
for char in astString.characters { | ||
|
||
if char == "(" && !quoteStarted { | ||
let node = Node() | ||
if data.characters.count > 0, let lastNode = stack.last, let chuzzledData = data.chuzzle() { | ||
lastNode.contents = chuzzledData | ||
if lastNode.contents == "source_file" { sources += [lastNode] } | ||
} | ||
stack.append(node) | ||
data = "" | ||
} else if char == ")" && !quoteStarted { | ||
if case let poppedNode = stack.removeLast() where stack.count > 0 { | ||
if data.characters.count > 0, let chuzzledData = data.chuzzle() { | ||
poppedNode.contents = chuzzledData | ||
} | ||
stack.last!.nodes += [poppedNode] | ||
|
||
} | ||
data = "" | ||
} else { | ||
data = data + String(char) | ||
if char == "\"" || char == "'" { | ||
if quoteChar == nil { | ||
quoteChar = char | ||
quoteStarted = true | ||
} else if char == quoteChar { | ||
quoteChar = nil | ||
quoteStarted = false | ||
} | ||
} | ||
} | ||
|
||
} | ||
return sources | ||
} | ||
|
||
private extension String { | ||
func substringBetween(char: Character) -> String? { | ||
guard let firstIndex = self.characters.indexOf(char) where firstIndex != self.endIndex else { | ||
return nil | ||
} | ||
let choppedString = self[firstIndex.successor()..<self.endIndex] | ||
guard let secondIndex = choppedString.characters.indexOf(char) else { return nil } | ||
return choppedString[choppedString.startIndex..<secondIndex] | ||
} | ||
} |
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are you assuming that tests will never be compiled in Release mode? Otherwise you'd need to add the new generated files down in the .Release branch as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this was totally a work around just to see if it was working, as mentioned in description this file needs refactoring.... I don't even like what I did here 😆