Skip to content

Commit

Permalink
✨ feat: support package access level (#78)
Browse files Browse the repository at this point in the history
* ✨ feat: support `package` access level

* chore: separate tests for `package` access modifier

* chore: handle `package` test differently when compiling < Swift 5.9
  • Loading branch information
MarcoEidinger authored Dec 31, 2023
1 parent 274d46c commit d4d8be5
Show file tree
Hide file tree
Showing 11 changed files with 50 additions and 9 deletions.
3 changes: 2 additions & 1 deletion Configuration/Examples/Rich/.swiftplantuml.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ elements:
- public
showMembersWithAccessLevel:
- public
- package
- internal
- private
showGenerics: true
Expand Down Expand Up @@ -74,4 +75,4 @@ texts:
on <i>several</i> lines and using <font color=red>html</font>
legend: Some boxed text
caption: Second to last
footer: The end
footer: The end
2 changes: 2 additions & 0 deletions Configuration/Schema/json-schema-swiftplantuml.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@
"havingAccessLevel": [
"open",
"public",
"package",
"internal",
"private"
],
"showMembersWithAccessLevel": [
"open",
"public",
"package",
"internal",
"private"
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ public enum AccessLevel: String, Codable {
case open
/// `public`
case `public`
/// `package`
case `package`
/// `internal`
case `internal`
/// `private`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import Foundation
/// Options which and how elements shall be considered for class diagram generation
public struct ElementOptions: Codable {
/// only elements (classes, structs, ...) with the specified access level will be processed and rendered in the class diagram
public private(set) var havingAccessLevel: [AccessLevel] = [.open, .public, .internal, .private]
public private(set) var havingAccessLevel: [AccessLevel] = [.open, .public, .package, .internal, .private]
/// only members (properties and functions) with the specified access level will be processed and renderd in the class diagram
public private(set) var showMembersWithAccessLevel: [AccessLevel] = [.open, .public, .internal, .private]
public private(set) var showMembersWithAccessLevel: [AccessLevel] = [.open, .public, .package, .internal, .private]

/// show nested types
public private(set) var showNestedTypes: Bool = true
Expand Down Expand Up @@ -57,8 +57,8 @@ public struct ElementOptions: Codable {

/// memberwise initializer
public init(
havingAccessLevel: [AccessLevel] = [.open, .public, .internal, .private],
showMembersWithAccessLevel: [AccessLevel] = [.open, .public, .internal, .private],
havingAccessLevel: [AccessLevel] = [.open, .public, .package, .internal, .private],
showMembersWithAccessLevel: [AccessLevel] = [.open, .public, .package, .internal, .private],
showNestedTypes: Bool = true,
showGenerics: Bool = true,
showExtensions: ExtensionVisualization? = nil,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ extension ElementAccessibility {
switch self {
case .public, .open:
return "+"
case .internal:
case .internal, .package:
return "~"
case .private, .fileprivate:
return "-"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ internal enum ElementAccessibility: String, RawRepresentable, Comparable {
case open = "source.lang.swift.accessibility.open"
/// `public`
case `public` = "source.lang.swift.accessibility.public"
/// `package`
case package = "source.lang.swift.accessibility.package"
/// `internal`
case `internal` = "source.lang.swift.accessibility.internal"
/// `private`
Expand All @@ -99,8 +101,10 @@ internal enum ElementAccessibility: String, RawRepresentable, Comparable {
private var value: Int {
switch self {
case .open:
return 6
return 7
case .public:
return 6
case .package:
return 5
case .internal:
return 4
Expand All @@ -119,6 +123,8 @@ internal enum ElementAccessibility: String, RawRepresentable, Comparable {
self.init(rawValue: "source.lang.swift.accessibility.open")
case .public:
self.init(rawValue: "source.lang.swift.accessibility.public")
case .package:
self.init(rawValue: "source.lang.swift.accessibility.package")
case .internal:
self.init(rawValue: "source.lang.swift.accessibility.internal")
case .private:
Expand Down
4 changes: 2 additions & 2 deletions Tests/SwiftPlantUMLFrameworkTests/ConfigurationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import XCTest
final class PlantUMLConfigurationTests: XCTestCase {
func testDefault() {
let config = Configuration()
XCTAssertEqual(config.elements.havingAccessLevel.count, 4)
XCTAssertEqual(config.elements.showMembersWithAccessLevel.count, 4)
XCTAssertEqual(config.elements.havingAccessLevel.count, 5)
XCTAssertEqual(config.elements.showMembersWithAccessLevel.count, 5)
}

func testDecodingObsoleteShowExtensionsBooleanProperty() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import XCTest
final class ElementAccessibilityTests: XCTestCase {
func testComparison() {
XCTAssertTrue(ElementAccessibility.open > ElementAccessibility.public)
XCTAssertTrue(ElementAccessibility.public > ElementAccessibility.package)
XCTAssertTrue(ElementAccessibility.public > ElementAccessibility.internal)
XCTAssertTrue(ElementAccessibility.public > ElementAccessibility.private)
XCTAssertTrue(ElementAccessibility.package > ElementAccessibility.internal)
XCTAssertTrue(ElementAccessibility.internal > ElementAccessibility.private)
XCTAssertTrue(ElementAccessibility.private > ElementAccessibility.fileprivate)
XCTAssertTrue(ElementAccessibility.private > ElementAccessibility.other)
Expand All @@ -15,6 +17,7 @@ final class ElementAccessibilityTests: XCTestCase {
func testIndicator() {
XCTAssertEqual(ElementAccessibility.open.indicator, "+")
XCTAssertEqual(ElementAccessibility.public.indicator, "+")
XCTAssertEqual(ElementAccessibility.package.indicator, "~")
XCTAssertEqual(ElementAccessibility.internal.indicator, "~")
XCTAssertEqual(ElementAccessibility(orig: .internal)?.indicator, "~")
XCTAssertEqual(ElementAccessibility.private.indicator, "-")
Expand Down
11 changes: 11 additions & 0 deletions Tests/SwiftPlantUMLFrameworkTests/PlantUMLScriptTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,17 @@ final class PlantUMLScriptTests: XCTestCase {
XCTAssertTrue(script.text.contains(headerText))
}

func testPackageAccessModifierE2E() {
guard let items = try! SyntaxStructure.create(from: getTestFile(named: "packageAccessModifier"))?.substructure else { return XCTFail("cannot read test data") }
let script = PlantUMLScript(items: items)
let expected = try! getTestFileContent(named: "packageAccessModifierAsPlantUML")
#if swift(>=5.9)
XCTAssertEqual(script.text.noSpacesAndNoLineBreaks, expected.noSpacesAndNoLineBreaks)
#else
XCTAssertNotNil("Compiling SwiftPlantUML with a lower Swift version will have incorrect result, i.e. ~{static}package()~aStaticPackageInstanceMethod()")
#endif
}

func getTestFile(named: String = "basics") throws -> URL {
// https://stackoverflow.com/questions/47177036/use-resources-in-unit-tests-with-swift-package-manager
let path = Bundle.module.path(forResource: named, ofType: "txt", inDirectory: "TestData") ?? "nonesense"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import Foundation

package struct anPackageInternalStruct {
static package func aStaticPackageInstanceMethod()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
@startuml
' STYLE START
hide empty members
skinparam shadowing false
' STYLE END
set namespaceSeparator none


class "anPackageInternalStruct" as anPackageInternalStruct << (S, SkyBlue) struct >> { ~{static} aStaticPackageInstanceMethod()
}
@enduml

0 comments on commit d4d8be5

Please sign in to comment.