Skip to content

Commit

Permalink
Library interface update
Browse files Browse the repository at this point in the history
  • Loading branch information
numen31337 committed Dec 4, 2018
1 parent 4b794b1 commit 0319357
Show file tree
Hide file tree
Showing 7 changed files with 161 additions and 28 deletions.
2 changes: 1 addition & 1 deletion Day.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "Day"
s.version = "1.1"
s.version = "1.1.1"
s.summary = "Mini lib to represent a Day"
s.description = "This is the mini library to represent a Day value with serialization functionality"
s.homepage = "https://github.com/numen31337/Day"
Expand Down
2 changes: 2 additions & 0 deletions Day.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
/* End PBXContainerItemProxy section */

/* Begin PBXFileReference section */
E90301F221B69655002FB724 /* README.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = SOURCE_ROOT; };
E9D466311FC5A65600F2BEC3 /* Day.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Day.framework; sourceTree = BUILT_PRODUCTS_DIR; };
E9D466341FC5A65600F2BEC3 /* Day.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Day.h; sourceTree = "<group>"; };
E9D466351FC5A65600F2BEC3 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
Expand Down Expand Up @@ -87,6 +88,7 @@
E9D466511FC5A69900F2BEC3 /* SwiftlyLRU.swift */,
E9D466341FC5A65600F2BEC3 /* Day.h */,
E9D466351FC5A65600F2BEC3 /* Info.plist */,
E90301F221B69655002FB724 /* README.md */,
);
path = Day;
sourceTree = "<group>";
Expand Down
100 changes: 100 additions & 0 deletions Day.xcodeproj/xcshareddata/xcschemes/Day.xcscheme
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1010"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
<BuildActionEntries>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "E9D466301FC5A65600F2BEC3"
BuildableName = "Day.framework"
BlueprintName = "Day"
ReferencedContainer = "container:Day.xcodeproj">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
codeCoverageEnabled = "YES"
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
<TestableReference
skipped = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "E9D466391FC5A65600F2BEC3"
BuildableName = "DayTests.xctest"
BlueprintName = "DayTests"
ReferencedContainer = "container:Day.xcodeproj">
</BuildableReference>
</TestableReference>
</Testables>
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "E9D466301FC5A65600F2BEC3"
BuildableName = "Day.framework"
BlueprintName = "Day"
ReferencedContainer = "container:Day.xcodeproj">
</BuildableReference>
</MacroExpansion>
<AdditionalOptions>
</AdditionalOptions>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "E9D466301FC5A65600F2BEC3"
BuildableName = "Day.framework"
BlueprintName = "Day"
ReferencedContainer = "container:Day.xcodeproj">
</BuildableReference>
</MacroExpansion>
<AdditionalOptions>
</AdditionalOptions>
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
debugDocumentVersioning = "YES">
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "E9D466301FC5A65600F2BEC3"
BuildableName = "Day.framework"
BlueprintName = "Day"
ReferencedContainer = "container:Day.xcodeproj">
</BuildableReference>
</MacroExpansion>
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
</Scheme>
9 changes: 0 additions & 9 deletions Day/Day.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,6 @@ public struct Day {
public init(_ day: Day) {
rawValue = day.rawValue
}

public init?(_ string: String) {
guard let day = DayString(string)?.day else { return nil }
rawValue = day.rawValue
}
}

///MARK: Helpers
Expand Down Expand Up @@ -67,10 +62,6 @@ extension Day: Comparable {
}
}

extension Day: CustomStringConvertible {
public var description: String { return DayString(self).stringValue }
}

extension Day: Hashable {
public var hashValue: Int { return rawValue.hashValue }
}
Expand Down
31 changes: 28 additions & 3 deletions Day/StringSerializableDate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import Foundation

