Skip to content

Commit

Permalink
fixing issue with interpolated verbatim string literal
Browse files Browse the repository at this point in the history
also fixing some issues with unit tests
closes #221
  • Loading branch information
belav committed May 19, 2021
1 parent 1383497 commit f518fd8
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 15 deletions.
19 changes: 10 additions & 9 deletions Src/CSharpier.Tests/CommandLineFormatterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public void Format_Writes_Failed_To_Compile()

var result = this.Format();

result.lines.First().Should().Be(@"/Invalid.cs - failed to compile");
result.lines.First().Should().Contain(@"Invalid.cs - failed to compile");
}

[Test]
Expand Down Expand Up @@ -66,7 +66,7 @@ public void Format_Checks_Unformatted_File()

exitCode.Should().Be(1);
this.GetFileContent(unformattedFilePath).Should().Be(UnformattedClass);
lines.First().Should().Contain(@"/Unformatted.cs - was not formatted");
lines.First().Should().Contain(@"Unformatted.cs - was not formatted");
}

[Test]
Expand Down Expand Up @@ -140,13 +140,14 @@ public void Ignore_Reports_Errors()

var (exitCode, lines) = this.Format();

var path = this.fileSystem.Path.Combine(GetRootPath(), ".csharpierignore");

exitCode.Should().Be(1);
lines.Should()
.Contain(
$"The .csharpierignore file at {GetRootPath()}/.csharpierignore could not be parsed due to the following line:"
$"The .csharpierignore file at {path} could not be parsed due to the following line:"
);
// our testing code replaces the \ with /
lines.Should().Contain("/Src/Uploads/*.cs");
lines.Should().Contain(@"\Src\Uploads\*.cs");
}

private (int exitCode, IList<string> lines) Format(
Expand Down Expand Up @@ -178,18 +179,18 @@ public void Ignore_Reports_Errors()

private string GetRootPath()
{
return OperatingSystem.IsWindows() ? "c:/test" : "/Test";
return OperatingSystem.IsWindows() ? @"c:\test" : "/Test";
}

private string GetFileContent(string path)
{
path = Path.Combine(GetRootPath(), path);
path = this.fileSystem.Path.Combine(GetRootPath(), path);
return this.fileSystem.File.ReadAllText(path);
}

private void WhenThereExists(string path, string contents)
{
path = Path.Combine(GetRootPath(), path);
path = this.fileSystem.Path.Combine(GetRootPath(), path);
this.fileSystem.AddFile(path, new MockFileData(contents));
}

Expand All @@ -206,7 +207,7 @@ public void WriteLine(string line = null)

if (line != null)
{
this.Lines.Add(line.Replace("\\", "/"));
this.Lines.Add(line);
}
}
}
Expand Down
26 changes: 25 additions & 1 deletion Src/CSharpier.Tests/LineEndingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace CSharpier.Tests
public class LineEndingTests
{
[Test]
public void LineEndings_Should_Not_Affect_Printed_Output()
public void LineEndings_Should_Not_Affect_Printed_Output_With_Verbatim_String()
{
// this is a verbatim string that is just the right size to format differently if it has \n vs \r\n in it
var code =
Expand All @@ -30,6 +30,30 @@ public void LineEndings_Should_Not_Affect_Printed_Output()
lfResult.Code.Should().Be(crLfResult.Code.Replace("\r\n", "\n"));
}

[Test]
public void LineEndings_Should_Not_Affect_Printed_Output_With_Interpolated_String()
{
// this is a verbatim string that is just the right size to format differently if it has \n vs \r\n in it
var code =
@"class ClassName
{
private string blah = @$""one1111111111111111111111111111111
two
three
four"";
}
";
var codeWithLf = code.Replace("\r\n", "\n");
var codeWithCrLf = codeWithLf.Replace("\n", "\r\n");

var formatter = new CodeFormatter();
var printerOptions = new PrinterOptions { EndOfLine = EndOfLine.Auto, Width = 80 };
var lfResult = formatter.Format(codeWithLf, printerOptions);
var crLfResult = formatter.Format(codeWithCrLf, printerOptions);

lfResult.Code.Should().Be(crLfResult.Code.Replace("\r\n", "\n"));
}

[TestCase("\r\n", EndOfLine.LF)]
[TestCase("\n", EndOfLine.CRLF)]
public void LineEndings_In_Verbatim_String_Should_Respect_Options(
Expand Down
5 changes: 2 additions & 3 deletions Src/CSharpier/CommandLineFormatter.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.IO.Abstractions;
Expand Down Expand Up @@ -50,8 +49,8 @@ IgnoreFile ignoreFile
IConsole console,
CancellationToken cancellationToken
) {
var baseDirectoryPath = File.Exists(commandLineOptions.DirectoryOrFile)
? Path.GetDirectoryName(commandLineOptions.DirectoryOrFile)
var baseDirectoryPath = fileSystem.File.Exists(commandLineOptions.DirectoryOrFile)
? fileSystem.Path.GetDirectoryName(commandLineOptions.DirectoryOrFile)
: commandLineOptions.DirectoryOrFile;

if (baseDirectoryPath == null)
Expand Down
11 changes: 9 additions & 2 deletions Src/CSharpier/SyntaxPrinter/Token.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
using System.Collections.Generic;
using System.Linq;
using CSharpier.DocTypes;
using CSharpier.SyntaxPrinter.SyntaxNodePrinters;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;

namespace CSharpier.SyntaxPrinter
{
Expand Down Expand Up @@ -58,8 +60,13 @@ public static Doc PrintWithSuffix(SyntaxToken syntaxToken, Doc suffixDoc)
ShouldSkipNextLeadingTrivia = false;

if (
syntaxToken.Kind() == SyntaxKind.StringLiteralToken
&& syntaxToken.Text.StartsWith("@")
(syntaxToken.Kind() == SyntaxKind.StringLiteralToken
&& syntaxToken.Text.StartsWith("@"))
|| (syntaxToken.Kind() == SyntaxKind.InterpolatedStringTextToken
&& syntaxToken.Parent!.Parent
is InterpolatedStringExpressionSyntax interpolatedStringExpressionSyntax
&& interpolatedStringExpressionSyntax.StringStartToken.Kind()
== SyntaxKind.InterpolatedVerbatimStringStartToken)
) {
var lines = syntaxToken.Text.Replace("\r", string.Empty).Split(new[] { '\n' });
docs.Add(Doc.Join(Doc.LiteralLine, lines.Select(o => new StringDoc(o))));
Expand Down

0 comments on commit f518fd8

Please sign in to comment.