Running unit tests from DUB single file packages

Johannes Loher johannes.loher at fg4f.de
Tue Dec 1 11:40:38 UTC 2020


Hello everybody,

when solving today's problem for the adventofcode [1], I decided to try
out DUB's single file package feature, in order to keep things short.

When doing the adventofcode, I always use the given examples as unit
tests in order to verify that my solutions are actually correct.
However, I am having trouble running the unit tests when using the
single file package format:

```
johannesloher at saiph:..020/day1/part1-mir(master)> dub test --single
main.d

Package main (configuration "application") defines no import paths, use
{"importPaths": [...]} or the default package directory structure to fix
this.

Generating test runner configuration 'main-test-application' for
'application' (executable).

Source file
'/home/johannesloher/Development/aoc2020/day1/part1-mir/main.d' not
found in any import path.
```

I tried defining `importPaths` as suggested in the error message but
apparently that's not possible for single file packages:

```
Single-file packages are not allowed to specify import paths.
```

WARNING: Spoilers for day 1 of the adventofcode, please stop reading if
you have not solved it yet and want to do so by yourself.

Here is the source for my main.d file:

```
#!/usr/bin/env dub
/+ dub.sdl:
    name "main"
    dependency "mir-algorithm" version="~>3.10.12"
+/

import std;
import mir.combinatorics;

void main()
{
    File("input",
"r").byLine.map!(to!int).array.multiplyNEntriesThatSumTo(2, 2020).writeln;
}

alias product = partial!(reverseArgs!(fold!((a, b) => a * b)), 1);

int multiplyNEntriesThatSumTo(int[] input, int n, int requiredSum)
{
    return input.combinations(n).filter!(combination => combination.sum
== requiredSum)
        .map!(combination => combination.product)
        .front;
}

unittest
{
    auto input = [1721, 979, 366, 299, 675, 1456];

    assert(input.multiplyNEntriesThatSumTo(2, 2020) == 514_579);
}
```

Any hints on how to execute unit tests from single file DUB packages? Is
it even possible at the moment? Thanks in advance for any help!


[1] https://adventofcode.com/


More information about the Digitalmars-d-learn mailing list