Value.swift 8.8 KB

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