Skip to content

Commit

Permalink
Merge pull request #52 from faberNovel/release/v12.1.0
Browse files Browse the repository at this point in the history
Release 12.1.0
  • Loading branch information
alexandre-pod authored Jan 15, 2024
2 parents 802d9d8 + 1badd64 commit 67dd95a
Show file tree
Hide file tree
Showing 14 changed files with 417 additions and 12 deletions.
2 changes: 1 addition & 1 deletion ADUtils.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |spec|
spec.name = 'ADUtils'
spec.version = '12.0.1'
spec.version = '12.1.0'
spec.authors = 'Fabernovel'
spec.homepage = 'https://github.com/faberNovel/ADUtils'
spec.summary = 'Fabernovel\'s toolbox for iOS'
Expand Down
184 changes: 181 additions & 3 deletions ADUtilsTests/AttributedStringTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
//
//

import Foundation
import UIKit
import SwiftUI
import Nimble
import SnapshotTesting
import Quick
import ADUtils
@testable import ADUtils
@testable import ADUtilsApp

@MainActor
Expand Down Expand Up @@ -54,7 +55,7 @@ class AttributedStringTest: QuickSpec {
assertSnapshot(matching: label, as: .image, named: imageName)
}

it("Snapshots should match") {
it("NSAttributedString snapshots should match") {
let format = "Test %1$@ %2$@"
let value1 = "Toto"
let value2 = "Testi testo"
Expand All @@ -67,5 +68,182 @@ class AttributedStringTest: QuickSpec {
let formatWithEmoji = "Test 🦁 %1$@ %2$@"
stringTest(formatWithEmoji, [value1, value2], "EmojiInFormat")
}

if #available(iOS 15.0, *) {
testAttributedString()
}
}

@available(iOS 15.0, tvOS 15.0, *)
class func testAttributedString() {

guard
let smallFont = UIFont(name: "HelveticaNeue", size: 12.0),
let bigFont = UIFont(name: "HelveticaNeue", size: 24.0) else {
fail("Font HelveticaNeue do not exists")
return
}

let attributes = AttributeContainer {
$0.foregroundColor = .red
$0.font = smallFont
}
let attributes1 = AttributeContainer {
$0.foregroundColor = .green
$0.font = smallFont
}
let attributes2 = AttributeContainer {
$0.foregroundColor = .blue
$0.font = bigFont
}

let arguments = ["11", "22", "33", "44"]
let alternatingFormatAttributes = [attributes1, attributes2, attributes1, attributes2]


it("AttributedString snapshot should match") {
testAllAttributedStringImplementations(
format: "%@-%@-%@-%@",
arguments: arguments,
defaultAttributes: attributes,
differentFormatAttributes: alternatingFormatAttributes,
imageName: "differentAttributes"
)
testAllAttributedStringImplementations(
format: "%@-%@-%@-%@",
arguments: arguments,
defaultAttributes: attributes,
differentFormatAttributes: [attributes1, attributes1, attributes1, attributes1],
imageName: "sameAttributes"
)
testAllAttributedStringImplementations(
format: "%1$@-%2$@-%3$@-%4$@",
arguments: arguments,
defaultAttributes: attributes,
differentFormatAttributes: alternatingFormatAttributes,
imageName: "explicitIndexInOrder"
)
testAllAttributedStringImplementations(
format: "%4$@-%3$@-%2$@-%1$@",
arguments: arguments,
defaultAttributes: attributes,
differentFormatAttributes: alternatingFormatAttributes,
imageName: "explicitIndexReversed"
)
testAllAttributedStringImplementations(
format: "%2$@-%1$@-%@-%@",
arguments: arguments,
defaultAttributes: attributes,
differentFormatAttributes: alternatingFormatAttributes,
imageName: "mixedImplicitExplicitOrder"
)
testAllAttributedStringImplementations(
format: "begin 🦁%1$@🦁-🦁%2$@🦁-🦁%3$@🦁-🦁%4$@🦁 end",
arguments: arguments,
defaultAttributes: attributes,
differentFormatAttributes: alternatingFormatAttributes,
imageName: "string with emojis"
)
}

it("should allow to create an AttributedString from a format and multiple AttributedString") {
let baseFormat = "This is a simple text followed by %1$@."
let defaultAttributes = AttributeContainer {
$0.font = UIFont.systemFont(ofSize: 14)
}

let subPartFormat = "a bold text containing a %1$@ and then finishing"
let boldAttributes = AttributeContainer {
$0.font = UIFont.boldSystemFont(ofSize: 14)
}

let linkArgument = "link"
let linkAttributes = AttributeContainer {
$0.underlineStyle = .single
$0.link = URL(string: "https://www.fabernovel.com")
$0.font = UIFont.boldSystemFont(ofSize: 14)
}

let attributedLink = AttributedString(linkArgument, attributes: linkAttributes)

let subPartArgument = subPartFormat.attributedString(
arguments: [attributedLink],
defaultAttributes: boldAttributes
)

testAllAttributedStringImplementations(
format: baseFormat,
arguments: [subPartArgument],
defaultAttributes: defaultAttributes,
imageName: "combined attributed strings"
)
}
}

