Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
23 changes: 21 additions & 2 deletions src/VarDump/CSharpDumper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,18 @@ private void ValidateOptions(DumpOptions options)
{
throw new FormatException($"Bad format specifier. {options.IntegralNumericFormat}");
}
if (options.NewLine == null)
{
throw new ArgumentNullException(nameof(options.NewLine));
}
}

public string Dump(object obj)
{
using var writer = new StringWriter();
using var writer = new StringWriter
{
NewLine = _options.NewLine
};

DumpImpl(obj, writer);

Expand All @@ -54,7 +61,19 @@ public void Dump(object obj, TextWriter textWriter)
throw new ArgumentNullException(nameof(textWriter));
}

DumpImpl(obj, textWriter);
var originalNewLine = textWriter.NewLine;
try
{
if (_options.IsNewLineSpecified)
{
textWriter.NewLine = _options.NewLine;
}
DumpImpl(obj, textWriter);
}
finally
{
textWriter.NewLine = originalNewLine;
}
}

private void DumpImpl(object obj, TextWriter textWriter)
Expand Down
28 changes: 26 additions & 2 deletions src/VarDump/Visitor/DumpOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,23 @@ public class DumpOptions
/// </summary>
public int MaxDepth { get; set; } = 25;

private string _newLine = Environment.NewLine;

/// <summary>
/// The newline string to use when dumping, default is <see cref="Environment.NewLine"/>.
/// </summary>
public string NewLine
{
get => _newLine;
set
{
_newLine = value;
IsNewLineSpecified = true;
}
}

internal bool IsNewLineSpecified { get; private set; }

/// <summary>
/// The layout to use for primitive collections, default is <see cref="CollectionLayout.MultiLine"/>.
/// </summary>
Expand Down Expand Up @@ -150,7 +167,7 @@ public bool WritablePropertiesOnly

public DumpOptions Clone()
{
return new DumpOptions
var clone = new DumpOptions
{
ConfigureKnownObjects = ConfigureKnownObjects,
DateKind = DateKind,
Expand All @@ -174,5 +191,12 @@ public DumpOptions Clone()
UsePredefinedMethods = UsePredefinedMethods,
TypeNamePolicy = TypeNamePolicy
};

if (IsNewLineSpecified)
{
clone.NewLine = NewLine;
}

return clone;
}
}
}
23 changes: 21 additions & 2 deletions src/VarDump/VisualBasicDumper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,18 @@ private void ValidateOptions(DumpOptions options)
{
throw new FormatException($"Bad format specifier. {options.IntegralNumericFormat}");
}
if (options.NewLine == null)
{
throw new ArgumentNullException(nameof(options.NewLine));
}
}

public string Dump(object obj)
{
using var writer = new StringWriter();
using var writer = new StringWriter
{
NewLine = _options.NewLine
};

DumpImpl(obj, writer);

Expand All @@ -54,7 +61,19 @@ public void Dump(object obj, TextWriter textWriter)
throw new ArgumentNullException(nameof(textWriter));
}

DumpImpl(obj, textWriter);
var originalNewLine = textWriter.NewLine;
try
{
if (_options.IsNewLineSpecified)
{
textWriter.NewLine = _options.NewLine;
}
DumpImpl(obj, textWriter);
}
finally
{
textWriter.NewLine = originalNewLine;
}
}

private void DumpImpl(object obj, TextWriter textWriter)
Expand Down
6 changes: 3 additions & 3 deletions test/VarDump.Extensions.UnitTests/VarDumpExtensionsSpec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public void DumpAnonymousTypeCSharp()
}
};

""", result);
""", result, ignoreLineEndingDifferences: true);
}

[Fact]
Expand Down Expand Up @@ -86,7 +86,7 @@ public void DumpAnonymousTypeCustomOptionsCSharp()
}
};

""", result);
""", result, ignoreLineEndingDifferences: true);
}

[Fact]
Expand Down Expand Up @@ -117,6 +117,6 @@ New With {
}
}

""", result);
""", result, ignoreLineEndingDifferences: true);
}
}
4 changes: 2 additions & 2 deletions test/VarDump.UnitTests/AnonymousTypeSpec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public void DumpAnonymousTypeCSharp()
}
};

""", result);
""", result, ignoreLineEndingDifferences: true);
}

[Fact]
Expand Down Expand Up @@ -69,7 +69,7 @@ New With {
}
}

""", result);
""", result, ignoreLineEndingDifferences: true);
}

[Fact]
Expand Down
34 changes: 17 additions & 17 deletions test/VarDump.UnitTests/ArraySpec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public void DumpArrayOfArraysCSharp()
}
};

""", result);
""", result, ignoreLineEndingDifferences: true);
}

[Fact]
Expand All @@ -48,7 +48,7 @@ public void DumpArrayOfArraysCSharpSingleLine()
new int[]{ 1, 2, 3 }
};

""", result);
""", result, ignoreLineEndingDifferences: true);
}

