Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#expect does not show output for try expression #756

Closed
Kyle-Ye opened this issue Oct 9, 2024 · 3 comments
Closed

#expect does not show output for try expression #756

Kyle-Ye opened this issue Oct 9, 2024 · 3 comments
Labels
bug Something isn't working

Comments

@Kyle-Ye
Copy link
Contributor

Kyle-Ye commented Oct 9, 2024

Description

Motivation

The original logic is a 3 step computation.

However I'd like to test multi instance and name the instance with number make me sad. So I compress the steps into one throwing function.

But then the #expect macro won't show the output for the left expression with try.

let instance1 = step1(1)
let instance1Data = try step2(instance1)
let instance1String = step3(instance1Data)
#expect(instance1String == "1")

let instance2 = step1(2)
let instance2Data = try step2(instance2)
let instance2String = step3(instance2Data)
#expect(instance2String == "2")

...

⬇️

func helper(_ input: Int) throws -> String {
  let instance = step1(input)
  let data = try step2(instance)
  return step3(data)
}

#expect(try helper(1) == "1")
#expect(try helper(2) == "2")
...

Reproduce

A simplified version to demonstrate the issue

enum MultiError: Error {
    case invalidInput
}

func multi(_ a: Int, _ b: Int) throws -> Int {
    guard a > 0, b > 0 else {
        throw MultiError.invalidInput
    }
    return a * b
}

func multiWithoutThrow(_ a: Int, _ b: Int) -> Int {
    return a * b
}

@Test func example() async throws {
    #expect(try multi(1, 2) == 3)
    #expect(multiWithoutThrow(1, 2) == 3)
}

Expected behavior

Expectation failed: (try multi(1, 2) → 2) == 3
Expectation failed: (multiWithoutThrow(1, 2) → 2) == 3

Actual behavior

Expectation failed: try multi(1, 2) == 3
Expectation failed: (multiWithoutThrow(1, 2) → 2) == 3

Steps to reproduce

See code snippet above

swift-testing version/commit hash

Xcode 16.0

Swift & OS version (output of swift --version ; uname -a)

No response

@Kyle-Ye Kyle-Ye added the bug Something isn't working label Oct 9, 2024
@Kyle-Ye
Copy link
Contributor Author

Kyle-Ye commented Oct 9, 2024

Debugging into swift-testing source code.

They both go into "Expectation failed: \(expectation.evaluatedExpression.expandedDescription())"

But their evaluatedExpression is different.

▿ Testing.__Expression.Kind.generic("try multi(1, 2) == 3")
  ▿ kind : Kind
    - generic : "try multi(1, 2) == 3"
  - runtimeValue : nil

▿ Testing.__Expression.Kind.binaryOperation(lhs: Testing.__Expression.Kind.generic("multi2(1, 2)"), operator: "==", rhs: Testing.__Expression.Kind.generic("3"))
  ▿ kind : Kind
    ▿ binaryOperation : 3 elements
      ▿ lhs : Testing.__Expression.Kind.generic("multi2(1, 2)")
        ▿ kind : Kind
          - generic : "multi2(1, 2)"
        ▿ runtimeValue : Optional<Value>
          ▿ some : 2
            - description : "2"
            - debugDescription : "2"
            ▿ typeInfo : Swift.Int
              ▿ _kind : _Kind
                - type : 0 elements
            - label : nil
            - isCollection : false
            - children : nil
      - operator : "=="
      ▿ rhs : Testing.__Expression.Kind.generic("3")
        ▿ kind : Kind
          - generic : "3"
        ▿ runtimeValue : Optional<Value>
          ▿ some : 3
            - description : "3"
            - debugDescription : "3"
            ▿ typeInfo : Swift.Int
              ▿ _kind : _Kind
                - type : 0 elements
            - label : nil
            - isCollection : false
            - children : nil
  - runtimeValue : nil

@Kyle-Ye
Copy link
Contributor Author

Kyle-Ye commented Oct 9, 2024

A temporary workaround is change it from #expect(try multi(1, 2) == 3) to #expect((try multi(1, 2)) == 3)

Then we'll get the expected result.

Expectation failed: ((try multi(1, 2)) → 2) == 3
image

@grynspan
Copy link
Contributor

grynspan commented Oct 9, 2024

Duplicate of #162

@grynspan grynspan marked this as a duplicate of #162 Oct 9, 2024
@grynspan grynspan closed this as not planned Won't fix, can't repro, duplicate, stale Oct 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants