Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
krystofwoldrich committed Dec 4, 2024
1 parent 87299dd commit a56cf84
Show file tree
Hide file tree
Showing 9 changed files with 175 additions and 31 deletions.
10 changes: 10 additions & 0 deletions Sentry.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@
15E0A8F22411A45A00F044E3 /* SentrySession.m in Sources */ = {isa = PBXBuildFile; fileRef = 15E0A8F12411A45A00F044E3 /* SentrySession.m */; };
33042A0D29DAF79A00C60085 /* SentryExtraContextProvider.m in Sources */ = {isa = PBXBuildFile; fileRef = 33042A0C29DAF79A00C60085 /* SentryExtraContextProvider.m */; };
33042A1729DC2C4300C60085 /* SentryExtraContextProviderTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 33042A1629DC2C4300C60085 /* SentryExtraContextProviderTests.swift */; };
33764D522D00AF3E0099E35A /* SentrySdkPackage+Private.h in Headers */ = {isa = PBXBuildFile; fileRef = 33764D512D00AF3E0099E35A /* SentrySdkPackage+Private.h */; settings = {ATTRIBUTES = (Private, ); }; };
33764D552D00B7920099E35A /* SentrySdkPackage+Equality.m in Sources */ = {isa = PBXBuildFile; fileRef = 33764D542D00B7920099E35A /* SentrySdkPackage+Equality.m */; };
339733E62D0096C100C632F1 /* SentrySdkPackage.m in Sources */ = {isa = PBXBuildFile; fileRef = 339733E52D0096C100C632F1 /* SentrySdkPackage.m */; };
339733E82D00972200C632F1 /* SentrySdkPackage.h in Headers */ = {isa = PBXBuildFile; fileRef = 339733E72D00972200C632F1 /* SentrySdkPackage.h */; settings = {ATTRIBUTES = (Private, ); }; };
33EB2A912C3412E4004FED3D /* SentryWithoutUIKit.h in Headers */ = {isa = PBXBuildFile; fileRef = 33EB2A8F2C3411AE004FED3D /* SentryWithoutUIKit.h */; settings = {ATTRIBUTES = (Public, ); }; };
Expand Down Expand Up @@ -1072,6 +1074,9 @@
33042A0B29DAF5F400C60085 /* SentryExtraContextProvider.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SentryExtraContextProvider.h; sourceTree = "<group>"; };
33042A0C29DAF79A00C60085 /* SentryExtraContextProvider.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SentryExtraContextProvider.m; sourceTree = "<group>"; };
33042A1629DC2C4300C60085 /* SentryExtraContextProviderTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SentryExtraContextProviderTests.swift; sourceTree = "<group>"; };
33764D512D00AF3E0099E35A /* SentrySdkPackage+Private.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "SentrySdkPackage+Private.h"; sourceTree = "<group>"; };
33764D532D00B7730099E35A /* SentrySdkPackage+Equality.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "SentrySdkPackage+Equality.h"; sourceTree = "<group>"; };
33764D542D00B7920099E35A /* SentrySdkPackage+Equality.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "SentrySdkPackage+Equality.m"; sourceTree = "<group>"; };
339733E52D0096C100C632F1 /* SentrySdkPackage.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SentrySdkPackage.m; sourceTree = "<group>"; };
339733E72D00972200C632F1 /* SentrySdkPackage.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = SentrySdkPackage.h; path = include/SentrySdkPackage.h; sourceTree = "<group>"; };
33EB2A8F2C3411AE004FED3D /* SentryWithoutUIKit.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = SentryWithoutUIKit.h; path = Public/SentryWithoutUIKit.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -2250,6 +2255,7 @@
7B88F2FD24BC5A4C00ADF90A /* SentrySdkInfo.h */,
7B88F2FF24BC5A7D00ADF90A /* SentrySdkInfo.m */,
339733E72D00972200C632F1 /* SentrySdkPackage.h */,
33764D512D00AF3E0099E35A /* SentrySdkPackage+Private.h */,
339733E52D0096C100C632F1 /* SentrySdkPackage.m */,
7BFC169A2524995700FF6266 /* SentryMessage.h */,
7BFC16A025249A9D00FF6266 /* SentryMessage.m */,
Expand Down Expand Up @@ -2887,7 +2893,9 @@
7B88F30124BC5C6D00ADF90A /* SentrySdkInfoTests.swift */,
7B26BBFA24C0A66D00A79CCC /* SentrySdkInfoNilTests.m */,
7B6D98E724C6D336005502FA /* SentrySdkInfo+Equality.h */,
33764D532D00B7730099E35A /* SentrySdkPackage+Equality.h */,
7B6D98E824C6D336005502FA /* SentrySdkInfo+Equality.m */,
33764D542D00B7920099E35A /* SentrySdkPackage+Equality.m */,
7B82D54824E2A2D400EE670F /* SentryIdTests.swift */,
7B04A9AA24EA5F8D00E710B1 /* SentryUserTests.swift */,
9286059829A50BAA00F96038 /* SentryGeoTests.swift */,
Expand Down Expand Up @@ -4141,6 +4149,7 @@
8E564AEF267AF24400FE117D /* SentryNetworkTracker.h in Headers */,
63FE715120DA4C1100CDBAE8 /* SentryCrashDebug.h in Headers */,
63FE70F520DA4C1000CDBAE8 /* SentryCrashMonitor_System.h in Headers */,
33764D522D00AF3E0099E35A /* SentrySdkPackage+Private.h in Headers */,
7B31C291277B04A000337126 /* SentryCrashPlatformSpecificDefines.h in Headers */,
7B77BE3527EC8445003C9020 /* SentryDiscardReasonMapper.h in Headers */,
7B610D602512390E00B0B5D9 /* SentrySDK+Private.h in Headers */,
Expand Down Expand Up @@ -4937,6 +4946,7 @@
63EED6C32237989300E02400 /* SentryOptionsTest.m in Sources */,
7BBD18B22451804C00427C76 /* SentryRetryAfterHeaderParserTests.swift in Sources */,
7BD337E424A356180050DB6E /* SentryCrashIntegrationTests.swift in Sources */,
33764D552D00B7920099E35A /* SentrySdkPackage+Equality.m in Sources */,
62C3168B2B1F865A000D7031 /* SentryTimeSwiftTests.swift in Sources */,
7BD4E8E827FD95900086C410 /* SentryMigrateSessionInitTests.m in Sources */,
7B6CC50224EE5A42001816D7 /* SentryHubTests.swift in Sources */,
Expand Down
11 changes: 11 additions & 0 deletions Sources/Sentry/SentrySdkPackage+Private.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#if __has_include(<Sentry/SentryDefines.h>)
# import <Sentry/SentrySdkPackage.h>
#else
# import "SentrySdkPackage.h"
#endif

