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

Simplify Test Case Discarding #53

Closed
moodmosaic opened this issue Nov 20, 2024 · 0 comments · Fixed by #54
Closed

Simplify Test Case Discarding #53

moodmosaic opened this issue Nov 20, 2024 · 0 comments · Fixed by #54

Comments

@moodmosaic
Copy link
Member

moodmosaic commented Nov 20, 2024

As part of the great work in #38, we introduced a way to discard test cases using helper functions like:

(define-read-only (can-test-protocol-burn-balance-decrease (address principal) (amount uint))
  (and
    (is-eq tx-sender deployer)
    (not (is-eq amount u0))
    (not (> amount (unwrap-panic (get-balance-available address))))
  )
)

(define-public (test-protocol-burn-balance-decrease (address principal) (amount uint))
  (let (
      (initial-balance (unwrap-panic (get-balance-available address)))
    )
    (ok
      (begin
        (try! (protocol-burn amount address))
        (asserts!
          (is-eq
            (unwrap-panic (get-balance-available address))
            (- initial-balance amount))
          ERR_FAILED_ASSERTION)
        true))))

While functional, this approach doesn't scale well for larger codebases:

  • The number of discard functions can grow significantly.
  • Maintenance becomes tricky—e.g., changing a function signature requires updates in multiple places.

Instead of using discard functions, we could simplify by signaling discards at the type level:

  • ok(true): Test passes.
  • ok(false): Test is discarded.
  • Errors or panics: Test fails.
(define-public (test-protocol-burn-balance-decrease (address principal) (amount uint))
  (let (
      (initial-balance (unwrap-panic (get-balance-available address)))
    )
    (ok
      (if
        (or
          (not (is-eq tx-sender deployer))
          (is-eq amount u0)
          (> amount initial-balance))
        false ;; Test is discarded.
        (begin
          (try! (protocol-burn amount address))
          (asserts!
            (is-eq
              (unwrap-panic (get-balance-available address))
              (- initial-balance amount))
            ERR_FAILED_ASSERTION) ;; Test fails.
          true))))) ;; Test passes.

This eliminates the need for separate discard functions, reducing boilerplate and improving maintainability.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant