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

Conversation

kevinsun-dev
Copy link
Contributor

Test Plan: #46780

Added Tests:

  • Usage in constant field initializers
  • Usage in nested patterns

@kevinsun-dev kevinsun-dev force-pushed the features/interpolated-string-constants branch from 6bd4fc6 to 26a4227 Compare August 14, 2020 21:16
@kevinsun-dev kevinsun-dev force-pushed the features/interpolated-string-constants branch from 26a4227 to 8333853 Compare August 14, 2020 21:17
@333fred
Copy link
Member

333fred commented Aug 14, 2020

@CyrusNajmabadi can you take a look at the introduce variable changes? I intend on updating the refactoring sometime next week to understand C# 9, so it's currently unconditional.

@333fred 333fred requested a review from CyrusNajmabadi August 14, 2020 21:34
@333fred
Copy link
Member

333fred commented Aug 14, 2020

Done review pass (commit 3)

@kevinsun-dev kevinsun-dev marked this pull request as ready for review August 17, 2020 16:12
@kevinsun-dev kevinsun-dev requested review from a team as code owners August 17, 2020 16:12
}

var expected = new[] { true, false };
var actual = tree.GetRoot().DescendantNodes().OfType<InterpolatedStringExpressionSyntax>().ToArray();
Copy link
Member

Choose a reason for hiding this comment

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

I would just remove this array and inline the constants. Assert.True and Assert.False.

@333fred
Copy link
Member

333fred commented Aug 17, 2020

@kevinsun-dev it looks like you have some trailing whitespace in the string constant for IntroduceVariableTests.TestNoConstantForInterpolatedStrings, that's causing the test failure.

@333fred
Copy link
Member

333fred commented Aug 17, 2020

 using System;
 class TestClass
 {
     static void Test(string[] args)
     {
-        var value = $"{DateTime.Now.ToString()}Text{args[0]}";   //<- whitespace here
+        var value = $"{DateTime.Now.ToString()}Text{args[0]}";
         Console.WriteLine(value);
     }
 }

[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.

Copy link
Member

@CyrusNajmabadi CyrusNajmabadi left a comment

Choose a reason for hiding this comment

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

IDE side LGTM with the addition of a test. thanks!

void M(string S1 = $""Testing"")
const string S0 = $""Faaaaaaaaaaaaaaaaaaaaaaaaall"";

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.

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


var actual = tree.GetRoot().DescendantNodes().OfType<InterpolatedStringExpressionSyntax>().ToArray();
Assert.True(model.GetConstantValue(actual[0]).HasValue);
Assert.Equal("Hello, world!", model.GetConstantValue(actual[0]).Value);
Copy link
Member

Choose a reason for hiding this comment

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

I don't know if this was covered elsewhere or is planned for later, but more cases should be covered with GetConstantValue on constant interpolated strings.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sounds good.

Copy link
Member

@jcouv jcouv left a comment

Choose a reason for hiding this comment

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

LGTM Thanks (iteration 6)

@jcouv jcouv self-assigned this Aug 17, 2020
Copy link
Member

@jcouv jcouv left a comment

Choose a reason for hiding this comment

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

LGTM Thanks (iteration 7)

@kevinsun-dev kevinsun-dev merged commit b2e7a73 into dotnet:features/interpolated-string-constants Aug 20, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

4 participants