Renamed the Sources folder as Libraries.

This commit is contained in:
2023-08-11 23:16:53 +02:00
parent 060d8a84a9
commit c29178535a
49 changed files with 15 additions and 15 deletions
@@ -0,0 +1,64 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftLibs open source project
//
// Copyright (c) 2023 Röck+Cöde VoF. and the SwiftLibs project authors
// Licensed under the EUPL 1.2 or later.
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftLibs project authors
//
//===----------------------------------------------------------------------===//
import SwiftLibsFoundation
import XCTest
final class Bool_InitTests: XCTestCase {
// MARK: Properties
private var strings: [String] = []
private var booleans: [Bool] = []
// MARK: Tests
func test_init_withPositiveStrings() {
// GIVEN
strings = ["YES", "Yes", "yes", "TRUE", "TrUe", "true", "One", "OnE", "one", "1"]
// WHEN
booleans = strings.map(Bool.init)
// THEN
booleans.forEach { boolean in
XCTAssertTrue(boolean)
}
}
func test_init_withNegativeStrings() {
// GIVEN
strings = ["NO", "No", "no", "FALSE", "FaLsE", "false", "ZERO", "ZeRo", "zero", "0"]
// WHEN
booleans = strings.map(Bool.init)
// THEN
booleans.forEach { boolean in
XCTAssertFalse(boolean)
}
}
func test_init_withOtherStrings() {
// GIVEN
strings = [.empty, "...", "something", "yes-", "false+", "X", "9"]
// WHEN
booleans = strings.map(Bool.init)
// THEN
booleans.forEach { boolean in
XCTAssertFalse(boolean)
}
}
}
@@ -0,0 +1,49 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftLibs open source project
//
// Copyright (c) 2023 Röck+Cöde VoF. and the SwiftLibs project authors
// Licensed under the EUPL 1.2 or later.
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftLibs project authors
//
//===----------------------------------------------------------------------===//
import Foundation
import SwiftLibsFoundation
import XCTest
final class Bundle_LocalisationBundleTests: XCTestCase {
// MARK: Properties
private let bundle = Bundle.module
private var languageCode: String!
// MARK: Tests
func test_localisation_withExistingLocalisationBundle() throws {
// GIVEN
languageCode = "en"
// WHEN
let localisationBundle = try bundle.localisation(for: languageCode)
// THEN
XCTAssertNotNil(localisationBundle)
XCTAssertEqual(localisationBundle.bundleURL.lastPathComponent, "en.lproj")
}
func test_localisation_withNonExistingLocalisationBundle() throws {
// GIVEN
languageCode = "nl"
// WHEN & THEN
XCTAssertThrowsError(try bundle.localisation(for: languageCode)) { error in
XCTAssertEqual(error as? BundleError, .bundleNotFound)
}
}
}
@@ -0,0 +1,75 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftLibs open source project
//
// Copyright (c) 2023 Röck+Cöde VoF. and the SwiftLibs project authors
// Licensed under the EUPL 1.2 or later.
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftLibs project authors
//
//===----------------------------------------------------------------------===//
import SwiftLibsFoundation
import XCTest
final class Collection_EmptyTests: XCTestCase {
// MARK: Properties
private var collection: (any Collection)!
private var result: Bool!
// MARK: Tests
func test_isNotEmpty_whenAnArrayIsEmpty() {
// GIVEN
collection = [Int]()
// WHEN
result = collection.isNotEmpty
// THEN
XCTAssertFalse(result)
}
func test_isNotEmpty_whenAnArrayIsNotEmpty() {
// GIVEN
collection = [0, 1, 2, 3, 4, 5]
// WHEN
result = collection.isNotEmpty
// THEN
XCTAssertTrue(result)
}
func test_isNotEmpty_whenADictionaryIsEmpty() {
// GIVEN
collection = [String: Int]()
// WHEN
result = collection.isNotEmpty
// THEN
XCTAssertFalse(result)
}
func test_isNotEmpty_whenADictionaryIsNotEmpty() {
// GIVEN
collection = [
"something": 0,
"else": 1,
"goes": 2,
"in": 3,
"here": 4
]
// WHEN
result = collection.isNotEmpty
// THEN
XCTAssertTrue(result)
}
}
@@ -0,0 +1,66 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftLibs open source project
//
// Copyright (c) 2023 Röck+Cöde VoF. and the SwiftLibs project authors
// Licensed under the EUPL 1.2 or later.
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftLibs project authors
//
//===----------------------------------------------------------------------===//
import SwiftLibsFoundation
import XCTest
final class Optional_NilTests: XCTestCase {
// MARK: Properties
private var optional: Int?
private var result: Bool!
// MARK: Tests
func test_isNil_whenOptionalHasNotBeenSet() {
// GIVEN
// WHEN
result = optional.isNil
// THEN
XCTAssertTrue(result)
}
func test_isNil_whenOptionalHasBeenSet() {
// GIVEN
optional = 0
// WHEN
result = optional.isNil
// THEN
XCTAssertFalse(result)
}
func test_isNotNil_whenOptionalHasNotBeenSet() {
// GIVEN
// WHEN
result = optional.isNotNil
// THEN
XCTAssertFalse(result)
}
func test_isNotNil_whenOptionalHasBeenSet() {
// GIVEN
optional = 0
// WHEN
result = optional.isNotNil
// THEN
XCTAssertTrue(result)
}
}
@@ -0,0 +1,59 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftLibs open source project
//
// Copyright (c) 2023 Röck+Cöde VoF. and the SwiftLibs project authors
// Licensed under the EUPL 1.2 or later.
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftLibs project authors
//
//===----------------------------------------------------------------------===//
import SwiftLibsFoundation
import XCTest
final class String_EmptyTests: XCTestCase {
// MARK: Tests
func test_empty() {
// GIVEN
let string = String.empty
// WHEN
// THEN
XCTAssertEqual(string, "")
}
func test_isNotEmpty_withEmptyString() {
// GIVEN
let string = String.empty
// WHEN
let result = string.isNotEmpty
// THEN
XCTAssertFalse(result)
}
func test_isNotEmpty_withFilledString() {
// GIVEN
let string = String.Test.string
// WHEN
let result = string.isNotEmpty
// THEN
XCTAssertTrue(result)
}
}
// MARK: - String+Constants
private extension String {
enum Test {
static let string = "Some test string..."
}
}
@@ -0,0 +1,196 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftLibs open source project
//
// Copyright (c) 2023 Röck+Cöde VoF. and the SwiftLibs project authors
// Licensed under the EUPL 1.2 or later.
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftLibs project authors
//
//===----------------------------------------------------------------------===//
import Foundation
import SwiftLibsFoundation
import XCTest
final class String_LocalisationTests: XCTestCase {
// MARK: Properties
private var languageCode: String!
private var stringToLocalise: String!
private var localisedString: String!
private var defaultValue: String?
// MARK: Tests
func test_localise_definedKey_inDefinedLocalisationBundle() {
// GIVEN
languageCode = "en"
stringToLocalise = .Seed.Localisation.someLocalisableString
// WHEN
localisedString = stringToLocalise.localise(for: languageCode, in: .module)
// THEN
XCTAssertEqual(localisedString, .Result.Localisation.someLocalisableString)
}
func test_localise_definedKey_inDefinedLocalisationBundle_withDefaultValue() {
// GIVEN
languageCode = "en"
stringToLocalise = .Seed.Localisation.otherLocalisableString
defaultValue = "Some default value goes here..."
// WHEN
localisedString = stringToLocalise.localise(
for: languageCode,
in: .module,
value: defaultValue
)
// THEN
XCTAssertEqual(localisedString, .Result.Localisation.otherLocalisableString)
}
func test_localise_definedKey_inDefinedLocalisationBundle_withDefinedTable() {
// GIVEN
languageCode = "en"
stringToLocalise = .Seed.Localisation.someLocalisableString
// WHEN
localisedString = stringToLocalise.localise(
for: languageCode,
in: .module,
table: "Some table name goes in here..."
)
// THEN
XCTAssertEqual(localisedString, stringToLocalise)
}
func test_localise_definedKey_inDefinedLocalisationBundle_withDefauledValue_andDefinedTable() {
// GIVEN
languageCode = "en"
stringToLocalise = .Seed.Localisation.otherLocalisableString
defaultValue = "Some default value goes in here..."
// WHEN
localisedString = stringToLocalise.localise(
for: languageCode,
in: .module,
value: defaultValue,
table: "Some table name goes in here..."
)
// THEN
XCTAssertEqual(localisedString, defaultValue)
}
func test_localise_definedKey_inNotDefinedLocalisationBundle() {
// GIVEN
languageCode = "nl"
stringToLocalise = .Seed.Localisation.someLocalisableString
// WHEN
localisedString = stringToLocalise.localise(for: languageCode, in: .module)
// THEN
XCTAssertEqual(localisedString, stringToLocalise)
}
func test_localise_definedKey_inNotDefinedLocalisationBundle_withDefaultValue() {
// GIVEN
languageCode = "nl"
stringToLocalise = .Seed.Localisation.otherLocalisableString
defaultValue = "Some default value goes in here..."
// WHEN
localisedString = stringToLocalise.localise(
for: languageCode,
in: .module,
value: defaultValue
)
// THEN
XCTAssertEqual(localisedString, defaultValue)
}
func test_localise_notDefinedKey_inDefinedLocalisationBundle() {
// GIVEN
languageCode = "en"
stringToLocalise = .Seed.Localisation.notLocalisableString
// WHEN
localisedString = stringToLocalise.localise(for: languageCode, in: .module)
// THEN
XCTAssertEqual(localisedString, stringToLocalise)
}
func test_localise_notDefinedKey_inDefinedLocalisationBundle_withDefaultValue() {
languageCode = "en"
stringToLocalise = .Seed.Localisation.notLocalisableString
defaultValue = "Some default value goes here..."
// WHEN
localisedString = stringToLocalise.localise(
for: languageCode,
in: .module,
value: defaultValue
)
// THEN
XCTAssertEqual(localisedString, defaultValue)
}
func test_localise_inDifferentBundle() {
// GIVEN
languageCode = "en"
stringToLocalise = .Seed.Localisation.someLocalisableString
// WHEN
localisedString = stringToLocalise.localise(for: languageCode, in: .main)
// THEN
XCTAssertEqual(localisedString, stringToLocalise)
}
func test_localise_inDifferentBundle_withDefaultValue() {
// GIVEN
languageCode = "en"
stringToLocalise = .Seed.Localisation.otherLocalisableString
defaultValue = "Some default value goes here..."
// WHEN
localisedString = stringToLocalise.localise(
for: languageCode,
in: .main,
value: defaultValue
)
// THEN
XCTAssertEqual(localisedString, defaultValue)
}
}
// MARK: - String+Seed
private extension String.Seed {
enum Localisation {
static let someLocalisableString = "test.core.bundle.some-localisable-string"
static let otherLocalisableString = "test.core.bundle.other-localisable-string"
static let notLocalisableString = "test.core.bundle.non-localisable-string"
}
}
// MARK: - String+Result
private extension String.Result {
enum Localisation {
static let someLocalisableString = "Some localisable string to use for testing purposes."
static let otherLocalisableString = "Other localisable string to use for testing purposes."
}
}
@@ -0,0 +1,34 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftLibs open source project
//
// Copyright (c) 2023 Röck+Cöde VoF. and the SwiftLibs project authors
// Licensed under the EUPL 1.2 or later.
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftLibs project authors
//
//===----------------------------------------------------------------------===//
import SwiftLibsFoundation
import XCTest
final class TimeZone_ZoneTests: XCTestCase {
// MARK: Properties
private var timeZone: TimeZone!
// MARK: Tests
func test_gmt() {
// GIVEN
// WHEN
timeZone = .gmt
// THEN
XCTAssertEqual(timeZone.identifier, "GMT")
XCTAssertEqual(timeZone.secondsFromGMT(), 0)
}
}
@@ -0,0 +1,75 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftLibs open source project
//
// Copyright (c) 2023 Röck+Cöde VoF. and the SwiftLibs project authors
// Licensed under the EUPL 1.2 or later.
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftLibs project authors
//
//===----------------------------------------------------------------------===//
import Foundation
import SwiftLibsFoundation
import XCTest
final class LossyCodableList_DecodableTests: XCTestCase {
// MARK: Properties
private let decoder = JSONDecoder()
private var dataToDecode: Data!
private var decodedList: TestCodableList!
// MARK: Tests
func test_decode_whenAllDataIsComplete() throws {
// GIVEN
dataToDecode = .Seed.itemsWithAllKeysHavingIntValues
// WHEN
decodedList = try decoder.decode(TestCodableList.self, from: dataToDecode)
// THEN
XCTAssertNotNil(decodedList)
XCTAssertTrue(decodedList.items.isNotEmpty)
XCTAssertEqual(decodedList.items, [
.init(key: "One", value: 1),
.init(key: "Two", value: 2),
.init(key: "Three", value: 3),
.init(key: "Four", value: 4)
])
}
func test_decode_whenSomeDataHasNil() throws {
// GIVEN
dataToDecode = .Seed.itemsWithSomeKeysAndValuesAreNil
// WHEN
decodedList = try decoder.decode(TestCodableList.self, from: dataToDecode)
// THEN
XCTAssertNotNil(decodedList)
XCTAssertTrue(decodedList.items.isNotEmpty)
XCTAssertEqual(decodedList.items, [
.init(key: "One", value: 1),
.init(key: "Three", value: 3)
])
}
func test_decode_whenAllDataHasNil() throws {
// GIVEN
dataToDecode = .Seed.itemsWithAllKeysAndValuesAreNil
// WHEN
decodedList = try decoder.decode(TestCodableList.self, from: dataToDecode)
// THEN
XCTAssertNotNil(decodedList)
XCTAssertTrue(decodedList.items.isEmpty)
XCTAssertEqual(decodedList.items, [])
}
}
@@ -0,0 +1,80 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftLibs open source project
//
// Copyright (c) 2023 Röck+Cöde VoF. and the SwiftLibs project authors
// Licensed under the EUPL 1.2 or later.
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftLibs project authors
//
//===----------------------------------------------------------------------===//
import Foundation
import XCTest
@testable import SwiftLibsFoundation
final class LossyCodableList_EncodableTests: XCTestCase {
// MARK: Properties
private let decoder = JSONDecoder()
private let encoder = JSONEncoder()
private var dataToDecode: Data!
private var encodedData: Data!
private var list: TestCodableList!
// MARK: Setup
override func setUpWithError() throws {
// This setting is used to guarantee that the properties of the model are being generated by sorted keys order.
encoder.outputFormatting = .sortedKeys
}
// MARK: Tests
func test_encode_whenAllKeysHaveIntValues() throws {
// GIVEN
dataToDecode = .Seed.itemsWithAllKeysHavingIntValues
list = try decoder.decode(TestCodableList.self, from: dataToDecode)
// WHEN
encodedData = try encoder.encode(list)
// THEN
XCTAssertNotNil(encodedData)
XCTAssertTrue(encodedData.isNotEmpty)
XCTAssertEqual(encodedData, .Result.allItemsNotFilteredOut)
}
func test_encode_whenSomeKeysAndValuesAreNil() throws {
// GIVEN
dataToDecode = .Seed.itemsWithSomeKeysAndValuesAreNil
list = try decoder.decode(TestCodableList.self, from: dataToDecode)
// WHEN
encodedData = try encoder.encode(list)
// THEN
XCTAssertNotNil(encodedData)
XCTAssertTrue(encodedData.isNotEmpty)
XCTAssertEqual(encodedData, .Result.someItemsFilteredOut)
}
func test_encode_whenAllKeysAndValuesAreNil() throws {
// GIVEN
dataToDecode = .Seed.itemsWithAllKeysAndValuesAreNil
list = try decoder.decode(TestCodableList.self, from: dataToDecode)
// WHEN
encodedData = try encoder.encode(list)
// THEN
XCTAssertNotNil(encodedData)
XCTAssertTrue(encodedData.isNotEmpty)
XCTAssertEqual(encodedData, .Result.allItemsFilteredOut)
}
}