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

[ios] impl unit test to SyncDateFromHTTPULReponse #66

Merged
merged 2 commits into from
Nov 29, 2024
Merged
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
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")
}

}
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
{
"originHash" : "7abf1dd76d62709d50ab97ba7b5907de6a95cbc813d2bf154db0688b92d85914",
"pins" : [
{
"identity" : "viewinspector",
"kind" : "remoteSourceControl",
"location" : "https://github.com/nalexn/ViewInspector",
"state" : {
"revision" : "07c090d73e0169c342a4ed46e9010be2781eca1e",
"version" : "0.9.8"
"revision" : "5acfa0a3c095ac9ad050abe51c60d1831e8321da",
"version" : "0.10.0"
}
}
],
"version" : 2
"version" : 3
}
Loading