Skip to content

Commit 5bebaa7

Browse files
authored
Merge pull request #1693 from wilzbach/enable-tests
Fix Issue 17501 - Runnable unittest problem with AST rewrite merged-on-behalf-of: Vladimir Panteleev <github@thecybershadow.net>
2 parents d27a81f + 4f7e8d8 commit 5bebaa7

File tree

6 files changed

+123
-25
lines changed

6 files changed

+123
-25
lines changed

.travis.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
dist: trusty
2+
sudo: false
3+
4+
language: d
5+
os:
6+
- linux
7+
d:
8+
- dmd
9+
- dmd-beta
10+
- dmd-nightly
11+
- ldc
12+
- ldc-beta
13+
14+
script:
15+
- make -f posix.mak test

assert_writeln_magic.d

Lines changed: 90 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,13 @@ private string formatNode(T)(const T t)
4343
return writer.data;
4444
}
4545

46-
class TestVisitor : ASTVisitor
46+
class TestVisitor(Out) : ASTVisitor
4747
{
4848
import dparse.lexer : tok, Token;
4949

50-
this(string fileName, string destFile)
50+
this(Out fl)
5151
{
52-
this.fileName = fileName;
53-
fl = FileLines(fileName, destFile);
52+
this.fl = fl;
5453
}
5554

5655
alias visit = ASTVisitor.visit;
@@ -77,6 +76,10 @@ class TestVisitor : ASTVisitor
7776
if (inFunctionCall)
7877
return;
7978

79+
// only look at `a == b` within the AssertExpression
80+
if (typeid(expr.assertion) != typeid(CmpExpression))
81+
return;
82+
8083
lastAssert = expr;
8184
inAssert = true;
8285
expr.accept(this);
@@ -172,15 +175,26 @@ private:
172175
Rebindable!(const AssertExpression) lastAssert;
173176
Rebindable!(const EqualExpression) lastEqualExpression;
174177

175-
string fileName;
176-
FileLines fl;
178+
Out fl;
177179
}
178180

179-
void parseFile(string fileName, string destFile)
181+
void parseString(Visitor)(ubyte[] sourceCode, string fileName, Visitor visitor)
180182
{
181183
import dparse.lexer;
182184
import dparse.parser : parseModule;
183185
import dparse.rollback_allocator : RollbackAllocator;
186+
187+
LexerConfig config;
188+
auto cache = StringCache(StringCache.defaultBucketCount);
189+
const(Token)[] tokens = getTokensForParser(sourceCode, config, &cache).array;
190+
191+
RollbackAllocator rba;
192+
auto m = parseModule(tokens, fileName, &rba);
193+
visitor.visit(m);
194+
}
195+
196+
void parseFile(string fileName, string destFile)
197+
{
184198
import std.array : uninitializedArray;
185199

186200
auto inFile = File(fileName);
@@ -192,14 +206,9 @@ void parseFile(string fileName, string destFile)
192206
return;
193207

194208
inFile.rawRead(sourceCode);
195-
LexerConfig config;
196-
auto cache = StringCache(StringCache.defaultBucketCount);
197-
const(Token)[] tokens = getTokensForParser(sourceCode, config, &cache).array;
198-
199-
RollbackAllocator rba;
200-
auto m = parseModule(tokens, fileName, &rba);
201-
auto visitor = new TestVisitor(fileName, destFile);
202-
visitor.visit(m);
209+
auto fl = FileLines(fileName, destFile);
210+
auto visitor = new TestVisitor!(typeof(fl))(fl);
211+
parseString(sourceCode, fileName, visitor);
203212
delete visitor;
204213
}
205214

@@ -211,6 +220,7 @@ string rebasePath(string path, string oldBase, string newBase)
211220
return buildPath(newBase, path.absolutePath.relativePath(oldBase.absolutePath));
212221
}
213222

223+
version(unittest) { void main(){} } else
214224
void main(string[] args)
215225
{
216226
import std.file;
@@ -324,3 +334,68 @@ struct FileLines
324334
lines[i] = line;
325335
}
326336
}
337+
338+
version(unittest)
339+
{
340+
struct FileLinesMock
341+
{
342+
string[] lines;
343+
string opIndex(size_t i) { return lines[i]; }
344+
void opIndexAssign(string line, size_t i) {
345+
lines[i] = line;
346+
}
347+
}
348+
auto runTest(string sourceCode)
349+
{
350+
import std.string : representation;
351+
auto mock = FileLinesMock(sourceCode.split("\n"));
352+
auto visitor = new TestVisitor!(typeof(mock))(mock);
353+
parseString(sourceCode.representation.dup, "testmodule", visitor);
354+
delete visitor;
355+
return mock;
356+
}
357+
}
358+
359+
360+
unittest
361+
{
362+
"Running tests for assert_writeln_magic".writeln;
363+
364+
// purposefully not indented
365+
string testCode = q{
366+
unittest
367+
{
368+
assert(equal(splitter!(a => a == ' ')("hello world"), [ "hello", "", "world" ]));
369+
assert(equal(splitter!(a => a == 0)(a), w));
370+
}
371+
};
372+
auto res = runTest(testCode);
373+
assert(res.lines[3 .. $ - 2] == [
374+
"assert(equal(splitter!(a => a == ' ')(\"hello world\"), [ \"hello\", \"\", \"world\" ]));",
375+
"assert(equal(splitter!(a => a == 0)(a), w));"
376+
]);
377+
}
378+
379+
unittest
380+
{
381+
string testCode = q{
382+
unittest
383+
{
384+
assert(1 == 2);
385+
assert(foo() == "bar");
386+
assert(foo() == bar);
387+
assert(arr == [0, 1, 2]);
388+
assert(r.back == 1);
389+
}
390+
};
391+
auto res = runTest(testCode);
392+
assert(res.lines[3 .. $ - 2] == [
393+
"writeln(1); // 2",
394+
"writeln(foo()); // \"bar\"",
395+
"writeln(foo()); // bar",
396+
"writeln(arr); // [0, 1, 2]",
397+
"writeln(r.back); // 1",
398+
]);
399+
400+
"Successfully ran tests for assert_writeln_magic".writeln;
401+
}

