Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Merged
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
20 changes: 20 additions & 0 deletions 20 docs/converter.md
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,26 @@ Verify("the source text", "texttoconvert");
<sup><a href='/src/Verify.Tests/Converters/ExtensionConverterTests.cs#L30-L32' title='Snippet source file'>snippet source</a> | <a href='#snippet-TextExtensionConverterVerify' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

A target for a text extension can also be built from a stream. The stream is read as text, so a converter can re-emit its source without converting it back to a string:

<!-- snippet: RegisterStreamConverterTextExtensionStream -->
<a id='snippet-RegisterStreamConverterTextExtensionStream'></a>
```cs
// A target for a text extension can be built from a stream, eg to re-emit the
// source of the conversion. The stream is read as text.
VerifierSettings.RegisterStreamConverter(
"streamtoconvert",
(_, stream, _) =>
new(
null,
[
new("streamtoconvert", stream),
new("txt", "derived from text")
]));
```
<sup><a href='/src/Verify.Tests/Converters/ExtensionConverterTests.cs#L39-L53' title='Snippet source file'>snippet source</a> | <a href='#snippet-RegisterStreamConverterTextExtensionStream' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->


### Cleanup

Expand Down
4 changes: 4 additions & 0 deletions 4 docs/mdsource/converter.source.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ snippet: RegisterStreamConverterTextExtension

snippet: TextExtensionConverterVerify

A target for a text extension can also be built from a stream. The stream is read as text, so a converter can re-emit its source without converting it back to a string:

snippet: RegisterStreamConverterTextExtensionStream


### Cleanup

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
the source text
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
derived from text
27 changes: 27 additions & 0 deletions 27 src/Verify.Tests/Converters/ExtensionConverterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,33 @@ public Task TextSplitter() =>
Verify("the source text", "texttoconvert");
#endregion

[ModuleInitializer]
public static void TextSplitterStreamInit()
{
FileExtensions.AddTextExtension("streamtoconvert");

#region RegisterStreamConverterTextExtensionStream

// A target for a text extension can be built from a stream, eg to re-emit the
// source of the conversion. The stream is read as text.
VerifierSettings.RegisterStreamConverter(
"streamtoconvert",
(_, stream, _) =>
new(
null,
[
new("streamtoconvert", stream),
new("txt", "derived from text")
]));

#endregion
}

// a conversion splitter for a text extension that re-emits its source stream
[Fact]
public Task TextSplitterStream() =>
Verify("the source text", "streamtoconvert");

[ModuleInitializer]
public static void RecursiveInit() =>
VerifierSettings.RegisterStreamConverter(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
nuspec,
props,
staticComparerExt,
streamtoconvert,
texttoconvert,
txt,
xml
Expand Down
5 changes: 5 additions & 0 deletions 5 src/Verify/IoHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,11 @@ public static async Task<StringBuilder> ReadStringBuilderWithFixedLines(this Str
internal static async Task<StringBuilder> ReadStringBuilderWithFixedLines(TextReader reader)
{
var contents = await reader.ReadToEndAsync();
return contents.ToStringBuilderWithFixedLines();
}

internal static StringBuilder ToStringBuilderWithFixedLines(this string contents)
{
var builder = new StringBuilder(contents);
if (contents.Contains('\r'))
{
Expand Down
26 changes: 17 additions & 9 deletions 26 src/Verify/Target.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,23 +57,31 @@ public Target(string extension, Stream data, string? name = null, bool performCo
{
Guards.AgainstBadExtension(extension);

Extension = extension;
Name = FileNameCleaner.SanitizeFilePath(name);
PerformConversion = performConversion;

// text is always stored as text, so for a text extension the stream is read here.
// eg a converter registered against a text extension re-emitting its source stream.
if (FileExtensions.IsTextExtension(extension))
{
throw new(
$"""
Don't pass a stream for text.
If {extension} is not a text extension then use `FileExtensions.RemoveTextExtensions(\"{extension}\")` at initialization;
Otherwise use `Target(string extension, string data)` or `Target(string extension, StringBuilder data, string? name)`.
""");
streamData = null;
stringBuilderData = ReadText(data);
return;
}

Extension = extension;
Name = FileNameCleaner.SanitizeFilePath(name);
PerformConversion = performConversion;
streamData = data;
stringBuilderData = null;
}

// the stream is owned by the target, so it is consumed here
static StringBuilder ReadText(Stream stream)
{
stream.MoveToStart();
using var reader = new StreamReader(stream);
return reader.ReadToEnd().ToStringBuilderWithFixedLines();
}

public Target(string extension, StringBuilder data, string? name = null)
{
ValidateExtension(extension);
Expand Down
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.