diff --git a/Sources/Hummingbird/Utils/FlatDictionary.swift b/Sources/Hummingbird/Utils/FlatDictionary.swift index e5bb12f48..c4f2cb3c3 100644 --- a/Sources/Hummingbird/Utils/FlatDictionary.swift +++ b/Sources/Hummingbird/Utils/FlatDictionary.swift @@ -22,7 +22,7 @@ /// functions will always return the first key found, but if you /// iterate through the key,value pairs you can access all values /// for a key -public struct FlatDictionary: Collection { +public struct FlatDictionary: Collection, ExpressibleByDictionaryLiteral { public typealias Element = (key: Key, value: Value) public typealias Index = Array.Index diff --git a/Tests/HummingbirdTests/UtilsTests.swift b/Tests/HummingbirdTests/UtilsTests.swift new file mode 100644 index 000000000..9f2b0e57f --- /dev/null +++ b/Tests/HummingbirdTests/UtilsTests.swift @@ -0,0 +1,39 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the Hummingbird server framework project +// +// Copyright (c) 2023 the Hummingbird authors +// Licensed under Apache License v2.0 +// +// See LICENSE.txt for license information +// See hummingbird/CONTRIBUTORS.txt for the list of Hummingbird authors +// +// SPDX-License-Identifier: Apache-2.0 +// +//===----------------------------------------------------------------------===// + +import Hummingbird +import XCTest + +class FlatDictionaryTests: XCTestCase { + func testLiteralInit() { + let a: FlatDictionary = ["test": "value", "key2": "value2"] + XCTAssertEqual(a["test"], "value") + XCTAssertEqual(a["key2"], "value2") + } + + func testKeyGetSet() { + var a: FlatDictionary = [:] + a["key"] = "value" + XCTAssertEqual(a["key"], "value") + a["key"] = nil + XCTAssertEqual(a["key2"], nil) + } + + func testKeyGetFirst() { + var a: FlatDictionary = [:] + a.append(key: "key", value: "value1") + a.append(key: "key", value: "value2") + XCTAssertEqual(a["key"], "value1") + } +}