Kwargs: assert functions (#6406)

Closes https://github.com/KittyCAD/modeling-app/issues/6408
This commit is contained in:
Adam Chalmers
2025-04-22 12:44:52 -05:00
committed by GitHub
parent 8be36d3d16
commit f99e44e371
73 changed files with 4790 additions and 4034 deletions

File diff suppressed because one or more lines are too long

40
docs/kcl/assertIs.md Normal file

File diff suppressed because one or more lines are too long

View File

@ -40,11 +40,7 @@ layout: manual
* [`arc`](kcl/arc)
* [`asin`](kcl/asin)
* [`assert`](kcl/assert)
* [`assertEqual`](kcl/assertEqual)
* [`assertGreaterThan`](kcl/assertGreaterThan)
* [`assertGreaterThanOrEq`](kcl/assertGreaterThanOrEq)
* [`assertLessThan`](kcl/assertLessThan)
* [`assertLessThanOrEq`](kcl/assertLessThanOrEq)
* [`assertIs`](kcl/assertIs)
* [`atan`](kcl/atan)
* [`atan2`](kcl/atan2)
* [`bezierCurve`](kcl/bezierCurve)

View File

@ -34,7 +34,7 @@ int(num: number): number
```js
n = int(ceil(5 / 2))
assertEqual(n, 3, 0.0001, "5/2 = 2.5, rounded up makes 3")
assert(n, isEqualTo = 3, error = "5/2 = 2.5, rounded up makes 3")
// Draw n cylinders.
startSketchOn(XZ)
|> circle(center = [0, 0], radius = 2)

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -49435,13 +49435,211 @@
},
{
"name": "assert",
"summary": "Check a value at runtime, and raise an error if the argument provided is false.",
"summary": "Check a value meets some expected conditions at runtime. Program terminates with an error if conditions aren't met. If you provide multiple conditions, they will all be checked and all must be met.",
"description": "",
"tags": [],
"keywordArguments": false,
"keywordArguments": true,
"args": [
{
"name": "data",
"name": "actual",
"type": "number",
"schema": {
"$schema": "https://spec.openapis.org/oas/3.0/schema/2019-04-02#/definitions/Schema",
"title": "TyF64",
"type": "number",
"format": "double"
},
"required": true,
"includeInSnippet": true,
"description": "Value to check. It will be compared with one of the comparison arguments.",
"labelRequired": false
},
{
"name": "isGreaterThan",
"type": "number",
"schema": {
"$schema": "https://spec.openapis.org/oas/3.0/schema/2019-04-02#/definitions/Schema",
"title": "Nullable_TyF64",
"allOf": [
{
"$ref": "#/components/schemas/TyF64"
}
],
"nullable": true,
"definitions": {
"TyF64": {
"type": "number",
"format": "double"
}
}
},
"required": false,
"description": "Comparison argument. If given, checks the `actual` value is greater than this.",
"labelRequired": true
},
{
"name": "isLessThan",
"type": "number",
"schema": {
"$schema": "https://spec.openapis.org/oas/3.0/schema/2019-04-02#/definitions/Schema",
"title": "Nullable_TyF64",
"allOf": [
{
"$ref": "#/components/schemas/TyF64"
}
],
"nullable": true,
"definitions": {
"TyF64": {
"type": "number",
"format": "double"
}
}
},
"required": false,
"description": "Comparison argument. If given, checks the `actual` value is less than this.",
"labelRequired": true
},
{
"name": "isGreaterThanOrEqual",
"type": "number",
"schema": {
"$schema": "https://spec.openapis.org/oas/3.0/schema/2019-04-02#/definitions/Schema",
"title": "Nullable_TyF64",
"allOf": [
{
"$ref": "#/components/schemas/TyF64"
}
],
"nullable": true,
"definitions": {
"TyF64": {
"type": "number",
"format": "double"
}
}
},
"required": false,
"description": "Comparison argument. If given, checks the `actual` value is greater than or equal to this.",
"labelRequired": true
},
{
"name": "isLessThanOrEqual",
"type": "number",
"schema": {
"$schema": "https://spec.openapis.org/oas/3.0/schema/2019-04-02#/definitions/Schema",
"title": "Nullable_TyF64",
"allOf": [
{
"$ref": "#/components/schemas/TyF64"
}
],
"nullable": true,
"definitions": {
"TyF64": {
"type": "number",
"format": "double"
}
}
},
"required": false,
"description": "Comparison argument. If given, checks the `actual` value is less than or equal to this.",
"labelRequired": true
},
{
"name": "isEqualTo",
"type": "number",
"schema": {
"$schema": "https://spec.openapis.org/oas/3.0/schema/2019-04-02#/definitions/Schema",
"title": "Nullable_TyF64",
"allOf": [
{
"$ref": "#/components/schemas/TyF64"
}
],
"nullable": true,
"definitions": {
"TyF64": {
"type": "number",
"format": "double"
}
}
},
"required": false,
"includeInSnippet": true,
"description": "Comparison argument. If given, checks the `actual` value is less than or equal to this.",
"labelRequired": true
},
{
"name": "tolerance",
"type": "number",
"schema": {
"$schema": "https://spec.openapis.org/oas/3.0/schema/2019-04-02#/definitions/Schema",
"title": "Nullable_TyF64",
"allOf": [
{
"$ref": "#/components/schemas/TyF64"
}
],
"nullable": true,
"definitions": {
"TyF64": {
"type": "number",
"format": "double"
}
}
},
"required": false,
"description": "If `isEqualTo` is used, this is the tolerance to allow for the comparison. This tolerance is used because KCL's number system has some floating-point imprecision when used with very large decimal places.",
"labelRequired": true
},
{
"name": "error",
"type": "String",
"schema": {
"$schema": "https://spec.openapis.org/oas/3.0/schema/2019-04-02#/definitions/Schema",
"title": "Nullable_String",
"type": "string",
"nullable": true,
"definitions": {
"TyF64": {
"type": "number",
"format": "double"
}
}
},
"required": false,
"description": "If the value was false, the program will terminate with this error message",
"labelRequired": true
}
],
"returnValue": {
"name": "",
"type": "()",
"schema": {
"$schema": "https://spec.openapis.org/oas/3.0/schema/2019-04-02#/definitions/Schema",
"title": "Null",
"type": "null"
},
"required": true,
"includeInSnippet": true,
"labelRequired": true
},
"unpublished": false,
"deprecated": false,
"examples": [
"n = 10\nassert(n, isEqualTo = 10)\nassert(\n n,\n isGreaterThanOrEqual = 0,\n isLessThan = 100,\n error = \"number should be between 0 and 100\",\n)\nassert(\n 1.0000000000012,\n isEqualTo = 1,\n tolerance = 0.0001,\n error = \"number should be almost exactly 1\",\n)"
]
},
{
"name": "assertIs",
"summary": "Asserts that a value is the boolean value true.",
"description": "",
"tags": [],
"keywordArguments": true,
"args": [
{
"name": "actual",
"type": "bool",
"schema": {
"$schema": "https://spec.openapis.org/oas/3.0/schema/2019-04-02#/definitions/Schema",
@ -49450,18 +49648,20 @@
},
"required": true,
"includeInSnippet": true,
"labelRequired": true
"description": "Value to check. If this is the boolean value true, assert passes. Otherwise it fails.",
"labelRequired": false
},
{
"name": "message",
"type": "string",
"name": "error",
"type": "String",
"schema": {
"$schema": "https://spec.openapis.org/oas/3.0/schema/2019-04-02#/definitions/Schema",
"title": "String",
"type": "string"
"title": "Nullable_String",
"type": "string",
"nullable": true
},
"required": true,
"includeInSnippet": true,
"required": false,
"description": "If the value was false, the program will terminate with this error message",
"labelRequired": true
}
],
@ -49480,340 +49680,7 @@
"unpublished": false,
"deprecated": false,
"examples": [
"myVar = true\nassert(myVar, \"should always be true\")"
]
},
{
"name": "assertEqual",
"summary": "Check that a numerical value equals another at runtime, otherwise raise an error.",
"description": "",
"tags": [],
"keywordArguments": false,
"args": [
{
"name": "left",
"type": "number",
"schema": {
"$schema": "https://spec.openapis.org/oas/3.0/schema/2019-04-02#/definitions/Schema",
"title": "double",
"type": "number",
"format": "double"
},
"required": true,
"includeInSnippet": true,
"labelRequired": true
},
{
"name": "right",
"type": "number",
"schema": {
"$schema": "https://spec.openapis.org/oas/3.0/schema/2019-04-02#/definitions/Schema",
"title": "double",
"type": "number",
"format": "double"
},
"required": true,
"includeInSnippet": true,
"labelRequired": true
},
{
"name": "epsilon",
"type": "number",
"schema": {
"$schema": "https://spec.openapis.org/oas/3.0/schema/2019-04-02#/definitions/Schema",
"title": "double",
"type": "number",
"format": "double"
},
"required": true,
"includeInSnippet": true,
"labelRequired": true
},
{
"name": "message",
"type": "string",
"schema": {
"$schema": "https://spec.openapis.org/oas/3.0/schema/2019-04-02#/definitions/Schema",
"title": "String",
"type": "string"
},
"required": true,
"includeInSnippet": true,
"labelRequired": true
}
],
"returnValue": {
"name": "",
"type": "()",
"schema": {
"$schema": "https://spec.openapis.org/oas/3.0/schema/2019-04-02#/definitions/Schema",
"title": "Null",
"type": "null"
},
"required": true,
"includeInSnippet": true,
"labelRequired": true
},
"unpublished": false,
"deprecated": false,
"examples": [
"n = 1.0285\no = 1.0286\nassertEqual(n, o, 0.01, \"n is within the given tolerance for o\")"
]
},
{
"name": "assertGreaterThan",
"summary": "Check that a numerical value is greater than another at runtime, otherwise raise an error.",
"description": "",
"tags": [],
"keywordArguments": false,
"args": [
{
"name": "left",
"type": "number",
"schema": {
"$schema": "https://spec.openapis.org/oas/3.0/schema/2019-04-02#/definitions/Schema",
"title": "double",
"type": "number",
"format": "double"
},
"required": true,
"includeInSnippet": true,
"labelRequired": true
},
{
"name": "right",
"type": "number",
"schema": {
"$schema": "https://spec.openapis.org/oas/3.0/schema/2019-04-02#/definitions/Schema",
"title": "double",
"type": "number",
"format": "double"
},
"required": true,
"includeInSnippet": true,
"labelRequired": true
},
{
"name": "message",
"type": "string",
"schema": {
"$schema": "https://spec.openapis.org/oas/3.0/schema/2019-04-02#/definitions/Schema",
"title": "String",
"type": "string"
},
"required": true,
"includeInSnippet": true,
"labelRequired": true
}
],
"returnValue": {
"name": "",
"type": "()",
"schema": {
"$schema": "https://spec.openapis.org/oas/3.0/schema/2019-04-02#/definitions/Schema",
"title": "Null",
"type": "null"
},
"required": true,
"includeInSnippet": true,
"labelRequired": true
},
"unpublished": false,
"deprecated": false,
"examples": [
"assertGreaterThan(2, 1, \"2 is greater than 1\")"
]
},
{
"name": "assertGreaterThanOrEq",
"summary": "Check that a numerical value is greater than or equal to another at runtime, otherwise raise an error.",
"description": "",
"tags": [],
"keywordArguments": false,
"args": [
{
"name": "left",
"type": "number",
"schema": {
"$schema": "https://spec.openapis.org/oas/3.0/schema/2019-04-02#/definitions/Schema",
"title": "double",
"type": "number",
"format": "double"
},
"required": true,
"includeInSnippet": true,
"labelRequired": true
},
{
"name": "right",
"type": "number",
"schema": {
"$schema": "https://spec.openapis.org/oas/3.0/schema/2019-04-02#/definitions/Schema",
"title": "double",
"type": "number",
"format": "double"
},
"required": true,
"includeInSnippet": true,
"labelRequired": true
},
{
"name": "message",
"type": "string",
"schema": {
"$schema": "https://spec.openapis.org/oas/3.0/schema/2019-04-02#/definitions/Schema",
"title": "String",
"type": "string"
},
"required": true,
"includeInSnippet": true,
"labelRequired": true
}
],
"returnValue": {
"name": "",
"type": "()",
"schema": {
"$schema": "https://spec.openapis.org/oas/3.0/schema/2019-04-02#/definitions/Schema",
"title": "Null",
"type": "null"
},
"required": true,
"includeInSnippet": true,
"labelRequired": true
},
"unpublished": false,
"deprecated": false,
"examples": [
"assertGreaterThanOrEq(2, 1, \"2 is greater than 1\")\nassertGreaterThanOrEq(1, 1, \"1 is equal to 1\")"
]
},
{
"name": "assertLessThan",
"summary": "Check that a numerical value is less than to another at runtime, otherwise raise an error.",
"description": "",
"tags": [],
"keywordArguments": false,
"args": [
{
"name": "left",
"type": "number",
"schema": {
"$schema": "https://spec.openapis.org/oas/3.0/schema/2019-04-02#/definitions/Schema",
"title": "double",
"type": "number",
"format": "double"
},
"required": true,
"includeInSnippet": true,
"labelRequired": true
},
{
"name": "right",
"type": "number",
"schema": {
"$schema": "https://spec.openapis.org/oas/3.0/schema/2019-04-02#/definitions/Schema",
"title": "double",
"type": "number",
"format": "double"
},
"required": true,
"includeInSnippet": true,
"labelRequired": true
},
{
"name": "message",
"type": "string",
"schema": {
"$schema": "https://spec.openapis.org/oas/3.0/schema/2019-04-02#/definitions/Schema",
"title": "String",
"type": "string"
},
"required": true,
"includeInSnippet": true,
"labelRequired": true
}
],
"returnValue": {
"name": "",
"type": "()",
"schema": {
"$schema": "https://spec.openapis.org/oas/3.0/schema/2019-04-02#/definitions/Schema",
"title": "Null",
"type": "null"
},
"required": true,
"includeInSnippet": true,
"labelRequired": true
},
"unpublished": false,
"deprecated": false,
"examples": [
"assertLessThan(1, 2, \"1 is less than 2\")"
]
},
{
"name": "assertLessThanOrEq",
"summary": "Check that a numerical value is less than or equal to another at runtime, otherwise raise an error.",
"description": "",
"tags": [],
"keywordArguments": false,
"args": [
{
"name": "left",
"type": "number",
"schema": {
"$schema": "https://spec.openapis.org/oas/3.0/schema/2019-04-02#/definitions/Schema",
"title": "double",
"type": "number",
"format": "double"
},
"required": true,
"includeInSnippet": true,
"labelRequired": true
},
{
"name": "right",
"type": "number",
"schema": {
"$schema": "https://spec.openapis.org/oas/3.0/schema/2019-04-02#/definitions/Schema",
"title": "double",
"type": "number",
"format": "double"
},
"required": true,
"includeInSnippet": true,
"labelRequired": true
},
{
"name": "message",
"type": "string",
"schema": {
"$schema": "https://spec.openapis.org/oas/3.0/schema/2019-04-02#/definitions/Schema",
"title": "String",
"type": "string"
},
"required": true,
"includeInSnippet": true,
"labelRequired": true
}
],
"returnValue": {
"name": "",
"type": "()",
"schema": {
"$schema": "https://spec.openapis.org/oas/3.0/schema/2019-04-02#/definitions/Schema",
"title": "Null",
"type": "null"
},
"required": true,
"includeInSnippet": true,
"labelRequired": true
},
"unpublished": false,
"deprecated": false,
"examples": [
"assertLessThanOrEq(1, 2, \"1 is less than 2\")\nassertLessThanOrEq(1, 1, \"1 is equal to 1\")"
"kclIsFun = true\nassertIs(kclIsFun)"
]
},
{
@ -113042,7 +112909,7 @@
"unpublished": false,
"deprecated": true,
"examples": [
"n = int(ceil(5 / 2))\nassertEqual(n, 3, 0.0001, \"5/2 = 2.5, rounded up makes 3\")\n// Draw n cylinders.\nstartSketchOn(XZ)\n |> circle(center = [0, 0], radius = 2)\n |> extrude(length = 5)\n |> patternTransform(\n instances = n,\n transform = fn(id) {\n return { translate = [4 * id, 0, 0] }\n },\n )"
"n = int(ceil(5 / 2))\nassert(n, isEqualTo = 3, error = \"5/2 = 2.5, rounded up makes 3\")\n// Draw n cylinders.\nstartSketchOn(XZ)\n |> circle(center = [0, 0], radius = 2)\n |> extrude(length = 5)\n |> patternTransform(\n instances = n,\n transform = fn(id) {\n return { translate = [4 * id, 0, 0] }\n },\n )"
]
},
{
@ -237871,7 +237738,7 @@
"unpublished": false,
"deprecated": false,
"examples": [
"arr = [1, 2, 3, 4]\nnew_arr = pop(arr)\nassertEqual(new_arr[0], 1, 0.00001, \"1 is the first element of the array\")\nassertEqual(new_arr[1], 2, 0.00001, \"2 is the second element of the array\")\nassertEqual(new_arr[2], 3, 0.00001, \"3 is the third element of the array\")"
"arr = [1, 2, 3, 4]\nnew_arr = pop(arr)\nassert(\n new_arr[0],\n isEqualTo = 1,\n tolerance = 0.00001,\n error = \"1 is the first element of the array\",\n)\nassert(\n new_arr[1],\n isEqualTo = 2,\n tolerance = 0.00001,\n error = \"2 is the second element of the array\",\n)\nassert(\n new_arr[2],\n isEqualTo = 3,\n tolerance = 0.00001,\n error = \"3 is the third element of the array\",\n)"
]
},
{
@ -251362,7 +251229,7 @@
"unpublished": false,
"deprecated": false,
"examples": [
"arr = [1, 2, 3]\nnew_arr = push(arr, 4)\nassertEqual(new_arr[3], 4, 0.00001, \"4 was added to the end of the array\")"
"arr = [1, 2, 3]\nnew_arr = push(arr, 4)\nassert(\n new_arr[3],\n isEqualTo = 4,\n tolerance = 0.1,\n error = \"4 was added to the end of the array\",\n)"
]
},
{
@ -262197,8 +262064,8 @@
"unpublished": false,
"deprecated": false,
"examples": [
"// This function adds two numbers.\nfn add(a, b) {\n return a + b\n}\n\n// This function adds an array of numbers.\n// It uses the `reduce` function, to call the `add` function on every\n// element of the `arr` parameter. The starting value is 0.\nfn sum(arr) {\n return reduce(arr, 0, add)\n}\n\n/* The above is basically like this pseudo-code:\nfn sum(arr):\n sumSoFar = 0\n for i in arr:\n sumSoFar = add(sumSoFar, i)\n return sumSoFar */\n\n// We use `assertEqual` to check that our `sum` function gives the\n// expected result. It's good to check your work!\nassertEqual(sum([1, 2, 3]), 6, 0.00001, \"1 + 2 + 3 summed is 6\")",
"// This example works just like the previous example above, but it uses\n// an anonymous `add` function as its parameter, instead of declaring a\n// named function outside.\narr = [1, 2, 3]\nsum = reduce(arr, 0, fn(i, result_so_far) {\n return i + result_so_far\n})\n\n// We use `assertEqual` to check that our `sum` function gives the\n// expected result. It's good to check your work!\nassertEqual(sum, 6, 0.00001, \"1 + 2 + 3 summed is 6\")",
"// This function adds two numbers.\nfn add(a, b) {\n return a + b\n}\n\n// This function adds an array of numbers.\n// It uses the `reduce` function, to call the `add` function on every\n// element of the `arr` parameter. The starting value is 0.\nfn sum(arr) {\n return reduce(arr, 0, add)\n}\n\n/* The above is basically like this pseudo-code:\nfn sum(arr):\n sumSoFar = 0\n for i in arr:\n sumSoFar = add(sumSoFar, i)\n return sumSoFar */\n\n// We use `assert` to check that our `sum` function gives the\n// expected result. It's good to check your work!\nassert(\n sum([1, 2, 3]),\n isEqualTo = 6,\n tolerance = 0.1,\n error = \"1 + 2 + 3 summed is 6\",\n)",
"// This example works just like the previous example above, but it uses\n// an anonymous `add` function as its parameter, instead of declaring a\n// named function outside.\narr = [1, 2, 3]\nsum = reduce(arr, 0, fn(i, result_so_far) {\n return i + result_so_far\n})\n\n// We use `assert` to check that our `sum` function gives the\n// expected result. It's good to check your work!\nassert(\n sum,\n isEqualTo = 6,\n tolerance = 0.1,\n error = \"1 + 2 + 3 summed is 6\",\n)",
"// Declare a function that sketches a decagon.\nfn decagon(radius) {\n // Each side of the decagon is turned this many radians from the previous angle.\n stepAngle = 1 / 10 * TAU\n\n // Start the decagon sketch at this point.\n startOfDecagonSketch = startSketchOn(XY)\n |> startProfileAt([cos(0) * radius, sin(0) * radius], %)\n\n // Use a `reduce` to draw the remaining decagon sides.\n // For each number in the array 1..10, run the given function,\n // which takes a partially-sketched decagon and adds one more edge to it.\n fullDecagon = reduce([1..10], startOfDecagonSketch, fn(i, partialDecagon) {\n // Draw one edge of the decagon.\n x = cos(stepAngle * i) * radius\n y = sin(stepAngle * i) * radius\n return line(partialDecagon, end = [x, y])\n })\n\n return fullDecagon\n}\n\n/* The `decagon` above is basically like this pseudo-code:\nfn decagon(radius):\n stepAngle = (1/10) * TAU\n plane = startSketchOn('XY')\n startOfDecagonSketch = startProfileAt([(cos(0)*radius), (sin(0) * radius)], plane)\n\n // Here's the reduce part.\n partialDecagon = startOfDecagonSketch\n for i in [1..10]:\n x = cos(stepAngle * i) * radius\n y = sin(stepAngle * i) * radius\n partialDecagon = line(partialDecagon, end = [x, y])\n fullDecagon = partialDecagon // it's now full\n return fullDecagon */\n\n// Use the `decagon` function declared above, to sketch a decagon with radius 5.\ndecagon(5.0)\n |> close()"
]
},
@ -262256,7 +262123,7 @@
"unpublished": false,
"deprecated": false,
"examples": [
"assertEqual(rem(7, divisor = 4), 3, 0.01, \"remainder is 3\")\nassertEqual(rem(-7, divisor = 4), -3, 0.01, \"remainder is -3\")\nassertEqual(rem(7, divisor = -4), 3, 0.01, \"remainder is 3\")\nassertEqual(rem(6, divisor = 2.5), 1, 0.01, \"remainder is 1\")\nassertEqual(rem(6.5, divisor = 2.5), 1.5, 0.01, \"remainder is 1.5\")\nassertEqual(rem(6.5, divisor = 2), 0.5, 0.01, \"remainder is 0.5\")"
"assert(rem(7, divisor = 4), isEqualTo = 3, error = \"remainder is 3\")\nassert(rem(-7, divisor = 4), isEqualTo = -3, error = \"remainder is -3\")\nassert(rem(7, divisor = -4), isEqualTo = 3, error = \"remainder is 3\")\nassert(rem(6, divisor = 2.5), isEqualTo = 1, error = \"remainder is 1\")\nassert(rem(6.5, divisor = 2.5), isEqualTo = 1.5, error = \"remainder is 1.5\")\nassert(rem(6.5, divisor = 2), isEqualTo = 0.5, error = \"remainder is 0.5\")"
]
},
{