diff --git a/Src/CSharpier.Tests/CommandLineFormatterTests.cs b/Src/CSharpier.Tests/CommandLineFormatterTests.cs index cae6fa072..124436ca7 100644 --- a/Src/CSharpier.Tests/CommandLineFormatterTests.cs +++ b/Src/CSharpier.Tests/CommandLineFormatterTests.cs @@ -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] @@ -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] @@ -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 lines) Format( @@ -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)); } @@ -206,7 +207,7 @@ public void WriteLine(string line = null) if (line != null) { - this.Lines.Add(line.Replace("\\", "/")); + this.Lines.Add(line); } } } diff --git a/Src/CSharpier.Tests/LineEndingTests.cs b/Src/CSharpier.Tests/LineEndingTests.cs index 4856b4a53..fe1a49a66 100644 --- a/Src/CSharpier.Tests/LineEndingTests.cs +++ b/Src/CSharpier.Tests/LineEndingTests.cs @@ -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 = @@ -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( diff --git a/Src/CSharpier/CommandLineFormatter.cs b/Src/CSharpier/CommandLineFormatter.cs index 15e89fa4d..49fb87f5d 100644 --- a/Src/CSharpier/CommandLineFormatter.cs +++ b/Src/CSharpier/CommandLineFormatter.cs @@ -1,5 +1,4 @@ using System; -using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.IO.Abstractions; @@ -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) diff --git a/Src/CSharpier/SyntaxPrinter/Token.cs b/Src/CSharpier/SyntaxPrinter/Token.cs index 7afc416ba..64d9bb69f 100644 --- a/Src/CSharpier/SyntaxPrinter/Token.cs +++ b/Src/CSharpier/SyntaxPrinter/Token.cs @@ -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 { @@ -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))));