Last active
May 4, 2016 09:02
-
-
Save noqisofon/982aae138fd902f96d76c7bf2f99f4ad to your computer and use it in GitHub Desktop.
( ノ╹◡◡╹)ノ OCaml の Test::Simple 目コピってみた
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
all: test-builder.dll test-simple.dll | |
test-builder.dll: test-builder.fs | |
fsharpc --nologo -a $^ | |
test-simple.dll: test-simple.fs | |
fsharpc --nologo -a $^ |
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
module Test.Builder | |
type status = | |
| NotOk | |
| Ok | |
type directive = | |
| Todo of string | |
type diagnostic = | |
| Diag of string list | |
type node = | |
| TestCaseNode of status * int * string * diagnostic option | |
| TodoTestCaseNode of status * int * string * directive * diagnostic option | |
| DiagnosticNode of diagnostic | |
| PlanNode of int | |
type test_document = | |
| Document of node list | |
let rec count_test_nodes nodes count = | |
match nodes with | |
| [] -> count | |
| TestCaseNode(_, _, _, _)::xs | |
| TodoTestCaseNode(_, _, _, _, _)::xs -> count_test_nodes xs count + 1 | |
| DiagnosticNode(_)::xs | |
| PlanNode(_)::xs -> count_test_nodes xs count | |
let count_tests (Document nodes) = | |
count_test_nodes nodes 0 | |
let count_test_failures (Document nodes) = | |
let rec loop nodes count = | |
match nodes with | |
| [] | |
-> count | |
| TestCaseNode(Ok, _, _, _)::xs | |
| TodoTestCaseNode(Ok, _, _, _, _)::xs | |
| DiagnosticNode(_)::xs | |
| PlanNode(_)::xs | |
-> loop xs count | |
| TestCaseNode(NotOk, _, _, _)::xs | |
| TodoTestCaseNode(NotOk, _, _, _, _)::xs | |
-> loop xs count + 1 | |
in loop nodes 0 | |
let create_failure_footer test_count failure_count = | |
DiagnosticNode( | |
Diag(["Looks like you failed " | |
+ failure_count.ToString() | |
+ " tests of " | |
+ test_count.ToString() | |
+ " run."]) | |
) | |
let create_count_footer test_count plan_count = | |
DiagnosticNode( | |
Diag(["Looks like you planed " | |
+ plan_count.ToString() | |
+ " tests but " | |
+ (if test_count < plan_count then | |
("only ran " + test_count.ToString()) | |
else | |
("ran " + ((test_count - plan_count).ToString()) + " extra")) | |
+ " ."]) | |
) | |
let init_document doc = | |
let count = count_tests doc in | |
let failures = count_test_failures doc in | |
match doc with | |
| Document(PlanNode(plan)::nodes) -> | |
Document( | |
PlanNode(plan)::nodes | |
@ | |
(if count = plan then [] else [ (create_count_footer count plan) ] ) | |
@ | |
(if failures = 0 then [] else [ (create_failure_footer count failures) ]) | |
) | |
| Document(nodes) -> | |
Document( | |
nodes | |
@ | |
[ PlanNode(count) ] | |
@ | |
(if failures = 0 then [] else [ (create_failure_footer count failures) ]) | |
) | |
let string_of_status status = | |
match status with | |
| OK -> "ok" | |
| NotOk -> "not ok" | |
let string_of_diagnostic (Diag lines) = | |
List.map (fun line -> "# " + line + "\n") lines | |
|> List.fold (+) "" | |
let string_of_directive (Todo s) = | |
"# TODO " + s | |
let string_of_node node = | |
let emit_diagnostic diag = | |
(match diag with | |
| None -> "\n" | |
| Some diag -> "\n" + (string_of_diagnostic diag)) | |
in | |
match node with | |
| TestCaseNode(status, num, desc, diag) -> | |
(string_of_status status) | |
+ " " | |
+ num.ToString() | |
+ " - " | |
+ desc | |
+ (emit_diagnostic diag) | |
| TodoTestCaseNode(status, num, desc, dir, diag) -> | |
(string_of_status status) | |
+ " " | |
+ num.ToString() | |
+ " - " | |
+ desc | |
+ (string_of_directive dir) | |
+ (emit_diagnostic diag) | |
| DiagnosticNode(diag) -> | |
(string_of_diagnostic diag) | |
| PlanNode(count) -> | |
"1.." + count.ToString() + "\n" | |
let string_of_document (Document nodes) = | |
List.map string_of_node nodes | |
|> String.concat "" |
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
#read "Test.Builder.dll" | |
module Test.Simple | |
let current_plan = ref (None : int option) | |
let running_tests = ref ([] : (int -> Test.Builder.node) list) | |
let run_isolated_test_suite plan test_suite = | |
let backup_tests = runnting_tests () | |
let backup_plan = current_plan () in | |
running_tests := ([] : (int -> Test.Builder.node) list); | |
current_plan := plan; | |
let document = assemble_test_run () in | |
running_tests := backup_tests; | |
current_plan := backup_plan; | |
document | |
let plan count test_suite = | |
let document = run_isolated_test_suite (Some count) test_suite in | |
Test.Builder.string_of_document document | |
let no_plan test_suite = | |
let document = run_isolated_test_suite None test_suite in | |
Test.Builder.string_of_document document | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment