ConfigValueOrigTests.swift 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // Copyright 2025 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. @testable import FirebaseRemoteConfig
  15. import XCTest
  16. class RemoteConfigValueOrigTests: XCTestCase {
  17. func testConfigValueWithDifferentValueTypes() throws {
  18. let valueA = "0.33333"
  19. let dataA = try XCTUnwrap(valueA.data(using: .utf8))
  20. let configValueA = RemoteConfigValue(data: dataA, source: .remote)
  21. XCTAssertEqual(configValueA.stringValue, valueA)
  22. XCTAssertEqual(configValueA.dataValue, dataA)
  23. XCTAssertEqual(
  24. configValueA.numberValue,
  25. NSNumber(floatLiteral: 0.33333)
  26. )
  27. XCTAssertEqual(configValueA.boolValue, (valueA as NSString).boolValue)
  28. let valueB = "NO"
  29. let dataB = try XCTUnwrap(valueB.data(using: .utf8))
  30. let configValueB = RemoteConfigValue(data: dataB, source: .remote)
  31. XCTAssertEqual(configValueB.boolValue, (valueB as NSString).boolValue)
  32. // Test JSON value.
  33. let jsonDictionary = ["key1": "value1"]
  34. let jsonArray = [["key1": "value1"], ["key2": "value2"]]
  35. let jsonData = try JSONSerialization.data(withJSONObject: jsonDictionary, options: [])
  36. let configValueC = RemoteConfigValue(data: jsonData, source: .remote)
  37. XCTAssertEqual(configValueC.jsonValue as? [String: String], jsonDictionary)
  38. let jsonArrayData = try JSONSerialization.data(withJSONObject: jsonArray, options: [])
  39. let configValueD = RemoteConfigValue(data: jsonArrayData, source: .remote)
  40. XCTAssertEqual(configValueD.jsonValue as? [[String: String]], jsonArray)
  41. }
  42. func testConfigValueToNumber() throws {
  43. let strValue1 = "0.33"
  44. let data1 = try XCTUnwrap(strValue1.data(using: .utf8))
  45. let value1 = RemoteConfigValue(data: data1, source: .remote)
  46. XCTAssertEqual(value1.numberValue.floatValue, Float(strValue1)!)
  47. let strValue2 = "3.14159265358979"
  48. let data2 = try XCTUnwrap(strValue2.data(using: .utf8))
  49. let value2 = RemoteConfigValue(data: data2, source: .remote)
  50. XCTAssertEqual(value2.numberValue.doubleValue, Double(strValue2)!)
  51. let strValue3 = "1000000000"
  52. let data3 = try XCTUnwrap(strValue3.data(using: .utf8))
  53. let value3 = RemoteConfigValue(data: data3, source: .remote)
  54. XCTAssertEqual(value3.numberValue.intValue, Int(strValue3)!)
  55. let strValue4 = "1000000000123"
  56. let data4 = try XCTUnwrap(strValue4.data(using: .utf8))
  57. let value4 = RemoteConfigValue(data: data4, source: .remote)
  58. XCTAssertEqual(value4.numberValue.int64Value, Int64(strValue4)!)
  59. }
  60. }