diff --git a/src/lib/indexExamples.js b/src/lib/indexExamples.js index c21854d..63a1a8c 100644 --- a/src/lib/indexExamples.js +++ b/src/lib/indexExamples.js @@ -314,6 +314,29 @@ def size(t: Tree[Int32]): Int32 = match t { case Tree.Node(l, r) => size(l) + size(r) }`; +export const testExample = `use Assert.{assertEq, assertErr} + +def divide(x: Int32, y: Int32): Result[String, Int32] = + if (y == 0) Err("Division by zero") else Ok(x / y) + +@Test +def testDivide01(): Unit \\ Assert = + assertEq(expected = Ok(5), divide(10, 2)) + +@Test +def testDivide02(): Unit \\ Assert = + assertErr(divide(10, 0))`; + +// Terminal output rather than Flix, so the home page renders this one through a +// plain
: the CodeSnippet component only knows the Flix grammar and would
+// colour the PASS lines as if they were code.
+export const testOutput = `Running 2 tests...
+
+ PASS testDivide01 1.4ms
+ PASS testDivide02 0.3ms
+
+Passed: 2, Failed: 0. Skipped: 0. Elapsed: 2.1ms.`;
+
export const datalogExample = `def reachable(g: List[(String, Int32, String)], minSpeed: Int32): List[(String, String)] =
let facts = inject g into Road/3;
let rules = #{
diff --git a/src/pages/index.astro b/src/pages/index.astro
index ccf250c..496596e 100644
--- a/src/pages/index.astro
+++ b/src/pages/index.astro
@@ -23,6 +23,8 @@ import {
applicativeForExample,
javaInteropExample,
terminationExample,
+ testExample,
+ testOutput,
datalogExample,
} from "../lib/indexExamples.js";
@@ -548,6 +550,43 @@ const vscodeSlides = [
+
+
+
+
+ Built-in Test Framework
+
+ Flix comes with a built-in test framework. A test is a function
+ marked with the @Test annotation that takes no
+ arguments and returns Unit.
+
+
+ Assertions come from the Assert module in the
+ standard library. Asserting is itself an effect, so a test
+ carries Assert in its signature and the test runner
+ is nothing more than its handler.
+
+
+ Run every test in a project with flix test, or run
+ them one at a time from the code lens above each test in Visual
+ Studio Code.
+
+
+
+
+
+
+ {/* Terminal output, not Flix, so it bypasses CodeSnippet and reuses
+ only its frame. overflow-x is set here because Astro's ships
+ that inline on the it renders and a bare one has nothing. */}
+
+
+ {testOutput}
+
+
+
+
+