-
-
Notifications
You must be signed in to change notification settings - Fork 333
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
263 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#import "SentryDefines.h" | ||
#import "SentrySerializable.h" | ||
#import <Foundation/Foundation.h> | ||
|
||
NS_ASSUME_NONNULL_BEGIN | ||
|
||
/// Approximate geographical location of the end user or device. | ||
/// | ||
/// Example of serialized data: | ||
/// { | ||
/// "geo": { | ||
/// "country_code": "US", | ||
/// "city": "Ashburn", | ||
/// "region": "San Francisco" | ||
/// } | ||
/// } | ||
NS_SWIFT_NAME(Geo) | ||
@interface SentryGeo : NSObject <SentrySerializable, NSCopying> | ||
|
||
/** | ||
* Optional: Human readable city name. | ||
*/ | ||
@property (nullable, atomic, copy) NSString *city; | ||
|
||
/** | ||
* Optional: Two-letter country code (ISO 3166-1 alpha-2). | ||
*/ | ||
@property (nullable, atomic, copy) NSString *countryCode; | ||
|
||
/** | ||
* Optional: Human readable region name or code. | ||
*/ | ||
@property (nullable, atomic, copy) NSString *region; | ||
|
||
- (BOOL)isEqual:(id _Nullable)other; | ||
|
||
- (BOOL)isEqualToGeo:(SentryGeo *)geo; | ||
|
||
- (NSUInteger)hash; | ||
|
||
@end | ||
|
||
NS_ASSUME_NONNULL_END |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
#import "SentryGeo.h" | ||
|
||
#import <Foundation/Foundation.h> | ||
|
||
NS_ASSUME_NONNULL_BEGIN | ||
|
||
@implementation SentryGeo | ||
|
||
- (id)copyWithZone:(nullable NSZone *)zone | ||
{ | ||
SentryGeo *copy = [[[self class] allocWithZone:zone] init]; | ||
|
||
if (copy != nil) { | ||
copy.city = self.city; | ||
copy.countryCode = self.countryCode; | ||
copy.region = self.region; | ||
} | ||
|
||
return copy; | ||
} | ||
|
||
- (NSDictionary<NSString *, id> *)serialize | ||
{ | ||
return @{ | ||
@"city" : self.city, | ||
@"country_code" : self.countryCode, | ||
@"region" : self.region | ||
}; | ||
} | ||
|
||
- (BOOL)isEqual:(id _Nullable)other | ||
{ | ||
if (other == self) { | ||
return YES; | ||
} | ||
if (!other || ![[other class] isEqual:[self class]]) { | ||
return NO; | ||
} | ||
|
||
return [self isEqualToGeo:other]; | ||
} | ||
|
||
- (BOOL)isEqualToGeo:(SentryGeo *)geo | ||
{ | ||
if (self == geo) { | ||
return YES; | ||
} | ||
if (geo == nil) { | ||
return NO; | ||
} | ||
|
||
NSString *otherCity = geo.city; | ||
if (self.city != otherCity && ![self.city isEqualToString:otherCity]) { | ||
return NO; | ||
} | ||
|
||
NSString *otherCountryCode = geo.countryCode; | ||
if (self.countryCode != otherCountryCode | ||
&& ![self.countryCode isEqualToString:otherCountryCode]) { | ||
return NO; | ||
} | ||
|
||
NSString *otherRegion = geo.region; | ||
if (self.region != otherRegion && ![self.region isEqualToString:otherRegion]) { | ||
return NO; | ||
} | ||
|
||
return YES; | ||
} | ||
|
||
- (NSUInteger)hash | ||
{ | ||
NSUInteger hash = 17; | ||
|
||
hash = hash * 23 + [self.city hash]; | ||
hash = hash * 23 + [self.countryCode hash]; | ||
hash = hash * 23 + [self.region hash]; | ||
|
||
return hash; | ||
} | ||
|
||
@end | ||
|
||
NS_ASSUME_NONNULL_END |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import XCTest | ||
|
||
class SentryGeoTests: XCTestCase { | ||
func testSerializationWithAllProperties() { | ||
let geo = TestData.geo.copy() as! Geo | ||
let actual = geo.serialize() | ||
|
||
// Changing the original doesn't modify the serialized | ||
geo.city = "" | ||
geo.countryCode = "" | ||
geo.region = "" | ||
|
||
XCTAssertEqual(TestData.geo.city, actual["city"] as? String) | ||
XCTAssertEqual(TestData.geo.countryCode, actual["country_code"] as? String) | ||
XCTAssertEqual(TestData.geo.region, actual["region"] as? String) | ||
} | ||
|
||
func testHash() { | ||
XCTAssertEqual(TestData.geo.hash(), TestData.geo.hash()) | ||
|
||
let geo2 = TestData.geo | ||
geo2.city = "Berlin" | ||
XCTAssertNotEqual(TestData.geo.hash(), geo2.hash()) | ||
} | ||
|
||
func testIsEqualToSelf() { | ||
XCTAssertEqual(TestData.geo, TestData.geo) | ||
XCTAssertTrue(TestData.geo.isEqual(to: TestData.geo)) | ||
} | ||
|
||
func testIsNotEqualToOtherClass() { | ||
XCTAssertFalse(TestData.geo.isEqual(1)) | ||
} | ||
|
||
func testIsEqualToCopy() { | ||
XCTAssertEqual(TestData.geo, TestData.geo.copy() as! Geo) | ||
} | ||
|
||
func testNotIsEqual() { | ||
testIsNotEqual { geo in geo.city = "" } | ||
testIsNotEqual { geo in geo.countryCode = "" } | ||
testIsNotEqual { geo in geo.region = "" } | ||
} | ||
|
||
func testIsNotEqual(block: (Geo) -> Void ) { | ||
let geo = TestData.geo.copy() as! Geo | ||
block(geo) | ||
XCTAssertNotEqual(TestData.geo, geo) | ||
} | ||
|
||
func testCopyWithZone_CopiesDeepCopy() { | ||
let geo = TestData.geo | ||
let copiedGeo = geo.copy() as! Geo | ||
|
||
// Modifying the original does not change the copy | ||
geo.city = "" | ||
geo.countryCode = "" | ||
geo.region = "" | ||
|
||
XCTAssertEqual(TestData.geo, copiedGeo) | ||
} | ||
} |
Oops, something went wrong.