Skip to content
Permalink
Browse files

Methods to retrieve the raw and semantic SDK versions (#209)

* Added NSString category to parse the raw ONESIGNAL_VERSION string
* Added unit tests for the new version parsing methods
  • Loading branch information
jamesstout authored and jkasten2 committed Mar 24, 2017
1 parent e2972b8 commit 4c1fb7b899b457fb050c95c24fef7cde1998c1ff
@@ -7,6 +7,10 @@
objects = {

/* Begin PBXBuildFile section */
1AF75EAE1E8567FD0097B315 /* NSString+OneSignal.m in Sources */ = {isa = PBXBuildFile; fileRef = 1AF75EAD1E8567FD0097B315 /* NSString+OneSignal.m */; };
1AF75EAF1E8569710097B315 /* NSString+OneSignal.m in Sources */ = {isa = PBXBuildFile; fileRef = 1AF75EAD1E8567FD0097B315 /* NSString+OneSignal.m */; };
1AF75EB01E8569720097B315 /* NSString+OneSignal.m in Sources */ = {isa = PBXBuildFile; fileRef = 1AF75EAD1E8567FD0097B315 /* NSString+OneSignal.m */; };
1AF75EB11E8569F60097B315 /* NSString+OneSignal.h in Headers */ = {isa = PBXBuildFile; fileRef = 1AF75EAC1E8567FD0097B315 /* NSString+OneSignal.h */; };
37E6B2BB19D9CAF300D0C601 /* UIKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 37E6B2BA19D9CAF300D0C601 /* UIKit.framework */; settings = {ATTRIBUTES = (Weak, ); }; };
3E464ED71D88ED1F00DCF7E9 /* UIKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 37E6B2BA19D9CAF300D0C601 /* UIKit.framework */; };
3E66F5821D90A2C600E45A01 /* SystemConfiguration.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 3E08E2701D49A5C8002176DE /* SystemConfiguration.framework */; };
@@ -114,6 +118,8 @@
/* End PBXCopyFilesBuildPhase section */

