File tree Expand file tree Collapse file tree
NativeAppTemplateTests/Extensions
NativeAppTemplate/Extensions Expand file tree Collapse file tree Original file line number Diff line number Diff line change 33// NativeAppTemplate
44//
55
6- import UIKit
6+ import Foundation
77
88extension String {
9- /// Generates a `UIImage` instance from this string using a specified
10- /// attributes and size.
11- ///
12- /// - Parameters:
13- /// - attributes: to draw this string with. Default is `nil`.
14- /// - size: of the image to return.
15- /// - Returns: a `UIImage` instance from this string using a specified
16- /// attributes and size, or `nil` if the operation fails.
17- func image( withAttributes attributes: [ NSAttributedString . Key : Any ] ? = nil , size: CGSize ? = nil ) -> UIImage ? {
18- let size = size ?? ( self as NSString ) . size ( withAttributes: attributes)
19- return UIGraphicsImageRenderer ( size: size) . image { _ in
20- ( self as NSString ) . draw (
21- in: CGRect ( origin: . zero, size: size) ,
22- withAttributes: attributes
23- )
24- }
25- }
26-
27- func isAlphanumeric( ) -> Bool {
28- rangeOfCharacter ( from: CharacterSet . alphanumerics. inverted) == nil && !isEmpty
29- }
30-
31- func isAlphanumeric( ignoreDiacritics: Bool = false ) -> Bool {
32- if ignoreDiacritics {
33- range ( of: " [^a-zA-Z0-9] " , options: . regularExpression) == nil && !isEmpty
34- } else {
35- isAlphanumeric ( )
36- }
37- }
38-
399 var isBlank : Bool {
4010 trimmingCharacters ( in: . whitespacesAndNewlines) . isEmpty
4111 }
Original file line number Diff line number Diff line change 33// NativeAppTemplate
44//
55
6- import CoreGraphics
76@testable import NativeAppTemplate
87import Testing
98
109struct StringExtensionsTest {
11- @Test ( arguments: [
12- ( " hello123 " , true ) ,
13- ( " ABC " , true ) ,
14- ( " abc " , true ) ,
15- ( " 123 " , true ) ,
16- ( " Hello World " , false ) ,
17- ( " hello! " , false ) ,
18- ( " hello@world " , false ) ,
19- ( " " , false )
20- ] )
21- func isAlphanumeric( input: String , expected: Bool ) {
22- #expect( input. isAlphanumeric ( ) == expected)
23- }
24-
25- @Test
26- func isAlphanumericIgnoringDiacritics( ) {
27- #expect( " hello123 " . isAlphanumeric ( ignoreDiacritics: true ) == true )
28- #expect( " ABC123 " . isAlphanumeric ( ignoreDiacritics: true ) == true )
29- #expect( " hello world " . isAlphanumeric ( ignoreDiacritics: true ) == false )
30- #expect( " " . isAlphanumeric ( ignoreDiacritics: true ) == false )
31- }
32-
33- @Test
34- func imageGeneration( ) {
35- let image = " A " . image ( size: CGSize ( width: 50 , height: 50 ) )
36- #expect( image != nil )
37- }
38-
3910 @Test ( arguments: [
4011 ( " " , true ) ,
4112 ( " " , true ) ,
You can’t perform that action at this time.
0 commit comments