Skip to content

Commit

Permalink
Merge pull request #44 from FredericRuaudel/fix-empty-array-crash
Browse files Browse the repository at this point in the history
Fix empty array encoding crash
  • Loading branch information
tanner0101 authored Jul 12, 2018
2 parents 500748e + 04afb7c commit 386a6ed
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
3 changes: 2 additions & 1 deletion Sources/SQLite/Codable/SQLiteDataEncoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ public struct SQLiteDataEncoder {
let encoder = _Encoder()
do {
try value.encode(to: encoder)
return encoder.data!
guard let data = encoder.data else { throw _DoJSONError() }
return data
} catch is _DoJSONError {
struct AnyEncodable: Encodable {
var encodable: Encodable
Expand Down
24 changes: 24 additions & 0 deletions Tests/SQLiteTests/SQLiteTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,30 @@ class SQLiteTests: XCTestCase {
}
group.wait()
}

func testNonEmptyArrayEncodingDecoding() throws {
let nonEmptyArray = ["foo", "bar"]
let encoder = SQLiteDataEncoder()

let data = try encoder.encode(nonEmptyArray)

let decoder = SQLiteDataDecoder()

let result = try decoder.decode([String].self, from: data)
XCTAssertEqual(result, nonEmptyArray, "Should convert back to original array")
}

func testEmptyArrayEncodingDecoding() throws {
let emptyArray = [String]()
let encoder = SQLiteDataEncoder()

let data = try encoder.encode(emptyArray)

let decoder = SQLiteDataDecoder()

let result = try decoder.decode([String].self, from: data)
XCTAssertEqual(result, emptyArray, "Should convert back to empty Array")
}

static let allTests = [
("testBenchmark", testBenchmark),
Expand Down

0 comments on commit 386a6ed

Please sign in to comment.