|
| 1 | +/* |
| 2 | + |
| 3 | + MIT License |
| 4 | + |
| 5 | + Copyright (c) 2016 Maxim Khatskevich ( [email protected]) |
| 6 | + |
| 7 | + Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | + of this software and associated documentation files (the "Software"), to deal |
| 9 | + in the Software without restriction, including without limitation the rights |
| 10 | + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | + copies of the Software, and to permit persons to whom the Software is |
| 12 | + furnished to do so, subject to the following conditions: |
| 13 | + |
| 14 | + The above copyright notice and this permission notice shall be included in all |
| 15 | + copies or substantial portions of the Software. |
| 16 | + |
| 17 | + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 23 | + SOFTWARE. |
| 24 | + |
| 25 | + */ |
| 26 | + |
| 27 | +import XCTest |
| 28 | + |
| 29 | +@testable |
| 30 | +import XCEValidatableValue |
| 31 | + |
| 32 | +//--- |
| 33 | + |
| 34 | +class EntityTests: XCTestCase {} |
| 35 | + |
| 36 | +//--- |
| 37 | + |
| 38 | +extension EntityTests |
| 39 | +{ |
| 40 | + func testMembersGetters() |
| 41 | + { |
| 42 | + enum FirstName: SomeValidatableValue |
| 43 | + { |
| 44 | + typealias Raw = String |
| 45 | + } |
| 46 | + |
| 47 | + struct TheEntity: SomeValidatableEntity |
| 48 | + { |
| 49 | + var wrap1: NonRequired<FirstName> |
| 50 | + var wrap1Opt: NonRequired<FirstName>? |
| 51 | + var wrap2: Required<FirstName> |
| 52 | + var wrap2Opt: Required<FirstName>? |
| 53 | + var somethingElse: String? |
| 54 | + } |
| 55 | + |
| 56 | + let entity = TheEntity( |
| 57 | + wrap1: .init(""), |
| 58 | + wrap1Opt: .init(""), |
| 59 | + wrap2: .init(""), |
| 60 | + wrap2Opt: .init(""), |
| 61 | + somethingElse: "else" |
| 62 | + ) |
| 63 | + |
| 64 | + //--- |
| 65 | + |
| 66 | + let allMembers = entity.allMembers |
| 67 | + let valMembers = entity.allValidatableMembers |
| 68 | + |
| 69 | + XCTAssertEqual(allMembers.count, 5) |
| 70 | + XCTAssertEqual(valMembers.count, 4) |
| 71 | + } |
| 72 | + |
| 73 | +// func testDisplayName() |
| 74 | +// { |
| 75 | +// struct SomeEntity: SomeValidatableEntity {} |
| 76 | +// |
| 77 | +// XCTAssert(SomeEntity.displayName == SomeEntity.intrinsicDisplayName) |
| 78 | +// |
| 79 | +// //--- |
| 80 | +// |
| 81 | +// struct CustomNamedEntity: SomeValidatableEntity |
| 82 | +// { |
| 83 | +// static |
| 84 | +// let someStr = "This is a custom named Entity" |
| 85 | +// |
| 86 | +// static |
| 87 | +// let displayName = someStr |
| 88 | +// } |
| 89 | +// |
| 90 | +// XCTAssert(CustomNamedEntity.displayName != CustomNamedEntity.intrinsicDisplayName) |
| 91 | +// XCTAssert(CustomNamedEntity.displayName == CustomNamedEntity.someStr) |
| 92 | +// } |
| 93 | +// |
| 94 | +// func testDefaultValueReport() |
| 95 | +// { |
| 96 | +// struct SomeEntity: SomeValidatableEntity {} |
| 97 | +// |
| 98 | +// let defaultReport = SomeEntity.defaultReport(with: []) |
| 99 | +// |
| 100 | +// let report = SomeEntity.prepareReport(with: []) |
| 101 | +// |
| 102 | +// XCTAssert(report == defaultReport) |
| 103 | +// } |
| 104 | +// |
| 105 | +// func testCustomEntityReport() |
| 106 | +// { |
| 107 | +// struct SomeEntity: SomeValidatableEntity |
| 108 | +// { |
| 109 | +// static |
| 110 | +// let customReport = ("This is", "it!") |
| 111 | +// |
| 112 | +// //--- |
| 113 | +// |
| 114 | +// static |
| 115 | +// var onCustomizeReport: OnCustomizeEntityReport |
| 116 | +// { |
| 117 | +// // by default, we don't adjust anything in the report |
| 118 | +// return { |
| 119 | +// |
| 120 | +// _, report in |
| 121 | +// |
| 122 | +// //--- |
| 123 | +// |
| 124 | +// report = customReport |
| 125 | +// } |
| 126 | +// } |
| 127 | +// } |
| 128 | +// |
| 129 | +// let defaultReport = SomeEntity.defaultReport(with: []) |
| 130 | +// |
| 131 | +// let report = SomeEntity.prepareReport(with: []) |
| 132 | +// |
| 133 | +// XCTAssert(report != defaultReport) |
| 134 | +// XCTAssert(report == SomeEntity.customReport) |
| 135 | +// } |
| 136 | +// |
| 137 | +// func testManualValidation() |
| 138 | +// { |
| 139 | +// struct ManualValidationEntity: SomeValidatableEntity |
| 140 | +// { |
| 141 | +// static |
| 142 | +// let someStr = "Is invalid" |
| 143 | +// |
| 144 | +// func validate() throws |
| 145 | +// { |
| 146 | +// let issues: [Error] = [ |
| 147 | +// ValidationError.valueIsNotValid( |
| 148 | +// origin: "Some wrapper", |
| 149 | +// value: "Some value", |
| 150 | +// failedConditions: ["Test condition"], |
| 151 | +// report: (title: "Some test value", message: type(of: self).someStr) |
| 152 | +// ) |
| 153 | +// ] |
| 154 | +// |
| 155 | +// throw issues.asValidationIssues(for: self) |
| 156 | +// } |
| 157 | +// } |
| 158 | +// |
| 159 | +// //--- |
| 160 | +// |
| 161 | +// do |
| 162 | +// { |
| 163 | +// try ManualValidationEntity().validate() |
| 164 | +// |
| 165 | +// XCTFail("Should not get here ever") |
| 166 | +// } |
| 167 | +// catch ValidationError.entityIsNotValid( |
| 168 | +// let origin, |
| 169 | +// let issues, |
| 170 | +// _ |
| 171 | +// ) |
| 172 | +// { |
| 173 | +// XCTAssert(origin == ManualValidationEntity.displayName) |
| 174 | +// XCTAssert(issues.count == 1) // exactly as we've sent |
| 175 | +// |
| 176 | +// let report = (issues[0] as! ValidationError).report |
| 177 | +// |
| 178 | +// XCTAssert(report.message == ManualValidationEntity.someStr) |
| 179 | +// } |
| 180 | +// catch |
| 181 | +// { |
| 182 | +// print(error) |
| 183 | +// XCTFail("Should not get here ever") |
| 184 | +// } |
| 185 | +// } |
| 186 | +// |
| 187 | +// func testAutoValidatable() |
| 188 | +// { |
| 189 | +// struct SimpleWrapper: SomeValidatableValue, |
| 190 | +// SomeValidatable |
| 191 | +// { |
| 192 | +// static |
| 193 | +// let someStr = "Is invalid" |
| 194 | +// |
| 195 | +// enum Specification: SomeValueSpecification |
| 196 | +// { |
| 197 | +// typealias RawValue = String? |
| 198 | +// } |
| 199 | +// |
| 200 | +// var rawValue: Specification.RawValue |
| 201 | +// |
| 202 | +// init(wrappedValue: Specification.RawValue) { self.rawValue = wrappedValue } |
| 203 | +// |
| 204 | +// func validate() throws |
| 205 | +// { |
| 206 | +// if |
| 207 | +// rawValue == nil |
| 208 | +// { |
| 209 | +// throw ValidationError.mandatoryValueIsNotSet( |
| 210 | +// origin: type(of: self).displayName, |
| 211 | +// report: ( |
| 212 | +// title: "Mandatory value is missing", |
| 213 | +// message: type(of: self).someStr |
| 214 | +// ) |
| 215 | +// ) |
| 216 | +// } |
| 217 | +// } |
| 218 | +// } |
| 219 | +// |
| 220 | +// struct AutoValidationEntity: SomeValidatableEntity |
| 221 | +// { |
| 222 | +// let stringWrapper: SimpleWrapper |
| 223 | +// } |
| 224 | +// |
| 225 | +// //--- |
| 226 | +// |
| 227 | +// do |
| 228 | +// { |
| 229 | +// try AutoValidationEntity |
| 230 | +// .init(stringWrapper: SimpleWrapper(wrappedValue: nil)) |
| 231 | +// .validate() |
| 232 | +// |
| 233 | +// XCTFail("Should not get here ever") |
| 234 | +// } |
| 235 | +// catch ValidationError.entityIsNotValid( |
| 236 | +// let origin, |
| 237 | +// let issues, |
| 238 | +// _ |
| 239 | +// ) |
| 240 | +// { |
| 241 | +// XCTAssert(origin == AutoValidationEntity.displayName) |
| 242 | +// XCTAssert(issues.count == 1) |
| 243 | +// |
| 244 | +// let report = (issues[0] as! ValidationError).report |
| 245 | +// |
| 246 | +// XCTAssert(report.message == SimpleWrapper.someStr) |
| 247 | +// } |
| 248 | +// catch |
| 249 | +// { |
| 250 | +// print(error) |
| 251 | +// XCTFail("Should not get here ever") |
| 252 | +// } |
| 253 | +// |
| 254 | +// //--- |
| 255 | +// |
| 256 | +// do |
| 257 | +// { |
| 258 | +// try AutoValidationEntity |
| 259 | +// .init(stringWrapper: SimpleWrapper(wrappedValue: "Some valid value")) |
| 260 | +// .validate() |
| 261 | +// } |
| 262 | +// catch |
| 263 | +// { |
| 264 | +// print(error) |
| 265 | +// XCTFail("Should not get here ever") |
| 266 | +// } |
| 267 | +// } |
| 268 | +} |
0 commit comments