Skip to content

Commit

Permalink
Added ExpressibleBy*Literal
Browse files Browse the repository at this point in the history
  • Loading branch information
Zach Eriksen committed Sep 29, 2020
1 parent 8e07c7b commit 9e2fd46
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
44 changes: 44 additions & 0 deletions Sources/E.num/Variable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,47 @@ public enum Variable: Equatable, Hashable {
case array([Variable])
case dictionary([Variable: Variable])
}

extension Variable: ExpressibleByBooleanLiteral {
public init(booleanLiteral value: Bool) {
self = .bool(value)
}
}

extension Variable: ExpressibleByIntegerLiteral {
public init(integerLiteral value: Int) {
self = .int(value)
}
}

extension Variable: ExpressibleByFloatLiteral {
public init(floatLiteral value: Float) {
self = .float(value)
}
}

extension Variable: ExpressibleByStringLiteral {
public init(stringLiteral value: String) {
self = .string(value)
}
}

extension Variable: ExpressibleByArrayLiteral {
public init(arrayLiteral: Variable...) {
self = .array(arrayLiteral)
}
}

extension Variable: ExpressibleByDictionaryLiteral {
public init(dictionaryLiteral elements: (Variable, Variable)...) {
let dictionary = Variable.dictionary([:])

if case .dictionary(var dictionary) = dictionary {
elements.forEach { (key, value) in
dictionary[key] = value
}
}

self = dictionary
}
}
5 changes: 2 additions & 3 deletions Tests/E.numTests/E_numTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@ import XCTest
@testable import E_num

internal struct E_num {
var text: Variable = .string("Hello, World!")
var text: Variable = "Hello, World!"
}


final class E_numTests: XCTestCase {
func testExample() {
// This is an example of a functional test case.
// Use XCTAssert and related functions to verify your tests produce the correct
// results.
XCTAssertEqual(E_num().text, .string("Hello, World!"))
XCTAssertEqual(E_num().text, "Hello, World!")
}

static var allTests = [
Expand Down

0 comments on commit 9e2fd46

Please sign in to comment.