-
Notifications
You must be signed in to change notification settings - Fork 677
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(tests): Add clarity-parser-string-to-cv.prop.test.ts property-ba…
…sed tests - Implemented property-based testing for string, uint, and tuple conversions using fast-check. - Defined custom arbitraries for generating 128-bit unsigned (uint128) and signed (int128) integers. - Enhanced test robustness and coverage by automatically testing a wide range of input values. - Included fast-check's shrinking feature to identify minimal failure cases, aiding in effective debugging.
- Loading branch information
1 parent
c85906d
commit 3e9b3af
Showing
3 changed files
with
102 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
contrib/core-contract-tests/tests/clar/clarity-parser-string-to-cv.prop.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { describe, expect, it } from "vitest"; | ||
import { stringToCV } from "./utils/string-to-cv"; | ||
import { ClarityType, intCV, stringUtf8CV, uintCV } from "@stacks/transactions"; | ||
import fc from "fast-check"; | ||
|
||
const uint128 = fc | ||
.tuple(fc.bigUintN(64), fc.bigUintN(64)) | ||
.map(([hi, lo]) => (hi << BigInt(64)) | lo); | ||
|
||
const int128 = fc | ||
.tuple(fc.bigIntN(64), fc.bigIntN(64)) | ||
.map(([hi, lo]) => (hi << BigInt(64)) | lo); | ||
|
||
describe("verify string to cv conversion", () => { | ||
it("should convert string to cv", () => { | ||
fc.assert(fc.property(fc.string(), (someStr) => { | ||
const result = stringToCV(someStr, { "string-utf8": { length: 100 } }); | ||
expect(result).toEqual({ | ||
type: "string", | ||
value: stringUtf8CV(someStr), | ||
}); | ||
})); | ||
}); | ||
|
||
it("should convert uint to cv", () => { | ||
fc.assert(fc.property(uint128, (someUInt) => { | ||
const result = stringToCV(`u${someUInt}`, "uint128"); | ||
expect(result).toEqual({ | ||
type: "uint", | ||
value: uintCV(someUInt), | ||
}); | ||
})); | ||
}); | ||
|
||
it("should convert tuple to cv", () => { | ||
fc.assert(fc.property(fc.record({ | ||
key: fc.stringMatching(/^[a-zA-Z][a-zA-Z0-9]{0,9}$/), | ||
val: int128 }), (r) => { | ||
const result = stringToCV(`{${r.key}: ${r.val}}`, { | ||
tuple: [{ name: r.key, type: "int128" }], | ||
}); | ||
expect(result).toEqual({ | ||
type: "tuple", | ||
value: { data: { [r.key]: intCV(r.val) }, type: ClarityType.Tuple }, | ||
}); | ||
})); | ||
}); | ||
}); | ||
|
||
describe("custom arbitraries for 128-bit unsigned/signed integers", () => { | ||
it("generates 128-bit unsigned integers in range [0, 2^128 - 1]", () => { | ||
fc.assert(fc.property(uint128, (actual) => | ||
actual >= BigInt(0) && | ||
actual <= BigInt("340282366920938463463374607431768211455") | ||
)); | ||
}); | ||
|
||
it("generates 128-bit signed integers in range [-2^127, 2^127 - 1]", () => { | ||
fc.assert(fc.property(int128, (actual) => | ||
actual >= BigInt("-170141183460469231731687303715884105728") && | ||
actual <= BigInt( "170141183460469231731687303715884105727") | ||
)); | ||
}); | ||
}); |