@interface SentrySdkPackage ()

+ (nullable instancetype)getSentrySDKPackage:(NSUInteger)packageManger;

@end
19 changes: 13 additions & 6 deletions Sources/Sentry/SentrySdkPackage.m
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ - (nullable instancetype)initWithDict:(NSDictionary *)dict
};
}

+ (nullable NSString *)getSentrySDKPackageName
+ (nullable NSString *)getSentrySDKPackageName:(SentryPackageManagerOption)packageManager
{
switch (SENTRY_PACKAGE_INFO) {
switch (packageManager) {
case SentrySwiftPackageManager:
return @"spm:getsentry/%@";
case SentryCocoaPods:
Expand All @@ -87,19 +87,26 @@ + (nullable NSString *)getSentrySDKPackageName
}
}

+ (nullable SentrySdkPackage *)getSentrySDKPackage
+ (nullable SentrySdkPackage *)getSentrySDKPackage:(SentryPackageManagerOption)packageManager
{

if (SENTRY_PACKAGE_INFO == SentryPackageManagerUnkown) {
if (packageManager == SentryPackageManagerUnkown) {
return nil;
}

NSString *name = [SentrySdkPackage getSentrySDKPackageName];
NSString *name = [SentrySdkPackage getSentrySDKPackageName:packageManager];
if (nil == name) {
return nil;
}

return [[SentrySdkPackage alloc] initWithName:name andVersion:SentryMeta.versionString];
return
[[SentrySdkPackage alloc] initWithName:[NSString stringWithFormat:name, SentryMeta.sdkName]
andVersion:SentryMeta.versionString];
}

+ (nullable SentrySdkPackage *)getSentrySDKPackage
{
return [SentrySdkPackage getSentrySDKPackage:SENTRY_PACKAGE_INFO];
}

@end
Expand Down
3 changes: 3 additions & 0 deletions Sources/Sentry/include/SentrySdkPackage.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ NS_ASSUME_NONNULL_BEGIN
@interface SentrySdkPackage : NSObject <SentryInternalSerializable>
SENTRY_NO_INIT

- (instancetype)initWithName:(NSString *)name
andVersion:(NSString *)version NS_DESIGNATED_INITIALIZER;

/**
* Initialize Package frrom serialized dictionary if possible else returns nil
*/
Expand Down
5 changes: 5 additions & 0 deletions Tests/SentryTests/Protocol/SentrySdkInfo+Equality.m
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ - (BOOL)isEqual:(id _Nullable)object
return NO;
}

if (![self.packages isEqualToSet:otherSdkInfo.packages]) {
return NO;
}

return YES;
}

Expand All @@ -28,6 +32,7 @@ - (NSUInteger)hash

