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

Format all test contracts consistently #61

Merged
merged 1 commit into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 73 additions & 44 deletions example/contracts/cargo.tests.clar
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,16 @@
(let
(
(create-shipment-num-calls
(default-to u0 (get called (map-get? context "create-new-shipment"))))
(default-to u0 (get called (map-get? context "create-new-shipment")))
)
)
(if (> create-shipment-num-calls u0)
(> (var-get last-shipment-id) u0)
true)))
(if
(is-eq create-shipment-num-calls u0)
true
(> (var-get last-shipment-id) u0)
)
)
)

;; Properties

Expand All @@ -17,90 +22,114 @@
(define-constant ERR_CONTRACT_CALL_FAILED (err 2))
(define-constant ERR_SHIPMENT_NOT_FOUND (err 3))

(define-public (test-create-new-shipment (starting-location (string-ascii 25))
(receiver principal))
(define-public (test-create-new-shipment
(starting-location (string-ascii 25))
(receiver principal)
)
(let
(
(shipment-id-before (get-last-shipment-id))
;; Call create-new-shipment and check the last shipment ID incremented
;; by 1.
;; Call create-new-shipment and assert based on the result.
(result
(unwrap!
(create-new-shipment starting-location receiver)
ERR_CONTRACT_CALL_FAILED))
ERR_CONTRACT_CALL_FAILED
)
)
)
(asserts!
(is-eq result "Shipment created successfully")
ERR_ASSERTION_FAILED)
(ok true)))
ERR_ASSERTION_FAILED
)
(ok true)
)
)

(define-public (test-get-last-shipment-id (starting-location (string-ascii 25))
(receiver principal))
(define-public (test-get-last-shipment-id
(starting-location (string-ascii 25))
(receiver principal)
)
(let
(
(shipment-id-before (get-last-shipment-id))
)
((shipment-id-before (get-last-shipment-id)))
(unwrap!
(create-new-shipment starting-location receiver)
ERR_CONTRACT_CALL_FAILED)
ERR_CONTRACT_CALL_FAILED
)
;; Verify the last shipment ID is incremented by 1.
(asserts!
(is-eq
(get-last-shipment-id)
(+ shipment-id-before u1))
ERR_ASSERTION_FAILED)
(ok true)))
(is-eq (get-last-shipment-id) (+ u1 shipment-id-before))
ERR_ASSERTION_FAILED
)
(ok true)
)
)

(define-public (test-update-shipment (starting-location (string-ascii 25))
(receiver principal)
(new-location (string-ascii 25)))
(define-public (test-update-shipment
(starting-location (string-ascii 25))
(receiver principal)
(new-location (string-ascii 25))
)
(let
(
(create-result
(unwrap!
(create-new-shipment starting-location receiver)
ERR_CONTRACT_CALL_FAILED))
ERR_CONTRACT_CALL_FAILED
)
)
(shipment-id (get-last-shipment-id))
(update-result
(unwrap!
(update-shipment shipment-id new-location)
ERR_CONTRACT_CALL_FAILED))
ERR_CONTRACT_CALL_FAILED
)
)
(updated-shipment
(default-to
{
{
status: "Does not exist",
location: "Does not exist",
shipper: tx-sender,
receiver: tx-sender
}
(get-shipment-optional shipment-id)))
}
(get-shipment-optional shipment-id)
)
)
)
;; Verify the location is updated.
(asserts!
(is-eq
(get location updated-shipment)
new-location)
ERR_ASSERTION_FAILED)
(ok true)))
(is-eq (get location updated-shipment) new-location)
ERR_ASSERTION_FAILED
)
(ok true)
)
)

