Skip to content
This repository has been archived by the owner on Jan 30, 2024. It is now read-only.

Commit

Permalink
Add tests for Spacing feature
Browse files Browse the repository at this point in the history
  • Loading branch information
freak4pc committed Apr 10, 2020
1 parent 8251335 commit c1544ca
Show file tree
Hide file tree
Showing 13 changed files with 206 additions and 34 deletions.
23 changes: 15 additions & 8 deletions Sources/PrismCore/TemplateParser/TemplateParser+Token.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ extension TemplateParser {
case colorRed(Int)
case colorGreen(Int)
case colorBlue(Int)
case colorAlpha(Double)
case colorAlpha(Float)
case colorARGB(String)
case colorRGB(String)
case colorIdentity(identity: Project.AssetIdentity,
Expand Down Expand Up @@ -166,8 +166,8 @@ extension TemplateParser {
let .textStyleIdentity(identity, style),
let .spacingIdentity(identity, style):
baseString = style.identifier(for: identity)
case .colorAlpha(let a):
baseString = String(format: "%.2f", a)
case .colorAlpha(let alpha):
baseString = String(format: "%.2f", alpha)
case .colorRed(let c),
.colorGreen(let c),
.colorBlue(let c):
Expand All @@ -183,14 +183,14 @@ extension TemplateParser {
baseString = alignment
case .textStyleLetterSpacing(let spacing):
if let spacing = spacing {
baseString = "\(spacing.roundToNearest())"
baseString = spacing.roundedToNearest()
}
case .textStyleLineHeight(let height):
if let height = height {
baseString = "\(height.roundToNearest())"
baseString = height.roundedToNearest()
}
case .spacingValue(let value):
baseString = "\(value.roundToNearest())"
baseString = value.roundedToNearest()
}

guard let output = baseString else { return nil }
Expand All @@ -202,7 +202,14 @@ extension TemplateParser {
// MARK: - Private Helpers
private extension Float {
/// Round the Float to the nearest 2 floating points
func roundToNearest() -> Float {
(self * 100).rounded(.toNearestOrEven) / 100
func roundedToNearest() -> String {
let value = (self * 100).rounded(.toNearestOrEven) / 100

let int = Int(value)
if Float(int) == value {
return "\(int)"
} else {
return "\(value)"
}
}
}
6 changes: 3 additions & 3 deletions Sources/ZeplinAPI/Models/Color.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public protocol RawColorRepresentable: Codable, Equatable {
var r: Int { get }
var g: Int { get }
var b: Int { get }
var a: Double { get }
var a: Float { get }
}

public extension RawColorRepresentable {
Expand All @@ -38,13 +38,13 @@ public struct Color: RawColorRepresentable {
public let r: Int
public let g: Int
public let b: Int
public let a: Double
public let a: Float
}

/// A raw color with red, green, blue and alpha values.
public struct RawColor: RawColorRepresentable {
public let r: Int
public let g: Int
public let b: Int
public let a: Double
public let a: Float
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[
{
"id":"5e1f8d32e75119b93cc515ee",
"name":"spacing-xxl",
"value":40.2,
"color":{
"r":153,
"b":204,
"g":102,
"a":0.2
},
"section":{
"id":"5e849158f07b4c2451164e5e"
}
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
[
{
"id":"5e8f8tt2e75y19b93cc515ee",
"name":"spacing-m",
"value":16,
"color":{
"r":113,
"b":24,
"g":12,
"a":0.7
},
"section":{
"id":"5e849158f07b4c2451164e5e"
}
},
{
"id":"5e8f8d32e75119b961s515ee",
"name":"spacing-s",
"value":12,
"color":{
"r":113,
"b":24,
"g":12,
"a":0.7
},
"section":{
"id":"5e849158f07b4c2451164e5e"
}
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
[
{
"id":"5e62d3ae75119b93cc515ee",
"name":"spacing-xl",
"value":32,
"color":{
"r":113,
"b":24,
"g":12,
"a":0.7
},
"section":{
"id":"5e849158f07b4c2451164e5e"
}
},
{
"id":"5e8f8d32xa1119b93cc515ee",
"name":"spacing-l",
"value":24,
"color":{
"r":113,
"b":24,
"g":12,
"a":0.7
},
"section":{
"id":"5e849158f07b4c2451164e5e"
}
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
[
{
"id":"5e62d3ae75119b91cc512ee",
"name":"spacing-xs",
"value":8,
"color":{
"r":113,
"b":24,
"g":12,
"a":0.7
},
"section":{
"id":"5e849158f07b4c2451164e5e"
}
},
{
"id":"5e8f4d32xa1119b937c515ee",
"name":"spacing-xxs",
"value":4,
"color":{
"r":113,
"b":24,
"g":12,
"a":0.7
},
"section":{
"id":"5e849158f07b4c2451164e5e"
}
}
]
7 changes: 4 additions & 3 deletions Tests/Mocks/PrismMock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,10 @@ extension Prism {
guard type == .successful else {
return try? MockResponse(for: request, data: ",|[".data(using: .utf8))
}

guard let data = try? Data(contentsOf: mocksURL.appendingPathComponent("\(path).json")) else {
fatalError("Can't find mock for \(url)")

let mockPath = mocksURL.appendingPathComponent("\(path).json")
guard let data = try? Data(contentsOf: mockPath) else {
fatalError("Can't find mock for \(url) at \(mockPath)")
}

return try? MockResponse(for: request, data: data)
Expand Down
60 changes: 50 additions & 10 deletions Tests/TemplateParserSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ class TemplateParserSpec: QuickSpec {
let parser = TemplateParser(project: project)

let template = """
/// This file was generated using Prism, Gett's Design System code generator.
/// https://github.com/GettEngineering/Prism
/// This file was generated using Prism
fake line 1
fake line 2
Expand All @@ -48,8 +47,7 @@ class TemplateParserSpec: QuickSpec {
let parser = TemplateParser(project: project)

let template = """
/// This file was generated using Prism, Gett's Design System code generator.
/// https://github.com/GettEngineering/Prism
/// This file was generated using Prism
fake line 1
fake line 2
Expand All @@ -73,6 +71,31 @@ class TemplateParserSpec: QuickSpec {
named: "Text Styles Loop should provide valid output")
}
}

describe("Spacing Loop") {
it("should produce valid output") {
let projectResult = Prism(jwtToken: "fake").mock(type: .successful)
let project = try! projectResult.get()
let parser = TemplateParser(project: project)

let template = """
/// This file was generated using Prism
fake line 1
fake line 2
Some SpacingSructure {
{{% FOR spacing %}}
{{%spacing.identity%}}, {{% spacing.identity.camelcase %}}, {{%spacing.identity.snakecase%}} = {{% spacing.value %}}
{{% END spacing %}}
}
"""

assertSnapshot(matching: try! parser.parse(template: template),
as: .lines,
named: "Spacing Loop should provide valid output")
}
}

describe("Invalid tokens") {
context("text style") {
Expand Down Expand Up @@ -104,14 +127,30 @@ class TemplateParserSpec: QuickSpec {
}.to(throwError(TemplateParser.Error.unknownToken(token: "color.nonExistentToken")))
}
}

context("spacing") {
it("should throw an error") {
let projectResult = try! Prism(jwtToken: "fake").mock(type: .successful).get()
let parser = TemplateParser(project: projectResult)

expect {
try parser.parse(template: """
{{% FOR spacing %}}
Bad token {{%spacing.nonExistentToken%}}
{{% END spacing %}}
""")
}.to(throwError(TemplateParser.Error.unknownToken(token: "spacing.nonExistentToken")))
}
}
}

describe("Text Style without color identity") {
it("should throw error when accessed") {
let projectResult = try! Prism(jwtToken: "fake").mock(type: .successful).get()
let modifiedResult = ProjectAssets(id: projectResult.id,
colors: [],
textStyles: Array(projectResult.textStyles.prefix(1)))
textStyles: Array(projectResult.textStyles.prefix(1)),
spacing: [])

let parser = TemplateParser(project: modifiedResult)
expect {
Expand All @@ -131,8 +170,7 @@ class TemplateParserSpec: QuickSpec {
let parser = TemplateParser(project: project)

let template = """
/// This file was generated using Prism, Gett's Design System code generator.
/// https://github.com/GettEngineering/Prism
/// This file was generated using Prism
fake line 1
fake line 2
Expand All @@ -155,8 +193,7 @@ class TemplateParserSpec: QuickSpec {
let parser = TemplateParser(project: project)

let template = """
/// This file was generated using Prism, Gett's Design System code generator.
/// https://github.com/GettEngineering/Prism
/// This file was generated using Prism
fake line 1
fake line 2
Expand Down Expand Up @@ -211,6 +248,8 @@ class TemplateParserSpec: QuickSpec {
expect { try TemplateParser.Token(rawTextStyleToken: UUID().uuidString,
textStyle: project.textStyles[0],
colors: project.colors) }.to(throwError())
expect { try TemplateParser.Token(rawSpacingToken: UUID().uuidString,
spacing: project.spacing[0]) }.to(throwError())
}
}

Expand Down Expand Up @@ -240,7 +279,8 @@ class TemplateParserSpec: QuickSpec {
{{%textStyle.identity.snakecase|uppercase%}}
{{%textStyle.fontName|uppercase%}}
{{%textStyle.fontName|replace(-,_)%}}
{{%textStyle.fontName|lowercase|replace(-,_)%}}
{{%textStyle.fontName|replace("-","_")%}}
{{%textStyle.fontName|lowercase|replace(-,"_")%}}
==============================================
{{% END textStyle %}}
"""
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/// This file was generated using Prism, Gett's Design System code generator.
/// https://github.com/GettEngineering/Prism
/// This file was generated using Prism

fake line 1
fake line 2
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Base Heading 26.0
Base Heading 26
Base Subhead 22.53
Body
Body 2 24.0
Body 2 24
Highlight
Large Heading 24.0
Large Heading 24
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/// This file was generated using Prism

fake line 1
fake line 2

Some SpacingSructure {
spacing-xxs, spacingXXS, spacing_xxs = 4
spacing-xs, spacingXS, spacing_xs = 8
spacing-s, spacingS, spacing_s = 12
spacing-m, spacingM, spacing_m = 16
spacing-l, spacingL, spacing_l = 24
spacing-xl, spacingXL, spacing_xl = 32
spacing-xxl, spacingXXL, spacing_xxl = 40.2
}
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
/// This file was generated using Prism, Gett's Design System code generator.
/// https://github.com/GettEngineering/Prism
/// This file was generated using Prism

fake line 1
fake line 2

Some Structure {
line height is 26.0, Base Heading, baseHeading, base_heading = LucidaGrande-Bold, 26.0, green, green, green, letter spacing is: 0.4, #62a60e, #ff62a60e, 98, 166, 14, 1.00
line height is 26, Base Heading, baseHeading, base_heading = LucidaGrande-Bold, 26.0, green, green, green, letter spacing is: 0.4, #62a60e, #ff62a60e, 98, 166, 14, 1.00
We can also access optional stuff without an IF, which will result in an empty string like so:
line height is 22.53, Base Subhead, baseSubhead, base_subhead = LucidaGrande-Bold, 22.0, red, red, red, letter spacing is: 0.49, #a60e0e, #ffa60e0e, 166, 14, 14, 1.00
We can also access optional stuff without an IF, which will result in an empty string like so:
Body, body, body = LucidaGrande, 14.0, blue, blue, blue, letter spacing is: 0.5, #0e28a6, #ff0e28a6, 14, 40, 166, 1.00
We can also access optional stuff without an IF, which will result in an empty string like so:
line height is 24.0, Body 2, body2, body_2 = MyCustomFont-Regular, 14.0, Blue Sky, blueSky, blue_sky, letter spacing is: 1.5, #62b6df, #ff62b6df, 98, 182, 223, 1.00, alignment is right
line height is 24, Body 2, body2, body_2 = MyCustomFont-Regular, 14.0, Blue Sky, blueSky, blue_sky, letter spacing is: 1.5, #62b6df, #ff62b6df, 98, 182, 223, 1.00, alignment is right
This is an attempt of an indented multi-line
block containing a text alignment, which is right
and also capable of inlining another condition
like getting the ARGB value #ff62b6df, right?
We can also access optional stuff without an IF, which will result in an empty string like so: right
Highlight, highlight, highlight = AppleGothic, 18.0, purple, purple, purple, #a30ea6, #ffa30ea6, 163, 14, 166, 1.00
We can also access optional stuff without an IF, which will result in an empty string like so:
line height is 24.0, Large Heading, largeHeading, large_heading = MyCustomFont-Light, 32.0, Clear Reddish, clearReddish, clear_reddish, #df6369, #ccdf6369, 223, 99, 105, 0.80, alignment is left
line height is 24, Large Heading, largeHeading, large_heading = MyCustomFont-Light, 32.0, Clear Reddish, clearReddish, clear_reddish, #df6369, #ccdf6369, 223, 99, 105, 0.80, alignment is left
This is an attempt of an indented multi-line
block containing a text alignment, which is left
and also capable of inlining another condition
Expand Down
Loading

0 comments on commit c1544ca

Please sign in to comment.