Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
5 changes: 5 additions & 0 deletions Clarinet.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ telemetry = false
cache_dir = './.cache'
requirements = []

[contracts.annotations_test]
path = "tests/contracts/generator-tests/annotations_test.clar"
clarity_version = 3
epoch = "3.1"

[repl.analysis]
passes = ['check_checker']

Expand Down
10 changes: 9 additions & 1 deletion deployments/default.simnet-plan.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,12 @@ genesis:
- cost-voting
- bns
plan:
batches: []
batches:
- id: 0
transactions:
- emulated-contract-publish:
contract-name: annotations_test
emulated-sender: ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM
path: tests/contracts/generator-tests/annotations_test.clar
clarity-version: 3
epoch: "3.1"
1 change: 1 addition & 0 deletions example/tests/my-contract_test.clar
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
)
)

;; @caller 'ST000000000000000000002AMW42H
(define-public (test-a-times-b2)
(begin
(asserts! (is-eq (ok u108) (contract-call? .my-contract a-times-b u9 u12))
Expand Down
25 changes: 13 additions & 12 deletions src/clarunit-generator.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import { Simnet, tx } from "@hirosystems/clarinet-sdk";
import { describe, it } from "vitest";
import {
extractTestAnnotations,
} from "./parser/clarity-parser";
import { extractTestAnnotations } from "./parser/clarity-parser";
import { expectOkTrue, isValidTestFunction } from "./parser/test-helpers";
import { FunctionAnnotations } from "./parser/clarity-parser-flow-tests";

/**
* Returns true if the contract is a test contract
* @param contractName name of the contract
* @returns
* @returns
*/
function isTestContract(contractName: string) {
return (
Expand Down Expand Up @@ -44,10 +42,11 @@ export function generateUnitTests(simnet: Simnet) {
annotations[functionName] || {};

const mineBlocksBefore =
parseInt(annotations["mine-blocks-before"] as string) || 0;
parseInt(functionAnnotations["mine-blocks-before"] as string) || 0;
Comment thread
friedger marked this conversation as resolved.

const testDescription = `${functionCall.name}${functionAnnotations.name ? `: ${functionAnnotations.name}` : ""
}`;
const testDescription = `${functionCall.name}${
functionAnnotations.name ? `: ${functionAnnotations.name}` : ""
}`;
it(testDescription, () => {
// handle prepare function for this test
if (hasDefaultPrepareFunction && !functionAnnotations.prepare)
Expand All @@ -56,11 +55,13 @@ export function generateUnitTests(simnet: Simnet) {
delete functionAnnotations.prepare;

// handle caller address for this test
const callerAddress = functionAnnotations.caller
? annotations.caller[0] === "'"
? `${(annotations.caller as string).substring(1)}`
: accounts.get(annotations.caller)!
: accounts.get("deployer")!;
const callerAddress =
functionAnnotations.caller &&
typeof functionAnnotations.caller === "string"
? functionAnnotations.caller[0] === "'"
? `${(functionAnnotations.caller as string).substring(1)}`
: accounts.get(functionAnnotations.caller)!
: accounts.get("deployer")!;

if (functionAnnotations.prepare) {
// mine block with prepare function call
Expand Down
8 changes: 8 additions & 0 deletions tests/clarity-parser-flow.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ describe("verify clarity parser for flow tests", () => {
functionName: "allow-contract-caller",
},
});
expect(callInfos["test-simple-flow"][3]).toEqual({
callAnnotations: { caller: "'ST000000000000000000002AMW42H" },
callInfo: {
args: [],
contractName: "",
functionName: "my-test-function",
},
});
});

it("should parse flow test with bad annotations", () => {
Expand Down
4 changes: 4 additions & 0 deletions tests/clarity-parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ describe("verify clarity parser", () => {
"mine-before": "20",
name: "all annotation test 2",
});

expect(result["test-all-annotations-3"]).toEqual({
caller: "'ST000000000000000000002AMW42H",
});
});

it("should parse with bad annotations", () => {
Expand Down
3 changes: 3 additions & 0 deletions tests/clarunit.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { clarunit } from "../src/index";

clarunit(simnet);
27 changes: 27 additions & 0 deletions tests/contracts/generator-tests/annotations_test.clar
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

;; test block-height at launch
;; One block is need to advance to epoch 2.5
(define-public (test-block-height-at-launch)
(begin
(asserts! (is-eq u3 stacks-block-height) (err (concat "expected block height 3, found " (int-to-ascii stacks-block-height))))
(ok true)))

;; @mine-blocks-before 10
(define-public (test-mine-blocks-before)
(begin
(asserts! (is-eq u13 stacks-block-height) (err (concat "expected block height 13, found " (int-to-ascii stacks-block-height))))
(ok true)))

;; @caller wallet_1
(define-public (test-caller)
(begin
(asserts! (is-eq tx-sender 'ST1SJ3DTE5DN7X54YDH5D64R3BCB6A2AG2ZQ8YPD5) (err tx-sender))
(asserts! (is-eq contract-caller 'ST1SJ3DTE5DN7X54YDH5D64R3BCB6A2AG2ZQ8YPD5) (err contract-caller))
(ok true)))

;; @caller 'SP3FBR2AGK5H9QBDH3EEN6DF8EK8JY7RX8QJ5SVTE
(define-public (test-caller-2)
(begin
(asserts! (is-eq tx-sender 'SP3FBR2AGK5H9QBDH3EEN6DF8EK8JY7RX8QJ5SVTE) (err tx-sender))
(asserts! (is-eq contract-caller 'SP3FBR2AGK5H9QBDH3EEN6DF8EK8JY7RX8QJ5SVTE) (err contract-caller))
(ok true)))
4 changes: 4 additions & 0 deletions tests/contracts/parser-tests/all-annotations.clar
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,8 @@
;; @mine-before 20
;; @caller wallet_2
(define-public (test-all-annotations-2)
(ok true))

;; @caller 'ST000000000000000000002AMW42H
(define-public (test-all-annotations-3)
(ok true))
2 changes: 2 additions & 0 deletions tests/contracts/parser-tests/simple-flow.clar
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
(try! (my-test-function2))
;; @caller wallet_1
(unwrap! (contract-call? 'ST000000000000000000002AMW42H.pox-4 allow-contract-caller .pox4-self-service-multi none) (err "allow-contract-caller failed"))
;; @caller 'ST000000000000000000002AMW42H
(try! (my-test-function))
(ok true)))

(define-public (my-test-function)
Expand Down