Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[features/interpolated-constant-strings] Adding Tests #46828

Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
142 changes: 81 additions & 61 deletions src/Compilers/CSharp/Test/Semantic/Semantics/ConstantTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3520,20 +3520,30 @@ public class A : System.Attribute
public A(string name)
{
this.name = name;
}
}
}

[A($""ITEM"")]
class C
{
void M(string S1 = $""Testing"")
string S0 = $""Faaaaaaaaaaaaaaaaaaaaaaaaall"";
kevinsun-dev marked this conversation as resolved.
Show resolved Hide resolved

class Namae
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Namae [](start = 10, length = 5)

nit: That's a weird namae ;-)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: That's a weird 名前 ;-)

Indeed it is.

{
switch(S1){
case $""Level 5"":
break;
case $""Radio Noise"":
goto case $""Level 5"";
public string X { get; }
}

void M(string S1 = $""Testing"", Namae n = null)
{
if (n is Namae { X : $""ConstantInterpolatedString""}){
switch(S1){
case $""Level 5"":
break;
case $""Radio Noise"":
goto case $""Level 5"";
}
}
S0 = S1;
}
}";
var comp = CreateCompilation(source, parseOptions: TestOptions.RegularPreview);
Expand Down Expand Up @@ -3645,6 +3655,11 @@ public A(string name)
[A($""ITEM"")]
class C
{
class Namae
kevinsun-dev marked this conversation as resolved.
Show resolved Hide resolved
{
public string X { get; }
}

#pragma warning disable CS0219 // unused locals
void M1()
{
Expand All @@ -3660,74 +3675,79 @@ void M1()
const string S6 = $""Failed to {VS}"";
}

void M2(string S1 = $""Testing"", object O1 = null)
void M2(string S1 = $""Testing"", object O = null, Namae N = null)
{
switch(S1){
case $""Level 5"":
break;
}

switch(O1){
case $""Number 3"":
break;
case $""Radio Noise"":
goto case $""Number 3"";
if (N is Namae { X : $""ConstantInterpolatedString""}){
switch(O){
case $""Number 3"":
break;
case $""Radio Noise"":
goto case $""Number 3"";
}
}
}
}";
var comp = CreateCompilation(source, parseOptions: TestOptions.RegularPreview);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: usage of Preview should be replaced with CSharp9 at some point

comp.VerifyDiagnostics(
// (15,27): error CS0133: The expression being assigned to 'S6' must be constant
// const string S6 = $"Failed to {VS}";
Diagnostic(ErrorCode.ERR_NotConstantExpression, @"$""Failed to {VS}""").WithArguments("S6").WithLocation(27, 27));
// (32,27): error CS0133: The expression being assigned to 'S6' must be constant
// const string S6 = $"Failed to {VS}";
Diagnostic(ErrorCode.ERR_NotConstantExpression, @"$""Failed to {VS}""").WithArguments("S6").WithLocation(32, 27));

