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