protocol StringSerializableDate: CustomStringConvertible {
protocol StringSerializableDate {
var stringValue: String { get }
static var formatter: DateFormatter { get }

Expand All @@ -18,8 +18,6 @@ protocol StringSerializableDate: CustomStringConvertible {
}

extension StringSerializableDate {
var description: String { return stringValue }

init(_ date: Date) {
let parsedString = Self.formatter.string(from: date)
assert(Self.isCorrectFormat(parsedString))
Expand Down Expand Up @@ -94,3 +92,30 @@ struct DayTimeString: StringSerializableDate {
return true
}
}

extension Date {
public static func date(fromSerializedDateAndTimeString string: String) -> Date? {
return DayTimeString(string)?.date
}

public static func date(fromSerializedDay string: String) -> Date? {
return DayString(string)?.day.rawValue
}

public var serializedDayString: String { return DayString(self).stringValue }

public var serializedDayAndTimeString: String { return DayTimeString(self).stringValue }
}

extension Day {
public init?(_ string: String) {
guard let day = DayString(string)?.day else { return nil }
rawValue = day.rawValue
}

public var serializedDayString: String { return DayString(self).stringValue }
}

extension Day: CustomStringConvertible {
public var description: String { return serializedDayString }
}
43 changes: 29 additions & 14 deletions DayTests/StringSerializableDate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
//

import XCTest
@testable import Day
import Day

class DayTests: XCTestCase {
let formatter: DateFormatter = {
Expand All @@ -28,35 +28,50 @@ class DayTests: XCTestCase {

func testDayFromString() {
let correctResultDate = formatter.date(from: "2018.11.04 00:00")
let dayString = DayString("2018.11.04")!.day.rawValue
let dayString = Day("2018.11.04")!.rawValue
let dateString = Date.date(fromSerializedDay: "2018.11.04")

XCTAssertEqual(correctResultDate, dayString)
XCTAssertEqual(correctResultDate, dateString)
}

func testDayTimeFromString() {
let correctResultDate = formatter.date(from: "2018.11.04 11:10")
let dayString = DayTimeString("2018.11.04.11.10")!.date
let dayString = Date.date(fromSerializedDateAndTimeString: "2018.11.04.11.10")

XCTAssertEqual(correctResultDate, dayString)
}

func testStringFromDay() {
func testDayString() {
let date = formatter.date(from: "2018.11.04 11:10")!
let string = DayString(date).stringValue
XCTAssertEqual(string, "2018.11.04")
let stringFromDay = Day(date).serializedDayString
let stringFromDate = date.serializedDayString

XCTAssertEqual(stringFromDay, "2018.11.04")
XCTAssertEqual(stringFromDate, "2018.11.04")
}

func testStringFromDayTime() {
let date = formatter.date(from: "2018.11.04 11:10")!
let string = DayTimeString(date).stringValue
let string = date.serializedDayAndTimeString
XCTAssertEqual(string, "2018.11.04.11.10")
}
func testDayStringFail() {
let failedValue = DayTimeString("2018+11+04")

func testDayStringInitFail() {
let failedValue = Day("2018+11+04")
XCTAssertNil(failedValue)
}

func testDateStringInitFail() {
let failedValue1 = Date.date(fromSerializedDateAndTimeString: "2018.11.04+11:10")
let failedValue2 = Date.date(fromSerializedDay: "2018+11+04")

XCTAssertNil(failedValue1)
XCTAssertNil(failedValue2)
}

func testDayTimeStringFail() {
let failedValue = DayTimeString("2018.11.04+11:10")
XCTAssertNil(failedValue)
func testDayDescription() {
XCTAssertEqual("\(String(describing: Day("2018.11.04")!))", "2018.11.04")
XCTAssertEqual(Day("2018.11.04")!.description, "2018.11.04")
}
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ let day = Day(date) //Day value from the date by dropping time components

Serialization:<br />
```swift
let serializedString = DayString(day).stringValue //A serialized string value
let serializedString = day.serializedDayString //A serialized string value
let day = Day(serializedString) //Day value from the serialized string
```

Expand Down

0 comments on commit 0319357

Please sign in to comment.