From 415816c78b6e3d6a8baeee2aba56adfb1ecc7a53 Mon Sep 17 00:00:00 2001 From: Eugene Date: Fri, 18 Jun 2021 07:40:07 +0300 Subject: [PATCH] Return the first step from `ConjunctionBuilder` The result builder and its tests are only supported in swift 5.4+. --- .../project.pbxproj | 8 +++++++ .../ConjunctionBuilder.swift | 24 +++++++++++++++++++ .../ConjunctionBuilderTests.swift | 21 ++++++++++++++++ 3 files changed, 53 insertions(+) create mode 100644 LightweightValidation/ConjunctionBuilder.swift create mode 100644 LightweightValidationTests/ConjunctionBuilderTests.swift diff --git a/LightweightValidation.xcodeproj/project.pbxproj b/LightweightValidation.xcodeproj/project.pbxproj index 0603264..fb5a33f 100644 --- a/LightweightValidation.xcodeproj/project.pbxproj +++ b/LightweightValidation.xcodeproj/project.pbxproj @@ -7,6 +7,8 @@ objects = { /* Begin PBXBuildFile section */ + 071DDA9F267DAACE00369489 /* ConjunctionBuilder.swift in Sources */ = {isa = PBXBuildFile; fileRef = 071DDA9E267DAACE00369489 /* ConjunctionBuilder.swift */; }; + 071DDAA3267DAAFF00369489 /* ConjunctionBuilderTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 071DDAA2267DAAFF00369489 /* ConjunctionBuilderTests.swift */; }; 14BE424E2441BD8800636D18 /* ResponseValidator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 14BE424D2441BD8800636D18 /* ResponseValidator.swift */; }; 7500D2F18796472EB6193460 /* ResponseValidatorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 75DD6FD50157DE481178EEC5 /* ResponseValidatorTests.swift */; }; 751856E71195D54F38792660 /* V.swift in Sources */ = {isa = PBXBuildFile; fileRef = 75A5B579658152CC9DDDFF0A /* V.swift */; }; @@ -25,6 +27,8 @@ /* End PBXContainerItemProxy section */ /* Begin PBXFileReference section */ + 071DDA9E267DAACE00369489 /* ConjunctionBuilder.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ConjunctionBuilder.swift; sourceTree = ""; }; + 071DDAA2267DAAFF00369489 /* ConjunctionBuilderTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ConjunctionBuilderTests.swift; sourceTree = ""; }; 14BE424D2441BD8800636D18 /* ResponseValidator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ResponseValidator.swift; sourceTree = ""; }; 7516E37763624BA4003C45E4 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.info; path = Info.plist; sourceTree = ""; }; 755F66236AF76130A0A3DDA4 /* StringError.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StringError.swift; sourceTree = ""; }; @@ -68,6 +72,7 @@ children = ( 75ACF14D556F65F52C437423 /* Info.plist */, 14BE424D2441BD8800636D18 /* ResponseValidator.swift */, + 071DDA9E267DAACE00369489 /* ConjunctionBuilder.swift */, 75A5B579658152CC9DDDFF0A /* V.swift */, 755F66236AF76130A0A3DDA4 /* StringError.swift */, ); @@ -88,6 +93,7 @@ children = ( 7516E37763624BA4003C45E4 /* Info.plist */, 75DD6FD50157DE481178EEC5 /* ResponseValidatorTests.swift */, + 071DDAA2267DAAFF00369489 /* ConjunctionBuilderTests.swift */, ); path = LightweightValidationTests; sourceTree = ""; @@ -195,6 +201,7 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( + 071DDA9F267DAACE00369489 /* ConjunctionBuilder.swift in Sources */, 14BE424E2441BD8800636D18 /* ResponseValidator.swift in Sources */, 751856E71195D54F38792660 /* V.swift in Sources */, 753FD777795B093E3ED5A224 /* StringError.swift in Sources */, @@ -205,6 +212,7 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( + 071DDAA3267DAAFF00369489 /* ConjunctionBuilderTests.swift in Sources */, 7500D2F18796472EB6193460 /* ResponseValidatorTests.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; diff --git a/LightweightValidation/ConjunctionBuilder.swift b/LightweightValidation/ConjunctionBuilder.swift new file mode 100644 index 0000000..57ea8f9 --- /dev/null +++ b/LightweightValidation/ConjunctionBuilder.swift @@ -0,0 +1,24 @@ +// +// ConjunctionBuilder.swift +// LightweightValidation +// +// Created by user on 19.06.2021. +// Copyright © 2021 yes. All rights reserved. +// + +#if swift(>=5.4) +import Foundation + +@resultBuilder +public struct ConjunctionBuilder { + public static func buildBlock (_ validations: V...) -> V { + validations[0] + } +} + +public extension V { + static func all(@ConjunctionBuilder _ validations: () -> V) -> V { + validations() + } +} +#endif diff --git a/LightweightValidationTests/ConjunctionBuilderTests.swift b/LightweightValidationTests/ConjunctionBuilderTests.swift new file mode 100644 index 0000000..9adae29 --- /dev/null +++ b/LightweightValidationTests/ConjunctionBuilderTests.swift @@ -0,0 +1,21 @@ +// +// ConjunctionBuilderTests.swift +// LightweightValidationTests +// +// Created by user on 19.06.2021. +// Copyright © 2021 yes. All rights reserved. +// + +#if swift(>=5.4) +import LightweightValidation +import XCTest + +class ConjunctionBuilderTests: XCTestCase { + func testValidStepShouldReturnItself() { + let sut = Response.SimpleValidationResult.all { + true StringError("never happens") + } + XCTAssertTrue(sut.isValue) + } +} +#endif