Skip to content

Commit

Permalink
Merge pull request #10 from thejohnlima/fix/parse_json_functions
Browse files Browse the repository at this point in the history
Fix functions used to parse data from local files
  • Loading branch information
thejohnlima authored Jul 11, 2021
2 parents 45552ea + 2cf3129 commit 5c4e3aa
Show file tree
Hide file tree
Showing 7 changed files with 157 additions and 9 deletions.
11 changes: 10 additions & 1 deletion .swiftpm/xcode/xcshareddata/xcschemes/LMStorage.xcscheme
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,16 @@
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES"
codeCoverageEnabled = "YES">
onlyGenerateCoverageForSpecifiedTargets = "YES">
<CodeCoverageTargets>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "LMStorage"
BuildableName = "LMStorage"
BlueprintName = "LMStorage"
ReferencedContainer = "container:">
</BuildableReference>
</CodeCoverageTargets>
<Testables>
<TestableReference
skipped = "NO">
Expand Down
5 changes: 4 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ let package = Package(
),
.testTarget(
name: "LMStorageTests",
dependencies: ["LMStorage"]
dependencies: ["LMStorage"],
resources: [
.process("Resources")
]
)
]
)
18 changes: 11 additions & 7 deletions Sources/LMStorage/LMCodable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -110,23 +110,27 @@ extension LMCodable {
hasher.combine(identifier)
}

/// Get object from local JSON file
/// Get object from local file
///
/// - Parameter file: JSON file name
/// - Parameter file: File name
/// - Parameter type: File type
/// - Parameter bundle: File bundle
/// - Returns: Object
public static func getItem<T: LMCodable>(from file: String) -> T? {
if let path = Bundle.main.path(forResource: file, ofType: "json"),
public static func getItem<T: LMCodable>(from file: String, type: String = "json", bundle: Bundle? = .main) -> T? {
if let path = bundle?.path(forResource: file, ofType: type),
let data = try? Data(contentsOf: URL(fileURLWithPath: path)) {
return try? JSONDecoder().decode(T.self, from: data)
}
return nil
}

/// Get objects from local JSON file
/// Get objects from local file
/// - Parameter file: JSON file name
/// - Parameter type: File type
/// - Parameter bundle: File bundle
/// - Returns: Array of objects
public static func getItems<T: LMCodable>(from file: String) -> [T] {
if let path = Bundle.main.path(forResource: file, ofType: "json"),
public static func getItems<T: LMCodable>(from file: String, type: String = "json", bundle: Bundle? = .main) -> [T] {
if let path = bundle?.path(forResource: file, ofType: type),
let data = try? Data(contentsOf: URL(fileURLWithPath: path)) {
let result = try? JSONDecoder().decode([T].self, from: data)
return result ?? []
Expand Down
72 changes: 72 additions & 0 deletions Tests/LMStorageTests/LMCodableTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// MIT License
//
// Copyright (c) 2020 John Lima
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

@testable
import LMStorage
import XCTest

final class LMCodableTests: XCTestCase {

// MARK: - Properties
private var sign: Sign?
private var signs: [Sign] = []

static var allTests = [
("testSuccessItemLocalParse", testSuccessItemLocalParse)
]

// MARK: - Overrides
override func setUp() {
super.setUp()
sign = Sign.getItem(from: "sign", bundle: .module)
signs = Sign.getItems(from: "signs", bundle: .module)
}

override func tearDown() {
sign = nil
signs = []
super.tearDown()
}

// MARK: - Test Methods
func testSuccessItemLocalParse() {
XCTAssertEqual(sign?.dateRange, "Aug 23 - Sep 22")
XCTAssertEqual(sign?.currentDate, "July 11, 2021")
XCTAssertEqual(sign?.description, "You reach a milestone of some kind today, and should be able to build on it without too much trouble. In fact, now is a really good time for you to step back and reconsider your goals and ambitions.")
XCTAssertEqual(sign?.compatibility, "Cancer")
XCTAssertEqual(sign?.mood, "Serious")
XCTAssertEqual(sign?.color, "Orchid")
XCTAssertEqual(sign?.luckyNumber, "93")
XCTAssertEqual(sign?.luckyTime, "4pm")
}

func testSuccessItemsLocalParse() {
XCTAssertEqual(signs.last?.dateRange, "Mar 21 - Apr 20")
XCTAssertEqual(signs.last?.currentDate, "July 11, 2021")
XCTAssertEqual(signs.last?.description, "Your social energy is potent today -- but it's best spent on those you already know fairly well. Friends and family need your attention more than strangers, anyway, so hole up at home for now.")
XCTAssertEqual(signs.last?.compatibility, "Sagittarius")
XCTAssertEqual(signs.last?.mood, "Thoughtful")
XCTAssertEqual(signs.last?.color, "Teal")
XCTAssertEqual(signs.last?.luckyNumber, "42")
XCTAssertEqual(signs.last?.luckyTime, "4pm")
}
}
28 changes: 28 additions & 0 deletions Tests/LMStorageTests/Mock/Sign.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//
// File.swift
//
//
// Created by John Lima on 7/11/21.
//

@testable
import LMStorage

struct Sign: LMCodable {
let dateRange: String?
let currentDate: String?
let description: String?
let compatibility: String?
let mood: String?
let color: String?
let luckyNumber: String?
let luckyTime: String?

enum CodingKeys: String, CodingKey {
case description, compatibility, mood, color
case dateRange = "date_range"
case currentDate = "current_date"
case luckyNumber = "lucky_number"
case luckyTime = "lucky_time"
}
}
10 changes: 10 additions & 0 deletions Tests/LMStorageTests/Resources/sign.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"date_range": "Aug 23 - Sep 22",
"current_date": "July 11, 2021",
"description": "You reach a milestone of some kind today, and should be able to build on it without too much trouble. In fact, now is a really good time for you to step back and reconsider your goals and ambitions.",
"compatibility": "Cancer",
"mood": "Serious",
"color": "Orchid",
"lucky_number": "93",
"lucky_time": "4pm"
}
22 changes: 22 additions & 0 deletions Tests/LMStorageTests/Resources/signs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[
{
"date_range": "Aug 23 - Sep 22",
"current_date": "July 11, 2021",
"description": "You reach a milestone of some kind today, and should be able to build on it without too much trouble. In fact, now is a really good time for you to step back and reconsider your goals and ambitions.",
"compatibility": "Cancer",
"mood": "Serious",
"color": "Orchid",
"lucky_number": "93",
"lucky_time": "4pm"
},
{
"date_range": "Mar 21 - Apr 20",
"current_date": "July 11, 2021",
"description": "Your social energy is potent today -- but it's best spent on those you already know fairly well. Friends and family need your attention more than strangers, anyway, so hole up at home for now.",
"compatibility": "Sagittarius",
"mood": "Thoughtful",
"color": "Teal",
"lucky_number": "42",
"lucky_time": "4pm"
}
]

0 comments on commit 5c4e3aa

Please sign in to comment.