Skip to content

Commit 466ce13

Browse files
yasirfolio3jaeopt
authored andcommitted
Code coverage improvement. (#241)
* Fixes to improve code-coverage. * fixes according to review.
1 parent d368e03 commit 466ce13

13 files changed

+65
-104
lines changed

OptimizelySDK/OptimizelyTests/OptimizelyTests-APIs/OptimizelyClientTests_DatafileHandler.swift

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,9 @@ class OptimizelyClientTests_DatafileHandler: XCTestCase {
6464
let expectation = XCTestExpectation(description: "get datafile from cache")
6565

6666
client.start() { (result) in
67-
switch result {
68-
case .success(let data):
67+
if case let .success(data) = result{
6968
XCTAssert(!data.isEmpty)
7069
expectation.fulfill()
71-
case .failure:
72-
XCTAssert(false)
7370
}
7471
}
7572

OptimizelySDK/OptimizelyTests/OptimizelyTests-APIs/OptimizelyClientTests_ObjcAPIs.m

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,8 +215,16 @@ - (void)testCustomizationAPIs {
215215
NSData *datafileData = [datafile dataUsingEncoding:NSUTF8StringEncoding];
216216

217217
TestOPTLogger *logger = [[TestOPTLogger alloc] init];
218+
XCTAssertEqual(TestOPTLogger.logLevel, OptimizelyLogLevelInfo);
219+
218220
TestOPTEventDispatcher *eventDispatcher = [[TestOPTEventDispatcher alloc] init];
221+
EventForDispatch *event = [[EventForDispatch alloc] initWithUrl:nil body:[[NSData alloc] init]];
222+
[eventDispatcher dispatchEventWithEvent:event completionHandler:nil];
223+
[eventDispatcher flushEvents];
224+
219225
TestOPTUserProfileService *userProfileService = [[TestOPTUserProfileService alloc] init];
226+
(void)[userProfileService lookupWithUserId:@"dummy"];
227+
[userProfileService saveWithUserProfile:@{}];
220228

221229
// check all SDK initialization APIs for ObjC
222230
self.optimizely = [[OptimizelyClient alloc] initWithSdkKey:kSdkKey];
@@ -228,7 +236,7 @@ - (void)testCustomizationAPIs {
228236
periodicDownloadInterval:@(50)
229237
defaultLogLevel:OptimizelyLogLevelInfo];
230238

231-
[self.optimizely startWithCompletion:^(NSData * _Nullable data, NSError * _Nullable error) {}];
239+
[self.optimizely startWithCompletion:nil];
232240

233241
[self.optimizely startWithDatafile:datafile error:nil];
234242

OptimizelySDK/OptimizelyTests/OptimizelyTests-APIs/OptimizelyClientTests_ObjcOthers.m

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -226,11 +226,7 @@ - (void)testCustomEventDispatcher_DefaultEventDispatcher {
226226

227227
__block BOOL status = false;
228228
[eventDispatcher dispatchEventWithEvent:event completionHandler:^(NSData * data, NSError * error) {
229-
if(data != nil) {
230-
status = true;
231-
} else {
232-
status = false;
233-
}
229+
status = (data != nil);
234230

235231
[expectation fulfill];
236232
}];
@@ -251,6 +247,7 @@ - (void)testCustomEventDispatcher {
251247

252248
// check DefaultEventDispatcher work OK with ObjC clients
253249
MockOPTEventDispatcher *customEventDispatcher = [[MockOPTEventDispatcher alloc] init];
250+
[customEventDispatcher flushEvents];
254251

255252
OptimizelyClient *optimizely = [[OptimizelyClient alloc] initWithSdkKey:[self randomSdkKey]
256253
logger:nil

OptimizelySDK/OptimizelyTests/OptimizelyTests-APIs/OptimizelyClientTests_Others.swift

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -227,21 +227,18 @@ class OptimizelyClientTests_Others: XCTestCase {
227227
let optimizely = OptimizelyClient(sdkKey: kNotRealSdkKey)
228228

229229
let exp = expectation(description: "a")
230-
230+
var failureOccured = false
231231
optimizely.start { result in
232-
switch result {
233-
case .success:
234-
XCTAssert(false)
235-
case .failure(OptimizelyError.dataFileVersionInvalid):
236-
XCTAssert(true)
237-
case .failure(let error):
238-
XCTAssert(false, "wrong error type: \(error)")
232+
233+
if case .failure(OptimizelyError.dataFileVersionInvalid) = result {
234+
failureOccured = true
239235
}
240236

241237
exp.fulfill()
242238
}
243239

244240
wait(for: [exp], timeout: 10)
241+
XCTAssertTrue(failureOccured)
245242
}
246243

247244
func testHandlerReinitializeOnBackgroundDatafileUpdate() {

OptimizelySDK/OptimizelyTests/OptimizelyTests-Common/BatchEventBuilderTest.swift

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,7 @@ class BatchEventBuilderTests: XCTestCase {
2929
optimizely = OptimizelyClient(sdkKey: "", periodicDownloadInterval: 0)
3030

3131
let datafile = OTUtils.loadJSONDatafile(datafileName)
32-
do {
33-
try optimizely?.start(datafile: datafile!)
34-
} catch {
35-
print(error)
36-
XCTAssert(false)
37-
}
32+
XCTAssertNoThrow(try optimizely?.start(datafile: datafile!))
3833
}
3934

4035
override func tearDown() {

OptimizelySDK/OptimizelyTests/OptimizelyTests-Common/BucketTests_BucketVariation.swift

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -132,11 +132,7 @@ extension BucketTests_BucketVariation {
132132

133133
for (idx, test) in tests.enumerated() {
134134
variation = bucketer.bucketExperiment(config: self.config, experiment: experiment, bucketingId: test["userId"]!)
135-
if let _ = test["expect"] {
136-
XCTAssertEqual(test["expect"], variation?.key, "test[\(idx)] failed")
137-
} else {
138-
XCTAssertNil(experiment)
139-
}
135+
XCTAssertEqual(test["expect"], variation?.key, "test[\(idx)] failed")
140136
}
141137
}
142138

@@ -154,11 +150,7 @@ extension BucketTests_BucketVariation {
154150

155151
for (idx, test) in tests.enumerated() {
156152
variation = bucketer.bucketExperiment(config: self.config, experiment: experiment, bucketingId: test["userId"]!)
157-
if let _ = test["expect"] {
158-
XCTAssertEqual(test["expect"], variation?.key, "test[\(idx)] failed")
159-
} else {
160-
XCTAssertNil(experiment)
161-
}
153+
XCTAssertEqual(test["expect"], variation?.key, "test[\(idx)] failed")
162154
}
163155
}
164156

@@ -196,11 +188,7 @@ extension BucketTests_BucketVariation {
196188

197189
for (idx, test) in tests.enumerated() {
198190
variation = bucketer.bucketExperiment(config: self.config, experiment: experiment, bucketingId: test["userId"]!)
199-
if let _ = test["expect"] {
200-
XCTAssertEqual(test["expect"], variation?.key, "test[\(idx)] failed")
201-
} else {
202-
XCTAssertNil(experiment)
203-
}
191+
XCTAssertEqual(test["expect"], variation?.key, "test[\(idx)] failed")
204192
}
205193
}
206194

OptimizelySDK/OptimizelyTests/OptimizelyTests-Common/BucketTests_Others.swift

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,7 @@ class BucketTests_Others: XCTestCase {
2626
override func setUp() {
2727
// Put setup code here. This method is called before the invocation of each test method in the class.
2828
let data = OTUtils.loadJSONDatafile("grouped_experiments")
29-
do {
30-
config = try ProjectConfig(datafile: data!)
31-
} catch {
32-
print(error.localizedDescription)
33-
}
29+
config = try? ProjectConfig(datafile: data!)
3430
}
3531

3632
override func tearDown() {
@@ -84,11 +80,7 @@ extension BucketTests_Others {
8480

8581
for test in tests {
8682
let experiment = bucketer.bucketToExperiment(config: config!, group: group!, bucketingId: test["userId"]!)
87-
if let _ = test["expect"] {
88-
XCTAssertEqual(test["expect"]!, experiment?.key)
89-
} else {
90-
XCTAssertNil(experiment)
91-
}
83+
XCTAssertEqual(test["expect"]!, experiment?.key)
9284
}
9385
}
9486

OptimizelySDK/OptimizelyTests/OptimizelyTests-Common/DatafileHandlerTests.swift

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -108,12 +108,10 @@ class DatafileHandlerTests: XCTestCase {
108108
// initiate download task which should pass back a 304 but still return non nil
109109
// since the datafile was not in cache.
110110
handler.downloadDatafile(sdkKey: "localcdnTestSDKKey") { (result) in
111-
switch result {
112-
case .success(let data):
111+
112+
if case let .success(data) = result {
113113
XCTAssert(data != nil)
114114
expectation.fulfill()
115-
case .failure(let error):
116-
XCTAssert(error.errorDescription != nil)
117115
}
118116
}
119117

@@ -155,13 +153,10 @@ class DatafileHandlerTests: XCTestCase {
155153
// initiate download task which should pass back a 304 but still return non nil
156154
// since the datafile was not in cache.
157155
handler.downloadDatafile(sdkKey: "localcdnTestSDKKey") { (result) in
158-
switch result {
159-
case .success(let data):
156+
if case let .success(data) = result {
160157
// should come back as nil since got 304 and datafile in cache.
161158
XCTAssert(data == nil)
162159
expectation.fulfill()
163-
case .failure(let error):
164-
XCTAssert(error.errorDescription != nil)
165160
}
166161
}
167162

@@ -236,18 +231,16 @@ class DatafileHandlerTests: XCTestCase {
236231

237232
let expectation = XCTestExpectation(description: "should fail before 10")
238233
handler.downloadDatafile(sdkKey: "invalidKey1212121", resourceTimeoutInterval: 3) { (result) in
239-
switch result {
240-
case .failure(let error):
234+
235+
if case let .failure(error) = result {
241236
print(error)
242237
XCTAssert(true)
243238
expectation.fulfill()
244-
case .success(_):
245-
XCTAssert(false)
246239
}
247240
}
248241

249242
wait(for: [expectation], timeout: 5.0)
250-
243+
251244
}
252245

253246
func testDownloadWithoutTimeout() {
@@ -256,11 +249,8 @@ class DatafileHandlerTests: XCTestCase {
256249

257250
let expectation = XCTestExpectation(description: "will wait for response.")
258251
handler.downloadDatafile(sdkKey: "invalidKeyXXXXX") { (result) in
259-
switch result {
260-
case .failure(let error):
261-
print(error)
262-
XCTAssert(false)
263-
case .success(let data):
252+
253+
if case let .success(data) = result {
264254
print(data ?? "")
265255
XCTAssert(true)
266256
expectation.fulfill()

OptimizelySDK/OptimizelyTests/OptimizelyTests-DataModel/AttributeValueTests.swift

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -340,43 +340,46 @@ extension AttributeValueTests {
340340

341341
func testIsExactMatchWithInvalid() {
342342
let attr = AttributeValue.string("string")
343+
var tmpError: Error?
343344
do {
344345
_ = try attr.isExactMatch(with: ["invalidType"])
345-
XCTAssert(false)
346346
} catch {
347-
XCTAssert(true)
347+
tmpError = error
348348
}
349+
XCTAssertTrue(tmpError != nil)
349350
}
350351

351352
func testIsGreaterWithInvalid() {
352353
let attr = AttributeValue.string("string")
354+
var tmpError: Error?
353355
do {
354356
_ = try attr.isSubstring(of: ["invalidType"])
355-
XCTAssert(false)
356357
} catch {
357-
XCTAssert(true)
358+
tmpError = error
358359
}
360+
XCTAssertTrue(tmpError != nil)
359361
}
360362

361363
func testIsLessWithInvalid() {
362364
let attr = AttributeValue.double(100.23)
365+
var tmpError: Error?
363366
do {
364367
_ = try attr.isGreater(than: ["invalidType"])
365-
XCTAssert(false)
366368
} catch {
367-
XCTAssert(true)
369+
tmpError = error
368370
}
371+
XCTAssertTrue(tmpError != nil)
369372
}
370373

371374
func testIsSubstringWithInvalid() {
372375
let attr = AttributeValue.double(100.23)
376+
var tmpError: Error?
373377
do {
374378
_ = try attr.isLess(than: ["invalidType"])
375-
XCTAssert(false)
376379
} catch {
377-
XCTAssert(true)
380+
tmpError = error
378381
}
382+
XCTAssertTrue(tmpError != nil)
379383
}
380384

381-
382385
}

OptimizelySDK/OptimizelyTests/OptimizelyTests-DataModel/ConditionHolderTests.swift

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -292,23 +292,15 @@ extension ConditionHolderTests {
292292
extension ConditionHolderTests {
293293

294294
func testDecode_Invalid() {
295-
do {
296-
let _: ConditionHolder = try OTUtils.model(from: [120])
297-
XCTAssert(false)
298-
} catch {
299-
XCTAssert(true)
300-
}
295+
let result: ConditionHolder? = try? OTUtils.model(from: [120])
296+
XCTAssertTrue(result == nil)
301297
}
302298

303299
func testOperaterOnEmptyConditionArray() {
304300
let array: [ConditionHolder] = []
305301

306-
do {
307-
_ = try array.evaluate(op: .and, project: nil, attributes: nil)
308-
XCTAssert(false, "evaluate on empty array must throw exception")
309-
} catch {
310-
XCTAssert(true)
311-
}
302+
let result = try? array.evaluate(op: .and, project: nil, attributes: nil)
303+
XCTAssertTrue(result == nil)
312304
}
313305

314306
}

0 commit comments

Comments
 (0)