Skip to content

Commit 46e922e

Browse files
authored
Extract and test RUNNABLE_EXAMPLE tests including Phobos (#3437)
* Extract and test RUNNABLE_EXAMPLEs in spec too Fix module.dd. * Extract and test RUNNABLE_EXAMPLE tests from Phobos * Remove unintended pattern underscore * Fix Issue 23425 - Extract and test RUNNABLE_EXAMPLE tests including Phobos * Rename variable * Fix spec tests * Move imports; use stripExtension
1 parent e3281f4 commit 46e922e

File tree

2 files changed

+26
-12
lines changed

2 files changed

+26
-12
lines changed

spec/module.dd

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,6 @@ $(H3 $(LNAME2 name_lookup, Symbol Name Lookup))
235235

236236
$(P The simplest form of importing is to just list the modules being imported:)
237237

238-
$(RUNNABLE_EXAMPLE
239238
---------
240239
module myapp.main;
241240

@@ -251,7 +250,6 @@ class Foo : BaseClass
251250
}
252251
}
253252
---------
254-
)
255253

256254
$(P When a symbol name is used unqualified, a two-phase lookup is used.
257255
First, the module scope is searched, starting from the innermost scope.
@@ -408,7 +406,7 @@ $(H3 $(LNAME2 selective_imports, Selective Imports))
408406
$(P Specific symbols can be exclusively imported from a module and bound into
409407
the current namespace:)
410408

411-
$(RUNNABLE_EXAMPLE
409+
$(SPEC_RUNNABLE_EXAMPLE_FAIL
412410
---
413411
import std.stdio : writeln, foo = write;
414412

@@ -429,7 +427,7 @@ $(H3 $(LNAME2 renamed_selective_imports, Renamed and Selective Imports))
429427

430428
$(P When renaming and selective importing are combined:)
431429

432-
$(RUNNABLE_EXAMPLE
430+
$(SPEC_RUNNABLE_EXAMPLE_FAIL
433431
------------
434432
import io = std.stdio : foo = writeln;
435433

tools/dspec_tester.d

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,15 @@ auto ddocMacroToCode(R)(R text)
8787

8888
int main(string[] args)
8989
{
90-
import std.file, std.getopt, std.path;
90+
import std.file, std.getopt;
9191
import std.parallelism : parallel;
92+
import std.path;
9293
import std.process : environment;
9394
import std.typecons : Tuple;
9495

95-
auto specDir = __FILE_FULL_PATH__.dirName.dirName.buildPath("spec");
96+
const rootDir = __FILE_FULL_PATH__.dirName.dirName;
97+
const specDir = rootDir.buildPath("spec");
98+
const stdDir = rootDir.buildPath("..", "phobos", "std");
9699
bool hasFailed;
97100

98101
config.dmdBinPath = environment.get("DMD", "dmd");
@@ -121,15 +124,27 @@ int main(string[] args)
121124
SpecType("$(SPEC_RUNNABLE_EXAMPLE_COMPILE", CompileConfig.TestMode.compile),
122125
SpecType("$(SPEC_RUNNABLE_EXAMPLE_RUN", CompileConfig.TestMode.run),
123126
SpecType("$(SPEC_RUNNABLE_EXAMPLE_FAIL", CompileConfig.TestMode.fail),
127+
SpecType("$(RUNNABLE_EXAMPLE", CompileConfig.TestMode.run),
124128
];
125-
foreach (file; specDir.dirEntries("*.dd", SpanMode.depth).parallel(1))
129+
auto files = chain(specDir.dirEntries("*.dd", SpanMode.depth),
130+
stdDir.dirEntries("*.d", SpanMode.depth));
131+
foreach (file; files.parallel(1))
126132
{
133+
// auto-import current module if in phobos
134+
string modImport = file.name.find(stdDir);
135+
if (modImport.length)
136+
{
137+
modImport = "std" ~ modImport[stdDir.length..$];
138+
modImport = modImport.stripExtension();
139+
modImport = modImport.findSplitBefore(dirSeparator ~ "package")[0];
140+
modImport = modImport.replace(dirSeparator, ".");
141+
}
127142
auto allTests = specTypes.map!(c => findExamples(file, c.key)
128-
.map!(e => compileAndCheck(e, CompileConfig(c.mode))))
129-
.joiner;
143+
.map!(e => compileAndCheck(e, CompileConfig(c.mode), modImport)))
144+
.joiner;
130145
if (!allTests.empty)
131146
{
132-
writefln("%s: %d examples found", file.baseName, allTests.walkLength);
147+
writefln("%s: %d examples found", file[rootDir.length+1..$], allTests.walkLength);
133148
if (allTests.any!(a => a != 0))
134149
hasFailed = true;
135150
}
@@ -154,7 +169,7 @@ Params:
154169
155170
Returns: the exit code of the compiler invocation.
156171
*/
157-
auto compileAndCheck(R)(R buffer, CompileConfig config)
172+
auto compileAndCheck(R)(R buffer, CompileConfig config, string modImport)
158173
{
159174
import std.process;
160175
import std.uni : isWhite;
@@ -186,7 +201,8 @@ auto compileAndCheck(R)(R buffer, CompileConfig config)
186201
if (!buffer.find!(a => !a.isWhite).startsWith("module"))
187202
{
188203
buffer = "import std.stdio;\n" ~ buffer; // used too often
189-
204+
if (modImport.length)
205+
buffer = "import " ~ modImport ~ ";" ~ buffer;
190206
if (!hasMain)
191207
buffer = "void main() {\n" ~ buffer ~ "\n}";
192208
}

0 commit comments

Comments
 (0)