(define-public (test-get-shipment (starting-location (string-ascii 25))
(receiver principal))
(define-public (test-get-shipment
(starting-location (string-ascii 25))
(receiver principal)
)
(let
(
(create-result
(unwrap!
(create-new-shipment starting-location receiver)
ERR_CONTRACT_CALL_FAILED))
ERR_CONTRACT_CALL_FAILED
)
)
(shipment-id (get-last-shipment-id))
(retrieved-shipment
(unwrap!
(get-shipment-optional shipment-id)
ERR_SHIPMENT_NOT_FOUND))
ERR_SHIPMENT_NOT_FOUND
)
)
)
;; Verify the location is correct.
(asserts!
(is-eq
(get location retrieved-shipment)
starting-location)
ERR_ASSERTION_FAILED)
(ok true)))
(is-eq (get location retrieved-shipment) starting-location)
ERR_ASSERTION_FAILED
)
(ok true)
)
)
2 changes: 1 addition & 1 deletion example/contracts/counter.clar
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
;; (define-public (increment)
;; (let ((current-counter (var-get counter)))
;; (if (> current-counter u1000) ;; Introduce a bug for large values.
;; (ok (var-set counter u0)) ;; Reset counter to zero if it exceeds 1000.
;; (ok (var-set counter u0)) ;; Reset counter to zero if it exceeds 1000.
;; (ok (var-set counter (+ current-counter u1)))
;; )
;; )
Expand Down
53 changes: 39 additions & 14 deletions example/contracts/counter.tests.clar
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,54 @@

(define-read-only (invariant-counter-gt-zero)
(let
((increment-num-calls (default-to u0 (get called (map-get? context "increment"))))
(decrement-num-calls (default-to u0 (get called (map-get? context "decrement")))))
(if (> increment-num-calls decrement-num-calls)
(> (var-get counter) u0)
true)))
(
(increment-num-calls
(default-to u0
(get called (map-get? context "increment"))
)
)
(decrement-num-calls
(default-to u0
(get called (map-get? context "decrement"))
)
)
)
(if
(<= increment-num-calls decrement-num-calls)
true
(> (var-get counter) u0)
)
)
)

;; Properties

;; This test catches the bug in the counter contract.
(define-public (test-increment)
(let ((counter-before (get-counter)))
(let
((counter-before (get-counter)))
(unwrap-panic (increment))
(asserts! (is-eq (get-counter) (+ counter-before u1)) (err u404))
(ok true)))
(ok true)
)
)

;; Test that takes a parameter. This will be run using property-based techniques.
;; Test that takes a parameter. This will be run using property-based
;; techniques.
(define-public (test-add (n uint))
(let ((counter-before (get-counter)))
(ok
(if
(> n u1)
(let
((counter-before (get-counter)))
(ok
(if
(<= n u1)
;; Discard the test if `add` cannot be called with the given parameter.
false
(begin
(try! (add n))
(asserts! (is-eq (get-counter) (+ counter-before n)) (err u403))
true)
true))))
true
)
)
)
)
)
55 changes: 30 additions & 25 deletions example/contracts/reverse.tests.clar
Original file line number Diff line number Diff line change
Expand Up @@ -5,44 +5,49 @@
(define-public (test-reverse (seq (list 127 int)))
(begin
(asserts!
(is-eq seq
(reverse
(reverse seq)))
ERR_ASSERTION_FAILED)
(ok true)))
(is-eq seq (reverse (reverse seq)))
ERR_ASSERTION_FAILED
)
(ok true)
)
)

(define-public (test-reverse-uint (seq (list 127 uint)))
(begin
(asserts!
(is-eq seq
(reverse-uint
(reverse-uint seq)))
ERR_ASSERTION_FAILED)
(ok true)))
(is-eq seq (reverse-uint (reverse-uint seq)))
ERR_ASSERTION_FAILED
)
(ok true)
)
)

(define-public (test-reverse-buff (seq (buff 127)))
(begin
(asserts!
(is-eq seq
(reverse-buff
(reverse-buff seq)))
ERR_ASSERTION_FAILED)
(ok true)))
(is-eq seq (reverse-buff (reverse-buff seq)))
ERR_ASSERTION_FAILED
)
(ok true)
)
)

(define-public (test-reverse-string (seq (string-utf8 127)))
(begin
(asserts!
(is-eq seq
(reverse-string
(reverse-string seq)))
ERR_ASSERTION_FAILED)
(ok true)))
(is-eq seq (reverse-string (reverse-string seq)))
ERR_ASSERTION_FAILED
)
(ok true)
)
)

(define-public (test-reverse-ascii (seq (string-ascii 127)))
(begin
(asserts!
(is-eq seq
(reverse-ascii
(reverse-ascii seq)))
ERR_ASSERTION_FAILED)
(ok true)))
(is-eq seq (reverse-ascii (reverse-ascii seq)))
ERR_ASSERTION_FAILED
)
(ok true)
)
)
Loading