From 07dffef19f842fb45ad79c99ec694335b7a2824f Mon Sep 17 00:00:00 2001 From: "Q. F. Schroll" Date: Mon, 7 May 2018 21:47:06 +0200 Subject: [PATCH] use unittest as an example --- std/typecons.d | 109 +++++++++++++++++++------------------------------ 1 file changed, 43 insertions(+), 66 deletions(-) diff --git a/std/typecons.d b/std/typecons.d index 86e5474d7..1ed6449a4 100644 --- a/std/typecons.d +++ b/std/typecons.d @@ -1216,40 +1216,6 @@ if (distinctFieldNames!(Specs)) * $(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 * 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 { @@ -1257,6 +1223,48 @@ if (distinctFieldNames!(Specs)) 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 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.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)`); - - 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