/* Begin PBXFileReference section */
1AF75EAC1E8567FD0097B315 /* NSString+OneSignal.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSString+OneSignal.h"; sourceTree = "<group>"; };
1AF75EAD1E8567FD0097B315 /* NSString+OneSignal.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSString+OneSignal.m"; sourceTree = "<group>"; };
37747F9319147D6500558FAD /* libOneSignal.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libOneSignal.a; sourceTree = BUILT_PRODUCTS_DIR; };
37E6B2BA19D9CAF300D0C601 /* UIKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = UIKit.framework; path = System/Library/Frameworks/UIKit.framework; sourceTree = SDKROOT; };
3E08E2701D49A5C8002176DE /* SystemConfiguration.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = SystemConfiguration.framework; path = System/Library/Frameworks/SystemConfiguration.framework; sourceTree = SDKROOT; };
@@ -291,6 +297,8 @@
912412461E73349500E41FD7 /* Categories */ = {
isa = PBXGroup;
children = (
1AF75EAC1E8567FD0097B315 /* NSString+OneSignal.h */,
1AF75EAD1E8567FD0097B315 /* NSString+OneSignal.m */,
912412001E73342200E41FD7 /* OneSignalSelectorHelpers.h */,
912412011E73342200E41FD7 /* OneSignalSelectorHelpers.m */,
912412081E73342200E41FD7 /* UIApplicationDelegate+OneSignal.h */,
@@ -331,6 +339,7 @@
912412411E73342200E41FD7 /* UNUserNotificationCenter+OneSignal.h in Headers */,
912412351E73342200E41FD7 /* OneSignalTrackIAP.h in Headers */,
912412111E73342200E41FD7 /* OneSignalAlertViewDelegate.h in Headers */,
1AF75EB11E8569F60097B315 /* NSString+OneSignal.h in Headers */,
91C7725C1E7CA7A800D612D0 /* OneSignalNotificationSettingsIOS7.h in Headers */,
912412311E73342200E41FD7 /* OneSignalTracker.h in Headers */,
9124122D1E73342200E41FD7 /* OneSignalSelectorHelpers.h in Headers */,
@@ -496,6 +505,7 @@
9124121A1E73342200E41FD7 /* OneSignalHTTPClient.m in Sources */,
91F58D891E7C9A240017D24D /* OneSignalNotificationSettingsIOS7.m in Sources */,
912412221E73342200E41FD7 /* OneSignalLocation.m in Sources */,
1AF75EAE1E8567FD0097B315 /* NSString+OneSignal.m in Sources */,
912412361E73342200E41FD7 /* OneSignalTrackIAP.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
@@ -520,6 +530,7 @@
9124121B1E73342200E41FD7 /* OneSignalHTTPClient.m in Sources */,
91F58D8A1E7C9A240017D24D /* OneSignalNotificationSettingsIOS7.m in Sources */,
912412231E73342200E41FD7 /* OneSignalLocation.m in Sources */,
1AF75EB01E8569720097B315 /* NSString+OneSignal.m in Sources */,
912412371E73342200E41FD7 /* OneSignalTrackIAP.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
@@ -542,6 +553,7 @@
911E2CBD1E398AB3003112A4 /* UnitTests.m in Sources */,
9124121C1E73342200E41FD7 /* OneSignalHTTPClient.m in Sources */,
912412401E73342200E41FD7 /* UIApplicationDelegate+OneSignal.m in Sources */,
1AF75EAF1E8569710097B315 /* NSString+OneSignal.m in Sources */,
912412281E73342200E41FD7 /* OneSignalMobileProvision.m in Sources */,
912412141E73342200E41FD7 /* OneSignalAlertViewDelegate.m in Sources */,
912412441E73342200E41FD7 /* UNUserNotificationCenter+OneSignal.m in Sources */,
@@ -0,0 +1,19 @@
//
// NSString+OneSignal.h
//
// Created by James on 16/3/2017.
//
//

#import <Foundation/Foundation.h>

#ifndef NSString_OneSignal_h
#define NSString_OneSignal_h
@interface NSString (OneSignal)

- (NSString*)one_getVersionForRange:(NSRange)range;
- (NSString*)one_substringAfter:(NSString *)needle;
- (NSString*)one_getSemanticVersion;

@end
#endif
@@ -0,0 +1,47 @@
//
// NSString+OneSignal.m
//
// Created by James on 16/3/2017.
//
//

#import "NSString+OneSignal.h"

@implementation NSString (OneSignal)


- (NSString *)one_substringAfter:(NSString *)needle
{
NSRange r = [self rangeOfString:needle];
if (r.location == NSNotFound) return self;
return [self substringFromIndex:(r.location + r.length)];
}


- (NSString*)one_getVersionForRange:(NSRange)range {

unichar myBuffer[2];
[self getCharacters:myBuffer range:range];
NSString *ver = [NSString stringWithCharacters:myBuffer length:2];
if([ver hasPrefix:@"0"]){
return [ver one_substringAfter:@"0"];
}
else{
return ver;
}
}

- (NSString*)one_getSemanticVersion {

NSMutableString *tmpstr = [[NSMutableString alloc] initWithCapacity:5];

for ( int i = 0; i <=4; i+=2 ){
[tmpstr appendString:[self one_getVersionForRange:NSMakeRange(i, 2)]];
if (i != 4)[tmpstr appendString:@"."];
}

return (NSString*)tmpstr;
}


@end
@@ -230,6 +230,8 @@ typedef NS_ENUM(NSUInteger, ONE_S_LOG_LEVEL) {
+ (id)initWithLaunchOptions:(NSDictionary*)launchOptions appId:(NSString*)appId handleNotificationReceived:(OSHandleNotificationReceivedBlock)receivedCallback handleNotificationAction:(OSHandleNotificationActionBlock)actionCallback settings:(NSDictionary*)settings;

+ (NSString*)app_id;
+ (NSString*)sdk_version_raw;
+ (NSString*)sdk_semantic_version;

// Only use if you passed FALSE to autoRegister
+ (void)registerForPushNotifications;
@@ -38,6 +38,7 @@
#import "UNUserNotificationCenter+OneSignal.h"
#import "OneSignalSelectorHelpers.h"
#import "UIApplicationDelegate+OneSignal.h"
#import "NSString+OneSignal.h"

#import "OneSignalNotificationSettings.h"
#import "OneSignalNotificationSettingsIOS10.h"
@@ -169,6 +170,22 @@ + (NSString*)app_id {
return app_id;
}

+ (NSString*)sdk_version_raw {
return ONESIGNAL_VERSION;
}

+ (NSString*)sdk_semantic_version {

// examples:
// ONESIGNAL_VERSION = @"020402" returns 2.4.2
// ONESIGNAL_VERSION = @"001000" returns 0.10.0
// so that's 6 digits, where the first two are the major version
// the second two are the minor version and that last two, the patch.
// c.f. http://semver.org/

return [ONESIGNAL_VERSION one_getSemanticVersion];
}

+ (NSString*)mUserId {
return mUserId;
}
@@ -23,6 +23,7 @@
#import "OneSignalHelper.h"
#import "OneSignalTracker.h"
#import "OneSignalSelectorHelpers.h"
#import "NSString+OneSignal.h"

#include <pthread.h>
#include <mach/mach.h>
@@ -561,7 +562,8 @@ - (void)setUp {
lastSetCategories = nil;

preferredLanguagesArray = @[@"en-US"];



[OneSignalHelper performSelector:NSSelectorFromString(@"resetLocals")];


@@ -727,6 +729,98 @@ - (void)testBasicInitTest {
XCTAssertEqual(networkRequestCount, 1);
}

- (void)testVersionStringLength{

[self initOneSignal];
[self runBackgroundThreads];

XCTAssertEqual(ONESIGNAL_VERSION.length, 6, @"ONESIGNAL_VERSION length is not 6: length is %lu", (unsigned long)ONESIGNAL_VERSION.length);
XCTAssertEqual([OneSignal sdk_version_raw].length, 6, @"OneSignal sdk_version_raw length is not 6: length is %lu", (unsigned long)[OneSignal sdk_version_raw].length);


}

- (void)testSymanticVersioning {

[self initOneSignal];
[self runBackgroundThreads];

NSDictionary *versions = @{
@"011001" : @"1.10.1",
@"011006" : @"1.10.6",
@"011100" : @"1.11.0",
@"011101" : @"1.11.1",
@"011103" : @"1.11.3",
@"011200" : @"1.12.0",
@"011201" : @"1.12.1",
@"011202" : @"1.12.2",
@"011300" : @"1.13.0",
@"011301" : @"1.13.1",
@"011302" : @"1.13.2",
@"011303" : @"1.13.3",
@"020000" : @"2.0.0",
@"020001" : @"2.0.1",
@"020002" : @"2.0.2",
@"020004" : @"2.0.4",
@"020005" : @"2.0.5",
@"020007" : @"2.0.7",
@"020008" : @"2.0.8",
@"020009" : @"2.0.9",
@"020010" : @"2.0.10",
@"020011" : @"2.0.11",
@"020012" : @"2.0.12",
@"020100" : @"2.1.0",
@"020101" : @"2.1.1",
@"020102" : @"2.1.2",
@"020103" : @"2.1.3",
@"020104" : @"2.1.4",
@"020105" : @"2.1.5",
@"020106" : @"2.1.6",
@"020107" : @"2.1.7",
@"020109" : @"2.1.9",
@"020110" : @"2.1.10",
@"020111" : @"2.1.11",
@"020112" : @"2.1.12",
@"020114" : @"2.1.14",
@"020115" : @"2.1.15",
@"020116" : @"2.1.16",
@"020200" : @"2.2.0",
@"020201" : @"2.2.1",
@"020202" : @"2.2.2",
@"020203" : @"2.2.3",
@"020300" : @"2.3.0",
@"020301" : @"2.3.1",
@"020302" : @"2.3.2",
@"020303" : @"2.3.3",
@"020304" : @"2.3.4",
@"020305" : @"2.3.5",
@"020306" : @"2.3.6",
@"020307" : @"2.3.7",
@"020400" : @"2.4.0",
@"000400" : @"0.4.0",
@"000000" : @"0.0.0",
@"020401" : @"2.4.1",
@"020402" : @"2.4.2"};


[versions enumerateKeysAndObjectsUsingBlock:^(NSString* raw, NSString* semantic, BOOL* stop) {
XCTAssertEqualObjects([raw one_getSemanticVersion], semantic, @"Strings are not equal %@ %@", semantic, [raw one_getSemanticVersion] );
}];

NSDictionary *versionsThatFail = @{
@"011001" : @"1.0.1",
@"011086" : @"1.10.6",
@"011140" : @"1.11.0",
@"011106" : @"1.11.1",
@"091103" : @"1.11.3"};


[versionsThatFail enumerateKeysAndObjectsUsingBlock:^(NSString* raw, NSString* semantic, BOOL* stop) {
XCTAssertNotEqualObjects([raw one_getSemanticVersion], semantic, @"Strings are equal %@ %@", semantic, [raw one_getSemanticVersion] );
}];

}

- (void)testRegisterationOniOS7 {
mockIOSVersion = 7;

0 comments on commit 4c1fb7b

Please sign in to comment.
You can’t perform that action at this time.