Skip to content

Commit

Permalink
test(formattedRead): add unnittests for the tuple return type overloads
Browse files Browse the repository at this point in the history
Signed-off-by: João Lourenço <jlourenco5691@gmail.com>
  • Loading branch information
iK4tsu committed Mar 22, 2023
1 parent ed7234f commit 4489cd8
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions std/format/read.d
Original file line number Diff line number Diff line change
Expand Up @@ -712,6 +712,22 @@ if (Args.length && allSatisfy!(isType, Args))
}
}

///
@safe pure unittest
{
import std.exception : assertThrown;
import std.format : FormatException;
import std.typecons : No, tuple;

auto complete = "hello!34.5:124".formattedRead!(string, double, int)("%s!%s:%s");
assert(complete == tuple("hello", 34.5, 124));

assertThrown!FormatException("hello!34.5:".formattedRead!(string, double, int)("%s!%s:%s"));

auto missing = "hello!34.5:".formattedRead!(string, double, int)("%s!%s:%s", No.exhaustive);
assert(missing == tuple("hello", 34.5, int.init));
}

/// ditto
template formattedRead(alias fmt, Args...)
if (!isType!fmt && isSomeString!(typeof(fmt)) && Args.length && allSatisfy!(isType, Args))
Expand All @@ -730,6 +746,23 @@ if (!isType!fmt && isSomeString!(typeof(fmt)) && Args.length && allSatisfy!(isTy
}
}

/// The format string can be checked at compile-time:
@safe pure unittest
{
import std.exception : assertThrown;
import std.format : FormatException;
import std.typecons : No, tuple;

auto expected = tuple("hello", 124, 34.5);
auto result = "hello!124:34.5".formattedRead!("%s!%s:%s", string, int, double);
assert(result == expected);

assertThrown!FormatException("hello!34.5:".formattedRead!("%s!%s:%s", string, double, int));

auto missing = "hello!34.5:".formattedRead!("%s!%s:%s", string, double, int)(No.exhaustive);
assert(missing == tuple("hello", 34.5, int.init));
}

/**
Reads a value from the given _input range and converts it according to a
format specifier.
Expand Down

0 comments on commit 4489cd8

Please sign in to comment.