mirror of
https://github.com/dlang/phobos.git
synced 2025-04-30 15:10:46 +03:00
use unittest as an example
This commit is contained in:
parent
f73972eada
commit
07dffef19f
1 changed files with 43 additions and 66 deletions
109
std/typecons.d
109
std/typecons.d
|
@ -1216,40 +1216,6 @@ if (distinctFieldNames!(Specs))
|
||||||
* $(TROW $(P `%(inner%|sep%)`), $(P The format `inner` is one format, that is applied
|
* $(TROW $(P `%(inner%|sep%)`), $(P The format `inner` is one format, that is applied
|
||||||
* on all fields of the `Tuple`. The inner format must be compatible to all
|
* on all fields of the `Tuple`. The inner format must be compatible to all
|
||||||
* of them.)))
|
* of them.)))
|
||||||
*
|
|
||||||
* Example:
|
|
||||||
* ---
|
|
||||||
* Tuple!(int, double)[3] tupList = [ tuple(1, 1.0), tuple(2, 4.0), tuple(3, 9.0) ];
|
|
||||||
*
|
|
||||||
* // Default format
|
|
||||||
* assert(format("%s", tuple("a", 1)) == `Tuple!(string, int)("a", 1)`);
|
|
||||||
*
|
|
||||||
* // One Format for each individual component
|
|
||||||
* assert(format("%(%#x v %.4f w %#x%)", tuple(1, 1.0, 10)) == `0x1 v 1.0000 w 0xa`);
|
|
||||||
* assert(format( "%#x v %.4f w %#x" , tuple(1, 1.0, 10).expand) == `0x1 v 1.0000 w 0xa`);
|
|
||||||
*
|
|
||||||
* // One Format for all components
|
|
||||||
* assert(format("%(>%s<%| & %)", tuple("abc", 1, 2.3, [4, 5])) == `>abc< & >1< & >2.3< & >[4, 5]<`);
|
|
||||||
*
|
|
||||||
* // Array of Tuples
|
|
||||||
* assert(format("%(%(f(%d) = %.1f%); %)", tupList) == `f(1) = 1.0; f(2) = 4.0; f(3) = 9.0`);
|
|
||||||
*
|
|
||||||
*
|
|
||||||
* // Error: %( %) missing.
|
|
||||||
* assertThrown!FormatException(
|
|
||||||
* format("%d, %f", tuple(1, 2.0)) == `1, 2.0`
|
|
||||||
* );
|
|
||||||
*
|
|
||||||
* // Error: %( %| %) missing.
|
|
||||||
* assertThrown!FormatException(
|
|
||||||
* format("%d", tuple(1, 2)) == `1, 2`
|
|
||||||
* );
|
|
||||||
*
|
|
||||||
* // Error: %d inadequate for double.
|
|
||||||
* assertThrown!FormatException(
|
|
||||||
* format("%(%d%|, %)", tuple(1, 2.0)) == `1, 2.0`
|
|
||||||
* );
|
|
||||||
* ---
|
|
||||||
*/
|
*/
|
||||||
void toString(DG)(scope DG sink) const
|
void toString(DG)(scope DG sink) const
|
||||||
{
|
{
|
||||||
|
@ -1257,6 +1223,48 @@ if (distinctFieldNames!(Specs))
|
||||||
toString(sink, f);
|
toString(sink, f);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
///
|
||||||
|
static if (Types.length == 0)
|
||||||
|
@safe unittest
|
||||||
|
{
|
||||||
|
import std.format : format, FormatException;
|
||||||
|
import std.exception : assertThrown;
|
||||||
|
|
||||||
|
// enum tupStr = tuple(1, 1.0).toString; // toString is *impure* for double (issue 17628)
|
||||||
|
// static assert(tupStr == `Tuple!(int, double)(1, 1)`);
|
||||||
|
|
||||||
|
Tuple!(int, double)[3] tupList = [ tuple(1, 1.0), tuple(2, 4.0), tuple(3, 9.0) ];
|
||||||
|
|
||||||
|
// Default format
|
||||||
|
assert(format("%s", tuple("a", 1)) == `Tuple!(string, int)("a", 1)`);
|
||||||
|
|
||||||
|
// One Format for each individual component
|
||||||
|
assert(format("%(%#x v %.4f w %#x%)", tuple(1, 1.0, 10)) == `0x1 v 1.0000 w 0xa`);
|
||||||
|
assert(format( "%#x v %.4f w %#x" , tuple(1, 1.0, 10).expand) == `0x1 v 1.0000 w 0xa`);
|
||||||
|
|
||||||
|
// One Format for all components
|
||||||
|
assert(format("%(>%s<%| & %)", tuple("abc", 1, 2.3, [4, 5])) == `>abc< & >1< & >2.3< & >[4, 5]<`);
|
||||||
|
|
||||||
|
// Array of Tuples
|
||||||
|
assert(format("%(%(f(%d) = %.1f%); %)", tupList) == `f(1) = 1.0; f(2) = 4.0; f(3) = 9.0`);
|
||||||
|
|
||||||
|
|
||||||
|
// Error: %( %) missing.
|
||||||
|
assertThrown!FormatException(
|
||||||
|
format("%d, %f", tuple(1, 2.0)) == `1, 2.0`
|
||||||
|
);
|
||||||
|
|
||||||
|
// Error: %( %| %) missing.
|
||||||
|
assertThrown!FormatException(
|
||||||
|
format("%d", tuple(1, 2)) == `1, 2`
|
||||||
|
);
|
||||||
|
|
||||||
|
// Error: %d inadequate for double
|
||||||
|
assertThrown!FormatException(
|
||||||
|
format("%(%d%|, %)", tuple(1, 2.0)) == `1, 2.0`
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
/// ditto
|
/// ditto
|
||||||
void toString(DG, Char)(scope DG sink, const ref FormatSpec!Char fmt) const
|
void toString(DG, Char)(scope DG sink, const ref FormatSpec!Char fmt) const
|
||||||
{
|
{
|
||||||
|
@ -1929,39 +1937,8 @@ private template ReverseTupleSpecs(T...)
|
||||||
import std.format : format, FormatException;
|
import std.format : format, FormatException;
|
||||||
import std.exception : assertThrown;
|
import std.exception : assertThrown;
|
||||||
|
|
||||||
// enum tupStr = tuple(1, 1.0).toString; // toString is *impure*.
|
//enum tupStr = tuple(1, 1.0).toString; // toString is *impure*.
|
||||||
//static assert(tupStr == `Tuple!(int, double)(1, 1)`);
|
//static assert(tupStr == `Tuple!(int, double)(1, 1)`);
|
||||||
|
|
||||||
Tuple!(int, double)[3] tupList = [ tuple(1, 1.0), tuple(2, 4.0), tuple(3, 9.0) ];
|
|
||||||
|
|
||||||
// Default format
|
|
||||||
assert(format("%s", tuple("a", 1)) == `Tuple!(string, int)("a", 1)`);
|
|
||||||
|
|
||||||
// One Format for each individual component
|
|
||||||
assert(format("%(%#x v %.4f w %#x%)", tuple(1, 1.0, 10)) == `0x1 v 1.0000 w 0xa`);
|
|
||||||
assert(format( "%#x v %.4f w %#x" , tuple(1, 1.0, 10).expand) == `0x1 v 1.0000 w 0xa`);
|
|
||||||
|
|
||||||
// One Format for all components
|
|
||||||
assert(format("%(>%s<%| & %)", tuple("abc", 1, 2.3, [4, 5])) == `>abc< & >1< & >2.3< & >[4, 5]<`);
|
|
||||||
|
|
||||||
// Array of Tuples
|
|
||||||
assert(format("%(%(f(%d) = %.1f%); %)", tupList) == `f(1) = 1.0; f(2) = 4.0; f(3) = 9.0`);
|
|
||||||
|
|
||||||
|
|
||||||
// Error: %( %) missing.
|
|
||||||
assertThrown!FormatException(
|
|
||||||
format("%d, %f", tuple(1, 2.0)) == `1, 2.0`
|
|
||||||
);
|
|
||||||
|
|
||||||
// Error: %( %| %) missing.
|
|
||||||
assertThrown!FormatException(
|
|
||||||
format("%d", tuple(1, 2)) == `1, 2`
|
|
||||||
);
|
|
||||||
|
|
||||||
// Error: %d inadequate for double
|
|
||||||
assertThrown!FormatException(
|
|
||||||
format("%(%d%|, %)", tuple(1, 2.0)) == `1, 2.0`
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Issue 17803, parte uno
|
// Issue 17803, parte uno
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue