Value.swift 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. // Copyright 2021 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. import FirebaseRemoteConfig
  15. import XCTest
  16. @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
  17. class ValueTests: APITestBase {
  18. func testFetchAndActivateAllTypes() async throws {
  19. let status = try await config.fetchAndActivate()
  20. XCTAssertEqual(status, .successFetchedFromRemote)
  21. XCTAssertEqual(config[Constants.stringKey].stringValue, Constants.stringValue)
  22. XCTAssertEqual(config[Constants.intKey].numberValue.intValue, Constants.intValue)
  23. XCTAssertEqual(config[Constants.intKey].numberValue.int8Value, Int8(Constants.intValue))
  24. XCTAssertEqual(config[Constants.intKey].numberValue.int16Value, Int16(Constants.intValue))
  25. XCTAssertEqual(config[Constants.intKey].numberValue.int32Value, Int32(Constants.intValue))
  26. XCTAssertEqual(config[Constants.intKey].numberValue.int64Value, Int64(Constants.intValue))
  27. XCTAssertEqual(config[Constants.intKey].numberValue.uintValue, UInt(Constants.intValue))
  28. XCTAssertEqual(config[Constants.intKey].numberValue.uint8Value, UInt8(Constants.intValue))
  29. XCTAssertEqual(config[Constants.intKey].numberValue.uint16Value, UInt16(Constants.intValue))
  30. XCTAssertEqual(config[Constants.intKey].numberValue.uint32Value, UInt32(Constants.intValue))
  31. XCTAssertEqual(config[Constants.intKey].numberValue.uint64Value, UInt64(Constants.intValue))
  32. XCTAssertEqual(
  33. config[Constants.floatKey].numberValue.decimalValue,
  34. Decimal(Constants.doubleValue)
  35. )
  36. XCTAssertEqual(config[Constants.floatKey].numberValue.floatValue, Constants.floatValue)
  37. XCTAssertEqual(config[Constants.floatKey].numberValue.doubleValue, Constants.doubleValue)
  38. XCTAssertEqual(config[Constants.trueKey].boolValue, true)
  39. XCTAssertEqual(config[Constants.falseKey].boolValue, false)
  40. XCTAssertEqual(
  41. config[Constants.stringKey].dataValue,
  42. Constants.stringValue.data(using: .utf8)
  43. )
  44. XCTAssertEqual(
  45. config[Constants.jsonKey].jsonValue as! [String: AnyHashable],
  46. Constants.jsonValue
  47. )
  48. }
  49. func testStrongTypingViaSubscriptApi() async throws {
  50. let status = try await config.fetchAndActivate()
  51. XCTAssertEqual(status, .successFetchedFromRemote)
  52. XCTAssertEqual(config[decodedValue: Constants.stringKey], Constants.stringValue)
  53. XCTAssertEqual(config[decodedValue: Constants.intKey], Constants.intValue)
  54. XCTAssertEqual(config[decodedValue: Constants.floatKey], Constants.floatValue)
  55. XCTAssertEqual(config[decodedValue: Constants.floatKey], Constants.doubleValue)
  56. XCTAssertEqual(config[decodedValue: Constants.trueKey], true)
  57. XCTAssertEqual(config[decodedValue: Constants.falseKey], false)
  58. XCTAssertEqual(
  59. config[decodedValue: Constants.stringKey],
  60. Constants.stringValue.data(using: .utf8)
  61. )
  62. XCTAssertEqual(try XCTUnwrap(config[jsonValue: Constants.jsonKey]), Constants.jsonValue)
  63. }
  64. func testStrongTypingViaDecoder() async throws {
  65. let status = try await config.fetchAndActivate()
  66. XCTAssertEqual(status, .successFetchedFromRemote)
  67. XCTAssertEqual(
  68. try config[Constants.stringKey].decoded(asType: String.self),
  69. Constants.stringValue
  70. )
  71. XCTAssertEqual(try config[Constants.intKey].decoded(asType: Int.self), Constants.intValue)
  72. XCTAssertEqual(
  73. try config[Constants.intKey].decoded(asType: Int8.self),
  74. Int8(Constants.intValue)
  75. )
  76. XCTAssertEqual(
  77. try config[Constants.intKey].decoded(asType: Int16.self),
  78. Int16(Constants.intValue)
  79. )
  80. XCTAssertEqual(
  81. try config[Constants.intKey].decoded(asType: Int32.self),
  82. Int32(Constants.intValue)
  83. )
  84. XCTAssertEqual(
  85. try config[Constants.intKey].decoded(asType: Int64.self),
  86. Int64(Constants.intValue)
  87. )
  88. XCTAssertEqual(
  89. try config[Constants.intKey].decoded(asType: UInt.self),
  90. UInt(Constants.intValue)
  91. )
  92. XCTAssertEqual(
  93. try config[Constants.intKey].decoded(asType: UInt8.self),
  94. UInt8(Constants.intValue)
  95. )
  96. XCTAssertEqual(
  97. try config[Constants.intKey].decoded(asType: UInt16.self),
  98. UInt16(Constants.intValue)
  99. )
  100. XCTAssertEqual(
  101. try config[Constants.intKey].decoded(asType: UInt32.self),
  102. UInt32(Constants.intValue)
  103. )
  104. XCTAssertEqual(
  105. try config[Constants.intKey].decoded(asType: UInt64.self),
  106. UInt64(Constants.intValue)
  107. )
  108. XCTAssertEqual(
  109. try config[Constants.floatKey].decoded(asType: Decimal.self),
  110. Decimal(Constants.doubleValue)
  111. )
  112. XCTAssertEqual(
  113. try config[Constants.floatKey].decoded(asType: Float.self),
  114. Constants.floatValue
  115. )
  116. XCTAssertEqual(
  117. try config[Constants.floatKey].decoded(asType: Double.self),
  118. Constants.doubleValue
  119. )
  120. XCTAssertEqual(try config[Constants.trueKey].decoded(asType: Bool.self), true)
  121. XCTAssertEqual(try config[Constants.falseKey].decoded(asType: Bool.self), false)
  122. XCTAssertEqual(
  123. try config[Constants.stringKey].decoded(asType: Data.self),
  124. Constants.stringValue.data(using: .utf8)
  125. )
  126. }
  127. func testStrongTypingViaDecoderAlternateDecoderApi() async throws {
  128. let status = try await config.fetchAndActivate()
  129. XCTAssertEqual(status, .successFetchedFromRemote)
  130. let myString: String = try config[Constants.stringKey].decoded()
  131. XCTAssertEqual(myString, Constants.stringValue)
  132. let myInt: Int = try config[Constants.intKey].decoded()
  133. XCTAssertEqual(myInt, Constants.intValue)
  134. let myInt8: Int8 = try config[Constants.intKey].decoded()
  135. XCTAssertEqual(myInt8, Int8(Constants.intValue))
  136. let myInt16: Int16 = try config[Constants.intKey].decoded()
  137. XCTAssertEqual(myInt16, Int16(Constants.intValue))
  138. let myInt32: Int32 = try config[Constants.intKey].decoded()
  139. XCTAssertEqual(myInt32, Int32(Constants.intValue))
  140. let myInt64: Int64 = try config[Constants.intKey].decoded()
  141. XCTAssertEqual(myInt64, Int64(Constants.intValue))
  142. let myUInt: UInt = try config[Constants.intKey].decoded()
  143. XCTAssertEqual(myUInt, UInt(Constants.intValue))
  144. let myUInt8: UInt8 = try config[Constants.intKey].decoded()
  145. XCTAssertEqual(myUInt8, UInt8(Constants.intValue))
  146. let myUInt16: UInt16 = try config[Constants.intKey].decoded()
  147. XCTAssertEqual(myUInt16, UInt16(Constants.intValue))
  148. let myUInt32: UInt32 = try config[Constants.intKey].decoded()
  149. XCTAssertEqual(myUInt32, UInt32(Constants.intValue))
  150. let myUInt64: UInt64 = try config[Constants.intKey].decoded()
  151. XCTAssertEqual(myUInt64, UInt64(Constants.intValue))
  152. let myDecimal: Decimal = try config[Constants.floatKey].decoded()
  153. XCTAssertEqual(myDecimal, Decimal(Constants.doubleValue))
  154. let myFloat: Float = try config[Constants.floatKey].decoded()
  155. XCTAssertEqual(myFloat, Constants.floatValue)
  156. let myDouble: Double = try config[Constants.floatKey].decoded()
  157. XCTAssertEqual(myDouble, Constants.doubleValue)
  158. let myTrue: Bool = try config[Constants.trueKey].decoded()
  159. XCTAssertEqual(myTrue, true)
  160. let myFalse: Bool = try config[Constants.falseKey].decoded()
  161. XCTAssertEqual(myFalse, false)
  162. let myData: Data = try config[Constants.stringKey].decoded()
  163. XCTAssertEqual(myData, Constants.stringValue.data(using: .utf8))
  164. }
  165. func testStringFails() {
  166. XCTAssertEqual(config[decodedValue: "UndefinedKey"], "")
  167. }
  168. func testJSONFails() {
  169. XCTAssertNil(config[jsonValue: "UndefinedKey"])
  170. XCTAssertNil(config[jsonValue: Constants.stringKey])
  171. }
  172. func testDateDecodingNotYetSupported() async throws {
  173. let status = try await config.fetchAndActivate()
  174. XCTAssertEqual(status, .successFetchedFromRemote)
  175. do {
  176. let _: Date = try config[Constants.stringKey].decoded()
  177. } catch let RemoteConfigValueCodableError.unsupportedType(message) {
  178. XCTAssertEqual(message,
  179. "Date type is not currently supported for Remote Config Value decoding. " +
  180. "Please file a feature request")
  181. return
  182. }
  183. XCTFail("Failed to throw unsupported Date error.")
  184. }
  185. }