Skip to content

Commit

Permalink
add
Browse files Browse the repository at this point in the history
  • Loading branch information
BowTiedDevOps committed Apr 4, 2024
1 parent f7ef39b commit c924505
Show file tree
Hide file tree
Showing 43 changed files with 7,188 additions and 0 deletions.
9 changes: 9 additions & 0 deletions packages/bowtieddevops-test-package-5/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
logs
*.log
npm-debug.log*
coverage
*.info
costs-reports.json
node_modules
*.log.txt
history.txt
7 changes: 7 additions & 0 deletions packages/bowtieddevops-test-package-5/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Change Log

All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.

## [0.0.1](https://github.com/stacks-network/stacks-test-tools) (2024-01-22)
Initial version
16 changes: 16 additions & 0 deletions packages/bowtieddevops-test-package-5/Clarinet.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[project]
name = 'clarunit test contracts'
description = ''
authors = []
telemetry = false
cache_dir = './.cache'
requirements = []

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

[repl.analysis.check_checker]
strict = false
trusted_sender = false
trusted_caller = false
callee_filter = false
126 changes: 126 additions & 0 deletions packages/bowtieddevops-test-package-5/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
# clarunit

This package allows you to write unit tests for Clarity smart contracts in the
Clarity language itself, as opposed to TypeScript. `clarunit` will automatically
detect test files and test functions.

An example Clarinet-sdk project using `clarunit` can be found in the `example`
folder.

## Setup

1. Install package `@stacks/clarunit` using your favourite package manager. (Be sure to
pin the version!)
2. Create a test file in your existing `tests` folder, you can use any name but
using `clarunit.test.ts` is recommended.
3. Add the following to the newly created file:
```ts
import { clarunit } from "clarunit";
clarunit(simnet);
```
4. Run your tests per usual using `npm test` or `yarn test`.

`clarunit` takes configuration from Clarinet via `Clarinet.toml`. It
automatically detects all instantiated test contracts.

## Unit testing

### Adding tests

To write unit tests, follow these steps:

1. Create a new Clarity contract in the `./tests` folder. It can have any name
but it should end in `_test.clar`. Files that do not follow this convention
are ignored. (For example: `my-contract_test.clar` will be included and
`my-contract.clar` will not.)
2. Add the new Clarity contract to `Clarinet.toml`.
3. Write unit tests as public functions, the function name must start with
`test-`.

### Writing tests

Unit test functions should be public without parameters. If they return an `ok`
response of any kind, the test is considered to have passed whereas an `err`
indicates a failure. The failure value is printed so it can be used to provide a
helpful message. The body of the unit test is written like one would usually
write Clarity, using `try!` and `unwrap!` and so on as needed.

Example:

```clarity
(define-public (test-my-feature)
(begin
(unwrap! (contract-call? .some-project-contract my-feature) (err "Calling my-feature failed"))
(ok true)
)
)
```

### Prepare function

Sometimes you need to run some preparation logic that is common to all or
multiple unit tests. If the script detects a function called `prepare`, it will
be invoked before calling the unit test function itself. The `prepare` function
should return an `ok`, otherwise the test fails.

```clarity
(define-public (prepare)
(begin
(unwrap! (contract-call? .some-project-contract prepare-something) (err "Preparation failed"))
(ok true)
)
)
(define-public (test-something)
;; prepare will be executed before running the test.
)
```

### Annotations

You can add certain comment annotations before unit test functions to add
information or modify behaviour. Annotations are optional.

| Annotation | Description |
|-----------------------|----------------------------------------------------------------------------------------------------------------------------------------------|
| `@name` | Give the unit test a name, this text shows up when running unit tests. |
| `@no-prepare` | Do not call the `prepare` function before running this unit test. |
| `@prepare` | Override the default `prepare` function with another. The function name should follow the tag. |
| `@caller` | Override the default caller when running this unit test. Either specify an account name or standard principal prefixed by a single tick `'`. |
| `@mine-blocks-before` | Mine a number of blocks before running the test. The number of blocks should follow the tag. |

Examples:

