Skip to content

Commit

Permalink
PatternTextStyle: added initializer parameters. `PatternTextPlaceho…
Browse files Browse the repository at this point in the history
…lders` now conform to `ExpressibleByDictionaryLiteral`. Cleanup.
  • Loading branch information
oscbyspro committed Aug 31, 2022
1 parent bbd4666 commit 657122b
Show file tree
Hide file tree
Showing 15 changed files with 71 additions and 63 deletions.
9 changes: 8 additions & 1 deletion Sources/DiffableTextKitXPattern/Placeholders.swift
Expand Up @@ -11,7 +11,7 @@
// MARK: * Placeholders
//*============================================================================*

public struct PatternTextPlaceholders: Equatable {
public struct PatternTextPlaceholders: Equatable, ExpressibleByDictionaryLiteral {

@usableFromInline typealias Predicate = (Character) -> Bool

Expand Down Expand Up @@ -41,6 +41,13 @@ public struct PatternTextPlaceholders: Equatable {
self.option = .many(Many(many))
}

@inlinable public init(dictionaryLiteral elements: (Character, (Character) -> Bool)...) {
switch elements.count /* O(1) */ {
case 0: self.init()
case 1: self.init(elements[0])
default: self.init(Dictionary(elements, uniquingKeysWith: { $1 })) }
}

//=------------------------------------------------------------------------=
// MARK: Accessors
//=------------------------------------------------------------------------=
Expand Down
6 changes: 3 additions & 3 deletions Sources/DiffableTextKitXPattern/Style.swift
Expand Up @@ -24,14 +24,14 @@ Value: RangeReplaceableCollection, Value.Element == Character {

public var pattern: String
public var placeholders: Placeholders
public var hidden: Bool = false
public var hidden: Bool

//=------------------------------------------------------------------------=
// MARK: Initializers
//=------------------------------------------------------------------------=

@inlinable public init(_ pattern: String, placeholders: Placeholders = .init()) {
self.pattern = pattern; self.placeholders = placeholders
@inlinable public init(_ pattern: String, placeholders: Placeholders = .init(), hidden: Bool = false) {
self.pattern = pattern; self.placeholders = placeholders; self.hidden = hidden
}

//=------------------------------------------------------------------------=
Expand Down
Expand Up @@ -44,7 +44,7 @@ public extension View {
/// It is similar to `View/autocorrectionDisabled(_:)`.
///
/// ```
/// DiffableTextField("Text", value: $text, style: .normal)
/// DiffableTextField("Text...", value: $text, style: .normal)
/// .diffableTextViews_autocorrectionDisabled(true)
/// ```
///
Expand Down
2 changes: 1 addition & 1 deletion Sources/DiffableTextKitXUIKit/Environment+Font.swift
Expand Up @@ -44,7 +44,7 @@ public extension View {
/// It is similar to `View/font(_:)` but uses a SwiftUI-esque system font type.
///
/// ```
/// DiffableTextField("Monospaced", value: $value, style: style)
/// DiffableTextField("Amount", value: $amount, style: .number)
/// .diffableTextViews_font(.body.monospaced())
/// ```
///
Expand Down
Expand Up @@ -44,7 +44,7 @@ public extension View {
/// It is similar to `View/keyboardType(_:)`.
///
/// ```
/// TextField("Amount", value: $amount, style: .currency("USD"))
/// DiffableTextField("Amount", value: $amount, style: .number)
/// .diffableTextViews_keyboardType(.decimalPad)
/// ```
///
Expand Down
Expand Up @@ -44,7 +44,7 @@ public extension View {
/// It is similar to `View/multilineTextAlignment(_:)`.
///
/// ```
/// DiffableTextField("Amount", value: $value, style: .number)
/// DiffableTextField("Amount", value: $amount, style: .number)
/// .diffableTextViews_multilineTextAlignment(.trailing)
/// ```
///
Expand Down
4 changes: 2 additions & 2 deletions Sources/DiffableTextKitXUIKit/Environment+SubmitLabel.swift
Expand Up @@ -44,8 +44,8 @@ public extension View {
/// It is similar to `View/submitLabel(_:)`.
///
/// ```
/// DiffableTextField("Prints on submit...", value: $value, style: style)
/// .diffableTextViews_submitLabel(.return)
/// DiffableTextField("Search", value: $text, style: .normal)
/// .diffableTextViews_submitLabel(.search)
/// ```
///
/// **Notes**
Expand Down
Expand Up @@ -45,8 +45,12 @@ public extension View {
/// It is similar to `View/textContentType(_:)`.
///
/// ```
/// DiffableTextField("Enter your email", text: $emailAddress)
/// .diffableTextViews_textContentTypetextContentType(.emailAddress)
/// DiffableTextField("Phone", value: $address) {
/// .pattern("+## (###) ###-##-##")
/// .placeholders("#") { $0.isASCII && $0.isNumber }
/// .equals(())
/// }
/// .diffableTextViews_textContentType(.telephoneNumber)
/// ```
///
/// **Notes**
Expand Down
Expand Up @@ -41,7 +41,7 @@ public extension View {

/// Sets the text field style for diffable text views.
///
/// It is similar to View/textFieldStyle, but based on UIKit.
/// It is similar to `View/textFieldStyle(_:), but based on UIKit.
///
/// ```
/// DiffableTextField("Bordered", value: $value, style: style)
Expand All @@ -52,7 +52,7 @@ public extension View {
///
/// - The value is read when the view is set up.
/// - The default value is `UITextField.BorderStyle.none`.
/// - The `View/textFieldStyle` environment value is inaccessible.
/// - The `View/textFieldStyle(_:)` environment value is inaccessible.
///
@inlinable func diffableTextViews_textFieldStyle(_ style: UITextField.BorderStyle) -> some View {
self.environment(\.diffableTextViews_textFieldStyle, style)
Expand Down
4 changes: 2 additions & 2 deletions Sources/DiffableTextKitXUIKit/Environment+Tint.swift
Expand Up @@ -44,8 +44,8 @@ public extension View {
/// It is similar to `View/tint(_:)` and affects text selection.
///
/// ```
/// DiffableTextField("Look, selection is green!", value: $value)
/// .diffableTextViews_tint(.green)
/// DiffableTextField("Tinted", value: $value, style: style)
/// .diffableTextViews_tint(.gray)
/// ```
///
/// **Notes**
Expand Down
Expand Up @@ -59,7 +59,7 @@ public extension View {
///
/// - The value is read when the view is set up.
/// - The default value is `nil` (no toolbar installed).
/// - The `SwiftUI/toolbar` environment value is inaccessible.
/// - The `View/toolbar(_:)` environment value is inaccessible.
///
@inlinable func diffableTextViews_toolbarDoneButton(_ style: UIBarButtonItem.Style = .plain) -> some View {
self.environment(\.diffableTextViews_toolbarDoneButton, style)
Expand Down
21 changes: 15 additions & 6 deletions Tests/DiffableTextKitXPatternTests/Placeholders.swift
Expand Up @@ -30,14 +30,13 @@ final class PlaceholdersTests: XCTestCase {
lazy var many = Placeholders(["#": \.isNumber, "@": \.isLetter])

//=------------------------------------------------------------------------=
// MARK: Assertions
// MARK: Tests x Init
//=------------------------------------------------------------------------=

func AssertSubscriptResults(_ instance: Placeholders,
_ scenarios: () -> [(Character, Character, Bool?)]) {
for scenario in scenarios() {
XCTAssertEqual(instance[scenario.0]?(scenario.1), scenario.2)
}
func testExpressibleByDictionaryLiteral() {
XCTAssertEqual(none, [:])
XCTAssertEqual(some, ["#": \.isNumber])
XCTAssertEqual(many, ["#": \.isNumber, "@": \.isLetter])
}

//=------------------------------------------------------------------------=
Expand Down Expand Up @@ -91,6 +90,16 @@ final class PlaceholdersTests: XCTestCase {
Placeholders(["A": { _ in true }, "B": { _ in true }]),
Placeholders(["A": { _ in true }, "_": { _ in true }]))
}

//=------------------------------------------------------------------------=
// MARK: Assertions
//=------------------------------------------------------------------------=

func AssertSubscriptResults(_ instance: Placeholders, _ scenarios: () -> [(Character, Character, Bool?)]) {
for scenario in scenarios() {
XCTAssertEqual(instance[scenario.0]?(scenario.1), scenario.2)
}
}
}

#endif
30 changes: 13 additions & 17 deletions Tests/DiffableTextKitXPatternTests/Style+Hidden.swift
Expand Up @@ -24,7 +24,7 @@ final class StyleTestsOnHidden: XCTestCase, StyleTests {
//=------------------------------------------------------------------------=

let style = PatternTextStyle<String>
.pattern("+### (###) ##-##-##")
.pattern("+## (###) ###-##-##")
.placeholders("#") { $0.isASCII && $0.isNumber }
.hidden(true)

Expand All @@ -33,37 +33,33 @@ final class StyleTestsOnHidden: XCTestCase, StyleTests {
//=------------------------------------------------------------------------=

func testNone() {
XCTFormat___("", format: "+")
XCTInterpret("", format: "+", value: "")
OKFormat___("", format: "+")
OKInterpret("", format: "+", value: "")
}

func testSome() {
XCTFormat___("12300045", format: "+123 (000) 45")
XCTInterpret("12300045", format: "+123 (000) 45", value: "12300045")
OKFormat___("12000345", format: "+12 (000) 345")
OKInterpret("12000345", format: "+12 (000) 345", value: "12000345")
}

func testFull() {
XCTFormat___("123000456789", format: "+123 (000) 45-67-89")
XCTInterpret("123000456789", format: "+123 (000) 45-67-89", value: "123000456789")
OKFormat___("120003456789", format: "+12 (000) 345-67-89")
OKInterpret("120003456789", format: "+12 (000) 345-67-89", value: "120003456789")
}

func testMore() {
XCTFormat___("123000456789000", format: "+123 (000) 45-67-89|000")
XCTInterpret("123000456789000", format: "+123 (000) 45-67-89", value: "123000456789")
OKFormat___("120003456789000", format: "+12 (000) 345-67-89|000")
OKInterpret("120003456789000", format: "+12 (000) 345-67-89", value: "120003456789")
}

//=------------------------------------------------------------------------=
// MARK: Tests
//=------------------------------------------------------------------------=

func testNoneMismatch() {
XCTFormat___("ABC", format: "+|ABC")
XCTInterpret("ABC", format: "+", value: "")
OKFormat___("ABC", format: "+|ABC")
OKInterpret("ABC", format: "+", value: "")
}

func testSomeMismatch() {
XCTFormat___("12300045ABC", format: "+123 (000) 45|ABC")
XCTInterpret("12300045ABC", format: "+123 (000) 45", value: "12300045")
OKFormat___("12000345ABC", format: "+12 (000) 345|ABC")
OKInterpret("12000345ABC", format: "+12 (000) 345", value: "12000345")
}
}

Expand Down
30 changes: 13 additions & 17 deletions Tests/DiffableTextKitXPatternTests/Style+Visible.swift
Expand Up @@ -24,7 +24,7 @@ final class StyleTestsOnVisible: XCTestCase, StyleTests {
//=------------------------------------------------------------------------=

let style = PatternTextStyle<String>
.pattern("+### (###) ##-##-##")
.pattern("+## (###) ###-##-##")
.placeholders("#") { $0.isASCII && $0.isNumber }
.hidden(false)

Expand All @@ -33,37 +33,33 @@ final class StyleTestsOnVisible: XCTestCase, StyleTests {
//=------------------------------------------------------------------------=

func testNone() {
XCTFormat___("", format: "+### (###) ##-##-##")
XCTInterpret("", format: "+### (###) ##-##-##", value: "")
OKFormat___("", format: "+## (###) ###-##-##")
OKInterpret("", format: "+## (###) ###-##-##", value: "")
}

func testSome() {
XCTFormat___("12300045", format: "+123 (000) 45-##-##")
XCTInterpret("12300045", format: "+123 (000) 45-##-##", value: "12300045")
OKFormat___("12000345", format: "+12 (000) 345-##-##")
OKInterpret("12000345", format: "+12 (000) 345-##-##", value: "12000345")
}

func testFull() {
XCTFormat___("123000456789", format: "+123 (000) 45-67-89")
XCTInterpret("123000456789", format: "+123 (000) 45-67-89", value: "123000456789")
OKFormat___("120003456789", format: "+12 (000) 345-67-89")
OKInterpret("120003456789", format: "+12 (000) 345-67-89", value: "120003456789")
}

func testMore() {
XCTFormat___("123000456789000", format: "+123 (000) 45-67-89|000")
XCTInterpret("123000456789000", format: "+123 (000) 45-67-89", value: "123000456789")
OKFormat___("120003456789000", format: "+12 (000) 345-67-89|000")
OKInterpret("120003456789000", format: "+12 (000) 345-67-89", value: "120003456789")
}

//=------------------------------------------------------------------------=
// MARK: Tests
//=------------------------------------------------------------------------=

func testNoneMismatch() {
XCTFormat___("ABC", format: "+### (###) ##-##-##|ABC")
XCTInterpret("ABC", format: "+### (###) ##-##-##", value: "")
OKFormat___("ABC", format: "+## (###) ###-##-##|ABC")
OKInterpret("ABC", format: "+## (###) ###-##-##", value: "")
}

func testSomeMismatch() {
XCTFormat___("12300045ABC", format: "+123 (000) 45-##-##|ABC")
XCTInterpret("12300045ABC", format: "+123 (000) 45-##-##", value: "12300045")
OKFormat___("12000345ABC", format: "+12 (000) 345-##-##|ABC")
OKInterpret("12000345ABC", format: "+12 (000) 345-##-##", value: "12000345")
}
}

Expand Down
8 changes: 2 additions & 6 deletions Tests/DiffableTextKitXPatternTests/Style.swift
Expand Up @@ -33,10 +33,6 @@ protocol StyleTests: XCTestCase {
func testSome()
func testFull()
func testMore()

//=------------------------------------------------------------------------=
// MARK: Tests
//=------------------------------------------------------------------------=

func testNoneMismatch()
func testSomeMismatch()
Expand All @@ -52,11 +48,11 @@ extension StyleTests {
// MARK: Assertions
//=------------------------------------------------------------------------=

func XCTFormat___(_ input: String, format: String) {
func OKFormat___(_ input: String, format: String) {
XCTAssertEqual(style.format(input), format)
}

func XCTInterpret(_ input: String, format: String, value: String) {
func OKInterpret(_ input: String, format: String, value: String) {
let testable = style.interpret(input)
XCTAssertEqual(testable.value, value)
XCTAssertEqual(testable.snapshot.characters, format)
Expand Down

0 comments on commit 657122b

Please sign in to comment.