comp = CreateCompilation(source, parseOptions: TestOptions.Regular8);
comp.VerifyDiagnostics(
// (12,4): error CS8652: The feature 'constant interpolated strings' is currently in Preview and *unsupported*. To use Preview features, use the 'preview' language version.
// [A($"ITEM")]
Diagnostic(ErrorCode.ERR_FeatureInPreview, @"$""ITEM""").WithArguments("constant interpolated strings").WithLocation(12, 4),
// (18,27): error CS8652: The feature 'constant interpolated strings' is currently in Preview and *unsupported*. To use Preview features, use the 'preview' language version.
// const string S1 = $"Testing";
Diagnostic(ErrorCode.ERR_FeatureInPreview, @"$""Testing""").WithArguments("constant interpolated strings").WithLocation(18, 27),
// (19,27): error CS8652: The feature 'constant interpolated strings' is currently in Preview and *unsupported*. To use Preview features, use the 'preview' language version.
// const string S2 = $"{"Level 5"} {"Number 3"}";
Diagnostic(ErrorCode.ERR_FeatureInPreview, @"$""{""Level 5""} {""Number 3""}""").WithArguments("constant interpolated strings").WithLocation(19, 27),
// (20,27): error CS8652: The feature 'constant interpolated strings' is currently in Preview and *unsupported*. To use Preview features, use the 'preview' language version.
// const string S3 = $"{$"{"Spinning Top"}"}";
Diagnostic(ErrorCode.ERR_FeatureInPreview, @"$""{$""{""Spinning Top""}""}""").WithArguments("constant interpolated strings").WithLocation(20, 27),
// (21,27): error CS8652: The feature 'constant interpolated strings' is currently in Preview and *unsupported*. To use Preview features, use the 'preview' language version.
// const string S4 = $"Hybrid" + "Testing" + "123";
Diagnostic(ErrorCode.ERR_FeatureInPreview, @"$""Hybrid""").WithArguments("constant interpolated strings").WithLocation(21, 27),
// (22,50): error CS8652: The feature 'constant interpolated strings' is currently in Preview and *unsupported*. To use Preview features, use the 'preview' language version.
// const string S5 = "Hybrid" + "Testing" + $"321";
Diagnostic(ErrorCode.ERR_FeatureInPreview, @"$""321""").WithArguments("constant interpolated strings").WithLocation(22, 50),
// (23,27): error CS8652: The feature 'constant interpolated strings' is currently in Preview and *unsupported*. To use Preview features, use the 'preview' language version.
// const string F1 = $"{S1}";
Diagnostic(ErrorCode.ERR_FeatureInPreview, @"$""{S1}""").WithArguments("constant interpolated strings").WithLocation(23, 27),
// (24,32): error CS8652: The feature 'constant interpolated strings' is currently in Preview and *unsupported*. To use Preview features, use the 'preview' language version.
// const string F2 = F1 + $" the {S2}";
Diagnostic(ErrorCode.ERR_FeatureInPreview, @"$"" the {S2}""").WithArguments("constant interpolated strings").WithLocation(24, 32),
// (27,27): error CS8652: The feature 'constant interpolated strings' is currently in Preview and *unsupported*. To use Preview features, use the 'preview' language version.
// const string S6 = $"Failed to {VS}";
Diagnostic(ErrorCode.ERR_FeatureInPreview, @"$""Failed to {VS}""").WithArguments("constant interpolated strings").WithLocation(27, 27),
// (27,27): error CS0133: The expression being assigned to 'S6' must be constant
// const string S6 = $"Failed to {VS}";
Diagnostic(ErrorCode.ERR_NotConstantExpression, @"$""Failed to {VS}""").WithArguments("S6").WithLocation(27, 27),
// (30,25): error CS8652: The feature 'constant interpolated strings' is currently in Preview and *unsupported*. To use Preview features, use the 'preview' language version.
// void M2(string S1 = $"Testing", object O1 = null)
Diagnostic(ErrorCode.ERR_FeatureInPreview, @"$""Testing""").WithArguments("constant interpolated strings").WithLocation(30, 25),
// (33,18): error CS8652: The feature 'constant interpolated strings' is currently in Preview and *unsupported*. To use Preview features, use the 'preview' language version.
// case $"Level 5":
Diagnostic(ErrorCode.ERR_FeatureInPreview, @"$""Level 5""").WithArguments("constant interpolated strings").WithLocation(33, 18),
// (38,18): error CS8652: The feature 'constant interpolated strings' is currently in Preview and *unsupported*. To use Preview features, use the 'preview' language version.
// case $"Number 3":
Diagnostic(ErrorCode.ERR_FeatureInPreview, @"$""Number 3""").WithArguments("constant interpolated strings").WithLocation(38, 18),
// (40,18): error CS8652: The feature 'constant interpolated strings' is currently in Preview and *unsupported*. To use Preview features, use the 'preview' language version.
// case $"Radio Noise":
Diagnostic(ErrorCode.ERR_FeatureInPreview, @"$""Radio Noise""").WithArguments("constant interpolated strings").WithLocation(40, 18),
// (41,27): error CS8652: The feature 'constant interpolated strings' is currently in Preview and *unsupported*. To use Preview features, use the 'preview' language version.
// goto case $"Number 3";
Diagnostic(ErrorCode.ERR_FeatureInPreview, @"$""Number 3""").WithArguments("constant interpolated strings").WithLocation(41, 27));
// (12,4): error CS8652: The feature 'constant interpolated strings' is currently in Preview and *unsupported*. To use Preview features, use the 'preview' language version.
// [A($"ITEM")]
Diagnostic(ErrorCode.ERR_FeatureInPreview, @"$""ITEM""").WithArguments("constant interpolated strings").WithLocation(12, 4),
// (23,27): error CS8652: The feature 'constant interpolated strings' is currently in Preview and *unsupported*. To use Preview features, use the 'preview' language version.
// const string S1 = $"Testing";
Diagnostic(ErrorCode.ERR_FeatureInPreview, @"$""Testing""").WithArguments("constant interpolated strings").WithLocation(23, 27),
// (24,27): error CS8652: The feature 'constant interpolated strings' is currently in Preview and *unsupported*. To use Preview features, use the 'preview' language version.
// const string S2 = $"{"Level 5"} {"Number 3"}";
Diagnostic(ErrorCode.ERR_FeatureInPreview, @"$""{""Level 5""} {""Number 3""}""").WithArguments("constant interpolated strings").WithLocation(24, 27),
// (25,27): error CS8652: The feature 'constant interpolated strings' is currently in Preview and *unsupported*. To use Preview features, use the 'preview' language version.
// const string S3 = $"{$"{"Spinning Top"}"}";
Diagnostic(ErrorCode.ERR_FeatureInPreview, @"$""{$""{""Spinning Top""}""}""").WithArguments("constant interpolated strings").WithLocation(25, 27),
// (26,27): error CS8652: The feature 'constant interpolated strings' is currently in Preview and *unsupported*. To use Preview features, use the 'preview' language version.
// const string S4 = $"Hybrid" + "Testing" + "123";
Diagnostic(ErrorCode.ERR_FeatureInPreview, @"$""Hybrid""").WithArguments("constant interpolated strings").WithLocation(26, 27),
// (27,50): error CS8652: The feature 'constant interpolated strings' is currently in Preview and *unsupported*. To use Preview features, use the 'preview' language version.
// const string S5 = "Hybrid" + "Testing" + $"321";
Diagnostic(ErrorCode.ERR_FeatureInPreview, @"$""321""").WithArguments("constant interpolated strings").WithLocation(27, 50),
// (28,27): error CS8652: The feature 'constant interpolated strings' is currently in Preview and *unsupported*. To use Preview features, use the 'preview' language version.
// const string F1 = $"{S1}";
Diagnostic(ErrorCode.ERR_FeatureInPreview, @"$""{S1}""").WithArguments("constant interpolated strings").WithLocation(28, 27),
// (29,32): error CS8652: The feature 'constant interpolated strings' is currently in Preview and *unsupported*. To use Preview features, use the 'preview' language version.
// const string F2 = F1 + $" the {S2}";
Diagnostic(ErrorCode.ERR_FeatureInPreview, @"$"" the {S2}""").WithArguments("constant interpolated strings").WithLocation(29, 32),
// (32,27): error CS8652: The feature 'constant interpolated strings' is currently in Preview and *unsupported*. To use Preview features, use the 'preview' language version.
// const string S6 = $"Failed to {VS}";
Diagnostic(ErrorCode.ERR_FeatureInPreview, @"$""Failed to {VS}""").WithArguments("constant interpolated strings").WithLocation(32, 27),
// (32,27): error CS0133: The expression being assigned to 'S6' must be constant
// const string S6 = $"Failed to {VS}";
Diagnostic(ErrorCode.ERR_NotConstantExpression, @"$""Failed to {VS}""").WithArguments("S6").WithLocation(32, 27),
// (35,25): error CS8652: The feature 'constant interpolated strings' is currently in Preview and *unsupported*. To use Preview features, use the 'preview' language version.
// void M2(string S1 = $"Testing", object O = null, Namae N = null)
Diagnostic(ErrorCode.ERR_FeatureInPreview, @"$""Testing""").WithArguments("constant interpolated strings").WithLocation(35, 25),
// (38,18): error CS8652: The feature 'constant interpolated strings' is currently in Preview and *unsupported*. To use Preview features, use the 'preview' language version.
// case $"Level 5":
Diagnostic(ErrorCode.ERR_FeatureInPreview, @"$""Level 5""").WithArguments("constant interpolated strings").WithLocation(38, 18),
// (42,30): error CS8652: The feature 'constant interpolated strings' is currently in Preview and *unsupported*. To use Preview features, use the 'preview' language version.
// if (N is Namae { X : $"ConstantInterpolatedString"}){
Diagnostic(ErrorCode.ERR_FeatureInPreview, @"$""ConstantInterpolatedString""").WithArguments("constant interpolated strings").WithLocation(42, 30),
// (44,22): error CS8652: The feature 'constant interpolated strings' is currently in Preview and *unsupported*. To use Preview features, use the 'preview' language version.
// case $"Number 3":
Diagnostic(ErrorCode.ERR_FeatureInPreview, @"$""Number 3""").WithArguments("constant interpolated strings").WithLocation(44, 22),
// (46,22): error CS8652: The feature 'constant interpolated strings' is currently in Preview and *unsupported*. To use Preview features, use the 'preview' language version.
// case $"Radio Noise":
Diagnostic(ErrorCode.ERR_FeatureInPreview, @"$""Radio Noise""").WithArguments("constant interpolated strings").WithLocation(46, 22),
// (47,31): error CS8652: The feature 'constant interpolated strings' is currently in Preview and *unsupported*. To use Preview features, use the 'preview' language version.
// goto case $"Number 3";
Diagnostic(ErrorCode.ERR_FeatureInPreview, @"$""Number 3""").WithArguments("constant interpolated strings").WithLocation(47, 31));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4025,7 +4025,7 @@ static void F()
}

