Run unitest as default behaviour of a program.

BoQsc vaidas.boqsc at gmail.com
Fri Apr 5 08:40:52 UTC 2024


Testing is quite important when things change rapidly.

To make sure the debugging is easier, I decided to make programs 
that would notify me about something not working as expected.

I'm getting tired with specifying `-unittest` flag on every 
script (.cmd .bat .sh .bash) and manually in a command line or 
terminal.

Wonder if there is any way to make `unittest{}` the **main** 
method of a source file.

#### Example program
Here is an example of my program.
As you can see the **main** method currently used for showing 
notice on how to run the test.
I would like to not need that.

```
void main() {
	writeln("Please run:    rdmd -unittest Path_Parser.d    to start 
the test. ");
}

string[] splitPathString(string text) {

	string word;
     string[] wordList;
	
     foreach (char letter; text) {
	
         if (letter == '/' || letter == '\\') {
			wordList ~= word;
			word = "";
			continue;
		}
		
		word ~= letter;
     }
     return wordList;
}

unittest
{
	string[] paths = [
		"./directory/folder/subfolder/",
		"./directory/folder/subfolder/",
		r"C:\Users\Windows10\Documents\GitHub\Operating-System\Operating_System\System\Print_Folder_Tree\Path_Parser",
	];

     foreach (path; paths) writeln(path);

     foreach (string path; paths) {
		foreach (directory; splitPathString(path)) {
             write(directory, );
             write("\n");
		}
     }
}

```


More information about the Digitalmars-d-learn mailing list