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

Add unit tests in Clarity #4100

Closed
wants to merge 12 commits into from
4 changes: 3 additions & 1 deletion contrib/core-contract-tests/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ npm-debug.log*
coverage
*.info
costs-reports.json
node_modules
node_modules
*.log.txt
history.txt
8 changes: 8 additions & 0 deletions contrib/core-contract-tests/Clarinet.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,11 @@ costs_version = 1
[contracts.bns]
path = "../../stackslib/src/chainstate/stacks/boot/bns.clar"
depends_on = []

[contracts.bns_test]
path = "contracts/bns-tests/bns_test.clar"
depends_on = []

[contracts.bns_flow_test]
path = "contracts/bns-tests/bns_flow_test.clar"
depends_on = []
19 changes: 19 additions & 0 deletions contrib/core-contract-tests/contracts/bns-tests/bns_flow_test.clar
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
(define-constant ERR_NAMESPACE_NOT_FOUND 1005)

;; @name: test delegation to wallet_2, stacking and revoking
(define-public (test-name-registration)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In a future PR I think it's doable (and a bit challenging too) to enable tests with parameters, to run those tests also as fuzz tests:

(define-public (test-name-registration (hashed-salted-fqn (buff 20))
                                       (stx-to-burn uint)
                                       (namespace (buff 20))
                                       (name (buff 48))
                                       (salt (buff 20))
                                       (zonefile-hash (buff 20)))

The tricky part might be the extra work around regexes, though the heavily lifting part around fast-check shouldn't be an issue.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can specify parameter testing / fuzzing with annotations. The regexes originally came from a concept implementation to enable us to write Clarity tests in Clarity. After that, they were good enough so we just left it. If we want to make it more robust we will need to switch to a LISP interpreter, should not be too hard.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we can sort this part out, I can integrate this with fast-check and we'll have it generate/fuzz everything for us. What's very nice about this is the shrinking part; if there's a bug/edge-case detected, fast-check will give us the minimum counterexample and a seed to reproduce the failure (if the bug/edge-case is detected when running on CI).

(begin
;; @caller wallet_1
(unwrap! (contract-call? .bns name-preorder 0x0123456789abcdef01230123456789abcdef0123 u1000000) (err "name-preorder by wallet 1 should succeed"))
;; @caller wallet_2
(unwrap! (contract-call? .bns name-preorder 0x30123456789abcdef01230123456789abcdef012 u1000000) (err "name-preorder by wallet 2 should succeed"))

;; @mine-blocks-before 100
;; @caller wallet_1
(try! (register))
(ok true)))

(define-public (register)
(let ((result (contract-call? .bns name-register 0x123456 0x123456 0x123456 0x)))
(asserts! (is-eq result (err ERR_NAMESPACE_NOT_FOUND)) (err "name-register should fail"))
(ok true)))
10 changes: 10 additions & 0 deletions contrib/core-contract-tests/contracts/bns-tests/bns_test.clar
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
(define-constant ERR_NAMESPACE_NOT_FOUND 1005)

;; @name: test preorder and publish with invalid names
;; @caller: wallet_1
(define-public (test-name-registration)
(begin
(unwrap! (contract-call? .bns name-preorder 0x0123456789abcdef01230123456789abcdef0123 u1000000) (err "preorder should succeeed"))
(let ((result (contract-call? .bns name-register 0x123456 0x123456 0x123456 0x)))
(asserts! (is-eq result (err ERR_NAMESPACE_NOT_FOUND)) (err "registration should fail"))
(ok true))))
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
;; @name all annotation test
;; @description all annotation test
;; @mine-before 10
;; @caller wallet_1
(define-public (test-all-annotations-1)
(ok true))

;; @name all annotation test 2
;; @description all annotation test 2
;; @mine-before 20
;; @caller wallet_2
(define-public (test-all-annotations-2)
(ok true))
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
;; @namexx bad annotation test
;; @mine-after 10
;; @callerxx wallet_1
(define-public (test-bad-annotations)
(ok false))
12 changes: 12 additions & 0 deletions contrib/core-contract-tests/contracts/parser-tests/bad-flow.clar
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
;;@name bad annotation flow test
(define-public (test-bad-flow)
(begin
;; @caller wallet_1
(try! (my-test-function))
(unwrap! (contract-call? invalid-syntax))
(unwrap! (contract-call? .bns))
(ok true)))


(define-private (my-test-function)
(ok true))
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
(define-public (test-no-annotations)
(ok true))
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
;; @name simple annotation test
(define-public (test-simple-annotations)
;; @mine-before is ignored here
(ok true))
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
;;@name simple flow test
(define-public (test-simple-flow)
(begin
;; @caller wallet_1
(try! (my-test-function))
;; @caller wallet_2
(unwrap! (contract-call? .bns name-resolve 0x 0x))
(ok true)))

(define-public (my-test-function)
(ok true))
68 changes: 68 additions & 0 deletions contrib/core-contract-tests/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions contrib/core-contract-tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
"@hirosystems/clarinet-sdk": "^1.1.0",
"@stacks/transactions": "^6.9.0",
"chokidar-cli": "^3.0.0",
"fast-check": "^3.13.1",
"path": "^0.12.7",
"typescript": "^5.2.2",
"vite": "^4.4.9",
"vitest": "^0.34.4",
Expand Down
Loading