@@ -29,6 +29,60 @@ final class SystemFontFamilyTests: XCTestCase {
2929 let ( sut, _, _) = makeSUT ( )
3030 XCTAssertTrue ( sut. familyName. isEmpty)
3131 }
32+
33+ func test_accessibilityWeight_deliversCorrectWeight( ) {
34+ let ( sut, _, _) = makeSUT ( )
35+ let lightWeights : [ Typography . FontWeight ] = [ . ultralight, . thin, . light]
36+ let midWeights : [ Typography . FontWeight ] = [ . regular, . medium, . semibold]
37+ let heavyWeights : [ Typography . FontWeight ] = [ . bold, . heavy, . black]
38+
39+ for weight in lightWeights {
40+ XCTAssertEqual ( sut. accessibilityBoldWeight ( for: weight) . rawValue, weight. rawValue + 100 )
41+ }
42+
43+ for weight in midWeights {
44+ XCTAssertEqual ( sut. accessibilityBoldWeight ( for: weight) . rawValue, weight. rawValue + 200 )
45+ }
46+
47+ for weight in heavyWeights {
48+ XCTAssertEqual ( sut. accessibilityBoldWeight ( for: weight) , . black)
49+ }
50+ }
51+
52+ func test_layout_deliversCorrectFont( ) throws {
53+ let ( sut, _, traitCollections) = makeSUT ( )
54+
55+ for weight in Typography . systemFamily. supportedWeights {
56+ for traitCollection in traitCollections {
57+ let typography = Typography ( fontFamily: sut, fontWeight: weight, fontSize: 16 , lineHeight: 24 )
58+ let layoutDynamic = typography. generateLayout ( compatibleWith: traitCollection)
59+ let layoutFixed = typography. fixed. generateLayout ( compatibleWith: traitCollection)
60+ XCTAssertEqual ( layoutDynamic. font. fontName, layoutFixed. font. fontName)
61+ }
62+ }
63+ }
64+
65+ func test_layoutWithLegibilityWeightBold_deliversHeavierFont( ) throws {
66+ // Given
67+ let ( sut, _, _) = makeSUT ( )
68+ let traitRegular = UITraitCollection ( legibilityWeight: . regular)
69+ let traitBold = UITraitCollection ( legibilityWeight: . bold)
70+ var weights = Typography . systemFamily. supportedWeights
71+ weights. removeLast ( ) // don't consider .black because we cannot go heavier
72+
73+ for weight in weights {
74+ // When
75+ let typography = Typography ( fontFamily: sut, fontWeight: weight, fontSize: 16 , lineHeight: 24 )
76+ let layoutRegular = typography. generateLayout ( compatibleWith: traitRegular)
77+ let layoutBold = typography. generateLayout ( compatibleWith: traitBold)
78+
79+ let weightRegular = try XCTUnwrap ( self . weight ( from: layoutRegular. font) )
80+ let weightBold = try XCTUnwrap ( self . weight ( from: layoutBold. font) )
81+
82+ // Then
83+ XCTAssertGreaterThan ( weightBold. rawValue, weightRegular. rawValue)
84+ }
85+ }
3286}
3387
3488// We use large tuples in makeSUT()
@@ -46,4 +100,33 @@ private extension SystemFontFamilyTests {
46100 ]
47101 return ( sut, pointSizes, traitCollections)
48102 }
103+
104+ func weight( from font: UIFont ) -> Typography . FontWeight ? {
105+ guard let weightString = font. fontName. components ( separatedBy: " - " ) . last else {
106+ return nil
107+ }
108+
109+ switch weightString. lowercased ( with: Locale ( identifier: " en_US " ) ) {
110+ case " ultralight " , " extralight " :
111+ return . ultralight
112+ case " thin " :
113+ return . thin
114+ case " light " :
115+ return . light
116+ case " regular " :
117+ return . regular
118+ case " medium " :
119+ return . medium
120+ case " semibold " , " demibold " :
121+ return . semibold
122+ case " bold " :
123+ return . bold
124+ case " heavy " , " extrabold " :
125+ return . heavy
126+ case " black " :
127+ return . black
128+ default :
129+ return nil
130+ }
131+ }
49132}
0 commit comments