Skip to content

Commit 95a5d02

Browse files
committed
fix: add generator tests
1 parent d89e32e commit 95a5d02

File tree

5 files changed

+57
-13
lines changed

5 files changed

+57
-13
lines changed

Clarinet.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ telemetry = false
66
cache_dir = './.cache'
77
requirements = []
88

9+
[contracts.annotations_test]
10+
path = "tests/contracts/generator-tests/annotations_test.clar"
11+
clarity_version = 3
12+
epoch = "3.1"
13+
914
[repl.analysis]
1015
passes = ['check_checker']
1116

deployments/default.simnet-plan.yaml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,12 @@ genesis:
5656
- cost-voting
5757
- bns
5858
plan:
59-
batches: []
59+
batches:
60+
- id: 0
61+
transactions:
62+
- emulated-contract-publish:
63+
contract-name: annotations_test
64+
emulated-sender: ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM
65+
path: tests/contracts/generator-tests/annotations_test.clar
66+
clarity-version: 3
67+
epoch: "3.1"

src/clarunit-generator.ts

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
import { Simnet, tx } from "@hirosystems/clarinet-sdk";
22
import { describe, it } from "vitest";
3-
import {
4-
extractTestAnnotations,
5-
} from "./parser/clarity-parser";
3+
import { extractTestAnnotations } from "./parser/clarity-parser";
64
import { expectOkTrue, isValidTestFunction } from "./parser/test-helpers";
75
import { FunctionAnnotations } from "./parser/clarity-parser-flow-tests";
86

97
/**
108
* Returns true if the contract is a test contract
119
* @param contractName name of the contract
12-
* @returns
10+
* @returns
1311
*/
1412
function isTestContract(contractName: string) {
1513
return (
@@ -44,10 +42,11 @@ export function generateUnitTests(simnet: Simnet) {
4442
annotations[functionName] || {};
4543

4644
const mineBlocksBefore =
47-
parseInt(annotations["mine-blocks-before"] as string) || 0;
45+
parseInt(functionAnnotations["mine-blocks-before"] as string) || 0;
4846

49-
const testDescription = `${functionCall.name}${functionAnnotations.name ? `: ${functionAnnotations.name}` : ""
50-
}`;
47+
const testDescription = `${functionCall.name}${
48+
functionAnnotations.name ? `: ${functionAnnotations.name}` : ""
49+
}`;
5150
it(testDescription, () => {
5251
// handle prepare function for this test
5352
if (hasDefaultPrepareFunction && !functionAnnotations.prepare)
@@ -56,11 +55,13 @@ export function generateUnitTests(simnet: Simnet) {
5655
delete functionAnnotations.prepare;
5756

5857
// handle caller address for this test
59-
const callerAddress = functionAnnotations.caller && typeof functionAnnotations.caller === "string"
60-
? functionAnnotations.caller[0] === "'"
61-
? `${(functionAnnotations.caller as string).substring(1)}`
62-
: accounts.get(functionAnnotations.caller)!
63-
: accounts.get("deployer")!;
58+
const callerAddress =
59+
functionAnnotations.caller &&
60+
typeof functionAnnotations.caller === "string"
61+
? functionAnnotations.caller[0] === "'"
62+
? `${(functionAnnotations.caller as string).substring(1)}`
63+
: accounts.get(functionAnnotations.caller)!
64+
: accounts.get("deployer")!;
6465

6566
if (functionAnnotations.prepare) {
6667
// mine block with prepare function call

tests/clarunit.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { clarunit } from "../src/index";
2+
3+
clarunit(simnet);
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2+
;; test block-height at launch
3+
;; One block is need to advance to epoch 2.5
4+
(define-public (test-block-height-at-launch)
5+
(begin
6+
(asserts! (is-eq u3 stacks-block-height) (err (concat "expected block height 3, found " (int-to-ascii stacks-block-height))))
7+
(ok true)))
8+
9+
;; @mine-blocks-before 10
10+
(define-public (test-mine-blocks-before)
11+
(begin
12+
(asserts! (is-eq u13 stacks-block-height) (err (concat "expected block height 13, found " (int-to-ascii stacks-block-height))))
13+
(ok true)))
14+
15+
;; @caller wallet_1
16+
(define-public (test-caller)
17+
(begin
18+
(asserts! (is-eq tx-sender 'ST1SJ3DTE5DN7X54YDH5D64R3BCB6A2AG2ZQ8YPD5) (err tx-sender))
19+
(asserts! (is-eq contract-caller 'ST1SJ3DTE5DN7X54YDH5D64R3BCB6A2AG2ZQ8YPD5) (err contract-caller))
20+
(ok true)))
21+
22+
;; @caller 'SP3FBR2AGK5H9QBDH3EEN6DF8EK8JY7RX8QJ5SVTE
23+
(define-public (test-caller-2)
24+
(begin
25+
(asserts! (is-eq tx-sender 'SP3FBR2AGK5H9QBDH3EEN6DF8EK8JY7RX8QJ5SVTE) (err tx-sender))
26+
(asserts! (is-eq contract-caller 'SP3FBR2AGK5H9QBDH3EEN6DF8EK8JY7RX8QJ5SVTE) (err contract-caller))
27+
(ok true)))

0 commit comments

Comments
 (0)