Skip to content

Commit

Permalink
Add AsyncContinueOnCapturedContext to SepSpec (#240)
Browse files Browse the repository at this point in the history
  • Loading branch information
nietras authored Jan 26, 2025
1 parent 1b2277b commit 151525c
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 10 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2187,7 +2187,8 @@ namespace nietras.SeparatedValues
public readonly struct SepSpec : System.IEquatable<nietras.SeparatedValues.SepSpec>
{
public SepSpec() { }
public SepSpec(nietras.SeparatedValues.Sep sep, System.Globalization.CultureInfo? cultureInfo) { }
public SepSpec(nietras.SeparatedValues.Sep sep, System.Globalization.CultureInfo? cultureInfo, bool asyncContinueOnCapturedContext) { }
public bool AsyncContinueOnCapturedContext { get; init; }
public System.Globalization.CultureInfo? CultureInfo { get; init; }
public nietras.SeparatedValues.Sep Sep { get; init; }
}
Expand Down
4 changes: 2 additions & 2 deletions src/Sep.Test/SepReaderColTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -295,11 +295,11 @@ static void Run(SepReader.ColAction action, string colValue = ColText, Func<SepR
() => Sep.Reader(useConfigure).FromText(text),
() => Sep.Default.Reader(useConfigure).FromText(text),
() => ((Sep?)null).Reader(useConfigure).FromText(text),
() => new SepSpec(Sep.Default, null).Reader(useConfigure).FromText(text),
() => new SepSpec(Sep.Default, null, false).Reader(useConfigure).FromText(text),
};
if (configure is null)
{
createReaders.Add(() => new SepSpec(Sep.Default, null).Reader().FromText(text));
createReaders.Add(() => new SepSpec(Sep.Default, null, false).Reader().FromText(text));
}

foreach (var createReader in createReaders)
Expand Down
19 changes: 18 additions & 1 deletion src/Sep.Test/SepSpecTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,31 @@ public void SepSpecTest_Defaults()

Assert.AreEqual(Sep.Default, sut.Sep);
Assert.AreSame(SepDefaults.CultureInfo, sut.CultureInfo);
Assert.IsFalse(sut.AsyncContinueOnCapturedContext);
}

[TestMethod]
public void SepSpecTest_Ctor_2()
{
var sut = new SepSpec(new(';'), CultureInfo.CurrentCulture);

Assert.AreEqual(new Sep(';'), sut.Sep);
Assert.AreSame(CultureInfo.CurrentCulture, sut.CultureInfo);
Assert.IsFalse(sut.AsyncContinueOnCapturedContext);
}

[TestMethod]
public void SepSpecTest_With()
{
var sut = new SepSpec() with { Sep = new(';'), CultureInfo = CultureInfo.CurrentCulture };
var sut = new SepSpec() with
{
Sep = new(';'),
CultureInfo = CultureInfo.CurrentCulture,
AsyncContinueOnCapturedContext = true,
};

Assert.AreEqual(new Sep(';'), sut.Sep);
Assert.AreSame(CultureInfo.CurrentCulture, sut.CultureInfo);
Assert.IsTrue(sut.AsyncContinueOnCapturedContext);
}
}
2 changes: 1 addition & 1 deletion src/Sep.Test/SepWriterColTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ static async ValueTask Run(SepWriter.ColAction action, string? expectedColValue
[
() => Sep.Writer(o => o with { CultureInfo = cultureInfo ?? SepDefaults.CultureInfo }).ToText(),
() => Sep.Default.Writer(o => o with { CultureInfo = cultureInfo ?? SepDefaults.CultureInfo }).ToText(),
() => new SepSpec(Sep.Default, cultureInfo ?? SepDefaults.CultureInfo).Writer(o => o with { }).ToText(),
() => new SepSpec(Sep.Default, cultureInfo ?? SepDefaults.CultureInfo, false).Writer(o => o with { }).ToText(),
];
foreach (var createWriter in createWriters)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Sep/SepReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ internal SepReader(Info info, in SepReaderOptions options, TextReader reader)
}

public bool IsEmpty { get; private set; }
public SepSpec Spec => new(new(_separator), _cultureInfo);
public SepSpec Spec => new(new(_separator), _cultureInfo, _continueOnCapturedContext);
public bool HasHeader { get => _hasHeader; private set => _hasHeader = value; }
public bool HasRows { get; private set; }
public SepReaderHeader Header => _header;
Expand Down
6 changes: 5 additions & 1 deletion src/Sep/SepReaderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ public static partial class SepReaderExtensions
public static SepReaderOptions Reader(this Sep sep) => new(sep);
public static SepReaderOptions Reader(this Sep? sep) => new(sep);
public static SepReaderOptions Reader(this SepSpec spec) =>
new SepReaderOptions(spec.Sep) with { CultureInfo = spec.CultureInfo };
new SepReaderOptions(spec.Sep) with
{
CultureInfo = spec.CultureInfo,
AsyncContinueOnCapturedContext = spec.AsyncContinueOnCapturedContext
};

public static SepReaderOptions Reader(this Sep sep, Func<SepReaderOptions, SepReaderOptions> configure)
{
Expand Down
7 changes: 6 additions & 1 deletion src/Sep/SepSpec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,19 @@ namespace nietras.SeparatedValues;

public readonly record struct SepSpec
{
public SepSpec() : this(Sep.Default, SepDefaults.CultureInfo) { }
public SepSpec() : this(Sep.Default, SepDefaults.CultureInfo, false) { }

public SepSpec(Sep sep, CultureInfo? cultureInfo)
: this(sep, cultureInfo, false) { }

public SepSpec(Sep sep, CultureInfo? cultureInfo, bool asyncContinueOnCapturedContext)
{
Sep = sep;
CultureInfo = cultureInfo;
AsyncContinueOnCapturedContext = asyncContinueOnCapturedContext;
}

public Sep Sep { get; init; }
public CultureInfo? CultureInfo { get; init; }
public bool AsyncContinueOnCapturedContext { get; init; } = false;
}
2 changes: 1 addition & 1 deletion src/Sep/SepWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ internal SepWriter(SepWriterOptions options, TextWriter writer, ISepTextWriterDi
Header = new(this);
}

public SepSpec Spec => new(_sep, _cultureInfo);
public SepSpec Spec => new(_sep, _cultureInfo, _continueOnCapturedContext);
public SepWriterHeader Header { get; }

public Row NewRow()
Expand Down
6 changes: 5 additions & 1 deletion src/Sep/SepWriterExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ public static partial class SepWriterExtensions

public static SepWriterOptions Writer(this Sep sep) => new(sep);
public static SepWriterOptions Writer(this SepSpec spec) =>
new SepWriterOptions(spec.Sep) with { CultureInfo = spec.CultureInfo };
new SepWriterOptions(spec.Sep) with
{
CultureInfo = spec.CultureInfo,
AsyncContinueOnCapturedContext = spec.AsyncContinueOnCapturedContext
};

public static SepWriterOptions Writer(this Sep sep, Func<SepWriterOptions, SepWriterOptions> configure)
{
Expand Down

0 comments on commit 151525c

Please sign in to comment.