digitalmars.D.learn - Unit tests via DUB
- alexanderzhirov (37/37) Apr 02 2022 I don't quite understand why compiling unit tests using DUB
- Salih Dincer (2/4) Apr 02 2022 Source code?
- Alexander Zhirov (25/30) Apr 02 2022 A common example from a textbook
- Salih Dincer (6/37) Apr 02 2022 is there main()? if isn't it, please add the following line at
- Alexander Zhirov (41/83) Apr 02 2022 I managed to compile the file itself with this command:
- Steven Schveighoffer (18/57) Apr 02 2022 So to explain a couple things here:
- Andre Pany (5/8) Apr 03 2022 Have a look here
I don't quite understand why compiling unit tests using DUB doesn't work. JSON: ```json { "authors": [ "alexander" ], "description": "Array Slicing", "license": "proprietary", "name": "array_slicing", "targetName": "program", "targetPath": "bin" } ``` Projects structure: ```sh ├── bin ├── dub.json └── source └── app.d ``` Errors: ```sh No source files found in configuration 'library'. Falling back to "dub -b unittest". Performing "unittest" build using /usr/bin/dmd for x86_64. array_slicing ~master: building configuration "application"... Linking... /usr/bin/ld: /usr/lib64/gcc/x86_64-solus-linux/11/../../../../lib64/crt1.o: в функции «_start»: /home/build/YPKG/root/glibc/build/glibc.git/csu/../sysdeps/x86_64/start.S:110: неопределённая ссылка на «main» collect2: ошибка: выполнение ld завершилось с кодом возврата 1 Error: linker exited with status 1 /usr/bin/dmd failed with exit code 1. ```
Apr 02 2022
On Saturday, 2 April 2022 at 11:53:12 UTC, alexanderzhirov wrote:I don't quite understand why compiling unit tests using DUB doesn't work.Source code?
Apr 02 2022
On Saturday, 2 April 2022 at 13:12:04 UTC, Salih Dincer wrote:On Saturday, 2 April 2022 at 11:53:12 UTC, alexanderzhirov wrote:A common example from a textbook ```d import std.array; bool binarySearch(T)(T[] input, T value) { while (!input.empty) { auto i = input.length / 2; auto mid = input[i]; if (mid > value) input = input[0 .. i]; else if (mid < value) input = input[i + 1 .. $]; else return true; } return false; } unittest { assert(binarySearch([1, 3, 6, 7, 9, 15], 7)); assert(!binarySearch([1, 3, 6, 7, 9, 15], 5)); } ```I don't quite understand why compiling unit tests using DUB doesn't work.Source code?
Apr 02 2022
On Saturday, 2 April 2022 at 13:31:47 UTC, Alexander Zhirov wrote:On Saturday, 2 April 2022 at 13:12:04 UTC, Salih Dincer wrote:is there main()? if isn't it, please add the following line at the end: ```d void main() {} ```On Saturday, 2 April 2022 at 11:53:12 UTC, alexanderzhirov wrote:A common example from a textbook ```d import std.array; bool binarySearch(T)(T[] input, T value) { while (!input.empty) { auto i = input.length / 2; auto mid = input[i]; if (mid > value) input = input[0 .. i]; else if (mid < value) input = input[i + 1 .. $]; else return true; } return false; } unittest { assert(binarySearch([1, 3, 6, 7, 9, 15], 7)); assert(!binarySearch([1, 3, 6, 7, 9, 15], 5)); } ```I don't quite understand why compiling unit tests using DUB doesn't work.Source code?
Apr 02 2022
On Saturday, 2 April 2022 at 14:35:49 UTC, Salih Dincer wrote:On Saturday, 2 April 2022 at 13:31:47 UTC, Alexander Zhirov wrote:I managed to compile the file itself with this command: ```sh dmd -main -unittest app.d ``` It was also possible to compile with the addition of the *main()* function to the project. In the case of dub, I added entry to the project *"buildOptions": ["unittests"]* and the file now looks like this: ```json { "authors": [ "alexander" ], "description": "Array Slicing", "license": "proprietary", "name": "array_slicing", "targetName": "program", "targetPath": "bin", "buildOptions": ["unittests"] } ``` And when I call dub, I get this message ```sh The following compiler flags have been specified in the package description file. They are handled by DUB and direct use in packages is discouraged. Alternatively, you can set the DFLAGS environment variable to pass custom flags to the compiler, or use one of the suggestions below: unittests: Call DUB with --build=unittest Performing "debug" build using dmd for x86_64. array_slicing ~master: building configuration "application"... Linking... Running bin/program 1 modules passed unittests ``` But as you can see, the unit test works. But how do I do all this right?On Saturday, 2 April 2022 at 13:12:04 UTC, Salih Dincer wrote:is there main()? if isn't it, please add the following line at the end: ```d void main() {} ```On Saturday, 2 April 2022 at 11:53:12 UTC, alexanderzhirov wrote:A common example from a textbook ```d import std.array; bool binarySearch(T)(T[] input, T value) { while (!input.empty) { auto i = input.length / 2; auto mid = input[i]; if (mid > value) input = input[0 .. i]; else if (mid < value) input = input[i + 1 .. $]; else return true; } return false; } unittest { assert(binarySearch([1, 3, 6, 7, 9, 15], 7)); assert(!binarySearch([1, 3, 6, 7, 9, 15], 5)); } ```I don't quite understand why compiling unit tests using DUB doesn't work.Source code?
Apr 02 2022
On 4/2/22 7:53 AM, alexanderzhirov wrote:I don't quite understand why compiling unit tests using DUB doesn't work. JSON: ```json { "authors": [ "alexander" ], "description": "Array Slicing", "license": "proprietary", "name": "array_slicing", "targetName": "program", "targetPath": "bin" } ``` Projects structure: ```sh ├── bin ├── dub.json └── source └── app.d ``` Errors: ```sh No source files found in configuration 'library'. Falling back to "dub -b unittest". Performing "unittest" build using /usr/bin/dmd for x86_64. array_slicing ~master: building configuration "application"... Linking... /usr/bin/ld: /usr/lib64/gcc/x86_64-solus-linux/11/../../../../lib64/crt1.o: в функции «_start»: /home/build/YPKG/root/glibc/build/glibc.git/csu/../sysdeps/x86_64/start.S:110: неопределённая ссылка на «main» collect2: ошибка: выполнение ld завершилось с кодом возврата 1 Error: linker exited with status 1 /usr/bin/dmd failed with exit code 1. ```So to explain a couple things here: 1. you have a source file called `app.d`, which means to dub that this is an executable target, not a library. 2. `dub test` is made for *libraries*. What it does is add its own test runner and main function. Because it thinks you are trying to test an executable, it defaults to running `dub -b unittest` which is the same as building an application, but just adds the `-unittest` flag to the compiler. 3. Your build has no `main` function, so the linker fails. Solutions: 1. Add a `main()` function to your program. 2. Change the name of your source file so it doesn't think this is an application 3. Declare the project is a library via `targetType`. Without knowing what you are doing with the build, I'd say likely you should add the main function as suggested by Salih Dincer. -Steve
Apr 02 2022
On Saturday, 2 April 2022 at 11:53:12 UTC, alexanderzhirov wrote:I don't quite understand why compiling unit tests using DUB doesn't work. [...]Have a look here https://andre2007.github.io/d-tips/dub/application_template/ Kind regards Andre
Apr 03 2022