Skip to content

Commit 5aeec12

Browse files
committed
Adding command line parser and tests
1 parent eb21b83 commit 5aeec12

File tree

4 files changed

+204
-0
lines changed

4 files changed

+204
-0
lines changed

SwiftDraw.xcodeproj/project.pbxproj

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,9 @@
178178
D9A843941E85F1C1002A804B /* quad.svg in Resources */ = {isa = PBXBuildFile; fileRef = D9A843921E85F1C1002A804B /* quad.svg */; };
179179
D9A843951E8611D6002A804B /* curves.svg in Resources */ = {isa = PBXBuildFile; fileRef = D9A843901E855672002A804B /* curves.svg */; };
180180
D9A843961E8611D7002A804B /* curves.svg in Resources */ = {isa = PBXBuildFile; fileRef = D9A843901E855672002A804B /* curves.svg */; };
181+
D9BA810021B9C5E5005159E7 /* CommandLine.swift in Sources */ = {isa = PBXBuildFile; fileRef = D9BA80FF21B9C5E5005159E7 /* CommandLine.swift */; };
182+
D9BA810221B9C624005159E7 /* CommandLine.Arguments.swift in Sources */ = {isa = PBXBuildFile; fileRef = D9BA810121B9C624005159E7 /* CommandLine.Arguments.swift */; };
183+
D9BA810521B9C665005159E7 /* CommandLine.ArgumentsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D9BA810421B9C665005159E7 /* CommandLine.ArgumentsTests.swift */; };
181184
D9C47AC21ED54FA100178D06 /* mask.svg in Resources */ = {isa = PBXBuildFile; fileRef = D9C47ABF1ED54FA100178D06 /* mask.svg */; };
182185
D9C47AC31ED54FA100178D06 /* mask.svg in Resources */ = {isa = PBXBuildFile; fileRef = D9C47ABF1ED54FA100178D06 /* mask.svg */; };
183186
D9C47AC41ED54FA100178D06 /* transform.svg in Resources */ = {isa = PBXBuildFile; fileRef = D9C47AC01ED54FA100178D06 /* transform.svg */; };
@@ -322,6 +325,9 @@
322325
D965B5851E3C5C87003E859E /* starry.svg */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = starry.svg; sourceTree = "<group>"; };
323326
D9A843901E855672002A804B /* curves.svg */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xml; path = curves.svg; sourceTree = "<group>"; };
324327
D9A843921E85F1C1002A804B /* quad.svg */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xml; path = quad.svg; sourceTree = "<group>"; };
328+
D9BA80FF21B9C5E5005159E7 /* CommandLine.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CommandLine.swift; sourceTree = "<group>"; };
329+
D9BA810121B9C624005159E7 /* CommandLine.Arguments.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CommandLine.Arguments.swift; sourceTree = "<group>"; };
330+
D9BA810421B9C665005159E7 /* CommandLine.ArgumentsTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CommandLine.ArgumentsTests.swift; sourceTree = "<group>"; };
325331
D9C47ABF1ED54FA100178D06 /* mask.svg */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xml; path = mask.svg; sourceTree = "<group>"; };
326332
D9C47AC01ED54FA100178D06 /* transform.svg */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xml; path = transform.svg; sourceTree = "<group>"; };
327333
D9C47AC11ED54FA100178D06 /* viewbox.svg */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xml; path = viewbox.svg; sourceTree = "<group>"; };
@@ -615,6 +621,23 @@
615621
path = Samples;
616622
sourceTree = "<group>";
617623
};
624+
D9BA80FE21B9C5C1005159E7 /* CommandLine */ = {
625+
isa = PBXGroup;
626+
children = (
627+
D9BA80FF21B9C5E5005159E7 /* CommandLine.swift */,
628+
D9BA810121B9C624005159E7 /* CommandLine.Arguments.swift */,
629+
);
630+
name = CommandLine;
631+
sourceTree = "<group>";
632+
};
633+
D9BA810321B9C645005159E7 /* CommandLine */ = {
634+
isa = PBXGroup;
635+
children = (
636+
D9BA810421B9C665005159E7 /* CommandLine.ArgumentsTests.swift */,
637+
);
638+
name = CommandLine;
639+
sourceTree = "<group>";
640+
};
618641
D9CFB5CE21A73E0F000D2875 /* CommandLine */ = {
619642
isa = PBXGroup;
620643
children = (
@@ -671,6 +694,7 @@
671694
D9F3EBB41DFF5BA3006691A1 /* SwiftDraw.h */,
672695
D962D2A11E6ECE2000B825C6 /* SwiftDraw-macOS.h */,
673696
D9F3EBB51DFF5BA3006691A1 /* Info.plist */,
697+
D9BA80FE21B9C5C1005159E7 /* CommandLine */,
674698
D90DB3EA219CCECF00D374D2 /* Extensions */,
675699
D90DB3DD219CCDFB00D374D2 /* Image */,
676700
D90DB3A3219CCC2100D374D2 /* LayerTree */,
@@ -684,6 +708,7 @@
684708
D9F3EBBE1DFF5BA3006691A1 /* SwiftDrawTests */ = {
685709
isa = PBXGroup;
686710
children = (
711+
D9BA810321B9C645005159E7 /* CommandLine */,
687712
D90DB3F2219CCF1A00D374D2 /* Extensions */,
688713
D9F3AD5C21A2172800BAFA55 /* Image */,
689714
D90DB3C5219CCC7200D374D2 /* LayerTree */,
@@ -950,8 +975,10 @@
950975
D90DB3B5219CCC4100D374D2 /* LayerTree.swift in Sources */,
951976
D90DB455219CD27800D374D2 /* Parser.XML.Gradient.swift in Sources */,
952977
D90DB42C219CD1C600D374D2 /* DOM.Switch.swift in Sources */,
978+
D9BA810221B9C624005159E7 /* CommandLine.Arguments.swift in Sources */,
953979
D90DB451219CD27800D374D2 /* Parser.XML.Color.swift in Sources */,
954980
D90DB3B9219CCC4100D374D2 /* LayerTree.Layer.swift in Sources */,
981+
D9BA810021B9C5E5005159E7 /* CommandLine.swift in Sources */,
955982
D90DB461219CD27800D374D2 /* Parser.XML.Image.swift in Sources */,
956983
D90DB459219CD27800D374D2 /* Parser.XML.Text.swift in Sources */,
957984
D90DB3E6219CCE0E00D374D2 /* Image.swift in Sources */,
@@ -1002,6 +1029,7 @@
10021029
D90DB486219CD2C300D374D2 /* StyleTests.swift in Sources */,
10031030
D90DB3D4219CCC8A00D374D2 /* LayerTree.LayerTests.swift in Sources */,
10041031
D90DB476219CD2C300D374D2 /* CoordinateTests.swift in Sources */,
1032+
D9BA810521B9C665005159E7 /* CommandLine.ArgumentsTests.swift in Sources */,
10051033
D90DB48E219CD2C300D374D2 /* Parser.GraphicAttributeTests.swift in Sources */,
10061034
D9F3AD5F21A218CF00BAFA55 /* ImageTests.swift in Sources */,
10071035
D90DB480219CD2C300D374D2 /* Parser.XML.ColorTests.swift in Sources */,
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
//
2+
// CommandLine.Arguments.swift
3+
// SwiftDraw
4+
//
5+
// Created by Simon Whitty on 7/12/18.
6+
// Copyright 2018 Simon Whitty
7+
//
8+
// Distributed under the permissive zlib license
9+
// Get the latest version from here:
10+
//
11+
// https://github.com/swhitty/SwiftDraw
12+
//
13+
// This software is provided 'as-is', without any express or implied
14+
// warranty. In no event will the authors be held liable for any damages
15+
// arising from the use of this software.
16+
//
17+
// Permission is granted to anyone to use this software for any purpose,
18+
// including commercial applications, and to alter it and redistribute it
19+
// freely, subject to the following restrictions:
20+
//
21+
// 1. The origin of this software must not be misrepresented; you must not
22+
// claim that you wrote the original software. If you use this software
23+
// in a product, an acknowledgment in the product documentation would be
24+
// appreciated but is not required.
25+
//
26+
// 2. Altered source versions must be plainly marked as such, and must not be
27+
// misrepresented as being the original software.
28+
//
29+
// 3. This notice may not be removed or altered from any source distribution.
30+
//
31+
32+
import Foundation
33+
34+
extension CommandLine {
35+
36+
enum Modifier: String {
37+
case format
38+
case output
39+
40+
static func parse(from string: String) -> Modifier? {
41+
guard string.hasPrefix("--") else {
42+
return nil
43+
}
44+
45+
return Modifier(rawValue: String(string.dropFirst(2)))
46+
}
47+
}
48+
49+
static func parseModifiers(from args: [String]) throws -> [Modifier: String] {
50+
var args = args
51+
var modifiers = [Modifier: String]()
52+
while let pair = args.takePair() {
53+
if let modifier = Modifier.parse(from: pair.0) {
54+
modifiers[modifier] = pair.1
55+
} else {
56+
throw Error.invalid
57+
}
58+
}
59+
60+
guard args.isEmpty else {
61+
throw CommandLine.Error.invalid
62+
}
63+
64+
return modifiers
65+
}
66+
67+
enum Error: Swift.Error {
68+
case invalid
69+
}
70+
}
71+
72+
private extension Array where Element == String {
73+
74+
mutating func takePair() -> (String, String)? {
75+
guard count > 1 else {
76+
return nil
77+
}
78+
79+
let pair = (self[0], self[1])
80+
removeFirst(2)
81+
return pair
82+
}
83+
}

SwiftDraw/CommandLine.swift

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//
2+
// CommandLine.swift
3+
// SwiftDraw
4+
//
5+
// Created by Simon Whitty on 7/12/18.
6+
// Copyright 2018 Simon Whitty
7+
//
8+
// Distributed under the permissive zlib license
9+
// Get the latest version from here:
10+
//
11+
// https://github.com/swhitty/SwiftDraw
12+
//
13+
// This software is provided 'as-is', without any express or implied
14+
// warranty. In no event will the authors be held liable for any damages
15+
// arising from the use of this software.
16+
//
17+
// Permission is granted to anyone to use this software for any purpose,
18+
// including commercial applications, and to alter it and redistribute it
19+
// freely, subject to the following restrictions:
20+
//
21+
// 1. The origin of this software must not be misrepresented; you must not
22+
// claim that you wrote the original software. If you use this software
23+
// in a product, an acknowledgment in the product documentation would be
24+
// appreciated but is not required.
25+
//
26+
// 2. Altered source versions must be plainly marked as such, and must not be
27+
// misrepresented as being the original software.
28+
//
29+
// 3. This notice may not be removed or altered from any source distribution.
30+
//
31+
32+
import Foundation
33+
34+
struct CommandLine {
35+
36+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
//
2+
// CommandLine.ArgumentsTests.swift
3+
// SwiftDraw
4+
//
5+
// Created by Simon Whitty on 7/12/18.
6+
// Copyright 2018 Simon Whitty
7+
//
8+
// Distributed under the permissive zlib license
9+
// Get the latest version from here:
10+
//
11+
// https://github.com/swhitty/SwiftDraw
12+
//
13+
// This software is provided 'as-is', without any express or implied
14+
// warranty. In no event will the authors be held liable for any damages
15+
// arising from the use of this software.
16+
//
17+
// Permission is granted to anyone to use this software for any purpose,
18+
// including commercial applications, and to alter it and redistribute it
19+
// freely, subject to the following restrictions:
20+
//
21+
// 1. The origin of this software must not be misrepresented; you must not
22+
// claim that you wrote the original software. If you use this software
23+
// in a product, an acknowledgment in the product documentation would be
24+
// appreciated but is not required.
25+
//
26+
// 2. Altered source versions must be plainly marked as such, and must not be
27+
// misrepresented as being the original software.
28+
//
29+
// 3. This notice may not be removed or altered from any source distribution.
30+
//
31+
32+
import XCTest
33+
@testable import SwiftDraw
34+
35+
final class CommandLineArgumentsTests: XCTestCase {
36+
37+
func testParseModifiers() throws {
38+
let modifiers = try CommandLine.parseModifiers(from: ["--format", "some", "--output", "more"])
39+
XCTAssertEqual(modifiers, [.format: "some", .output: "more"])
40+
}
41+
42+
func testParseModifiersThrowsForOddPairs() {
43+
XCTAssertThrowsError(try CommandLine.parseModifiers(from: ["--format"]))
44+
XCTAssertThrowsError(try CommandLine.parseModifiers(from: ["--format", "png", "--output"]))
45+
}
46+
47+
func testParseModifiersThrowsForUnknownModifiers() {
48+
XCTAssertThrowsError(try CommandLine.parseModifiers(from: ["--unknown", "png"]))
49+
XCTAssertThrowsError(try CommandLine.parseModifiers(from: ["--format", "png", "--unknown", "more"]))
50+
}
51+
52+
func testParseModifiersThrowsForMissingPrefix() {
53+
XCTAssertThrowsError(try CommandLine.parseModifiers(from: ["format", "png"]))
54+
XCTAssertThrowsError(try CommandLine.parseModifiers(from: ["--format", "png", "output", "more"]))
55+
}
56+
57+
}

0 commit comments

Comments
 (0)