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

Fix: annotations mine-blocks-before and caller #9

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
7 changes: 6 additions & 1 deletion packages/clarunit/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,10 @@
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.

## Unrelease

- fixes mine-blocks-before annotation and caller annotation

## [0.0.1](https://github.com/stacks-network/stacks-test-tools) (2024-01-22)
Initial version

Initial version
5 changes: 5 additions & 0 deletions packages/clarunit/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 = 2
epoch = "2.1"

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

Expand Down
10 changes: 9 additions & 1 deletion packages/clarunit/deployments/default.simnet-plan.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,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: 2
epoch: "2.1"
4 changes: 2 additions & 2 deletions packages/clarunit/package-lock.json

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

27 changes: 14 additions & 13 deletions packages/clarunit/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 All @@ -32,22 +30,24 @@ export function generateUnitTests(simnet: Simnet) {
const hasDefaultPrepareFunction =
contract.functions.findIndex((f) => f.name === "prepare") >= 0;

const source = simnet.getContractSource(contractFQN)!;
const annotations: any = extractTestAnnotations(source);

contract.functions.forEach((functionCall) => {
if (!isValidTestFunction(functionCall)) {
return;
}

const functionName = functionCall.name;
const source = simnet.getContractSource(contractFQN)!;
const annotations: any = extractTestAnnotations(source);
const functionAnnotations: FunctionAnnotations =
annotations[functionName] || {};

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

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,10 +56,11 @@ 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)!
const callerString = functionAnnotations.caller as string;
const callerAddress = callerString
? callerString[0] === "'"
? `${callerString.substring(1)}`
: accounts.get(callerString)!
: accounts.get("deployer")!;

if (functionAnnotations.prepare) {
Expand Down
3 changes: 3 additions & 0 deletions packages/clarunit/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);
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
;; 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 u1 block-height) (err (concat "expected block height 1, found " (int-to-ascii block-height))))
(ok true)))

;; @mine-blocks-before 10
(define-public (test-mine-blocks-before)
(begin
(asserts! (is-eq u11 block-height) (err (concat "expected block height 10, found " (int-to-ascii 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)))