Skip to content

Commit

Permalink
test: more config parser tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kucaahbe committed Feb 3, 2024
1 parent 63ca33f commit 9add34c
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
37 changes: 37 additions & 0 deletions source/config_parser.d
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,43 @@ CFG";
warnings[1] ~ " is incorrect");
}

/// _config file can not be opened
unittest
{
import std.exception;
import std.regex;

auto config = Config();
auto path = Path("not_found");

immutable auto exceptionMsg = collectExceptionMsg!ConfigFileException(parse_config(path, config));
immutable auto expectedMsg = "config file " ~ path.absolute ~ " does not exist";

assert(exceptionMsg == expectedMsg, exceptionMsg ~ ` != ` ~ expectedMsg);
}

/// _config file content is not valid UTF
unittest
{
import std.file;
import std.exception;
import std.regex;
import test_file;

if (".test".exists) ".test".rmdirRecurse;
scope(exit) if (".test".exists) ".test".rmdirRecurse;

auto config = Config();
auto path = Path(".test/app/install.sdl");
ubyte[] not_utf_data = [207, 250, 237];
TestFile(path.absolute, not_utf_data).create;

immutable auto exceptionMsg = collectExceptionMsg!ConfigFileException(parse_config(path, config));

assert(matchFirst(exceptionMsg, "failed to read config file"),
`"` ~ exceptionMsg ~ `" doesn't match ~/failed to read config file/`);
}

private void processSDLNode(ref SDLNode node, ref Config config, ref string[] warnings)
{
if (node.qualifiedName == INSTALL_DIRECTIVE) {
Expand Down
9 changes: 9 additions & 0 deletions source/test_file.d
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ version(unittest)
test_content = content;
}

this(string src, ubyte[] content)
{
this(src);
test_content_b = content;
}

ref TestFile create()
{
immutable auto dir = path.absolute.dirName;
Expand All @@ -27,11 +33,14 @@ version(unittest)

if (test_content.length > 0) {
std_file.write(path.absolute, test_content);
} else if (test_content_b.length > 0) {
std_file.write(path.absolute, test_content_b);
}

return this;
}

private string test_content;
private ubyte[] test_content_b;
}
}

0 comments on commit 9add34c

Please sign in to comment.