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 Nov 24, 2024
1 parent 20d987c commit 176b128
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions std/format/read.d
Original file line number Diff line number Diff line change
Expand Up @@ -741,6 +741,19 @@ 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"));
}

/// ditto
template formattedRead(alias fmt, Args...)
if (!isType!fmt && isSomeString!(typeof(fmt)) && Args.length && allSatisfy!(isType, Args))
Expand All @@ -759,6 +772,20 @@ 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));
}

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

0 comments on commit 176b128

Please sign in to comment.