Skip to content

Commit 18c8e40

Browse files
committed
Fix short-circuit semantics for || and &&
1 parent 1d685a1 commit 18c8e40

File tree

3 files changed

+16
-8
lines changed

3 files changed

+16
-8
lines changed

scalactic/src/main/scala/org/scalactic/Bool.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ trait Bool {
115115
* @param bool another <code>Bool</code>
116116
* @return a <code>Bool</code> that represents the result of logical <code>and</code>
117117
*/
118-
def &&(bool: Bool): Bool =
118+
def &&(bool: => Bool): Bool =
119119
if (value)
120120
new AndBool(this, bool, prettifier)
121121
else
@@ -127,7 +127,7 @@ trait Bool {
127127
* @param bool another <code>Bool</code>
128128
* @return a <code>Bool</code> that represents the result of logical <code>and</code>
129129
*/
130-
def &(bool: Bool): Bool = &&(bool)
130+
def &(bool: => Bool): Bool = &&(bool)
131131

132132
/**
133133
* Logical <code>or</code> this <code>Bool</code> with another <code>Bool</code>
@@ -369,7 +369,7 @@ private[scalactic] class SimpleBool(expression: Boolean, val prettifier: Prettif
369369
* @param bool1 the first <code>Bool</code>
370370
* @param bool2 the second <code>Bool</code>
371371
*/
372-
private[scalactic] class AndBool(bool1: Bool, bool2: Bool, val prettifier: Prettifier) extends Bool {
372+
private[scalactic] class AndBool(bool1: Bool, bool2: => Bool, val prettifier: Prettifier) extends Bool {
373373

374374
/**
375375
* the result of <code>bool1.value</code> logical <code>AND</code> <code>bool2.value</code>
@@ -439,7 +439,7 @@ private[scalactic] class AndBool(bool1: Bool, bool2: Bool, val prettifier: Prett
439439
* @param bool1 the first <code>Bool</code>
440440
* @param bool2 the second <code>Bool</code>
441441
*/
442-
private[scalactic] class OrBool(bool1: Bool, bool2: Bool, val prettifier: Prettifier) extends Bool {
442+
private[scalactic] class OrBool(bool1: Bool, bool2: => Bool, val prettifier: Prettifier) extends Bool {
443443

444444
/**
445445
* the result of <code>bool1.value</code> logical <code>OR</code> <code>bool2.value</code>

scalatest-test-dotty/src/test/scala/org/scalatest/AssertionsSpec.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -255,10 +255,10 @@ class AssertionsSpec extends FunSpec {
255255
FailureMessages.wasLessThanOrEqualTo(prettifier, left, right)
256256

257257
def commaAnd(left: String, right: String): String =
258-
FailureMessages.commaAnd(prettifier, UnquotedString(left), UnquotedString(right))
258+
FailureMessages.commaAnd(prettifier, left, right)
259259

260260
def commaBut(left: String, right: String): String =
261-
FailureMessages.commaBut(prettifier, UnquotedString(left), UnquotedString(right))
261+
FailureMessages.commaBut(prettifier, left, right)
262262

263263
def wasFalse(left: String): String =
264264
left + " was false"
@@ -703,7 +703,7 @@ class AssertionsSpec extends FunSpec {
703703
assert(e.message === Some(equaled(3, 3)))
704704
assert(e.failedCodeFileName === (Some(fileName)))
705705
assert(e.failedCodeLineNumber === (Some(thisLineNumber - 4)))
706-
}
706+
} */
707707

708708
it("should do nothing when is used to check a == 3 && b == 5") {
709709
assert(a == 3 && b == 5)
@@ -888,7 +888,7 @@ class AssertionsSpec extends FunSpec {
888888
it("should do nothing when it is used to check a == 3 && { println(\"hi\"); b == 5}") {
889889
assert(a == 3 && { println("hi"); b == 5})
890890
}
891-
891+
/*
892892
it("should throw TestFailedException with correct message and stack depth when is usesd to check a == 3 && { println(\"hi\"); b == 3}") {
893893
val e = intercept[TestFailedException] {
894894
assert(a == 3 && { println("hi"); b == 3})

scalatest/src/main/scala/org/scalatest/AssertionsMacro.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,10 +163,18 @@ object AssertionsMacro {
163163
val left = parse(lhs.seal[Boolean], prettifier)
164164
val right = parse(rhs.seal[Boolean], prettifier)
165165
'(~left || ~right)
166+
case "|" =>
167+
val left = parse(lhs.seal[Boolean], prettifier)
168+
val right = parse(rhs.seal[Boolean], prettifier)
169+
'(~left | ~right)
166170
case "&&" =>
167171
val left = parse(lhs.seal[Boolean], prettifier)
168172
val right = parse(rhs.seal[Boolean], prettifier)
169173
'(~left && ~right)
174+
case "&" =>
175+
val left = parse(lhs.seal[Boolean], prettifier)
176+
val right = parse(rhs.seal[Boolean], prettifier)
177+
'(~left & ~right)
170178
case _ =>
171179
defaultCase
172180
}

0 commit comments

Comments
 (0)