Skip to content

Commit

Permalink
[ios] impl unit test for sync date from http url response
Browse files Browse the repository at this point in the history
  • Loading branch information
RyosukeCla committed Nov 29, 2024
1 parent 9cdf55b commit e8d68d4
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
4 changes: 4 additions & 0 deletions ios/Nativebrik/Nativebrik.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
C13634642AEF92F700B9F437 /* embedding.swift in Sources */ = {isa = PBXBuildFile; fileRef = C13634632AEF92F700B9F437 /* embedding.swift */; };
C13634662AF242B100B9F437 /* decode.swift in Sources */ = {isa = PBXBuildFile; fileRef = C13634652AF242B100B9F437 /* decode.swift */; };
C13634682AF36D7600B9F437 /* repository.swift in Sources */ = {isa = PBXBuildFile; fileRef = C13634672AF36D7600B9F437 /* repository.swift */; };
C14FEF532CF95D8B00D243A0 /* ios-compat.swift in Sources */ = {isa = PBXBuildFile; fileRef = C14FEF522CF95D8300D243A0 /* ios-compat.swift */; };
C1F488CC2B5521FC009D7DAD /* placeholder.swift in Sources */ = {isa = PBXBuildFile; fileRef = C1F488CB2B5521FC009D7DAD /* placeholder.swift */; };
C1FFFD7A2B22E98B00ACD132 /* yoga.xcframework in Frameworks */ = {isa = PBXBuildFile; fileRef = C136344F2AEB7D5800B9F437 /* yoga.xcframework */; };
C1FFFD7D2B22E98C00ACD132 /* YogaKit.xcframework in Frameworks */ = {isa = PBXBuildFile; fileRef = C13634522AEB7D6600B9F437 /* YogaKit.xcframework */; };
Expand Down Expand Up @@ -124,6 +125,7 @@
C13634652AF242B100B9F437 /* decode.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = decode.swift; sourceTree = "<group>"; };
C13634672AF36D7600B9F437 /* repository.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = repository.swift; sourceTree = "<group>"; };
C13634882B036E5D00B9F437 /* PrivacyInfo.xcprivacy */ = {isa = PBXFileReference; lastKnownFileType = text.xml; name = PrivacyInfo.xcprivacy; path = Nativebrik/PrivacyInfo.xcprivacy; sourceTree = "<group>"; };
C14FEF522CF95D8300D243A0 /* ios-compat.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "ios-compat.swift"; sourceTree = "<group>"; };
C1F488CB2B5521FC009D7DAD /* placeholder.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = placeholder.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

Expand Down Expand Up @@ -283,6 +285,7 @@
C13633A92AEB769800B9F437 /* NativebrikTests */ = {
isa = PBXGroup;
children = (
C14FEF522CF95D8300D243A0 /* ios-compat.swift */,
C13633AA2AEB769800B9F437 /* sdk.swift */,
C13634552AEB81EC00B9F437 /* experiment-test.swift */,
C136345F2AEBB49900B9F437 /* user.swift */,
Expand Down Expand Up @@ -470,6 +473,7 @@
files = (
C13634562AEB81EC00B9F437 /* experiment-test.swift in Sources */,
C13634622AEBBC6500B9F437 /* remote-config.swift in Sources */,
C14FEF532CF95D8B00D243A0 /* ios-compat.swift in Sources */,
C13633AB2AEB769800B9F437 /* sdk.swift in Sources */,
C13634642AEF92F700B9F437 /* embedding.swift in Sources */,
C13634682AF36D7600B9F437 /* repository.swift in Sources */,
Expand Down
8 changes: 8 additions & 0 deletions ios/Nativebrik/Nativebrik/util/ios-compat.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ import Foundation

private var DATETIME_OFFSET: Int64 = 0

func __for_test_sync_datetime_offset(offset: Int64) {
DATETIME_OFFSET = offset
}

func __for_test_get_datetime_offset() -> Int64 {
return DATETIME_OFFSET
}

func syncDateFromHTTPURLResponse(t0: Date, res: HTTPURLResponse) {
let t1 = getCurrentDate()

Expand Down
39 changes: 39 additions & 0 deletions ios/Nativebrik/NativebrikTests/ios-compat.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//
// ios-compat.swift
// Nativebrik
//
// Created by Ryosuke Suzuki on 2024/11/29.
//

import XCTest
import ViewInspector
@testable import Nativebrik

final class IOSCompatTest: XCTestCase {
func testSyncDateFromHTTPULReponseShouldWork() throws {
__for_test_sync_datetime_offset(offset: 0)
let url = URL(string: "https://example.com")!
let now = Date()
let tomorrow = now.addingTimeInterval(24 * 60 * 60)
let formatter = DateFormatter()
formatter.dateFormat = "EEE, dd MMM yyyy HH:mm:ss zzz"
formatter.locale = Locale(identifier: "en_US_POSIX")
let formattedDate = formatter.string(from: tomorrow)
let res = HTTPURLResponse(url: url, statusCode: 400, httpVersion: "2.0", headerFields: [
"Date": formattedDate
])!
syncDateFromHTTPURLResponse(t0: now, res: res)
let offset = __for_test_get_datetime_offset()
let diff = abs(offset - 24 * 60 * 60 * 1000)
assert(diff < 1000, "time offset should be around 24 hours")
}

func testGetCurrentDate() throws {
__for_test_sync_datetime_offset(offset: 24 * 60 * 60 * 1000)
let deviceCurrent = Date()
let syncedCurrent = getCurrentDate()
let diff = syncedCurrent.timeIntervalSince1970 - deviceCurrent.timeIntervalSince1970
assert(abs(diff - 24 * 60 * 60) < 2, "diff should be around 2 sec")
}

}

0 comments on commit e8d68d4

Please sign in to comment.