Skip to content

Commit

Permalink
Improve className resolution (#8) (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
kam800 authored and ar4s committed Nov 6, 2019
1 parent 5884684 commit a8d1491
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Sources/SwiftTestReporter/TestObserver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ extension TestObserver: XCTestObservation {
/// XCTest creates additional XCTestSuite:
/// - "All Test" which contains all test suites
/// - test suite for each module in tests (e.g. SwiftTestReporterTests)
let className = getXCTestClassName(testSuite)
let className = testSuite.customClassName
return className == "XCTestCaseSuite"
}

Expand Down
21 changes: 9 additions & 12 deletions Sources/SwiftTestReporter/Utils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,13 @@ private func lastChunk(_ phrase: String, separator: Character = ".") -> String {
return phrase.split(separator: separator).last?.description ?? phrase
}

/// Simple helper that returns class name for XCTest.
/// There are some difference beween original XCTest and XCTest in open source and this
/// helper solves one of the problems - missing property (className).
/// - Parameter obj: XCTest's instance
/// - Returns: A normalized name for XCTest's instance
func getXCTestClassName(_ obj: XCTest) -> String {
#if os(Linux)
let className = "\(obj.self)"
#else
let className = obj.className
#endif
return lastChunk(className)
extension XCTest {
/// Simple helper that returns class name for XCTest.
/// There are some difference between original XCTest on different platforms and XCTest in open source and this
/// helper solves one of the problems - missing property (className).
/// - Returns: A normalized name for XCTest's instance
var customClassName : String {
let fullClassName = "\(type(of: self))"
return fullClassName
}
}
8 changes: 7 additions & 1 deletion Tests/SwiftTestReporterTests/UtilsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,14 @@ import XCTest

class UtilsTests: XCTestCase {
func testShouldReturnProperlyClassName() {
let xcTest: XCTest = self
XCTAssertEqual(xcTest.customClassName, "UtilsTests")
}

func testShouldReturnProperlyClassNameForNestedClass() {
class FooTestCase: XCTest {}
XCTAssertEqual(getXCTestClassName(self), "UtilsTests")
let xcTest: XCTest = FooTestCase()
XCTAssertEqual(xcTest.customClassName, "FooTestCase")
}

static var allTests = [
Expand Down

0 comments on commit a8d1491

Please sign in to comment.