-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of github.com:boinx/BXSwiftUtils
# Conflicts: # BXSwiftUtils.xcodeproj/project.pbxproj
- Loading branch information
Showing
3 changed files
with
92 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// | ||
// Operators.swift | ||
// BXSwiftUtils | ||
// | ||
// Created by Benjamin Federer on 05.12.18. | ||
// Copyright © 2018 Boinx Software Ltd. & Imagine GbR. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
infix operator ||= : AssignmentPrecedence | ||
infix operator &&= : AssignmentPrecedence | ||
|
||
extension Bool | ||
{ | ||
public static func ||= (lhs: inout Bool, rhs: @autoclosure () throws -> Bool) rethrows | ||
{ | ||
if (!lhs) | ||
{ | ||
lhs = try rhs() | ||
} | ||
} | ||
|
||
public static func &&= (lhs: inout Bool, rhs: @autoclosure () throws -> Bool) rethrows | ||
{ | ||
if (lhs) | ||
{ | ||
lhs = try rhs() | ||
} | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
BXSwiftUtilsTests/Math & Geometry/Bool+OperatorsTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// | ||
// Bool+OperatorsTests.swift | ||
// BXSwiftUtils | ||
// | ||
// Created by Benjamin Federer on 05.12.18. | ||
// Copyright © 2018 Boinx Software Ltd. & Imagine GbR. All rights reserved. | ||
// | ||
|
||
import XCTest | ||
import BXSwiftUtils | ||
|
||
class Bool_OperatorsTests : XCTestCase | ||
{ | ||
func testORAndAssign() | ||
{ | ||
var boolValue = false | ||
|
||
boolValue ||= false | ||
XCTAssertFalse(boolValue) // 0 0 | 0 | ||
|
||
boolValue ||= true | ||
XCTAssertTrue(boolValue) // 0 1 | 1 | ||
|
||
boolValue ||= true | ||
XCTAssertTrue(boolValue) // 1 1 | 1 | ||
|
||
boolValue ||= false | ||
XCTAssertTrue(boolValue) // 1 0 | 1 | ||
} | ||
|
||
func testANDAndAssign() | ||
{ | ||
var boolValue = false | ||
|
||
boolValue &&= false | ||
XCTAssertFalse(boolValue) // 0 0 | 0 | ||
|
||
boolValue &&= true | ||
XCTAssertFalse(boolValue) // 0 1 | 0 | ||
|
||
boolValue = true | ||
|
||
boolValue &&= true | ||
XCTAssertTrue(boolValue) // 1 1 | 1 | ||
|
||
boolValue &&= false | ||
XCTAssertFalse(boolValue) // 1 0 | 0 | ||
} | ||
} |