|
59 | 59 | static NSString *const kDeviceOSKey = @"device_os"; |
60 | 60 | static NSString *const kEMMPasscodeInfoKey = @"emm_passcode_info"; |
61 | 61 |
|
| 62 | +@interface GIDEMMSupport (Private) |
| 63 | ++ (NSDictionary<NSString *, NSString *> *) |
| 64 | + dictionaryWithStringValuesFromDictionary:(NSDictionary *)originalDictionary; |
| 65 | +@end |
| 66 | + |
62 | 67 | @interface GIDEMMSupportTest : XCTestCase |
63 | 68 | // The view controller that has been presented, if any. |
64 | 69 | @property(nonatomic, strong, nullable) UIViewController *presentedViewController; |
@@ -274,6 +279,138 @@ - (void)testHandleTokenFetchEMMError_errorIsNotEMM { |
274 | 279 | [self waitForExpectations:@[ called ] timeout:1]; |
275 | 280 | } |
276 | 281 |
|
| 282 | +# pragma mark - String Conversion Tests |
| 283 | + |
| 284 | +- (void)testStringConversion_withAnyNumber_isConvertedToString { |
| 285 | + NSDictionary *inputDictionary = @{ @"number_key": @12345 }; |
| 286 | + |
| 287 | + NSDictionary *resultDictionary = [GIDEMMSupport |
| 288 | + dictionaryWithStringValuesFromDictionary:inputDictionary]; |
| 289 | + |
| 290 | + XCTAssertTrue([resultDictionary[@"number_key"] isKindOfClass:[NSString class]]); |
| 291 | + XCTAssertEqualObjects(resultDictionary[@"number_key"], @"12345", |
| 292 | + @"The NSNumber should be converted to a string."); |
| 293 | +} |
| 294 | + |
| 295 | +- (void)testStringConversion_withNumberOne_isConvertedToString { |
| 296 | + NSDictionary *inputDictionary = @{ @"number_key": @1 }; |
| 297 | + |
| 298 | + NSDictionary *resultDictionary = [GIDEMMSupport |
| 299 | + dictionaryWithStringValuesFromDictionary:inputDictionary]; |
| 300 | + |
| 301 | + XCTAssertTrue([resultDictionary[@"number_key"] isKindOfClass:[NSString class]]); |
| 302 | + XCTAssertEqualObjects(resultDictionary[@"number_key"], @"1", |
| 303 | + @"The NSNumber should be converted to a string."); |
| 304 | +} |
| 305 | + |
| 306 | +- (void)testStringConversion_withNumberZero_isConvertedToString { |
| 307 | + NSDictionary *inputDictionary = @{ @"number_key": @0}; |
| 308 | + |
| 309 | + NSDictionary *resultDictionary = [GIDEMMSupport |
| 310 | + dictionaryWithStringValuesFromDictionary:inputDictionary]; |
| 311 | + |
| 312 | + XCTAssertTrue([resultDictionary[@"number_key"] isKindOfClass:[NSString class]]); |
| 313 | + XCTAssertEqualObjects(resultDictionary[@"number_key"], @"0", |
| 314 | + @"The NSNumber should be converted to a string."); |
| 315 | +} |
| 316 | + |
| 317 | +- (void)testStringConversion_withBooleanYes_isConvertedToTrueString { |
| 318 | + NSDictionary *inputDictionary = @{ @"bool_key": @YES }; |
| 319 | + |
| 320 | + NSDictionary *resultDictionary = [GIDEMMSupport |
| 321 | + dictionaryWithStringValuesFromDictionary:inputDictionary]; |
| 322 | + |
| 323 | + XCTAssertTrue([resultDictionary[@"bool_key"] isKindOfClass:[NSString class]], |
| 324 | + @"The value should be an NSString."); |
| 325 | + XCTAssertEqualObjects(resultDictionary[@"bool_key"], @"true", |
| 326 | + @"The boolean YES should be converted to the string 'true'."); |
| 327 | +} |
| 328 | + |
| 329 | +- (void)testStringConversion_withBooleanNo_isConvertedToFalseString { |
| 330 | + NSDictionary *inputDictionary = @{ @"bool_key": @NO }; |
| 331 | + |
| 332 | + NSDictionary *resultDictionary = [GIDEMMSupport |
| 333 | + dictionaryWithStringValuesFromDictionary:inputDictionary]; |
| 334 | + |
| 335 | + XCTAssertTrue([resultDictionary[@"bool_key"] isKindOfClass:[NSString class]], |
| 336 | + @"The value should be an NSString."); |
| 337 | + XCTAssertEqualObjects(resultDictionary[@"bool_key"], @"false", |
| 338 | + @"The boolean NO should be converted to the string 'false'."); |
| 339 | +} |
| 340 | + |
| 341 | +- (void)testStringConversion_withString_remainsUnchanged { |
| 342 | + NSDictionary *inputDictionary = @{ @"string_key": @"hello" }; |
| 343 | + |
| 344 | + NSDictionary *resultDictionary = [GIDEMMSupport |
| 345 | + dictionaryWithStringValuesFromDictionary:inputDictionary]; |
| 346 | + |
| 347 | + XCTAssertTrue([resultDictionary[@"string_key"] isKindOfClass:[NSString class]], |
| 348 | + @"The value should still be an NSString."); |
| 349 | + XCTAssertEqualObjects(resultDictionary[@"string_key"], @"hello", |
| 350 | + @"The original string value should be preserved."); |
| 351 | +} |
| 352 | + |
| 353 | +- (void)testStringConversion_withArray_isConvertedToJSONString { |
| 354 | + NSDictionary *inputDictionary = @{ |
| 355 | + @"array_key": @[ @1, @"two", @YES ] |
| 356 | + }; |
| 357 | + |
| 358 | + NSDictionary *resultDictionary = [GIDEMMSupport |
| 359 | + dictionaryWithStringValuesFromDictionary:inputDictionary]; |
| 360 | + |
| 361 | + XCTAssertTrue([resultDictionary[@"array_key"] isKindOfClass:[NSString class]], |
| 362 | + @"The value should be an NSString."); |
| 363 | + XCTAssertEqualObjects(resultDictionary[@"array_key"], @"[1,\"two\",true]", |
| 364 | + @"The array should be serialized into a JSON string."); |
| 365 | +} |
| 366 | + |
| 367 | +- (void)testStringConversion_withDictionary_isConvertedToJSONString { |
| 368 | + NSDictionary *valueAsDictionary = @{ @"nested_key": @"nested_value" }; |
| 369 | + NSDictionary *inputDictionary = @{ |
| 370 | + @"dict_key": @{ |
| 371 | + @"nested_key": @"nested_value" |
| 372 | + } |
| 373 | + }; |
| 374 | + |
| 375 | + NSDictionary *resultDictionary = [GIDEMMSupport |
| 376 | + dictionaryWithStringValuesFromDictionary:inputDictionary]; |
| 377 | + |
| 378 | + XCTAssertTrue([resultDictionary[@"dict_key"] isKindOfClass:[NSString class]], |
| 379 | + @"The value should be an NSString."); |
| 380 | + XCTAssertEqualObjects(resultDictionary[@"dict_key"], @"{\"nested_key\":\"nested_value\"}", |
| 381 | + @"The dictionary should be serialized into a JSON string."); |
| 382 | +} |
| 383 | + |
| 384 | +- (void)testStringConversion_withMixedTypes_allAreConverted { |
| 385 | + NSDictionary *inputDictionary = @{ |
| 386 | + @"string_key": @"hello", |
| 387 | + @"number_key": @987, |
| 388 | + @"bool_key": @YES, |
| 389 | + @"array_key": @[ @"a", @NO ], |
| 390 | + }; |
| 391 | + |
| 392 | + NSDictionary *resultDictionary = [GIDEMMSupport |
| 393 | + dictionaryWithStringValuesFromDictionary:inputDictionary]; |
| 394 | + |
| 395 | + XCTAssertTrue([resultDictionary[@"string_key"] isKindOfClass:[NSString class]]); |
| 396 | + XCTAssertEqualObjects(resultDictionary[@"string_key"], @"hello"); |
| 397 | + |
| 398 | + XCTAssertTrue([resultDictionary[@"number_key"] isKindOfClass:[NSString class]]); |
| 399 | + XCTAssertEqualObjects(resultDictionary[@"number_key"], @"987"); |
| 400 | + |
| 401 | + XCTAssertTrue([resultDictionary[@"bool_key"] isKindOfClass:[NSString class]]); |
| 402 | + XCTAssertEqualObjects(resultDictionary[@"bool_key"], @"true"); |
| 403 | + |
| 404 | + XCTAssertTrue([resultDictionary[@"array_key"] isKindOfClass:[NSString class]]); |
| 405 | + XCTAssertEqualObjects(resultDictionary[@"array_key"], @"[\"a\",false]"); |
| 406 | +} |
| 407 | + |
| 408 | +- (void)testStringConversion_withEmptyDictionary_returnsEmptyDictionary { |
| 409 | + NSDictionary *resultDictionary = [GIDEMMSupport dictionaryWithStringValuesFromDictionary:@{}]; |
| 410 | + |
| 411 | + XCTAssertEqual(resultDictionary.count, 0, @"The resulting dictionary should be empty."); |
| 412 | +} |
| 413 | + |
277 | 414 | # pragma mark - Helpers |
278 | 415 |
|
279 | 416 | - (NSString *)systemVersion { |
|
0 commit comments