Skip to content

Commit

Permalink
Fixes #1
Browse files Browse the repository at this point in the history
Gather all types based on name so extensions count towards their extending type.
  • Loading branch information
alexito4 committed Feb 11, 2020
1 parent d0430b1 commit e76ce10
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 21 deletions.
5 changes: 4 additions & 1 deletion Sources/SitrepCore/FileVisitor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,11 @@ class FileVisitor: SyntaxVisitor {
let inheritanceClause = node.inheritanceClause?.inheritedTypeCollection.map {
"\($0.typeName)".trimmingCharacters(in: .whitespacesAndNewlines)
} ?? []

let name = node.name
.trimmingCharacters(in: .whitespaces)

let newObject = Type(type: type, name: node.name, inheritance: inheritanceClause, comments: comments(for: node), body: nodeBody, strippedBody: nodeBodyStripped)
let newObject = Type(type: type, name: name, inheritance: inheritanceClause, comments: comments(for: node), body: nodeBody, strippedBody: nodeBodyStripped)

newObject.parent = current
current?.types.append(newObject)
Expand Down
28 changes: 14 additions & 14 deletions Sources/SitrepCore/Scan.swift
Original file line number Diff line number Diff line change
Expand Up @@ -84,27 +84,21 @@ public struct Scan {
func collate(_ scannedFiles: [File]) -> Results {
var results = Results(files: scannedFiles)

var allTypesAndLenght = [String: Int]()
var allTypesByName = [String: Type]()

for file in scannedFiles {
// order all our types by what they are
for item in file.results.rootNode.types {
let name = item.name
let typeLength = item.strippedBody.lines.count
allTypesAndLenght[name, default: 0] += typeLength
allTypesByName[name] = item

if item.type == .class {
results.classes.append(item)

let typeLength = item.strippedBody.lines.count

if typeLength > results.longestTypeLength {
results.longestTypeLength = typeLength
results.longestType = item
}
} else if item.type == .struct {
results.structs.append(item)

let typeLength = item.strippedBody.lines.count

if typeLength > results.longestTypeLength {
results.longestTypeLength = typeLength
results.longestType = item
}
} else if item.type == .enum {
results.enums.append(item)
} else if item.type == .protocol {
Expand All @@ -129,6 +123,12 @@ public struct Scan {
results.longestFileLength = fileLength
}
}

// find the longest type and its length
if let longestMatch = allTypesAndLenght.max(by: { $0.value < $1.value }) {
results.longestTypeLength = longestMatch.value
results.longestType = allTypesByName[longestMatch.key]!
}

return results
}
Expand Down
46 changes: 46 additions & 0 deletions Tests/SitrepCoreTests/Inputs/type_and_extension.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
struct Foobar {
var text = "Hello, World!"
}

extension Foobar {
func foo() -> Int {
var i: Int = 0
i += 1
i += 1
i += 1
i += 1
i += 1
i += 1
i += 1
i += 1
i += 1
i += 1
i += 1
i += 1
i += 1
i += 1
i += 1
i += 1
i += 1
i += 1
i += 1
i += 1
i += 1
i += 1
i += 1
i += 1
i += 1
i += 1
i += 1
i += 1
i += 1
i += 1
i += 1
i += 1
i += 1
i += 1
i += 1
i += 1
return i
}
}
23 changes: 17 additions & 6 deletions Tests/SitrepCoreTests/SitrepCoreTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ final class SitrepCoreTests: XCTestCase {
let app = Scan(rootURL: inputs)
let files = app.detectFiles()

XCTAssertEqual(files.count, 8)
XCTAssertEqual(files.count, 9)
}

func testBadFileScanning() throws {
Expand All @@ -137,7 +137,7 @@ final class SitrepCoreTests: XCTestCase {
let app = Scan(rootURL: inputs)
let (_, files, failures) = app.run(creatingReport: false)

XCTAssertEqual(files.count, 8)
XCTAssertEqual(files.count, 9)
XCTAssertEqual(failures.count, 0)
}

Expand All @@ -146,10 +146,10 @@ final class SitrepCoreTests: XCTestCase {
let (results, _, _) = app.run(creatingReport: false)

XCTAssertEqual(results.classes.count, 3)
XCTAssertEqual(results.structs.count, 2)
XCTAssertEqual(results.structs.count, 3)
XCTAssertEqual(results.enums.count, 1)
XCTAssertEqual(results.protocols.count, 4)
XCTAssertEqual(results.extensions.count, 1)
XCTAssertEqual(results.extensions.count, 2)
}

func testCollationImports() throws {
Expand Down Expand Up @@ -211,9 +211,19 @@ final class SitrepCoreTests: XCTestCase {
let app = Scan(rootURL: inputs)
let (_, files, failures) = app.run(creatingReport: true)

XCTAssertEqual(files.count, 8)
XCTAssertEqual(files.count, 9)
XCTAssertEqual(failures.count, 0)
}

func testLongestType() throws {
let app = Scan(rootURL: inputs)
let (results, _, _) = app.run(creatingReport: false)

// Code in extensions should count towards the length of the type they're extending.
// https://github.com/twostraws/Sitrep/issues/1
XCTAssertEqual(results.longestType?.name, "Foobar")
XCTAssertEqual(results.longestTypeLength, 45)
}

static var allTests = [
("testClassDetection", testClassDetection),
Expand All @@ -235,6 +245,7 @@ final class SitrepCoreTests: XCTestCase {
("testTextReportGeneration", testTextReportGeneration),
("testJSONReportGeneration", testJSONReportGeneration),
("testBodyStripperRemovedComments", testBodyStripperRemovedComments),
("testCreatingReport", testCreatingReport)
("testCreatingReport", testCreatingReport),
("testLongestType", testLongestType)
]
}

0 comments on commit e76ce10

Please sign in to comment.