ConfigValueTests.swift 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 RemoteConfigValueTests: XCTestCase {
  17. func testStringValue_validUTF8Data() throws {
  18. // Given
  19. let data = try XCTUnwrap("test string".data(using: .utf8))
  20. let configValue = RemoteConfigValue(data: data, source: .remote)
  21. // When
  22. let stringValue = configValue.stringValue
  23. // Then
  24. XCTAssertEqual(stringValue, "test string")
  25. }
  26. func testStringValue_invalidUTF8Data() {
  27. // Given
  28. let data = Data([0xFA, 0xDE, 0xD0, 0x0D]) // Invalid UTF-8
  29. let configValue = RemoteConfigValue(data: data, source: .remote)
  30. // When
  31. let stringValue = configValue.stringValue
  32. // Then
  33. XCTAssertEqual(stringValue, "")
  34. }
  35. func testStringValue_nilData() {
  36. // Given
  37. let configValue = RemoteConfigValue(data: nil, source: .remote)
  38. // When
  39. let stringValue = configValue.stringValue
  40. // Then
  41. XCTAssertEqual(stringValue, "")
  42. }
  43. func testStringValue_emptyData() {
  44. // Given
  45. let configValue = RemoteConfigValue(data: Data(), source: .remote)
  46. // When
  47. let stringValue = configValue.stringValue
  48. // Then
  49. XCTAssertEqual(stringValue, "")
  50. }
  51. func testNumberValue_validDoubleString() throws {
  52. // Given
  53. let data = try XCTUnwrap("123.45".data(using: .utf8))
  54. let configValue = RemoteConfigValue(data: data, source: .remote)
  55. // When
  56. let numberValue = configValue.numberValue
  57. // Then
  58. XCTAssertEqual(numberValue, 123.45)
  59. }
  60. func testNumberValue_invalidDoubleString() throws {
  61. // Given
  62. let data = try XCTUnwrap("not a number".data(using: .utf8))
  63. let configValue = RemoteConfigValue(data: data, source: .remote)
  64. // When
  65. let numberValue = configValue.numberValue
  66. // Then
  67. XCTAssertEqual(numberValue, 0)
  68. }
  69. func testNumberValue_emptyData() {
  70. // Given
  71. let configValue = RemoteConfigValue(data: nil, source: .remote)
  72. // When
  73. let numberValue = configValue.numberValue
  74. // Then
  75. XCTAssertEqual(numberValue, 0)
  76. }
  77. func testBoolValue_trueValues() throws {
  78. // Given
  79. let trueStrings = ["true", "TRUE", "1", "yes", "YES", "y", "Y"]
  80. for str in trueStrings {
  81. // When
  82. let data = try XCTUnwrap(str.data(using: .utf8))
  83. let configValue = RemoteConfigValue(data: data, source: .remote)
  84. // Then
  85. XCTAssertTrue(configValue.boolValue)
  86. }
  87. }
  88. func testBoolValue_falseValues() throws {
  89. // Given
  90. let falseStrings = ["false", "FALSE", "0", "no", "NO", "n", "N", "other"]
  91. for str in falseStrings {
  92. // When, Then (combined in loop)
  93. let data = try XCTUnwrap(str.data(using: .utf8))
  94. let configValue = RemoteConfigValue(data: data, source: .remote)
  95. XCTAssertFalse(configValue.boolValue)
  96. }
  97. }
  98. func testBoolValue_emptyData() {
  99. // Given
  100. let configValue = RemoteConfigValue(data: nil, source: .remote)
  101. // When
  102. let boolValue = configValue.boolValue
  103. // Then
  104. XCTAssertFalse(boolValue)
  105. }
  106. func testJsonValue_validJSON() throws {
  107. // Given
  108. let dict = ["key": "value"]
  109. let data = try JSONSerialization.data(withJSONObject: dict, options: [])
  110. let configValue = RemoteConfigValue(data: data, source: .remote)
  111. // When
  112. let jsonValue = configValue.jsonValue
  113. // Then
  114. XCTAssertEqual(jsonValue as? [String: String], dict)
  115. }
  116. func testJsonValue_invalidJSON() throws {
  117. // Given
  118. let data = try XCTUnwrap("invalid json".data(using: .utf8)) // Invalid JSON
  119. let configValue = RemoteConfigValue(data: data, source: .remote)
  120. // When
  121. let jsonValue = configValue.jsonValue
  122. // Then
  123. XCTAssertNil(jsonValue)
  124. }
  125. func testJsonValue_emptyData() {
  126. // Given
  127. let configValue = RemoteConfigValue(data: nil, source: .remote)
  128. // When
  129. let jsonValue = configValue.jsonValue
  130. // Then
  131. XCTAssertNil(jsonValue)
  132. }
  133. func testCopy() throws {
  134. // Given
  135. let data = try XCTUnwrap("test".data(using: .utf8))
  136. let configValue = RemoteConfigValue(data: data, source: .remote)
  137. // When
  138. let copiedValue = configValue.copy(with: nil) as! RemoteConfigValue
  139. // Then
  140. XCTAssertEqual(configValue.dataValue, copiedValue.dataValue)
  141. XCTAssertEqual(configValue.source, copiedValue.source)
  142. XCTAssertEqual(configValue.stringValue, copiedValue.stringValue)
  143. }
  144. func testDebugDescription() throws {
  145. // Given
  146. let data = try XCTUnwrap("test".data(using: .utf8))
  147. let configValue = RemoteConfigValue(data: data, source: .remote)
  148. // When
  149. let debugDescription = configValue.debugDescription
  150. // Then
  151. let expectedPrefix = "<RemoteConfigValue: "
  152. XCTAssertTrue(debugDescription.hasPrefix(expectedPrefix))
  153. XCTAssertTrue(debugDescription.contains("String: test"))
  154. XCTAssertTrue(debugDescription.contains("Boolean: true"))
  155. XCTAssertTrue(debugDescription.contains("Source: 0"))
  156. }
  157. }