From b7e1a5372bed50c3343ac686b8e24bdd468db40b Mon Sep 17 00:00:00 2001 From: Nick Cooke <36927374+ncooke3@users.noreply.github.com> Date: Fri, 10 Jan 2025 14:11:24 -0500 Subject: [PATCH] [Config] Port ConfigValueOrigTests.swift and generate new tests (#14329) --- .../SwiftUnit/ConfigValueOrigTests.swift | 72 ++++++++ .../Tests/SwiftUnit/ConfigValueTests.swift | 174 ++++++++++++++++++ 2 files changed, 246 insertions(+) create mode 100644 FirebaseRemoteConfig/Tests/SwiftUnit/ConfigValueOrigTests.swift create mode 100644 FirebaseRemoteConfig/Tests/SwiftUnit/ConfigValueTests.swift diff --git a/FirebaseRemoteConfig/Tests/SwiftUnit/ConfigValueOrigTests.swift b/FirebaseRemoteConfig/Tests/SwiftUnit/ConfigValueOrigTests.swift new file mode 100644 index 00000000000..ab68ad0e65c --- /dev/null +++ b/FirebaseRemoteConfig/Tests/SwiftUnit/ConfigValueOrigTests.swift @@ -0,0 +1,72 @@ +// Copyright 2025 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +@testable import FirebaseRemoteConfig +import XCTest + +class RemoteConfigValueOrigTests: XCTestCase { + func testConfigValueWithDifferentValueTypes() throws { + let valueA = "0.33333" + let dataA = try XCTUnwrap(valueA.data(using: .utf8)) + + let configValueA = RemoteConfigValue(data: dataA, source: .remote) + XCTAssertEqual(configValueA.stringValue, valueA) + XCTAssertEqual(configValueA.dataValue, dataA) + XCTAssertEqual( + configValueA.numberValue, + NSNumber(floatLiteral: 0.33333) + ) + XCTAssertEqual(configValueA.boolValue, (valueA as NSString).boolValue) + + let valueB = "NO" + let dataB = try XCTUnwrap(valueB.data(using: .utf8)) + let configValueB = RemoteConfigValue(data: dataB, source: .remote) + XCTAssertEqual(configValueB.boolValue, (valueB as NSString).boolValue) + + // Test JSON value. + let jsonDictionary = ["key1": "value1"] + let jsonArray = [["key1": "value1"], ["key2": "value2"]] + + let jsonData = try JSONSerialization.data(withJSONObject: jsonDictionary, options: []) + let configValueC = RemoteConfigValue(data: jsonData, source: .remote) + XCTAssertEqual(configValueC.jsonValue as? [String: String], jsonDictionary) + + let jsonArrayData = try JSONSerialization.data(withJSONObject: jsonArray, options: []) + let configValueD = RemoteConfigValue(data: jsonArrayData, source: .remote) + XCTAssertEqual(configValueD.jsonValue as? [[String: String]], jsonArray) + } + + func testConfigValueToNumber() throws { + let strValue1 = "0.33" + let data1 = try XCTUnwrap(strValue1.data(using: .utf8)) + let value1 = RemoteConfigValue(data: data1, source: .remote) + XCTAssertEqual(value1.numberValue.floatValue, Float(strValue1)!) + + let strValue2 = "3.14159265358979" + let data2 = try XCTUnwrap(strValue2.data(using: .utf8)) + let value2 = RemoteConfigValue(data: data2, source: .remote) + XCTAssertEqual(value2.numberValue.doubleValue, Double(strValue2)!) + + let strValue3 = "1000000000" + let data3 = try XCTUnwrap(strValue3.data(using: .utf8)) + let value3 = RemoteConfigValue(data: data3, source: .remote) + + XCTAssertEqual(value3.numberValue.intValue, Int(strValue3)!) + + let strValue4 = "1000000000123" + let data4 = try XCTUnwrap(strValue4.data(using: .utf8)) + let value4 = RemoteConfigValue(data: data4, source: .remote) + XCTAssertEqual(value4.numberValue.int64Value, Int64(strValue4)!) + } +} diff --git a/FirebaseRemoteConfig/Tests/SwiftUnit/ConfigValueTests.swift b/FirebaseRemoteConfig/Tests/SwiftUnit/ConfigValueTests.swift new file mode 100644 index 00000000000..2e30e76b8ec --- /dev/null +++ b/FirebaseRemoteConfig/Tests/SwiftUnit/ConfigValueTests.swift @@ -0,0 +1,174 @@ +// Copyright 2025 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +@testable import FirebaseRemoteConfig +import XCTest + +class RemoteConfigValueTests: XCTestCase { + func testStringValue_validUTF8Data() throws { + // Given + let data = try XCTUnwrap("test string".data(using: .utf8)) + let configValue = RemoteConfigValue(data: data, source: .remote) + // When + let stringValue = configValue.stringValue + // Then + XCTAssertEqual(stringValue, "test string") + } + + func testStringValue_invalidUTF8Data() { + // Given + let data = Data([0xFA, 0xDE, 0xD0, 0x0D]) // Invalid UTF-8 + let configValue = RemoteConfigValue(data: data, source: .remote) + // When + let stringValue = configValue.stringValue + // Then + XCTAssertEqual(stringValue, "") + } + + func testStringValue_nilData() { + // Given + let configValue = RemoteConfigValue(data: nil, source: .remote) + // When + let stringValue = configValue.stringValue + // Then + XCTAssertEqual(stringValue, "") + } + + func testStringValue_emptyData() { + // Given + let configValue = RemoteConfigValue(data: Data(), source: .remote) + // When + let stringValue = configValue.stringValue + // Then + XCTAssertEqual(stringValue, "") + } + + func testNumberValue_validDoubleString() throws { + // Given + let data = try XCTUnwrap("123.45".data(using: .utf8)) + let configValue = RemoteConfigValue(data: data, source: .remote) + // When + let numberValue = configValue.numberValue + // Then + XCTAssertEqual(numberValue, 123.45) + } + + func testNumberValue_invalidDoubleString() throws { + // Given + let data = try XCTUnwrap("not a number".data(using: .utf8)) + let configValue = RemoteConfigValue(data: data, source: .remote) + // When + let numberValue = configValue.numberValue + // Then + XCTAssertEqual(numberValue, 0) + } + + func testNumberValue_emptyData() { + // Given + let configValue = RemoteConfigValue(data: nil, source: .remote) + // When + let numberValue = configValue.numberValue + // Then + XCTAssertEqual(numberValue, 0) + } + + func testBoolValue_trueValues() throws { + // Given + let trueStrings = ["true", "TRUE", "1", "yes", "YES", "y", "Y"] + for str in trueStrings { + // When + let data = try XCTUnwrap(str.data(using: .utf8)) + let configValue = RemoteConfigValue(data: data, source: .remote) + // Then + XCTAssertTrue(configValue.boolValue) + } + } + + func testBoolValue_falseValues() throws { + // Given + let falseStrings = ["false", "FALSE", "0", "no", "NO", "n", "N", "other"] + + for str in falseStrings { + // When, Then (combined in loop) + let data = try XCTUnwrap(str.data(using: .utf8)) + let configValue = RemoteConfigValue(data: data, source: .remote) + XCTAssertFalse(configValue.boolValue) + } + } + + func testBoolValue_emptyData() { + // Given + let configValue = RemoteConfigValue(data: nil, source: .remote) + // When + let boolValue = configValue.boolValue + // Then + XCTAssertFalse(boolValue) + } + + func testJsonValue_validJSON() throws { + // Given + let dict = ["key": "value"] + let data = try JSONSerialization.data(withJSONObject: dict, options: []) + let configValue = RemoteConfigValue(data: data, source: .remote) + // When + let jsonValue = configValue.jsonValue + // Then + XCTAssertEqual(jsonValue as? [String: String], dict) + } + + func testJsonValue_invalidJSON() throws { + // Given + let data = try XCTUnwrap("invalid json".data(using: .utf8)) // Invalid JSON + let configValue = RemoteConfigValue(data: data, source: .remote) + // When + let jsonValue = configValue.jsonValue + // Then + XCTAssertNil(jsonValue) + } + + func testJsonValue_emptyData() { + // Given + let configValue = RemoteConfigValue(data: nil, source: .remote) + // When + let jsonValue = configValue.jsonValue + // Then + XCTAssertNil(jsonValue) + } + + func testCopy() throws { + // Given + let data = try XCTUnwrap("test".data(using: .utf8)) + let configValue = RemoteConfigValue(data: data, source: .remote) + // When + let copiedValue = configValue.copy(with: nil) as! RemoteConfigValue + // Then + XCTAssertEqual(configValue.dataValue, copiedValue.dataValue) + XCTAssertEqual(configValue.source, copiedValue.source) + XCTAssertEqual(configValue.stringValue, copiedValue.stringValue) + } + + func testDebugDescription() throws { + // Given + let data = try XCTUnwrap("test".data(using: .utf8)) + let configValue = RemoteConfigValue(data: data, source: .remote) + // When + let debugDescription = configValue.debugDescription + // Then + let expectedPrefix = "