-
Notifications
You must be signed in to change notification settings - Fork 0
/
export_tests_from_docs.m
executable file
·102 lines (82 loc) · 3.85 KB
/
export_tests_from_docs.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/Applications/Mathematica.app/Contents/MacOS/WolframKernel -script
noBroken = False;
getExamplesFromNotebook[nbImported_] :=
Module[{getInputFromCell, nb}, (Clear[getInputFromCell];
getInputFromCell[Cell[content_, "ExampleText", rest__]] :=
First[FrontEndExecute[
FrontEnd`ExportPacket[Cell[content, "ExampleText", rest],
"PlainText"]]] // TEMPTxt;
getInputFromCell[Cell[content_, "Input", rest__]] :=
First[FrontEndExecute[
FrontEnd`ExportPacket[Cell[content, "Input", rest],
"PlainText"]]] // TEMPIn;
getInputFromCell[Cell[content_, "Output", rest__]] :=
First[FrontEndExecute[
FrontEnd`ExportPacket[Cell[content, "Output", rest],
"PlainText"]]] // TEMPOut;
getInputFromCell[Cell[content_, style_String, rest__]] :=
First[FrontEndExecute[
FrontEnd`ExportPacket[Cell[content, style, rest],
"PlainText"]]] // TEMPUNDEFINED;
nb = Cases[nbImported,
Cell[content_,
style_ /; MemberQ[{"Input", "Output", "ExampleText"}, style],
rest__] -> getInputFromCell[Cell[content, style, rest]], {0,
Infinity}];
nb = nb //. {p___, TEMPIn[in_], TEMPOut[out_], n___} -> {p,
TEMPExample[in, out], n};
nb = nb //. {p___, TEMPIn[inNew_], TEMPExample[inOld_, out_], n___} -> {p,
TEMPExample[inNew <> "; " <> inOld, out], n};
nb = nb //. {p___, TEMPTxt[txt_], TEMPExample[in_, out_],
n___} -> {p, TEMPExample[in, out, txt], n};
nb = Select[nb, MatchQ[#, _TEMPExample] &];
nb)];
Clear[testFunc, exportTests, lastOutputVar];
lastOutputVar = Null;
testFunc[in_, out_, noBroken_] :=
Module[{isGood, inExpr, outExpr, inResStr, outResStr, inInact, outInact},
inExpr = ToExpression[in] /. % -> lastOutputVar;
outExpr = ToExpression[out];
lastOutputVar = outExpr;
inResStr = ToString[inExpr, OutputForm];
outResStr = ToString[outExpr, OutputForm];
inInact = ToExpression[in, StandardForm, HoldComplete];
outInact = ToExpression[out, StandardForm, HoldComplete];
isGood = ((inResStr) === (outResStr));
Print[If[isGood, "ok ", "not ok "],
" ", in, " \[DoubleLongRightArrow] ", out];
If[isGood, TapTestSame[inInact, outInact],
If[! noBroken, TapTestSameBROKEN[inInact, outInact],
Unevaluated@Sequence[]]]];
SetAttributes[TapSuite, HoldAllComplete];
exportTests[fileName_, noBroken_] :=
Module[{nb, processNotebook},
nb = getExamplesFromNotebook[Import[fileName]];
processNotebook[nb_] :=
Module[{result, finalResult, replaceExampleWithTest},
Clear[replaceExampleWithTest, result, finalResult];
result =
nb //. {p___, TEMPExample[in_, out_, txt_], n___} -> {p,
TapComment[txt], TEMPExample[in, out], n};
replaceExampleWithTest[{p___, TEMPExample[in_, out_],
n___}] := {p, testFunc[in, out, noBroken], n};
replaceExampleWithTest[any_List] := any;
result = FixedPoint[replaceExampleWithTest, result];
finalResult = TapSuite @@ result;
finalResult =
finalResult /. {TapTestSame[HoldComplete[in_],
HoldComplete[out_]] -> TapTestSame[in, out],
TapTestSameBROKEN[HoldComplete[in_], HoldComplete[out_]] ->
TapTestSameBROKEN[in, out]};
finalResult];
processNotebook[nb]];
exportFile[nbFileName_, outputFile_] := Module[{exportedCode}, (
Print["# GENERATING TESTS FROM: ", nbFileName];
Print["# WRITING OUTPUT TO: ", outputFile];
exportedCode = exportTests[nbFileName, noBroken];
If[!DirectoryQ[DirectoryName[outputFile]], CreateDirectory[DirectoryName[outputFile]]];
If[FileExistsQ[outputFile], DeleteFile[outputFile]];
Export[outputFile, {HoldComplete[Import["CompatTests.m"];], HoldComplete@#&@exportedCode}, "HeldExpressions"]
)];
run[]:=exportFile[$CommandLine[[-2]], $CommandLine[[-1]]];
UsingFrontEnd[TimeConstrained[run[], 60]];