RCNConfigValueTest.m 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * Copyright 2019 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import <XCTest/XCTest.h>
  17. #import "FirebaseRemoteConfig/Sources/RCNConfigValue_Internal.h"
  18. @interface FIRRemoteConfigValueTest : XCTestCase
  19. @end
  20. @implementation FIRRemoteConfigValueTest
  21. - (void)testConfigValueWithDifferentValueTypes {
  22. NSString *valueA = @"0.33333";
  23. NSData *dataA = [valueA dataUsingEncoding:NSUTF8StringEncoding];
  24. FIRRemoteConfigValue *configValueA =
  25. [[FIRRemoteConfigValue alloc] initWithData:dataA source:FIRRemoteConfigSourceRemote];
  26. XCTAssertEqualObjects(configValueA.stringValue, valueA);
  27. XCTAssertEqualObjects(configValueA.dataValue, dataA);
  28. XCTAssertEqualObjects(configValueA.numberValue, configValueA.numberValue);
  29. XCTAssertEqual(configValueA.boolValue, valueA.boolValue);
  30. NSString *valueB = @"NO";
  31. FIRRemoteConfigValue *configValueB =
  32. [[FIRRemoteConfigValue alloc] initWithData:[valueB dataUsingEncoding:NSUTF8StringEncoding]
  33. source:FIRRemoteConfigSourceRemote];
  34. XCTAssertEqual(configValueB.boolValue, valueB.boolValue);
  35. // Test JSON value.
  36. NSDictionary<NSString *, NSString *> *JSONDictionary = @{@"key1" : @"value1"};
  37. NSArray<NSDictionary<NSString *, NSString *> *> *JSONArray =
  38. @[ @{@"key1" : @"value1"}, @{@"key2" : @"value2"} ];
  39. NSError *error;
  40. NSData *JSONData = [NSJSONSerialization dataWithJSONObject:JSONDictionary options:0 error:&error];
  41. FIRRemoteConfigValue *configValueC =
  42. [[FIRRemoteConfigValue alloc] initWithData:JSONData source:FIRRemoteConfigSourceRemote];
  43. XCTAssertEqualObjects(configValueC.JSONValue, JSONDictionary);
  44. NSData *JSONArrayData = [NSJSONSerialization dataWithJSONObject:JSONArray options:0 error:&error];
  45. FIRRemoteConfigValue *configValueD =
  46. [[FIRRemoteConfigValue alloc] initWithData:JSONArrayData source:FIRRemoteConfigSourceRemote];
  47. XCTAssertEqualObjects(configValueD.JSONValue, JSONArray);
  48. }
  49. - (void)testFIRRemoteConfigValueToNumber {
  50. FIRRemoteConfigValue *value;
  51. NSString *strValue = @"not a number";
  52. NSData *data = [strValue dataUsingEncoding:NSUTF8StringEncoding];
  53. value = [[FIRRemoteConfigValue alloc] initWithData:data source:FIRRemoteConfigSourceRemote];
  54. XCTAssertNil(value.numberValue);
  55. data = nil;
  56. value = [[FIRRemoteConfigValue alloc] initWithData:data source:FIRRemoteConfigSourceRemote];
  57. XCTAssertNil(value.numberValue);
  58. strValue = @"0.33";
  59. data = [strValue dataUsingEncoding:NSUTF8StringEncoding];
  60. value = [[FIRRemoteConfigValue alloc] initWithData:data source:FIRRemoteConfigSourceRemote];
  61. XCTAssertEqual(value.numberValue.floatValue, strValue.floatValue);
  62. strValue = @"3.14159265358979";
  63. data = [strValue dataUsingEncoding:NSUTF8StringEncoding];
  64. value = [[FIRRemoteConfigValue alloc] initWithData:data source:FIRRemoteConfigSourceRemote];
  65. XCTAssertEqual(value.numberValue.doubleValue, strValue.doubleValue);
  66. strValue = @"1000000000";
  67. data = [strValue dataUsingEncoding:NSUTF8StringEncoding];
  68. value = [[FIRRemoteConfigValue alloc] initWithData:data source:FIRRemoteConfigSourceRemote];
  69. XCTAssertEqual(value.numberValue.intValue, strValue.intValue);
  70. strValue = @"1000000000123";
  71. data = [strValue dataUsingEncoding:NSUTF8StringEncoding];
  72. value = [[FIRRemoteConfigValue alloc] initWithData:data source:FIRRemoteConfigSourceRemote];
  73. XCTAssertEqual(value.numberValue.longLongValue, strValue.longLongValue);
  74. }
  75. @end