changelog/2.072.0.dd

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -372,13 +372,13 @@ $(BUGSTITLE Library Changes,
372372
or program may malfunction due to changed call order of module static construction/destruction.
373373
In order to preserve the program behaviour without the need to specify `--DRT-oncycle` command line argument,
374374
it is possible to instruct the linker by declaring the following array in the source code:)
375-
375+
376376
---
377377
extern(C) __gshared string[] rt_options = [ "oncycle=ignore" ];
378378
---
379-
379+
380380
($P For more information, please consult the D language specification, section Overriding Cycle Detection Abort in chapter 4
381-
and chapter 28.7 Configuring the Garbage Collector.)
381+
and chapter 28.7 Configuring the Garbage Collector.)
382382
)
383383

384384
$(LI $(LNAME2 std-digest-murmurhash, Implementation of `std.digest.murmurhash`).

changelog/2.073.0.dd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ $(BUGSTITLE Compiler changes,
3737
$(LI $(LNAME2 mscrtlib-option,New command line option $(B -mscrtlib=$(I libname)) for Windows MS-COFF object files.)
3838
$(P
3939
If building MS-COFF object files with -m64 or -m32mscoff, this option
40-
specifies the reference to the C runtime library $(I libname) that is
40+
specifies the reference to the C runtime library $(I libname) that is
4141
embedded into the object file containing `main`,
4242
`DllMain` or `WinMain` for automatic linking. The default is $(TT libcmt)
4343
(release version with static linkage), the other usual alternatives are

foundation.dd

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ $(H3 Official contacts)
6363
$(P The D Language Foundation has been incorporated with the state of Washington, USA.
6464
The employer ID is 47-5352856.)
6565

66-
$(P To contact the D Language Foundation, please direct emails to
67-
$(LINK2 mailto:foundation@dlang.org, foundation@dlang.org) and other correspondence to
66+
$(P To contact the D Language Foundation, please direct emails to
67+
$(LINK2 mailto:foundation@dlang.org, foundation@dlang.org) and other correspondence to
6868
6830 NE Bothell Way, Suite C-162, Kenmore WA 98028.)
6969

7070
)

posix.mak

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -560,7 +560,7 @@ ${STABLE_DMD}:
560560
unzip -qd ${STABLE_DMD_ROOT} $${TMPFILE}.zip && rm $${TMPFILE}.zip
561561

562562
${DUB}: ${DUB_DIR} ${STABLE_DMD}
563-
cd ${DUB_DIR} && DC="$(abspath ${STABLE_DMD}) -conf=$(abspath ${STABLE_DMD_CONF})" ./build.sh
563+
cd ${DUB_DIR} && DMD="${STABLE_DMD}" ./build.sh
564564

565565
################################################################################
566566
# chm help files
@@ -589,11 +589,16 @@ d.tag : chmgen.d $(STABLE_DMD) $(ALL_FILES) phobos-release druntime-release
589589

590590
ASSERT_WRITELN_BIN = $(GENERATED)/assert_writeln_magic
591591

592-
$(ASSERT_WRITELN_BIN): assert_writeln_magic.d $(DUB)
592+
$(ASSERT_WRITELN_BIN): assert_writeln_magic.d $(DUB) $(STABLE_DMD)
593593
@mkdir -p $(dir $@)
594594
$(DUB) build --single --compiler=$(STABLE_DMD) $<
595595
@mv ./assert_writeln_magic $@
596596

597+
$(ASSERT_WRITELN_BIN)_test: assert_writeln_magic.d $(DUB) $(STABLE_DMD)
598+
@mkdir -p $(dir $@)
599+
$(DUB) build --single --compiler=$(STABLE_DMD) --build=unittest $<
600+
@mv ./assert_writeln_magic $@
601+
597602
$(PHOBOS_FILES_GENERATED): $(PHOBOS_DIR_GENERATED)/%: $(PHOBOS_DIR)/% $(DUB) $(ASSERT_WRITELN_BIN)
598603
@mkdir -p $(dir $@)
599604
@if [ $(subst .,, $(suffix $@)) == "d" ] && [ "$@" != "$(PHOBOS_DIR_GENERATED)/index.d" ] ; then \
@@ -610,9 +615,12 @@ $(PHOBOS_STABLE_FILES_GENERATED): $(PHOBOS_STABLE_DIR_GENERATED)/%: $(PHOBOS_STA
610615
# Style tests
611616
################################################################################
612617

613-
test:
618+
test: $(ASSERT_WRITELN_BIN)_test
614619
@echo "Searching for trailing whitespace"
615-
if [[ $$(find . -type f -name "*.dd" -exec egrep -l " +$$" {} \;) ]] ; then $$(exit 1); fi
620+
@echo "Check for trailing whitespace"
621+
grep -n '[[:blank:]]$$' $$(find . -type f -name "*.dd") ; test $$? -eq 1
622+
@echo "Executing assert_writeln_magic tests"
623+
$<
616624

617625
################################################################################
618626
# Changelog generation

0 commit comments

Comments
 (0)