Skip to content

Commit

Permalink
Implements static string extension. Resolves #113 (#114)
Browse files Browse the repository at this point in the history
  • Loading branch information
fgroeger authored Sep 16, 2024
1 parent bae89cb commit a9c476b
Show file tree
Hide file tree
Showing 14 changed files with 91 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Sources/StringGenerator/Snippets/SourceFileSnippet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ struct SourceFileSnippet: Snippet {
ExtensionSnippet(extending: .type(.String)) {
StringStringsTableStructSnippet(stringsTable: sourceFile.stringExtension.stringsTableStruct)
StringInitializerSnippet(stringsTable: sourceFile.stringExtension.stringsTableStruct)
StringsTableConversionStaticMethodSnippet(
stringsTable: sourceFile.stringExtension.stringsTableStruct,
returnType: .type(.String)
)
}

ExtensionSnippet(
Expand Down
27 changes: 27 additions & 0 deletions Tests/PluginTests/PluginTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ final class PluginTests: XCTestCase {
String(localizable: .demoBasic),
"A basic string"
)
XCTAssertEqual(
.localizable(.demoBasic),
"A basic string"
)

XCTAssertEqual(
String(localized: .localizable(.multiline(2))),
Expand All @@ -23,6 +27,13 @@ final class PluginTests: XCTestCase {
spans 2 lines
"""
)
XCTAssertEqual(
.localizable(.multiline(2)),
"""
A string that
spans 2 lines
"""
)

XCTAssertEqual(
String(localized: .featureOne(.pluralExample(1))),
Expand All @@ -32,6 +43,10 @@ final class PluginTests: XCTestCase {
String(featureOne: .pluralExample(1)),
"1 string remaining"
)
XCTAssertEqual(
.featureOne(.pluralExample(1)),
"1 string remaining"
)

XCTAssertEqual(
String(localized: .featureOne(.pluralExample(10))),
Expand All @@ -41,6 +56,10 @@ final class PluginTests: XCTestCase {
String(featureOne: .pluralExample(10)),
"10 strings remaining"
)
XCTAssertEqual(
.featureOne(.pluralExample(10)),
"10 strings remaining"
)
}

func testSwiftUI() {
Expand All @@ -59,11 +78,19 @@ final class PluginTests: XCTestCase {
String(legacy: .simpleString("foo")),
"This simple string: foo"
)
XCTAssertEqual(
.legacy(.simpleString("foo")),
"This simple string: foo"
)

XCTAssertEqual(
String(legacy: .plural("John", 1)),
"Hello John, I have 1 plural"
)
XCTAssertEqual(
.legacy(.plural("John", 1)),
"Hello John, I have 1 plural"
)
}

func testComparisons() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,11 @@ extension String {
arguments: formatSpecifiers.arguments.map(\.value)
)
}

/// Creates a `String` that represents a localized value in the ‘FormatSpecifiers‘ strings table.
internal static func formatSpecifiers(_ formatSpecifiers: String.FormatSpecifiers) -> String {
String(formatSpecifiers: formatSpecifiers)
}
}

@available(macOS 13, iOS 16, tvOS 16, watchOS 9, *)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,11 @@ extension String {
arguments: localizable.arguments.map(\.value)
)
}

/// Creates a `String` that represents a localized value in the ‘Localizable‘ strings table.
internal static func localizable(_ localizable: String.Localizable) -> String {
String(localizable: localizable)
}
}

@available(macOS 13, iOS 16, tvOS 16, watchOS 9, *)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,11 @@ extension String {
arguments: multiline.arguments.map(\.value)
)
}

/// Creates a `String` that represents a localized value in the ‘Multiline‘ strings table.
internal static func multiline(_ multiline: String.Multiline) -> String {
String(multiline: multiline)
}
}

@available(macOS 13, iOS 16, tvOS 16, watchOS 9, *)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,11 @@ extension String {
arguments: positional.arguments.map(\.value)
)
}

/// Creates a `String` that represents a localized value in the ‘Positional‘ strings table.
internal static func positional(_ positional: String.Positional) -> String {
String(positional: positional)
}
}

@available(macOS 13, iOS 16, tvOS 16, watchOS 9, *)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,11 @@ extension String {
arguments: simple.arguments.map(\.value)
)
}

/// Creates a `String` that represents a localized value in the ‘Simple‘ strings table.
internal static func simple(_ simple: String.Simple) -> String {
String(simple: simple)
}
}

@available(macOS 13, iOS 16, tvOS 16, watchOS 9, *)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,11 @@ extension String {
arguments: substitution.arguments.map(\.value)
)
}

/// Creates a `String` that represents a localized value in the ‘Substitution‘ strings table.
internal static func substitution(_ substitution: String.Substitution) -> String {
String(substitution: substitution)
}
}

@available(macOS 13, iOS 16, tvOS 16, watchOS 9, *)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,11 @@ extension String {
arguments: variations.arguments.map(\.value)
)
}

/// Creates a `String` that represents a localized value in the ‘Variations‘ strings table.
internal static func variations(_ variations: String.Variations) -> String {
String(variations: variations)
}
}

@available(macOS 13, iOS 16, tvOS 16, watchOS 9, *)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,11 @@ extension String {
arguments: legacy.arguments.map(\.value)
)
}

/// Creates a `String` that represents a localized value in the ‘Legacy‘ strings table.
internal static func legacy(_ legacy: String.Legacy) -> String {
String(legacy: legacy)
}
}

@available(macOS 13, iOS 16, tvOS 16, watchOS 9, *)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,11 @@ extension String {
arguments: legacy.arguments.map(\.value)
)
}

/// Creates a `String` that represents a localized value in the ‘Legacy‘ strings table.
internal static func legacy(_ legacy: String.Legacy) -> String {
String(legacy: legacy)
}
}

@available(macOS 13, iOS 16, tvOS 16, watchOS 9, *)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,11 @@ extension String {
arguments: legacy.arguments.map(\.value)
)
}

/// Creates a `String` that represents a localized value in the ‘Legacy‘ strings table.
internal static func legacy(_ legacy: String.Legacy) -> String {
String(legacy: legacy)
}
}

@available(macOS 13, iOS 16, tvOS 16, watchOS 9, *)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,11 @@ extension String {
arguments: localizable.arguments.map(\.value)
)
}

/// Creates a `String` that represents a localized value in the ‘Localizable‘ strings table.
package static func localizable(_ localizable: String.Localizable) -> String {
String(localizable: localizable)
}
}

@available(macOS 13, iOS 16, tvOS 16, watchOS 9, *)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,11 @@ extension String {
arguments: localizable.arguments.map(\.value)
)
}

/// Creates a `String` that represents a localized value in the ‘Localizable‘ strings table.
public static func localizable(_ localizable: String.Localizable) -> String {
String(localizable: localizable)
}
}

@available(macOS 13, iOS 16, tvOS 16, watchOS 9, *)
Expand Down

0 comments on commit a9c476b

Please sign in to comment.