Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ExpressibleBy Protocols #138

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ Now you can create a document and do all sorts of Automerge things with it
```swift
let doc = Document()
let list = try! doc.putObject(obj: ObjId.ROOT, key: "colours", ty: .List)
try! doc.insert(obj: list, index: 0, value: .String("blue"))
try! doc.insert(obj: list, index: 1, value: .String("red"))
try! doc.insert(obj: list, index: 0, value: "blue")
try! doc.insert(obj: list, index: 1, value: "red")

let doc2 = doc.fork()
try! doc2.insert(obj: list, index: 0, value: .String("green"))
try! doc2.insert(obj: list, index: 0, value: "green")

try! doc.delete(obj: list, index: 0)

Expand Down
19 changes: 19 additions & 0 deletions Sources/Automerge/ScalarValue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,22 @@ extension ScalarValue: CustomStringConvertible {
}
}
}

extension ScalarValue: ExpressibleByStringLiteral, ExpressibleByBooleanLiteral, ExpressibleByIntegerLiteral, ExpressibleByNilLiteral {

public init(stringLiteral value: StringLiteralType) {
self = .String(value)
}

public init(booleanLiteral value: BooleanLiteralType) {
self = .Boolean(value)
}

public init(integerLiteral value: IntegerLiteralType) {
self = .Int(Int64(value))
}

public init(nilLiteral: ()) {
self = .Null
}
}
16 changes: 8 additions & 8 deletions Tests/AutomergeTests/CodableTests/AutomergeDecoderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ final class AutomergeDecoderTests: XCTestCase {
setupCache = [:]
doc = Document()

try! doc.put(obj: ObjId.ROOT, key: "name", value: .String("Joe"))
try! doc.put(obj: ObjId.ROOT, key: "name", value: "Joe")
try! doc.put(obj: ObjId.ROOT, key: "duration", value: .F64(3.14159))
try! doc.put(obj: ObjId.ROOT, key: "flag", value: .Boolean(true))
try! doc.put(obj: ObjId.ROOT, key: "count", value: .Int(5))
try! doc.put(obj: ObjId.ROOT, key: "uuid", value: .String("99CEBB16-1062-4F21-8837-CF18EC09DCD7"))
try! doc.put(obj: ObjId.ROOT, key: "url", value: .String("http://url.com"))
try! doc.put(obj: ObjId.ROOT, key: "flag", value: true)
try! doc.put(obj: ObjId.ROOT, key: "count", value: 5)
try! doc.put(obj: ObjId.ROOT, key: "uuid", value: "99CEBB16-1062-4F21-8837-CF18EC09DCD7")
try! doc.put(obj: ObjId.ROOT, key: "url", value: "http://url.com")
try! doc.put(obj: ObjId.ROOT, key: "date", value: .Timestamp(-905182980))
try! doc.put(obj: ObjId.ROOT, key: "data", value: .Bytes(Data("hello".utf8)))

Expand All @@ -24,9 +24,9 @@ final class AutomergeDecoderTests: XCTestCase {

let votes = try! doc.putObject(obj: ObjId.ROOT, key: "votes", ty: .List)
setupCache["votes"] = votes
try! doc.insert(obj: votes, index: 0, value: .Int(3))
try! doc.insert(obj: votes, index: 1, value: .Int(4))
try! doc.insert(obj: votes, index: 2, value: .Int(5))
try! doc.insert(obj: votes, index: 0, value: 3)
try! doc.insert(obj: votes, index: 1, value: 4)
try! doc.insert(obj: votes, index: 2, value: 5)

let list = try! doc.putObject(obj: ObjId.ROOT, key: "list", ty: .List)
setupCache["list"] = list
Expand Down
10 changes: 5 additions & 5 deletions Tests/AutomergeTests/CodableTests/AutomergeEncoderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ final class AutomergeEncoderTests: XCTestCase {
try XCTFail("Didn't find an object at \(String(describing: doc.get(obj: ObjId.ROOT, key: "notes")))")
}

XCTAssertEqual(try doc.get(obj: ObjId.ROOT, key: "url"), .Scalar(.String("http://url.com")))
XCTAssertEqual(try doc.get(obj: ObjId.ROOT, key: "url"), .Scalar("http://url.com"))
try debugPrint(doc.get(obj: ObjId.ROOT, key: "notes") as Any)
}

Expand Down Expand Up @@ -157,7 +157,7 @@ final class AutomergeEncoderTests: XCTestCase {
} else {
try XCTFail("Didn't find: \(String(describing: doc.get(obj: container_id, key: "count")))")
}
XCTAssertEqual(try doc.get(obj: container_id, key: "url"), .Scalar(.String("http://url.com")))
XCTAssertEqual(try doc.get(obj: container_id, key: "url"), .Scalar("http://url.com"))
} else {
try XCTFail("Didn't find: \(String(describing: doc.get(obj: ObjId.ROOT, key: "example")))")
}
Expand All @@ -176,9 +176,9 @@ final class AutomergeEncoderTests: XCTestCase {

if case let .Object(container_id, container_type) = try doc.get(obj: ObjId.ROOT, key: "numbers") {
XCTAssertEqual(container_type, ObjType.List)
XCTAssertEqual(try doc.get(obj: container_id, index: 0), .Scalar(.Int(1)))
XCTAssertEqual(try doc.get(obj: container_id, index: 1), .Scalar(.Int(2)))
XCTAssertEqual(try doc.get(obj: container_id, index: 2), .Scalar(.Int(3)))
XCTAssertEqual(try doc.get(obj: container_id, index: 0), .Scalar(1))
XCTAssertEqual(try doc.get(obj: container_id, index: 1), .Scalar(2))
XCTAssertEqual(try doc.get(obj: container_id, index: 2), .Scalar(3))
} else {
try XCTFail("Didn't find: \(String(describing: doc.get(obj: ObjId.ROOT, key: "example")))")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ final class AutomergeKeyEncoderImplTests: XCTestCase {

func testSimpleKeyEncode_Bool() throws {
try rootKeyedContainer.encode(true, forKey: .value)
XCTAssertEqual(try doc.get(obj: ObjId.ROOT, key: "value"), .Scalar(.Boolean(true)))
XCTAssertEqual(try doc.get(obj: ObjId.ROOT, key: "value"), .Scalar(true))

try cautiousKeyedContainer.encode(false, forKey: .value)
XCTAssertEqual(try doc.get(obj: ObjId.ROOT, key: "value"), .Scalar(.Boolean(false)))
XCTAssertEqual(try doc.get(obj: ObjId.ROOT, key: "value"), .Scalar(false))
}

func testSimpleKeyEncode_Bool_CautiousFailure() throws {
try doc.put(obj: ObjId.ROOT, key: "value", value: .Int(4))
try doc.put(obj: ObjId.ROOT, key: "value", value: 4)
XCTAssertThrowsError(
try cautiousKeyedContainer.encode(false, forKey: .value)
)
Expand Down Expand Up @@ -86,7 +86,7 @@ final class AutomergeKeyEncoderImplTests: XCTestCase {
}

func testSimpleKeyEncode_Float_CautiousFailure() throws {
try doc.put(obj: ObjId.ROOT, key: "value", value: .Int(4))
try doc.put(obj: ObjId.ROOT, key: "value", value: 4)
XCTAssertThrowsError(
try cautiousKeyedContainer.encode(Float(4.0), forKey: .value)
)
Expand All @@ -97,7 +97,7 @@ final class AutomergeKeyEncoderImplTests: XCTestCase {

func testSimpleKeyEncode_Int8() throws {
try rootKeyedContainer.encode(Int8(4), forKey: .value)
XCTAssertEqual(try doc.get(obj: ObjId.ROOT, key: "value"), .Scalar(.Int(4)))
XCTAssertEqual(try doc.get(obj: ObjId.ROOT, key: "value"), .Scalar(4))

try cautiousKeyedContainer.encode(Int8(5), forKey: .value)
}
Expand All @@ -111,7 +111,7 @@ final class AutomergeKeyEncoderImplTests: XCTestCase {

func testSimpleKeyEncode_Int16() throws {
try rootKeyedContainer.encode(Int16(4), forKey: .value)
XCTAssertEqual(try doc.get(obj: ObjId.ROOT, key: "value"), .Scalar(.Int(4)))
XCTAssertEqual(try doc.get(obj: ObjId.ROOT, key: "value"), .Scalar(4))

try cautiousKeyedContainer.encode(Int16(5), forKey: .value)
}
Expand All @@ -125,7 +125,7 @@ final class AutomergeKeyEncoderImplTests: XCTestCase {

func testSimpleKeyEncode_Int32() throws {
try rootKeyedContainer.encode(Int32(4), forKey: .value)
XCTAssertEqual(try doc.get(obj: ObjId.ROOT, key: "value"), .Scalar(.Int(4)))
XCTAssertEqual(try doc.get(obj: ObjId.ROOT, key: "value"), .Scalar(4))

try rootKeyedContainer.encode(Int32(5), forKey: .value)
}
Expand All @@ -139,7 +139,7 @@ final class AutomergeKeyEncoderImplTests: XCTestCase {

func testSimpleKeyEncode_Int64() throws {
try rootKeyedContainer.encode(Int64(4), forKey: .value)
XCTAssertEqual(try doc.get(obj: ObjId.ROOT, key: "value"), .Scalar(.Int(4)))
XCTAssertEqual(try doc.get(obj: ObjId.ROOT, key: "value"), .Scalar(4))

try cautiousKeyedContainer.encode(Int64(5), forKey: .value)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ final class AutomergeSingleValueEncoderImplTests: XCTestCase {

func testSimpleKeyEncode_Bool() throws {
try singleValueContainer.encode(true)
XCTAssertEqual(try doc.get(obj: ObjId.ROOT, key: "value"), .Scalar(.Boolean(true)))
XCTAssertEqual(try doc.get(obj: ObjId.ROOT, key: "value"), .Scalar(true))

try cautiousSingleValueContainer.encode(false)
XCTAssertEqual(try doc.get(obj: ObjId.ROOT, key: "value"), .Scalar(.Boolean(false)))
XCTAssertEqual(try doc.get(obj: ObjId.ROOT, key: "value"), .Scalar(false))
}

func testSimpleKeyEncode_Bool_CautiousFailure() throws {
Expand Down Expand Up @@ -76,7 +76,7 @@ final class AutomergeSingleValueEncoderImplTests: XCTestCase {
}

func testSimpleKeyEncode_Double_CautiousFailure() throws {
try doc.put(obj: ObjId.ROOT, key: "value", value: .Int(40))
try doc.put(obj: ObjId.ROOT, key: "value", value: 40)
XCTAssertThrowsError(
try cautiousSingleValueContainer.encode(Double(3.4))
)
Expand All @@ -88,14 +88,14 @@ final class AutomergeSingleValueEncoderImplTests: XCTestCase {

func testSimpleKeyEncode_Int8() throws {
try singleValueContainer.encode(Int8(4))
XCTAssertEqual(try doc.get(obj: ObjId.ROOT, key: "value"), .Scalar(.Int(4)))
XCTAssertEqual(try doc.get(obj: ObjId.ROOT, key: "value"), .Scalar(4))

try cautiousSingleValueContainer.encode(Int8(5))
XCTAssertEqual(try doc.get(obj: ObjId.ROOT, key: "value"), .Scalar(.Int(5)))
XCTAssertEqual(try doc.get(obj: ObjId.ROOT, key: "value"), .Scalar(5))
}

func testSimpleKeyEncode_Int_CautiousFailure() throws {
try doc.put(obj: ObjId.ROOT, key: "value", value: .String("40"))
try doc.put(obj: ObjId.ROOT, key: "value", value: "40")
XCTAssertThrowsError(
try cautiousSingleValueContainer.encode(Int(4))
)
Expand All @@ -115,26 +115,26 @@ final class AutomergeSingleValueEncoderImplTests: XCTestCase {

func testSimpleKeyEncode_Int16() throws {
try singleValueContainer.encode(Int16(4))
XCTAssertEqual(try doc.get(obj: ObjId.ROOT, key: "value"), .Scalar(.Int(4)))
XCTAssertEqual(try doc.get(obj: ObjId.ROOT, key: "value"), .Scalar(4))

try cautiousSingleValueContainer.encode(Int16(5))
XCTAssertEqual(try doc.get(obj: ObjId.ROOT, key: "value"), .Scalar(.Int(5)))
XCTAssertEqual(try doc.get(obj: ObjId.ROOT, key: "value"), .Scalar(5))
}

func testSimpleKeyEncode_Int32() throws {
try singleValueContainer.encode(Int32(4))
XCTAssertEqual(try doc.get(obj: ObjId.ROOT, key: "value"), .Scalar(.Int(4)))
XCTAssertEqual(try doc.get(obj: ObjId.ROOT, key: "value"), .Scalar(4))

try cautiousSingleValueContainer.encode(Int32(5))
XCTAssertEqual(try doc.get(obj: ObjId.ROOT, key: "value"), .Scalar(.Int(5)))
XCTAssertEqual(try doc.get(obj: ObjId.ROOT, key: "value"), .Scalar(5))
}

func testSimpleKeyEncode_Int64() throws {
try singleValueContainer.encode(Int64(4))
XCTAssertEqual(try doc.get(obj: ObjId.ROOT, key: "value"), .Scalar(.Int(4)))
XCTAssertEqual(try doc.get(obj: ObjId.ROOT, key: "value"), .Scalar(4))

try cautiousSingleValueContainer.encode(Int64(5))
XCTAssertEqual(try doc.get(obj: ObjId.ROOT, key: "value"), .Scalar(.Int(5)))
XCTAssertEqual(try doc.get(obj: ObjId.ROOT, key: "value"), .Scalar(5))
}

func testSimpleKeyEncode_UInt() throws {
Expand Down Expand Up @@ -178,7 +178,7 @@ final class AutomergeSingleValueEncoderImplTests: XCTestCase {
}

func testSimpleKeyEncode_UInt_CautiousFailure() throws {
try doc.put(obj: ObjId.ROOT, key: "value", value: .String("40"))
try doc.put(obj: ObjId.ROOT, key: "value", value: "40")
XCTAssertThrowsError(
try cautiousSingleValueContainer.encode(UInt(4))
)
Expand Down Expand Up @@ -207,7 +207,7 @@ final class AutomergeSingleValueEncoderImplTests: XCTestCase {
}

func testSimpleKeyEncode_Date_CautiousFailure() throws {
try doc.put(obj: ObjId.ROOT, key: "value", value: .String("40"))
try doc.put(obj: ObjId.ROOT, key: "value", value: "40")
let dateFormatter = ISO8601DateFormatter()
let earlyDate = dateFormatter.date(from: "1941-04-26T08:17:00Z")!
XCTAssertThrowsError(
Expand All @@ -224,7 +224,7 @@ final class AutomergeSingleValueEncoderImplTests: XCTestCase {
}

func testSimpleKeyEncode_Data_CautiousFailure() throws {
try doc.put(obj: ObjId.ROOT, key: "value", value: .String("40"))
try doc.put(obj: ObjId.ROOT, key: "value", value: "40")
XCTAssertThrowsError(
try cautiousSingleValueContainer.encode(Data("World".utf8))
)
Expand All @@ -248,7 +248,7 @@ final class AutomergeSingleValueEncoderImplTests: XCTestCase {
}

func testSimpleKeyEncode_Counter_CautiousFailure() throws {
try doc.put(obj: ObjId.ROOT, key: "value", value: .String("40"))
try doc.put(obj: ObjId.ROOT, key: "value", value: "40")
XCTAssertThrowsError(
try cautiousSingleValueContainer.encode(Counter(443))
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ final class AutomergeTargettedEncodeDecodeTests: XCTestCase {
let doc = Document()
trackForMemoryLeak(instance: doc)

try doc.put(obj: ObjId.ROOT, key: "int", value: .Int(34))
try doc.put(obj: ObjId.ROOT, key: "int", value: 34)

let automergeDecoder = AutomergeDecoder(doc: doc)

Expand Down Expand Up @@ -149,7 +149,7 @@ final class AutomergeTargettedEncodeDecodeTests: XCTestCase {
let doc = Document()
trackForMemoryLeak(instance: doc)

try doc.put(obj: ObjId.ROOT, key: "int", value: .Int(34))
try doc.put(obj: ObjId.ROOT, key: "int", value: 34)

let automergeDecoder = AutomergeDecoder(doc: doc)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ final class RetrieveObjectIdTests: XCTestCase {
setupCache = [:]
doc = Document()

let _ = try! doc.put(obj: ObjId.ROOT, key: "name", value: .String("joe"))
let _ = try! doc.put(obj: ObjId.ROOT, key: "name", value: "joe")

let topMap = try! doc.putObject(obj: ObjId.ROOT, key: "topMap", ty: .Map)
setupCache["topMap"] = topMap
Expand All @@ -26,7 +26,7 @@ final class RetrieveObjectIdTests: XCTestCase {
let nestedText = try! doc.insertObject(obj: list, index: 1, ty: .Text)
setupCache["nestedText"] = nestedText

let _ = try! doc.insert(obj: list, index: 2, value: .String("alex"))
let _ = try! doc.insert(obj: list, index: 2, value: "alex")

try! doc.put(obj: nestedMap, key: "image", value: .Bytes(Data()))
let deeplyNestedText = try! doc.putObject(obj: nestedMap, key: "notes", ty: .Text)
Expand Down
16 changes: 8 additions & 8 deletions Tests/AutomergeTests/TestCrud.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ import XCTest
class CrudTests: XCTestCase {
func testCreateAndPutGetInRoot() {
let doc = Document()
try! doc.put(obj: ObjId.ROOT, key: "key", value: .String("value"))
XCTAssertEqual(try! doc.get(obj: ObjId.ROOT, key: "key")!, .Scalar(.String("value")))
try! doc.put(obj: ObjId.ROOT, key: "key", value: "value")
XCTAssertEqual(try! doc.get(obj: ObjId.ROOT, key: "key")!, .Scalar("value"))
}

func testMapCrud() {
let doc = Document()
let map = try! doc.putObject(obj: ObjId.ROOT, key: "map", ty: .Map)
try! doc.put(obj: map, key: "nested", value: .String("nested"))
XCTAssertEqual(try! doc.get(obj: map, key: "nested")!, .Scalar(.String("nested")))
try! doc.put(obj: map, key: "nested", value: "nested")
XCTAssertEqual(try! doc.get(obj: map, key: "nested")!, .Scalar("nested"))

try! doc.delete(obj: map, key: "nested")
XCTAssertEqual(try! doc.get(obj: map, key: "nested"), nil)
Expand All @@ -28,12 +28,12 @@ class CrudTests: XCTestCase {
func testListCrud() {
let doc = Document()
let list = try! doc.putObject(obj: ObjId.ROOT, key: "list", ty: .List)
try! doc.insert(obj: list, index: 0, value: .String("elem1"))
XCTAssertEqual(try! doc.get(obj: list, index: 0)!, .Scalar(.String("elem1")))
try! doc.insert(obj: list, index: 0, value: "elem1")
XCTAssertEqual(try! doc.get(obj: list, index: 0)!, .Scalar("elem1"))

let nested = try! doc.insertObject(obj: list, index: 1, ty: .Map)
try! doc.put(obj: nested, key: "nested", value: .String("nested"))
XCTAssertEqual(try! doc.get(obj: nested, key: "nested"), .Scalar(.String("nested")))
try! doc.put(obj: nested, key: "nested", value: "nested")
XCTAssertEqual(try! doc.get(obj: nested, key: "nested"), .Scalar("nested"))

try! doc.delete(obj: list, index: 1)
XCTAssertEqual(try! doc.get(obj: list, index: 1), nil)
Expand Down
14 changes: 7 additions & 7 deletions Tests/AutomergeTests/TestEncodeAndApplyNewChanges.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,30 @@ import XCTest
class EncodeAndApplyNewChangesTestCase: XCTestCase {
func testEncodeAndApplyNew() {
let doc = Document()
try! doc.put(obj: ObjId.ROOT, key: "key1", value: .String("one"))
try! doc.put(obj: ObjId.ROOT, key: "key1", value: "one")

let doc2 = Document()
let changes1 = doc.encodeNewChanges()
try! doc2.applyEncodedChanges(encoded: changes1)
XCTAssertEqual(try! doc2.get(obj: ObjId.ROOT, key: "key1")!, .Scalar(.String("one")))
XCTAssertEqual(try! doc2.get(obj: ObjId.ROOT, key: "key1")!, .Scalar("one"))

try! doc.put(obj: ObjId.ROOT, key: "key1", value: .String("two"))
try! doc.put(obj: ObjId.ROOT, key: "key1", value: "two")
let changes2 = doc.encodeNewChanges()
try! doc2.applyEncodedChanges(encoded: changes2)
XCTAssertEqual(try! doc2.get(obj: ObjId.ROOT, key: "key1")!, .Scalar(.String("two")))
XCTAssertEqual(try! doc2.get(obj: ObjId.ROOT, key: "key1")!, .Scalar("two"))
}

func testEncodeAndApplyChangesSince() {
let doc = Document()
try! doc.put(obj: ObjId.ROOT, key: "key1", value: .String("one"))
try! doc.put(obj: ObjId.ROOT, key: "key1", value: "one")
let doc2 = doc.fork()

let heads = doc.heads()

try! doc.put(obj: ObjId.ROOT, key: "key1", value: .String("two"))
try! doc.put(obj: ObjId.ROOT, key: "key1", value: "two")

let changes = try! doc.encodeChangesSince(heads: heads)
try! doc2.applyEncodedChanges(encoded: changes)
XCTAssertEqual(try! doc2.get(obj: ObjId.ROOT, key: "key1")!, .Scalar(.String("two")))
XCTAssertEqual(try! doc2.get(obj: ObjId.ROOT, key: "key1")!, .Scalar("two"))
}
}
Loading
Loading