hirosystems / clarinet

Write, test and deploy high-quality smart contracts to the Stacks blockchain and Bitcoin.
https://hiro.so/clarinet
GNU General Public License v3.0
290 stars 129 forks source link

Support unit tests written in Clarity - `#[test]` #1456

Open lgalabru opened 1 month ago

lgalabru commented 1 month ago

A non negligible amount of developers want to write their tests in clarity. I wonder if this is something we could support "simply" by introducing a #[test] command processor. It could look like:

(define-public (increment-counter (step uint))
    (+ step (data-var-get counter)))

;; #[test]
(define-public (test-increment)
    (asserts! (is-eq u1 (increment-counter u1)) (err u1001)))

I think we would still be using js/ts for coordinating / structuring the test execution, but at least the logic would be written in clarity - developers would be calling something like:

describe("Counter contract tests", () => {
  it("ensures counter is counting", () => {
    const { result, events } = simnet.assertPublicFn(
      "counter",
      "test-increment",
      [Cl.uint(1)],
      deployer,
    );
  });
});

So when deploying such contracts, I think the #[test] directive would exclude the subsequent clarity statement from the contract?

hugocaillard commented 1 month ago

@lgalabru What do you think about the clarunit approach? https://www.npmjs.com/package/@stacks/clarunit (Similar ideas)

lgalabru commented 1 month ago

Oh nice, that probably works. I guess with this present proposal, clarity testing clarity would become a first class citizen. We can keep this issue open for a few week and passively collect feedbacks / demands?