Skip to content

#expect does not show output for try expression #756

Closed as not planned
Closed as not planned
@Kyle-Ye

Description

@Kyle-Ye

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug🪲 Something isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions