-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
265 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
namespace Avolantis.Text.QueryString; | ||
|
||
public partial class QueryStringWriter | ||
{ | ||
#region Without key | ||
|
||
/// <summary> | ||
/// Writes an integral value as a query parameter | ||
/// </summary> | ||
/// <param name="value">The value to write</param> | ||
public void WriteNumber(sbyte value) | ||
{ | ||
Add(null, value.ToString("F0")); | ||
} | ||
|
||
/// <inheritdoc cref="WriteNumber(sbyte)" /> | ||
public void WriteNumber(byte value) | ||
{ | ||
Add(null, value.ToString("F0")); | ||
} | ||
|
||
/// <inheritdoc cref="WriteNumber(sbyte)" /> | ||
public void WriteNumber(short value) | ||
{ | ||
Add(null, value.ToString("F0")); | ||
} | ||
|
||
/// <inheritdoc cref="WriteNumber(sbyte)" /> | ||
public void WriteNumber(ushort value) | ||
{ | ||
Add(null, value.ToString("F0")); | ||
} | ||
|
||
/// <inheritdoc cref="WriteNumber(sbyte)" /> | ||
public void WriteNumber(int value) | ||
{ | ||
Add(null, value.ToString("F0")); | ||
} | ||
|
||
/// <inheritdoc cref="WriteNumber(sbyte)" /> | ||
public void WriteNumber(uint value) | ||
{ | ||
Add(null, value.ToString("F0")); | ||
} | ||
|
||
/// <inheritdoc cref="WriteNumber(sbyte)" /> | ||
public void WriteNumber(long value) | ||
{ | ||
Add(null, value.ToString("F0")); | ||
} | ||
|
||
/// <inheritdoc cref="WriteNumber(sbyte)" /> | ||
public void WriteNumber(ulong value) | ||
{ | ||
Add(null, value.ToString("F0")); | ||
} | ||
|
||
/// <summary> | ||
/// Writes a floating-point value as a query parameter | ||
/// </summary> | ||
/// <param name="value">The value to write</param> | ||
/// <param name="precision">The number of fractional digits to write</param> | ||
public void WriteNumber(float value, int? precision = null) | ||
{ | ||
Add(null, value.ToString($"F{precision ?? _options.DefaultPrecision}")); | ||
} | ||
|
||
/// <inheritdoc cref="WriteNumber(float, int?)" /> | ||
public void WriteNumber(double value, int? precision = null) | ||
{ | ||
Add(null, value.ToString($"F{precision ?? _options.DefaultPrecision}")); | ||
} | ||
|
||
#endregion | ||
|
||
#region WithKey | ||
|
||
/// <summary> | ||
/// Writes an integral number query parameter | ||
/// </summary> | ||
/// <param name="key">The key of the parameter</param> | ||
/// <param name="value">The value to write</param> | ||
public void WriteNumber(string key, sbyte value) | ||
{ | ||
Add(key, value.ToString("F0")); | ||
} | ||
|
||
/// <inheritdoc cref="WriteNumber(string, sbyte)" /> | ||
public void WriteNumber(string key, byte value) | ||
{ | ||
Add(key, value.ToString("F0")); | ||
} | ||
|
||
/// <inheritdoc cref="WriteNumber(string, sbyte)" /> | ||
public void WriteNumber(string key, short value) | ||
{ | ||
Add(key, value.ToString("F0")); | ||
} | ||
|
||
/// <inheritdoc cref="WriteNumber(string, sbyte)" /> | ||
public void WriteNumber(string key, ushort value) | ||
{ | ||
Add(key, value.ToString("F0")); | ||
} | ||
|
||
/// <inheritdoc cref="WriteNumber(string, sbyte)" /> | ||
public void WriteNumber(string key, int value) | ||
{ | ||
Add(key, value.ToString("F0")); | ||
} | ||
|
||
/// <inheritdoc cref="WriteNumber(string, sbyte)" /> | ||
public void WriteNumber(string key, uint value) | ||
{ | ||
Add(key, value.ToString("F0")); | ||
} | ||
|
||
/// <inheritdoc cref="WriteNumber(string, sbyte)" /> | ||
public void WriteNumber(string key, long value) | ||
{ | ||
Add(key, value.ToString("F0")); | ||
} | ||
|
||
/// <inheritdoc cref="WriteNumber(string, sbyte)" /> | ||
public void WriteNumber(string key, ulong value) | ||
{ | ||
Add(key, value.ToString("F0")); | ||
} | ||
|
||
/// <summary> | ||
/// Writes a floating-point number query parameter | ||
/// </summary> | ||
/// <param name="key">The key of the parameter</param> | ||
/// <param name="value">The value to write</param> | ||
/// <param name="precision"></param> | ||
public void WriteNumber(string key, float value, int? precision = null) | ||
{ | ||
Add(key, value.ToString($"F{precision ?? _options.DefaultPrecision}")); | ||
} | ||
|
||
/// <inheritdoc cref="WriteNumber(string, float, int?)" /> | ||
public void WriteNumber(string key, double value, int? precision = null) | ||
{ | ||
Add(key, value.ToString($"F{precision ?? _options.DefaultPrecision}")); | ||
} | ||
|
||
#endregion | ||
} |
117 changes: 117 additions & 0 deletions
117
test/QueryString/Writer/QueryStringWriterNumericsTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
using Shouldly; | ||
using Xunit; | ||
|
||
namespace Avolantis.Text.QueryString; | ||
|
||
public class QueryStringWriterNumericsTests | ||
{ | ||
private const sbyte Sbyte = -10; | ||
private const byte Byte = 11; | ||
private const short Short = -12; | ||
private const ushort UShort = 13; | ||
private const int Int = -7; | ||
private const uint UInt = 69; | ||
private const long Long = -420; | ||
private const ulong ULong = 42; | ||
private const float Float = 12.34f; | ||
private const double Double = 3.14d; | ||
|
||
public QueryStringWriterNumericsTests() | ||
{ | ||
Target = new QueryParameterCollection(); | ||
Writer = new QueryStringWriter(Target, QueryStringSerializerOptions.Default); | ||
} | ||
|
||
private QueryParameterCollection Target { get; } | ||
private QueryStringWriter Writer { get; } | ||
|
||
[Fact] | ||
public void TestIntegralWithoutKey() | ||
{ | ||
Writer.WriteNumber(Sbyte); | ||
Writer.WriteNumber(Byte); | ||
Writer.WriteNumber(Short); | ||
Writer.WriteNumber(UShort); | ||
Writer.WriteNumber(Int); | ||
Writer.WriteNumber(UInt); | ||
Writer.WriteNumber(Long); | ||
Writer.WriteNumber(ULong); | ||
|
||
Target.Keys.ShouldContain(Sbyte.ToString("F0")); | ||
Target.Keys.ShouldContain(Byte.ToString("F0")); | ||
Target.Keys.ShouldContain(Short.ToString("F0")); | ||
Target.Keys.ShouldContain(UShort.ToString("F0")); | ||
Target.Keys.ShouldContain(Int.ToString("F0")); | ||
Target.Keys.ShouldContain(UInt.ToString("F0")); | ||
Target.Keys.ShouldContain(Long.ToString("F0")); | ||
Target.Keys.ShouldContain(ULong.ToString("F0")); | ||
} | ||
|
||
[Fact] | ||
public void TestIntegralWithKey() | ||
{ | ||
Writer.WriteNumber("key", Sbyte); | ||
Writer.WriteNumber("key", Byte); | ||
Writer.WriteNumber("key", Short); | ||
Writer.WriteNumber("key", UShort); | ||
Writer.WriteNumber("key", Int); | ||
Writer.WriteNumber("key", UInt); | ||
Writer.WriteNumber("key", Long); | ||
Writer.WriteNumber("key", ULong); | ||
|
||
Target.Keys.ShouldContain("key"); | ||
Target["key"].Count.ShouldBe(8); | ||
Target["key", 0].ShouldBe(Sbyte.ToString("F0")); | ||
Target["key", 1].ShouldBe(Byte.ToString("F0")); | ||
Target["key", 2].ShouldBe(Short.ToString("F0")); | ||
Target["key", 3].ShouldBe(UShort.ToString("F0")); | ||
Target["key", 4].ShouldBe(Int.ToString("F0")); | ||
Target["key", 5].ShouldBe(UInt.ToString("F0")); | ||
Target["key", 6].ShouldBe(Long.ToString("F0")); | ||
Target["key", 7].ShouldBe(ULong.ToString("F0")); | ||
} | ||
|
||
[Fact] | ||
public void TestFloatingPointWithoutKey() | ||
{ | ||
Writer.WriteNumber(Float); | ||
Writer.WriteNumber(Double); | ||
|
||
Target.Keys.ShouldContain(Float.ToString("F2")); | ||
Target.Keys.ShouldContain(Double.ToString("F2")); | ||
} | ||
|
||
[Fact] | ||
public void TestFloatingPointWithoutKeyWithCustomPrecision() | ||
{ | ||
Writer.WriteNumber(Float, 3); | ||
Writer.WriteNumber(Double, 4); | ||
|
||
Target.Keys.ShouldContain(Float.ToString("F3")); | ||
Target.Keys.ShouldContain(Double.ToString("F4")); | ||
} | ||
|
||
[Fact] | ||
public void TestFloatingPointWithKey() | ||
{ | ||
Writer.WriteNumber("key", Float); | ||
Writer.WriteNumber("key", Double); | ||
|
||
Target.Keys.ShouldContain("key"); | ||
Target["key"].Count.ShouldBe(2); | ||
Target["key", 0].ShouldBe(Float.ToString("F2")); | ||
Target["key", 1].ShouldBe(Double.ToString("F2")); | ||
} | ||
|
||
[Fact] | ||
public void TestFloatingPointWithKeyWithCustomPrecision() | ||
{ | ||
Writer.WriteNumber("key", Float, 5); | ||
Writer.WriteNumber("key", Double, 1); | ||
|
||
Target.Keys.ShouldContain("key"); | ||
Target["key"].Count.ShouldBe(2); | ||
Target["key", 0].ShouldBe(Float.ToString("F5")); | ||
Target["key", 1].ShouldBe(Double.ToString("F1")); | ||
} | ||
} |