From 0e8a42aedcde81a3610b61bac4bddf72d031f99d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sun, 14 Mar 2021 11:53:49 +0100 Subject: [PATCH] Tests for interpretations of SQLLiteral If those tests break, we have introduced a breaking change. --- Tests/GRDBTests/SQLLiteralTests.swift | 60 +++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/Tests/GRDBTests/SQLLiteralTests.swift b/Tests/GRDBTests/SQLLiteralTests.swift index 1b496aad0..bbd9c62f2 100644 --- a/Tests/GRDBTests/SQLLiteralTests.swift +++ b/Tests/GRDBTests/SQLLiteralTests.swift @@ -767,4 +767,64 @@ extension SQLLiteralTests { XCTAssertTrue(SQLLiteral(sql: "").isEmpty) XCTAssertTrue(SQLLiteral("").isEmpty) } + + func testProtocolResolution() { + // SQLLiteral can feed ordering, selection, and expressions. + acceptOrderingTerm(SQLLiteral("")) + acceptSelectable(SQLLiteral("")) + acceptSpecificExpressible(SQLLiteral("")) + acceptExpressible(SQLLiteral("")) + + // SQLLiteral can build complex expressions and orderings + _ = SQLLiteral("") + 1 + _ = SQLLiteral("").desc + + // Swift String literals are interpreted as String, even when SQLLiteral + // is an accepted type. + // + // should not compile: XCTAssertEqual(acceptOrderingTerm(""), String(describing: String.self)) + // should not compile: XCTAssertEqual(acceptSelectable(""), String(describing: String.self)) + // should not compile: XCTAssertEqual(acceptSpecificExpressible(""), String(describing: String.self)) + XCTAssertEqual(acceptExpressible(""), String(describing: String.self)) + + // When a literal can be interpreted as an ordering, a selection, or an + // expression, then the expression interpretation is favored. + // This test targets TableAlias subscript. + // + // should not compile: XCTAssertEqual(overloaded(""), "a") + XCTAssertEqual(overloaded(SQLLiteral("")), "SQLSpecificExpressible") + } +} + +// Support for testProtocolResolution() +@discardableResult +private func acceptOrderingTerm(_ x: SQLOrderingTerm) -> String { + String(describing: type(of: x)) +} + +@discardableResult +private func acceptSelectable(_ x: SQLSelectable) -> String { + String(describing: type(of: x)) +} + +@discardableResult +private func acceptSpecificExpressible(_ x: SQLSpecificExpressible) -> String { + String(describing: type(of: x)) +} + +@discardableResult +private func acceptExpressible(_ x: SQLExpressible) -> String { + String(describing: type(of: x)) +} + +private func overloaded(_ x: SQLOrderingTerm) -> String { + "SQLOrderingTerm" +} + +private func overloaded(_ x: SQLSelectable) -> String { + "SQLSelectable" +} + +private func overloaded(_ x: SQLSpecificExpressible & SQLSelectable & SQLOrderingTerm) -> String { + "SQLSpecificExpressible" }