@available(iOS 15.0, *)
private static func testAllAttributedStringImplementations(format: String,
arguments: [String],
defaultAttributes: AttributeContainer,
differentFormatAttributes: [AttributeContainer],
imageName: String,
file: StaticString = #file,
testName: String = #function,
line: UInt = #line) {
assert(arguments.count == differentFormatAttributes.count)
let attributedArguments = zip(arguments, differentFormatAttributes).map {
AttributedString($0.0, attributes: $0.1)
}
return testAllAttributedStringImplementations(
format: format,
arguments: attributedArguments,
defaultAttributes: defaultAttributes,
imageName: imageName,
file: file,
testName: testName,
line: line
)
}

@available(iOS 15.0, *)
private static func testAllAttributedStringImplementations(format: String,
arguments: [AttributedString],
defaultAttributes: AttributeContainer,
imageName: String,
file: StaticString = #file,
testName: String = #function,
line: UInt = #line) {
let attributedString = format.attributedString(
arguments: arguments,
defaultAttributes: defaultAttributes
)
assertAttributedStringSnapshot(attributedString, imageName, file: file, testName: testName, line: line)
if #available(iOS 16.0, *) {
let attributedStringUsingRegex = format.attributedStringUsingRegex(
arguments: arguments,
defaultAttributes: defaultAttributes
)
assertAttributedStringSnapshot(attributedStringUsingRegex, imageName, file: file, testName: testName, line: line)
}
let attributedStringUsingNSRegularExpression = format.attributedStringUsingNSRegularExpression(
arguments: arguments,
defaultAttributes: defaultAttributes
)
assertAttributedStringSnapshot(
attributedStringUsingNSRegularExpression,
imageName,
file: file,
testName: testName,
line: line
)
}

@available(iOS 15.0, *)
private static func assertAttributedStringSnapshot(_ attributedString: AttributedString,
_ imageName: String,
file: StaticString = #file,
testName: String = #function,
line: UInt = #line) {
let label = Text(attributedString).fixedSize()
assertSnapshot(matching: label, as: .image, named: imageName, file: file, testName: testName, line: line)
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ All notable changes to this project will be documented in this file.

## [Unreleased]

## [12.1.0] - 2024-01-15

### Added
- Add `String.attributedString(arguments:defaultAttributes:formatAttributes:)` and `String.attributedString(arguments:defaultAttributes:differentFormatAttributes:)` utility methods to create `AttributedString` from a list of strings and attributes.
- Add `String.attributedString(arguments:defaultAttributes:)` utility method to create `AttributedString` from a `String` format and a list of `AttributedString`.
- Add helper init for `AttributeContainer` to create one with a configuration closure.

## [12.0.1] - 2023-11-21

### Fixed
Expand Down
19 changes: 19 additions & 0 deletions Modules/ADUtils/AttributeContainer+Utils.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//
// AttributeContainer+Utils.swift
// ADUtils
//
// Created by Alexandre Podlewski on 14/11/2023.
//

import Foundation

@available(iOS 15, tvOS 15, *)
extension AttributeContainer {
/**
Create an `AttributeContainer` using a configuration closure
*/
public init(_ configurationBlock: (inout Self) -> Void) {
self.init()
configurationBlock(&self)
}
}
Loading

0 comments on commit 67dd95a

Please sign in to comment.