Skip to content

Commit c2cb7b7

Browse files
authored
Merge pull request #1 from vapor/test-cov
Test cov
2 parents ef28394 + 2cc473c commit c2cb7b7

39 files changed

+1010
-538
lines changed

.swift-version

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
DEVELOPMENT-SNAPSHOT-2016-08-18-a

.travis.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
os:
2+
- linux
3+
- osx
4+
language: generic
5+
sudo: required
6+
dist: trusty
7+
osx_image: xcode8
8+
script:
9+
- eval "$(curl -sL swift.vapor.sh/ci)"
10+
- eval "$(curl -sL swift.vapor.sh/codecov)"

Sources/Leaf/Argument.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,14 @@ public enum Argument {
1313
*/
1414
case constant(value: String)
1515
}
16+
17+
extension Argument {
18+
var value: Node? {
19+
switch self {
20+
case let .constant(value: value):
21+
return .string(value)
22+
case let .variable(path: _, value: value):
23+
return value
24+
}
25+
}
26+
}

Sources/Leaf/Buffer/Buffer+Leaf.swift

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ extension BufferProtocol where Element == Byte {
33
var comps: [Leaf.Component] = []
44
while let next = try nextComponent() {
55
if case let .tagTemplate(i) = next, i.isChain {
6-
guard comps.count > 0 else { throw "invalid chain component w/o preceeding tagTemplate" }
6+
guard comps.count > 0 else {
7+
throw ParseError.expectedLeadingTemplate(have: nil)
8+
}
79
while let last = comps.last {
810
var loop = true
911

@@ -30,16 +32,12 @@ extension BufferProtocol where Element == Byte {
3032

3133
mutating func nextComponent() throws -> Leaf.Component? {
3234
guard let token = current else { return nil }
33-
if token == TOKEN {
34-
let tagTemplate = try extractInstruction()
35-
return .tagTemplate(tagTemplate)
36-
} else {
37-
let raw = extractUntil { $0 == TOKEN }
38-
return .raw(raw)
39-
}
35+
guard token == TOKEN else { return .raw(extractUntil { $0 == TOKEN }) }
36+
let tagTemplate = try extractInstruction()
37+
return .tagTemplate(tagTemplate)
4038
}
4139

42-
mutating func extractUntil(_ until: @noescape (Element) -> Bool) -> [Element] {
40+
mutating func extractUntil(_ until: (Element) -> Bool) -> [Element] {
4341
var collection = Bytes()
4442
if let current = current {
4543
guard !until(current) else { return [] }
@@ -53,6 +51,14 @@ extension BufferProtocol where Element == Byte {
5351
}
5452
}
5553

54+
enum ParseError: LeafError {
55+
case tagTemplateNotFound(name: String)
56+
case missingBodyOpener(expected: String, have: String?)
57+
case missingBodyCloser(expected: String)
58+
case expectedOpenParenthesis
59+
case expectedLeadingTemplate(have: Leaf.Component?)
60+
}
61+
5662
/*
5763
Syntax
5864

@@ -77,11 +83,11 @@ extension BufferProtocol where Element == Byte {
7783
}
7884

7985
mutating func extractInstructionName() throws -> String {
80-
// can't extract section because of @@
81-
guard current == TOKEN else { throw "tagTemplate name must lead with token" }
8286
moveForward() // drop initial token from name. a secondary token implies chain
8387
let name = extractUntil { $0 == .openParenthesis }
84-
guard current == .openParenthesis else { throw "tagTemplate names must be alphanumeric and terminated with '('" }
88+
guard current == .openParenthesis else {
89+
throw ParseError.expectedOpenParenthesis
90+
}
8591
return name.string
8692
}
8793

@@ -98,7 +104,8 @@ extension BufferProtocol where Element == Byte {
98104

99105
mutating func extractSection(opensWith opener: Byte, closesWith closer: Byte) throws -> Bytes {
100106
guard current == opener else {
101-
throw "invalid body, missing opener: \([opener].string)"
107+
let have = current.flatMap { [$0] }?.string
108+
throw ParseError.missingBodyOpener(expected: [opener].string, have: have)
102109
}
103110

104111
var subBodies = 0
@@ -112,7 +119,7 @@ extension BufferProtocol where Element == Byte {
112119
}
113120

114121
guard current == closer else {
115-
throw "invalid body, missing closer: \([closer].string), got \([current])"
122+
throw ParseError.missingBodyCloser(expected: [closer].string)
116123
}
117124

118125
return body

Sources/Leaf/Buffer/Buffer.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ struct Buffer<T>: BufferProtocol {
77

88
private var buffer: IndexingIterator<[T]>
99

10-
init<S: Sequence where S.Iterator.Element == T>(_ sequence: S) {
10+
init<S: Sequence>(_ sequence: S) where S.Iterator.Element == T {
1111
buffer = sequence.array.makeIterator()
1212
// queue up first
1313
moveForward() // sets next

Sources/Leaf/Context.swift

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,12 @@ public final class Context {
3636

3737
// TODO: Subscripts
3838

39-
public func get(key: String) -> Node? {
40-
return queue.lazy.flatMap { $0[key] } .first
41-
}
42-
43-
public func get(path: String) -> Node? {
39+
subscript(path: String) -> Node? {
4440
let components = path.components(separatedBy: ".")
45-
return get(path: components)
41+
return self[components]
4642
}
4743

48-
public func get(path: [String]) -> Node? {
44+
subscript(path: [String]) -> Node? {
4945
for node in queue {
5046
guard let value = node[path] else { continue }
5147
return value
@@ -62,10 +58,3 @@ public final class Context {
6258
return queue.removeTip()
6359
}
6460
}
65-
66-
67-
extension Context {
68-
internal func renderedSelf() throws -> Bytes? {
69-
return try get(path: "self")?.rendered()
70-
}
71-
}

Sources/Leaf/Leaf.swift

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,16 @@ public final class Leaf {
2929

3030
extension Leaf: CustomStringConvertible {
3131
public var description: String {
32-
let components = self.components.map { $0.description } .joined(separator: ", ")
33-
return "Leaf: " + components
32+
let components = self.components.map { $0.description } .joined(separator: "\n")
33+
return "Leaf: \n" + components
3434
}
3535
}
3636

3737
extension Leaf: Equatable {}
3838
public func == (lhs: Leaf, rhs: Leaf) -> Bool {
39-
return lhs === rhs
39+
for (l, r) in zip(lhs.components, rhs.components) where l != r {
40+
return false
41+
}
42+
43+
return lhs.raw == rhs.raw
4044
}

Sources/Leaf/LeafComponent.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ extension Leaf.Component {
1010
internal mutating func addToChain(_ chainedInstruction: TagTemplate) throws {
1111
switch self {
1212
case .raw(_):
13-
throw "unable to chain \(chainedInstruction) w/o preceding tagTemplate"
13+
throw ParseError.expectedLeadingTemplate(have: self)
1414
case let .tagTemplate(current):
1515
self = .chain([current, chainedInstruction])
1616
case let .chain(chain):
@@ -39,6 +39,8 @@ public func == (lhs: Leaf.Component, rhs: Leaf.Component) -> Bool {
3939
return l == r
4040
case let (.tagTemplate(l), .tagTemplate(r)):
4141
return l == r
42+
case let (.chain(l), .chain(r)):
43+
return l == r
4244
default:
4345
return false
4446
}

Sources/Leaf/LeafError.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
// TODO: Real Errors
22
extension String: Swift.Error {}
3+
protocol LeafError: Swift.Error {}
4+

Sources/Leaf/Link.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ public final class Link<Value> { // TODO: Rename Context
5656
/**
5757
Sever the child of this Link. This will also lose all children of that link.
5858
*/
59+
@discardableResult
5960
public func dropChild() -> Link? {
6061
let child = self.child
6162
self.child = nil
@@ -75,8 +76,8 @@ public final class Link<Value> { // TODO: Rename Context
7576
}
7677

7778
/**
78-
Adds a new, or replaces the existing child associated with this link
79-
*/
79+
Adds a new, or replaces the existing child associated with this link
80+
*/
8081
public func addChild(_ link: Link?) {
8182
if let existingChild = child { existingChild.parent = nil }
8283
self.child = link

0 commit comments

Comments
 (0)