hash = hash * 23 + [self.name hash];
hash = hash * 23 + [self.version hash];
hash = hash * 23 + [self.packages hash];

return hash;
}
Expand Down
109 changes: 84 additions & 25 deletions Tests/SentryTests/Protocol/SentrySdkInfoTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ import XCTest
class SentrySdkInfoTests: XCTestCase {

private let sdkName = "sentry.cocoa"


override func setUp() {
SentryMeta.sdkName = sdkName
}

func testWithPatchLevelSuffix() {
let version = "50.10.20-beta1"
let actual = SentrySdkInfo(name: sdkName, andVersion: version)
Expand All @@ -26,70 +30,96 @@ class SentrySdkInfoTests: XCTestCase {
let actual = SentrySdkInfo(name: sdkName, andVersion: version).serialize()

if let sdkInfo = actual["sdk"] as? [String: Any] {
XCTAssertEqual(2, sdkInfo.count)
XCTAssertEqual(3, sdkInfo.count)
XCTAssertEqual(sdkName, sdkInfo["name"] as? String)
XCTAssertEqual(version, sdkInfo["version"] as? String)
XCTAssertEqual(0, (sdkInfo["packages"] as? [[String: Any]])?.count)
} else {
XCTFail("Serialization of SdkInfo doesn't contain sdk")
}
}

func testSPM_packageInfo() throws {
let version = "5.2.0"
let actual = SentrySdkInfo(name: sdkName, andVersion: version)
Dynamic(actual).packageManager = 0
let version = useVersion("5.2.0")
let actual = SentrySdkInfo(name: sdkName, andVersion: version, andPackages: [SentrySdkPackage.getSentrySDKPackage(0)!])
let serialization = actual.serialize()

if let sdkInfo = serialization["sdk"] as? [String: Any] {
XCTAssertEqual(3, sdkInfo.count)

let packageInfo = try XCTUnwrap(sdkInfo["packages"] as? [String: Any])
XCTAssertEqual(packageInfo["name"] as? String, "spm:getsentry/\(sdkName)")
XCTAssertEqual(packageInfo["version"] as? String, version)
let packages = try XCTUnwrap(sdkInfo["packages"] as? [[String: Any]])
XCTAssertEqual(1, packages.count)

XCTAssertEqual(packages[0]["name"] as? String, "spm:getsentry/\(sdkName)")
XCTAssertEqual(packages[0]["version"] as? String, version)
} else {
XCTFail("Serialization of SdkInfo doesn't contain sdk")
}
}

func testCarthage_packageInfo() throws {
let version = "5.2.0"
let actual = SentrySdkInfo(name: sdkName, andVersion: version)
Dynamic(actual).packageManager = 2
let version = useVersion("5.2.0")
let actual = SentrySdkInfo(name: sdkName, andVersion: version, andPackages: [SentrySdkPackage.getSentrySDKPackage(2)!])
let serialization = actual.serialize()

if let sdkInfo = serialization["sdk"] as? [String: Any] {
XCTAssertEqual(3, sdkInfo.count)

let packageInfo = try XCTUnwrap(sdkInfo["packages"] as? [String: Any])
XCTAssertEqual(packageInfo["name"] as? String, "carthage:getsentry/\(sdkName)")
XCTAssertEqual(packageInfo["version"] as? String, version)
let packages = try XCTUnwrap(sdkInfo["packages"] as? [[String: Any]])
XCTAssertEqual(1, packages.count)

XCTAssertEqual(packages[0]["name"] as? String, "carthage:getsentry/\(sdkName)")
XCTAssertEqual(packages[0]["version"] as? String, version)
} else {
XCTFail("Serialization of SdkInfo doesn't contain sdk")
}
}

func testcocoapods_packageInfo() throws {
let version = "5.2.0"
let actual = SentrySdkInfo(name: sdkName, andVersion: version)
Dynamic(actual).packageManager = 1
let version = useVersion("5.2.0")
let actual = SentrySdkInfo(name: sdkName, andVersion: version, andPackages: [SentrySdkPackage.getSentrySDKPackage(1)!])
let serialization = actual.serialize()

if let sdkInfo = serialization["sdk"] as? [String: Any] {
XCTAssertEqual(3, sdkInfo.count)

let packages = try XCTUnwrap(sdkInfo["packages"] as? [[String: Any]])
XCTAssertEqual(1, packages.count)

XCTAssertEqual(packages[0]["name"] as? String, "cocoapods:getsentry/\(sdkName)")
XCTAssertEqual(packages[0]["version"] as? String, version)
} else {
XCTFail("Serialization of SdkInfo doesn't contain sdk")
}
}

func test_multiple_packages() throws {
let version = useVersion("5.2.0")
let actual = SentrySdkInfo(name: sdkName, andVersion: version, andPackages: [
SentrySdkPackage(name: "package1", andVersion: "version1"),
SentrySdkPackage(name: "package2", andVersion: "version2")
])
let serialization = actual.serialize()

if let sdkInfo = serialization["sdk"] as? [String: Any] {
XCTAssertEqual(3, sdkInfo.count)

let packageInfo = try XCTUnwrap(sdkInfo["packages"] as? [String: Any])
XCTAssertEqual(packageInfo["name"] as? String, "cocoapods:getsentry/\(sdkName)")
XCTAssertEqual(packageInfo["version"] as? String, version)
let packages = try XCTUnwrap(sdkInfo["packages"] as? [[String: Any]])
XCTAssertEqual(2, packages.count)

XCTAssertEqual(packages[0]["name"] as? String, "package1")
XCTAssertEqual(packages[0]["version"] as? String, "version1")
XCTAssertEqual(packages[1]["name"] as? String, "package2")
XCTAssertEqual(packages[1]["version"] as? String, "version2")
} else {
XCTFail("Serialization of SdkInfo doesn't contain sdk")
}
}

func testNoPackageNames () {
let actual = SentrySdkInfo(name: sdkName, andVersion: "")
XCTAssertNil(Dynamic(actual).getPackageName(3).asString)
func testPackageManagerOption () {
XCTAssertNil(SentrySdkPackage.getSentrySDKPackage(3))
}

func testInitWithDict_SdkInfo() {
let version = "10.3.1"
let expected = SentrySdkInfo(name: sdkName, andVersion: version)
Expand All @@ -98,7 +128,31 @@ class SentrySdkInfoTests: XCTestCase {

XCTAssertEqual(expected, SentrySdkInfo(dict: dict))
}


func testInitWithDict_LegacyPackageInfo() {
let version = "10.3.1"
let expected = SentrySdkInfo(name: sdkName, andVersion: version)

let dict = ["sdk": [ "name": sdkName, "version": version, "packages": ["name": "package1", "version": "version1"]]]

XCTAssertEqual(expected, SentrySdkInfo(dict: dict))
}

func testInitWithDict_IncludePackages() {
let version = "10.3.1"
let expected = SentrySdkInfo(name: sdkName, andVersion: version, andPackages: [
SentrySdkPackage(name: "package1", andVersion: "version1"),
SentrySdkPackage(name: "package2", andVersion: "version2")
])

let dict = ["sdk": [ "name": sdkName, "version": version, "packages": [
["name": "package1", "version": "version1"],
["name": "package2", "version": "version2"]
]]]

XCTAssertEqual(expected, SentrySdkInfo(dict: dict))
}

func testInitWithDict_AllNil() {
let dict = ["sdk": [ "name": nil, "version": nil] as [String: Any?]]

Expand All @@ -120,4 +174,9 @@ class SentrySdkInfoTests: XCTestCase {
private func assertEmptySdkInfo(actual: SentrySdkInfo) {
XCTAssertEqual(SentrySdkInfo(name: "", andVersion: ""), actual)
}

private func useVersion(_ version: String) -> String {
SentryMeta.versionString = version
return version
}
}
13 changes: 13 additions & 0 deletions Tests/SentryTests/Protocol/SentrySdkPackage+Equality.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#import "SentrySdkPackage.h"

NS_ASSUME_NONNULL_BEGIN

@interface SentrySdkPackage (Equality)

- (BOOL)isEqual:(id _Nullable)object;

- (NSUInteger)hash;

@end

NS_ASSUME_NONNULL_END
35 changes: 35 additions & 0 deletions Tests/SentryTests/Protocol/SentrySdkPackage+Equality.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#import "SentrySdkPackage+Equality.h"

@implementation SentrySdkPackage (Equality)

- (BOOL)isEqual:(id _Nullable)object
{
if (object == self)
return YES;
if ([self class] != [object class])
return NO;

SentrySdkPackage *other = (SentrySdkPackage *)object;

if (![self.name isEqualToString:other.name]) {
return NO;
}

if (![self.version isEqualToString:other.version]) {
return NO;
}

return YES;
}

- (NSUInteger)hash
{
NSUInteger hash = 17;

hash = hash * 23 + [self.name hash];
hash = hash * 23 + [self.version hash];

return hash;
}

@end
1 change: 1 addition & 0 deletions Tests/SentryTests/SentryTests-Bridging-Header.h
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@
#import "SentryPerformanceTracker+Testing.h"
#import "SentryPropagationContext.h"
#import "SentrySampleDecision+Private.h"
#import "SentrySdkPackage+Private.h"
#import "SentrySerialization.h"
#import "SentrySession+Private.h"
#import "SentrySessionTracker.h"
Expand Down

0 comments on commit a56cf84

Please sign in to comment.