[WorkItem(976, "https://github.com/dotnet/roslyn/issues/976")]
[Fact(Skip = "PROTOTYPE(CONSTISTR) - Asserts that Interpolated Strings are not constant, but the first can be.")]
[Fact]
public void ConstantValueOfInterpolatedString()
{
var source = @"
Expand All @@ -4041,9 +4041,15 @@ static void Main(string[] args)
var comp = CreateCompilation(source, options: TestOptions.ReleaseExe);
var tree = comp.SyntaxTrees.Single();
var model = comp.GetSemanticModel(tree);

var expected = new bool[2];
kevinsun-dev marked this conversation as resolved.
Show resolved Hide resolved
expected[0] = true;
expected[1] = false;
var iterator = 0;
foreach (var interp in tree.GetRoot().DescendantNodes().OfType<InterpolatedStringExpressionSyntax>())
kevinsun-dev marked this conversation as resolved.
Show resolved Hide resolved
{
Assert.False(model.GetConstantValue(interp).HasValue);
Assert.Equal(expected[iterator], model.GetConstantValue(interp).HasValue);
iterator++;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3212,7 +3212,7 @@ void M()
}

[WorkItem(1065661, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/1065661")]
[Fact(Skip = "PROTOTYPE(CONSTISTR) - Switched from introducing a local to introducing a constant."), Trait(Traits.Feature, Traits.Features.CodeActionsIntroduceVariable)]
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsIntroduceVariable)]
public async Task TestIntroduceVariableTextDoesntSpanLines2()
{
await TestSmartTagTextAsync(
Expand All @@ -3226,7 +3226,7 @@ void M()
c""|];
}
}",
string.Format(FeaturesResources.Introduce_local_for_0, @"$@""a b c"""));
string.Format(FeaturesResources.Introduce_constant_for_0, @"$@""a b c"""));
}