```clarity
(define-public (prepare) (ok "Default prepare function"))
(define-public (custom-prepare) (ok "A custom prepare function"))
;; A test without any annotations
(define-public (test-zero) (ok true))
;; @name A normal test with a name, the prepare function will run before.
(define-public (test-one) (ok true))
;; @name This test will be executed without running the default prepare function.
;; @no-prepare
(define-public (test-two) (ok true))
;; @name Override the default prepare function, it will run custom-prepare instead.
;; @prepare custom-prepare
(define-public (test-three) (ok true))
;; @name This test will be called with tx-sender set to wallet_1 (from the settings toml file).
;; @caller wallet_1
(define-public (test-four) (ok true))
;; @name This test will be called with tx-sender set to the specified principal.
;; @caller 'ST2CY5V39NHDPWSXMW9QDT3HC3GD6Q6XX4CFRK9AG
(define-public (test-five) (ok true))
;; @name Five blocks are mined before this test is executed.
;; @mine-blocks-before 5
(define-public (test-six) (ok true))
```

// test
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
---
id: 0
name: "Simulated deployment, used as a default for `clarinet console`, `clarinet test` and `clarinet check`"
network: simnet
genesis:
wallets:
- name: deployer
address: ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM
balance: "100000000000000"
- name: faucet
address: STNHKEPYEPJ8ET55ZZ0M5A34J0R3N5FM2CMMMAZ6
balance: "100000000000000"
- name: wallet_1
address: ST1SJ3DTE5DN7X54YDH5D64R3BCB6A2AG2ZQ8YPD5
balance: "100000000000000"
- name: wallet_2
address: ST2CY5V39NHDPWSXMW9QDT3HC3GD6Q6XX4CFRK9AG
balance: "100000000000000"
- name: wallet_3
address: ST2JHG361ZXG51QTKY2NQCVBPPRRE2KZB1HR05NNC
balance: "100000000000000"
- name: wallet_4
address: ST2NEB84ASENDXKYGJPQW86YXQCEFEX2ZQPG87ND
balance: "100000000000000"
- name: wallet_5
address: ST2REHHS5J3CERCRBEPMGH7921Q6PYKAADT7JP2VB
balance: "100000000000000"
- name: wallet_6
address: ST3AM1A56AK2C1XAFJ4115ZSV26EB49BVQ10MGCS0
balance: "100000000000000"
- name: wallet_7
address: ST3PF13W7Z0RRM42A8VZRVFQ75SV1K26RXEP8YGKJ
balance: "100000000000000"
- name: wallet_8
address: ST3NBRSFKX28FQ2ZJ1MAKX58HKHSDGNV5N7R21XCP
balance: "100000000000000"
contracts:
- costs
- pox
- pox-2
- pox-3
- pox-4
- lockup
- costs-2
- costs-3
- cost-voting
- bns
plan:
batches: []
13 changes: 13 additions & 0 deletions packages/bowtieddevops-test-package-5/example/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

**/settings/Mainnet.toml
**/settings/Testnet.toml
.cache/**
history.txt

logs
*.log
npm-debug.log*
coverage
*.info
costs-reports.json
node_modules
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

{
"files.eol": "\n"
}
19 changes: 19 additions & 0 deletions packages/bowtieddevops-test-package-5/example/.vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

{
"version": "2.0.0",
"tasks": [
{
"label": "check contracts",
"group": "test",
"type": "shell",
"command": "clarinet check"
},
{
"type": "npm",
"script": "test",
"group": "test",
"problemMatcher": [],
"label": "npm test"
}
]
}
24 changes: 24 additions & 0 deletions packages/bowtieddevops-test-package-5/example/Clarinet.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[project]
name = 'example'
description = ''
authors = []
telemetry = false
cache_dir = './.cache'
requirements = []
[contracts.my-contract]
path = 'contracts/my-contract.clar'
clarity_version = 2
epoch = 2.4

[contracts.my-contract_test]
path = 'tests/my-contract_test.clar'
clarity_version = 2
epoch = 2.4
[repl.analysis]
passes = ['check_checker']

[repl.analysis.check_checker]
strict = false
trusted_sender = false
trusted_caller = false
callee_filter = false
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
(define-public (a-times-b (a uint) (b uint))
(ok (* a b))
)

(define-public (a-div-b (a uint) (b uint))
(err "not implemented")
)
Loading

0 comments on commit c924505

Please sign in to comment.