]> gitweb.factorcode.org Git - factor.git/blob - basis/tools/test/test-docs.factor
Solution to Project Euler problem 65
[factor.git] / basis / tools / test / test-docs.factor
1 USING: help.markup help.syntax kernel quotations io ;
2 IN: tools.test
3
4 ARTICLE: "tools.test.write" "Writing unit tests"
5 "Assert that a quotation outputs a specific set of values:"
6 { $subsection POSTPONE: unit-test }
7 "Assert that a quotation throws an error:"
8 { $subsection POSTPONE: must-fail }
9 { $subsection POSTPONE: must-fail-with }
10 "Assert that a quotation or word has a specific static stack effect (see " { $link "inference" } "):"
11 { $subsection POSTPONE: must-infer }
12 { $subsection POSTPONE: must-infer-as }
13 "All of the above are used like ordinary words but are actually parsing words. This ensures that parse-time state, namely the line number, can be associated with the test in question, and reported in test failures." ;
14
15 ARTICLE: "tools.test.run" "Running unit tests"
16 "The following words run test harness files; any test failures are collected and printed at the end:"
17 { $subsection test }
18 { $subsection test-all }
19 "The following word prints failures:"
20 { $subsection :test-failures }
21 "Test failures are reported using the " { $link "tools.errors" } " mechanism and are shown in the " { $link "ui.tools.error-list" } "."
22 $nl
23 "Unit test failures are instances of a class, and are stored in a global variable:"
24 { $subsection test-failure }
25 { $subsection test-failures } ;
26
27 ARTICLE: "tools.test" "Unit testing"
28 "A unit test is a piece of code which starts with known input values, then compares the output of a word with an expected output, where the expected output is defined by the word's contract."
29 $nl
30 "For example, if you were developing a word for computing symbolic derivatives, your unit tests would apply the word to certain input functions, comparing the results against the correct values. While the passing of these tests would not guarantee the algorithm is correct, it would at least ensure that what used to work keeps working, in that as soon as something breaks due to a change in another part of your program, failing tests will let you know."
31 $nl
32 "Unit tests for a vocabulary are placed in test files in the same directory as the vocabulary source file (see " { $link "vocabs.loader" } "). Two possibilities are supported:"
33 { $list
34     { "Tests can be placed in a file named " { $snippet { $emphasis "vocab" } "-tests.factor" } "." }
35     { "Tests can be placed in files in the " { $snippet "tests" } " subdirectory." }
36 }
37 "The latter is used for vocabularies with more extensive test suites."
38 $nl
39 "If the test harness needs to define words, they should be placed in a vocabulary named " { $snippet { $emphasis "vocab" } ".tests" } " where " { $emphasis "vocab" } " is the vocab being tested."
40 { $subsection "tools.test.write" }
41 { $subsection "tools.test.run" } ;
42
43 ABOUT: "tools.test"
44
45 HELP: unit-test
46 { $syntax "[ output ] [ input ] unit-test" }
47 { $values { "output" "a sequence of expected stack elements" } { "input" "a quotation run with an empty stack" } }
48 { $description "Runs a quotation with an empty stack, comparing the resulting stack with " { $snippet "output" } ". Elements are compared using " { $link = } ". Throws an error if the expected stack does not match the resulting stack." } ;
49
50 HELP: must-fail
51 { $values { "quot" "a quotation run with an empty stack" } }
52 { $description "Runs a quotation with an empty stack, expecting it to throw an error. If the quotation throws an error, this word returns normally. If the quotation does not throw an error, this word " { $emphasis "does" } " raise an error." }
53 { $notes "This word is used to test boundary conditions and fail-fast behavior." } ;
54
55 HELP: must-fail-with
56 { $values { "quot" "a quotation run with an empty stack" } { "pred" { $quotation "( error -- ? )" } } }
57 { $description "Runs a quotation with an empty stack, expecting it to throw an error which must satisfy " { $snippet "pred" } ". If the quotation does not throw an error, or if the error does not match the predicate, the unit test fails." }
58 { $notes "This word is used to test error handling code, ensuring that errors thrown by code contain the relevant debugging information." } ;
59
60 HELP: must-infer
61 { $values { "quot" quotation } }
62 { $description "Ensures that the quotation has a static stack effect without running it." }
63 { $notes "This word is used to test that code will compile with the optimizing compiler for optimum performance. See " { $link "compiler" } "." } ;
64
65 HELP: must-infer-as
66 { $values { "effect" "a pair with shape " { $snippet "{ inputs outputs }" } } { "quot" quotation } }
67 { $description "Ensures that the quotation has the indicated stack effect without running it." }
68 { $notes "This word is used to test that code will compile with the optimizing compiler for optimum performance. See " { $link "compiler" } "." } ;
69
70 HELP: test
71 { $values { "prefix" "a vocabulary name" } }
72 { $description "Runs unit tests for the vocabulary named " { $snippet "prefix" } " and all of its child vocabularies." } ;
73
74 HELP: test-all
75 { $description "Runs unit tests for all loaded vocabularies." } ;
76
77 HELP: :test-failures
78 { $description "Prints all pending unit test failures." } ;