[Fact]
Expand All @@ -75,7 +75,7 @@ public void Dump2DimensionalArrayCSharpSingleLine()
{ 4, 5, 6 }
};

""", result);
""", result, ignoreLineEndingDifferences: true);
}

[Fact]
Expand Down Expand Up @@ -114,7 +114,7 @@ public void DumpMultidimensionalArrayCSharpSingleLine()
}
};

""", result);
""", result, ignoreLineEndingDifferences: true);
}

[Fact]
Expand All @@ -130,7 +130,7 @@ public void DumpEmptyArrayCSharp()
"""
var arrayOfInt = new int[0, 0];

""", result);
""", result, ignoreLineEndingDifferences: true);
}

[Fact]
Expand All @@ -152,7 +152,7 @@ public void DumpImmutableArrayOfArraysCSharp()
}
}.ToImmutableArray();

""", result);
""", result, ignoreLineEndingDifferences: true);
}

[Fact]
Expand All @@ -179,7 +179,7 @@ public void Dump2DimensionalArrayCSharp()
}
};

""", result);
""", result, ignoreLineEndingDifferences: true);
}

[Fact]
Expand Down Expand Up @@ -208,7 +208,7 @@ public void Dump2DimensionalAnonymousArrayCSharp()
}
};

""", result);
""", result, ignoreLineEndingDifferences: true);
}

[Fact]
Expand All @@ -233,7 +233,7 @@ public void DumpArrayOfArraysAnonymousCSharp()
}
};

""", result);
""", result, ignoreLineEndingDifferences: true);
}

[Fact]
Expand All @@ -253,7 +253,7 @@ New Integer(){
}
}

""", result);
""", result, ignoreLineEndingDifferences: true);
}

[Fact]
Expand All @@ -274,7 +274,7 @@ public void DumpArrayOfArraysVbSingleLine()
New Integer(){ 1, 2, 3 }
}

""", result);
""", result, ignoreLineEndingDifferences: true);
}

[Fact]
Expand All @@ -300,7 +300,7 @@ public void Dump2DimensionalArrayVbSingleLine()
{ 4, 5, 6 }
}

""", result);
""", result, ignoreLineEndingDifferences: true);
}

[Fact]
Expand Down Expand Up @@ -338,7 +338,7 @@ public void DumpMultidimensionalArrayVisualBasicSingleLine()
}
}

""", result);
""", result, ignoreLineEndingDifferences: true);
}

[Fact]
Expand All @@ -354,7 +354,7 @@ public void DumpEmptyArrayVb()
"""
Dim arrayOfInteger = New Integer(0, 0) {}

""", result);
""", result, ignoreLineEndingDifferences: true);
}

[Fact]
Expand All @@ -380,7 +380,7 @@ public void Dump2DimensionalArrayVb()
}
}

""", result);
""", result, ignoreLineEndingDifferences: true);
}

[Fact]
Expand All @@ -406,7 +406,7 @@ New With {
}
}

""", result);
""", result, ignoreLineEndingDifferences: true);
}

[Fact]
Expand All @@ -428,6 +428,6 @@ New With {
}
}

""", result);
""", result, ignoreLineEndingDifferences: true);
}
}
14 changes: 7 additions & 7 deletions test/VarDump.UnitTests/CollectionSpec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public void DumpReadOnlyCollectionCSharp()
1
}.AsReadOnly();

""", result);
""", result, ignoreLineEndingDifferences: true);
}

[Fact]
Expand All @@ -38,7 +38,7 @@ public void DumpReadOnlyCollectionSingleLineCSharp()
Assert.Equal("""
var readOnlyCollectionOfInt = new List<int> { 1 }.AsReadOnly();

""", result);
""", result, ignoreLineEndingDifferences: true);
}

[Fact]
Expand All @@ -58,7 +58,7 @@ public void DumpEnumerableRangeCSharp()
2
};

""", result);
""", result, ignoreLineEndingDifferences: true);
}

[Fact]
Expand All @@ -83,7 +83,7 @@ public void DumpListOfListsCSharpSingleLine()
new List<int> { 1, 2, 3 }
};

""", result);
""", result, ignoreLineEndingDifferences: true);
}

[Fact]
Expand All @@ -101,7 +101,7 @@ public void DumpReadOnlyCollectionVisualBasic()
1
}.AsReadOnly()

""", result);
""", result, ignoreLineEndingDifferences: true);
}

[Fact]
Expand All @@ -116,7 +116,7 @@ public void DumpReadOnlyCollectionSingleLineVisualBasic()
Assert.Equal("""
Dim readOnlyCollectionOfInteger = New List(Of Integer) From { 1 }.AsReadOnly()

""", result);
""", result, ignoreLineEndingDifferences: true);
}

[Fact]
Expand All @@ -140,6 +140,6 @@ public void DumpListOfListsVisualBasicSingleLine()
New List(Of Integer) From { 1, 2, 3 }
}

""", result);
""", result, ignoreLineEndingDifferences: true);
}
}
Loading