Skip to content

Commit b7e1a53

Browse files
ncooke3paulb777
authored andcommitted
[Config] Port ConfigValueOrigTests.swift and generate new tests (#14329)
1 parent 3356ae6 commit b7e1a53

File tree

2 files changed

+246
-0
lines changed

2 files changed

+246
-0
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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 RemoteConfigValueOrigTests: XCTestCase {
19+
func testConfigValueWithDifferentValueTypes() throws {
20+
let valueA = "0.33333"
21+
let dataA = try XCTUnwrap(valueA.data(using: .utf8))
22+
23+
let configValueA = RemoteConfigValue(data: dataA, source: .remote)
24+
XCTAssertEqual(configValueA.stringValue, valueA)
25+
XCTAssertEqual(configValueA.dataValue, dataA)
26+
XCTAssertEqual(
27+
configValueA.numberValue,
28+
NSNumber(floatLiteral: 0.33333)
29+
)
30+
XCTAssertEqual(configValueA.boolValue, (valueA as NSString).boolValue)
31+
32+
let valueB = "NO"
33+
let dataB = try XCTUnwrap(valueB.data(using: .utf8))
34+
let configValueB = RemoteConfigValue(data: dataB, source: .remote)
35+
XCTAssertEqual(configValueB.boolValue, (valueB as NSString).boolValue)
36+
37+
// Test JSON value.
38+
let jsonDictionary = ["key1": "value1"]
39+
let jsonArray = [["key1": "value1"], ["key2": "value2"]]
40+
41+
let jsonData = try JSONSerialization.data(withJSONObject: jsonDictionary, options: [])
42+
let configValueC = RemoteConfigValue(data: jsonData, source: .remote)
43+
XCTAssertEqual(configValueC.jsonValue as? [String: String], jsonDictionary)
44+
45+
let jsonArrayData = try JSONSerialization.data(withJSONObject: jsonArray, options: [])
46+
let configValueD = RemoteConfigValue(data: jsonArrayData, source: .remote)
47+
XCTAssertEqual(configValueD.jsonValue as? [[String: String]], jsonArray)
48+
}
49+
50+
func testConfigValueToNumber() throws {
51+
let strValue1 = "0.33"
52+
let data1 = try XCTUnwrap(strValue1.data(using: .utf8))
53+
let value1 = RemoteConfigValue(data: data1, source: .remote)
54+
XCTAssertEqual(value1.numberValue.floatValue, Float(strValue1)!)
55+
56+
let strValue2 = "3.14159265358979"
57+
let data2 = try XCTUnwrap(strValue2.data(using: .utf8))
58+
let value2 = RemoteConfigValue(data: data2, source: .remote)
59+
XCTAssertEqual(value2.numberValue.doubleValue, Double(strValue2)!)
60+
61+
let strValue3 = "1000000000"
62+
let data3 = try XCTUnwrap(strValue3.data(using: .utf8))
63+
let value3 = RemoteConfigValue(data: data3, source: .remote)
64+
65+
XCTAssertEqual(value3.numberValue.intValue, Int(strValue3)!)
66+
67+
let strValue4 = "1000000000123"
68+
let data4 = try XCTUnwrap(strValue4.data(using: .utf8))
69+
let value4 = RemoteConfigValue(data: data4, source: .remote)
70+
XCTAssertEqual(value4.numberValue.int64Value, Int64(strValue4)!)
71+
}
72+
}
Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
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

Comments
 (0)