Value.swift 8.8 KB

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