[WorkItem(1097147, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/1097147")]
Expand Down Expand Up @@ -4565,7 +4565,7 @@ class TestClass

[WorkItem(976, "https://github.com/dotnet/roslyn/issues/976")]
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsIntroduceVariable)]
public async Task TestNoConstantForInterpolatedStrings1()
public async Task TestNoConstantForInterpolatedStrings()
{
var code =
@"using System;
Expand All @@ -4592,8 +4592,8 @@ static void Test(string[] args)
}

[WorkItem(976, "https://github.com/dotnet/roslyn/issues/976")]
[Fact(Skip = "PROTOTYPE(CONSTISTR) - Expected changed var into a private const."), Trait(Traits.Feature, Traits.Features.CodeActionsIntroduceVariable)]
public async Task TestNoConstantForInterpolatedStrings2()
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsIntroduceVariable)]
public async Task TestConstantForInterpolatedStrings()
Copy link
Member

@CyrusNajmabadi CyrusNajmabadi Aug 17, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you add a test where the interpolated string references a real constant value? i.e. 0 instead of just the unbound s? thanks!

could you add a test where there is an actual interpolation and the interpolation contains a constant? i.e. ([|$""Text{0}""|])

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: this would have to be ([|$""Text{""0""}""|]), as all constant values inside must be constant string expressions or it's not a constant string itself.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, added a test for that. A int constant should register as invalid and cause us to suggest a variable instead.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think Cyrus wanted a test for when the interpolation includes things that would still make it a constant value.

{
var code =
@"using System;
Expand All @@ -4610,11 +4610,12 @@ static void Test(string[] args)
@"using System;
class TestClass
{
private const string {|Rename:Value|} = $""Text{{s}}"";
333fred marked this conversation as resolved.
Show resolved Hide resolved

static void Test(string[] args)
{
var {|Rename:value|} = $""Text{{s}}"";
Console.WriteLine(value);
Console.WriteLine(value);
Console.WriteLine(Value);
Console.WriteLine(Value);
}
}";

Expand Down