Skip to content

Commit

Permalink
test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
adamdruppe committed Oct 24, 2023
1 parent 071de77 commit 75f6f32
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/* TEST_OUTPUT:
---
interpolatedexpressionsequence_postfix.d(10): Error: String postfixes on interpolated expression sequences are not allowed.
interpolatedexpressionsequence_postfix.d(11): Error: String postfixes on interpolated expression sequences are not allowed.
interpolatedexpressionsequence_postfix.d(12): Error: String postfixes on interpolated expression sequences are not allowed.
---
*/
void main() {
// all postfixes are banned
auto c = i"foo"c;
auto w = i"foo"w;
auto d = i"foo"d;
}
38 changes: 38 additions & 0 deletions compiler/test/runnable/interpolatedexpressionsequence.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import core.interpolation;

alias AliasSeq(T...) = T;

void main() {
int a = 1;
string b = "one";
// parser won't permit alias = i".." directly; i"..." is meant to
// be used as a function/template parameter at this time.
alias expr = AliasSeq!i"$a $(b)";
// elements from the source code are available at compile time, so
// we static assert those, but the values, of course, are different
static assert(expr[0] == InterpolationHeader());
static assert(expr[1] == InterpolatedExpression!"a"());
assert(expr[2] == a); // actual value not available at compile time
static assert(expr[3] == InterpolatedLiteral!" "());
// the parens around the expression are not included
static assert(expr[4] == InterpolatedExpression!"b"());
assert(expr[5] == b); // actual value not available at compile time
static assert(expr[6] == InterpolationFooter());

// it does currently allow `auto` to be used, it creates a value tuple
// you can embed any D expressions inside the parenthesis, and the
// token is not ended until you get the *outer* ) and ".
auto thing = i"$b $("$" ~ ')' ~ `"`)";
string s;
foreach(item; thing)
// all the items provided by core.interpolation have
// toString to return an appropriate value
//
// then this particular example only has embedded strings
// and chars, to we can append them directly
static if(__traits(hasMember, item, "toString"))
s ~= item.toString();
else
s ~= item;
assert(s == "one $)\"");
}

0 comments on commit 75f6f32

Please sign in to comment.