Skip to content

Commit

Permalink
Don't expand ntpdate into two subexpressions, we all know what ! means
Browse files Browse the repository at this point in the history
  • Loading branch information
grynspan committed Jan 24, 2025
1 parent 1a84ca2 commit 3009cf2
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 2 deletions.
6 changes: 6 additions & 0 deletions Sources/TestingMacros/Support/ConditionArgumentParsing.swift
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,12 @@ private final class _ContextInserter<C, M>: SyntaxRewriter where C: MacroExpansi
}
}

if node.operator.tokenKind == .prefixOperator("!") {
// Do not break apart the boolean negation operator from its expression
// (it adds visual noise but is really just flipping a bit.)
return _rewrite(node)
}

return _rewrite(
node
.with(\.expression, _visitChild(node.expression)),
Expand Down
2 changes: 2 additions & 0 deletions Tests/SubexpressionShowcase/SubexpressionShowcase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ func subexpressionShowcase() async throws {
#expect((123, 456) == (789, 0x12))
#expect((try g() > 500) && true)

#expect(!Bool(true))

do {
let n = Int.random(in: 0 ..< 100)
var m = n
Expand Down
4 changes: 2 additions & 2 deletions Tests/TestingMacrosTests/ConditionMacroTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ struct ConditionMacroTests {
##"#expect("a" == "b")"##:
##"Testing.__checkCondition({ (__ec: inout Testing.__ExpectationContext) -> Swift.Bool in __ec.__cmp({ $0 == $1 }, 0x0, "a", 0x2, "b", 0x200) }, sourceCode: [0x0: #""a" == "b""#], comments: [], isRequired: false, sourceLocation: Testing.SourceLocation.__here()).__expected()"##,
##"#expect(!Bool.random())"##:
##"Testing.__checkCondition({ (__ec: inout Testing.__ExpectationContext) -> Swift.Bool in __ec(!__ec(Bool.random(), 0x4), 0x0) }, sourceCode: [0x0: "!Bool.random()", 0x4: "Bool.random()"], comments: [], isRequired: false, sourceLocation: Testing.SourceLocation.__here()).__expected()"##,
##"Testing.__checkCondition({ (__ec: inout Testing.__ExpectationContext) -> Swift.Bool in __ec(!Bool.random(), 0x0) }, sourceCode: [0x0: "!Bool.random()"], comments: [], isRequired: false, sourceLocation: Testing.SourceLocation.__here()).__expected()"##,
##"#expect((true && false))"##:
##"Testing.__checkCondition({ (__ec: inout Testing.__ExpectationContext) -> Swift.Bool in __ec((__ec(__ec(true, 0x3c) && __ec(false, 0x21c), 0x1c)), 0x0) }, sourceCode: [0x0: "(true && false)", 0x1c: "true && false", 0x3c: "true", 0x21c: "false"], comments: [], isRequired: false, sourceLocation: Testing.SourceLocation.__here()).__expected()"##,
##"#expect(try x())"##:
Expand Down Expand Up @@ -118,7 +118,7 @@ struct ConditionMacroTests {
##"#require("a" == "b")"##:
##"Testing.__checkCondition({ (__ec: inout Testing.__ExpectationContext) -> Swift.Bool in try Testing.__requiringTry(__ec.__cmp({ $0 == $1 }, 0x0, "a", 0x2, "b", 0x200)) }, sourceCode: [0x0: #""a" == "b""#], comments: [], isRequired: true, sourceLocation: Testing.SourceLocation.__here()).__required()"##,
##"#require(!Bool.random())"##:
##"Testing.__checkCondition({ (__ec: inout Testing.__ExpectationContext) -> Swift.Bool in try Testing.__requiringTry(__ec(!__ec(Bool.random(), 0x4), 0x0)) }, sourceCode: [0x0: "!Bool.random()", 0x4: "Bool.random()"], comments: [], isRequired: true, sourceLocation: Testing.SourceLocation.__here()).__required()"##,
##"Testing.__checkCondition({ (__ec: inout Testing.__ExpectationContext) -> Swift.Bool in try Testing.__requiringTry(__ec(!Bool.random(), 0x0)) }, sourceCode: [0x0: "!Bool.random()"], comments: [], isRequired: true, sourceLocation: Testing.SourceLocation.__here()).__required()"##,
##"#require((true && false))"##:
##"Testing.__checkCondition({ (__ec: inout Testing.__ExpectationContext) -> Swift.Bool in try Testing.__requiringTry(__ec((__ec(__ec(true, 0x3c) && __ec(false, 0x21c), 0x1c)), 0x0)) }, sourceCode: [0x0: "(true && false)", 0x1c: "true && false", 0x3c: "true", 0x21c: "false"], comments: [], isRequired: true, sourceLocation: Testing.SourceLocation.__here()).__required()"##,
##"#require(try x())"##:
Expand Down

0 comments on commit 3009cf2

Please sign in to comment.