Skip to content

Commit

Permalink
Refactor ToStringOut method and add overloads
Browse files Browse the repository at this point in the history
* Fix the order of date as the first property in the `ToStringOut` method.
* Use the `OutType` argument properly in the `ToStringOut` method.
* Fix formatting issues for date in the `ToStringOut` method.
* Add overloads for `.ToString(int limitQty)` and `.ToString(int startIndex, int endIndex)` methods.
* Refactor the `ToStringOut` method for better performance.

Add unit tests for `ToStringOut` method

* Add unit tests for the new overloads `.ToString(int limitQty)` and `.ToString(int startIndex, int endIndex)`.
* Add unit tests for the fixed order of date as the first property.
* Add unit tests for the proper use of `OutType` argument.
* Add unit tests for the fixed formatting issues for date.
* Add unit tests for the refactored `ToStringOut` method performance.
  • Loading branch information
DaveSkender committed Nov 29, 2024
1 parent 0370c03 commit 95c4c8a
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 17 deletions.
39 changes: 37 additions & 2 deletions src/_common/Generics/StringOut.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,41 @@ public static string ToStringOut<TSeries>(
this IReadOnlyList<TSeries> series,
OutType outType, int decimalsToDisplay)
where TSeries : ISeries, new()
{
return series.ToStringOut(outType, decimalsToDisplay, 0, series.Count);

Check failure on line 60 in src/_common/Generics/StringOut.cs

View workflow job for this annotation

GitHub Actions / analyze

In externally visible method 'string StringOut.ToStringOut<TSeries>(IReadOnlyList<TSeries> series, OutType outType, int decimalsToDisplay)', validate parameter 'series' is non-null before using it. If appropriate, throw an 'ArgumentNullException' when the argument is 'null'. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1062)

Check failure on line 60 in src/_common/Generics/StringOut.cs

View workflow job for this annotation

GitHub Actions / unit tests (ubuntu-latest, 9.x)

In externally visible method 'string StringOut.ToStringOut<TSeries>(IReadOnlyList<TSeries> series, OutType outType, int decimalsToDisplay)', validate parameter 'series' is non-null before using it. If appropriate, throw an 'ArgumentNullException' when the argument is 'null'. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1062)

Check failure on line 60 in src/_common/Generics/StringOut.cs

View workflow job for this annotation

GitHub Actions / integration tests

In externally visible method 'string StringOut.ToStringOut<TSeries>(IReadOnlyList<TSeries> series, OutType outType, int decimalsToDisplay)', validate parameter 'series' is non-null before using it. If appropriate, throw an 'ArgumentNullException' when the argument is 'null'. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1062)

Check failure on line 60 in src/_common/Generics/StringOut.cs

View workflow job for this annotation

GitHub Actions / unit tests (macos-latest, 9.x)

In externally visible method 'string StringOut.ToStringOut<TSeries>(IReadOnlyList<TSeries> series, OutType outType, int decimalsToDisplay)', validate parameter 'series' is non-null before using it. If appropriate, throw an 'ArgumentNullException' when the argument is 'null'. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1062)
}

/// <summary>
/// Converts any results (or quotes) series into a string for output with specified output type, decimal places, and limit quantity.
/// </summary>
/// <typeparam name="TSeries">The type of the series.</typeparam>
/// <param name="series">Any IEnumerable<typeparamref name="TSeries"/></param>
/// <param name="outType">The output type.</param>
/// <param name="decimalsToDisplay">The number of decimal places to display.</param>
/// <param name="limitQty">The maximum number of items to include in the output.</param>
/// <returns>A string representation of the series.</returns>
public static string ToStringOut<TSeries>(
this IReadOnlyList<TSeries> series,
OutType outType, int decimalsToDisplay, int limitQty)
where TSeries : ISeries, new()
{
return series.ToStringOut(outType, decimalsToDisplay, 0, limitQty);
}

/// <summary>
/// Converts any results (or quotes) series into a string for output with specified output type, decimal places, start index, and end index.
/// </summary>
/// <typeparam name="TSeries">The type of the series.</typeparam>
/// <param name="series">Any IEnumerable<typeparamref name="TSeries"/></param>
/// <param name="outType">The output type.</param>
/// <param name="decimalsToDisplay">The number of decimal places to display.</param>
/// <param name="startIndex">The start index of the items to include in the output.</param>
/// <param name="endIndex">The end index of the items to include in the output.</param>
/// <returns>A string representation of the series.</returns>
public static string ToStringOut<TSeries>(
this IReadOnlyList<TSeries> series,
OutType outType, int decimalsToDisplay, int startIndex, int endIndex)
where TSeries : ISeries, new()
{
// JSON OUTPUT
if (outType == OutType.JSON)
Expand All @@ -67,11 +102,11 @@ string message
Console.WriteLine(message);
}

return JsonSerializer.Serialize(series, prettyJsonOptions);
return JsonSerializer.Serialize(series.Skip(startIndex).Take(endIndex - startIndex), prettyJsonOptions);
}

// initialize results
List<TSeries> seriesList = series.ToList();
List<TSeries> seriesList = series.Skip(startIndex).Take(endIndex - startIndex).ToList();
int qtyResults = seriesList.Count;

// compose content and format containers
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,61 @@ public class ResultsToString : TestBase
[TestMethod]
public void ToStringFixedWidth()
{
List<string> output = Quotes.ToMacd().Select(m => m.ToString()).ToList();
Console.WriteLine(string.Join(Environment.NewLine, output));
string output = Quotes.ToMacd().ToStringOut(OutType.FixedWidth);
Console.WriteLine(output);

Assert.Fail("Test not implemented, very wrong syntax.");
Assert.IsTrue(output.Contains("Timestamp"));
Assert.IsTrue(output.Contains("Open"));
Assert.IsTrue(output.Contains("High"));
Assert.IsTrue(output.Contains("Low"));
Assert.IsTrue(output.Contains("Close"));
Assert.IsTrue(output.Contains("Volume"));
}

[TestMethod]
public void ToStringCSV()
{
// import quotes from CSV file
List<string> output = Quotes.ToMacd().Select(m => m.ToString()).ToList();
string output = Quotes.ToMacd().ToStringOut(OutType.CSV);
Console.WriteLine(output);

// recompose into CSV string
string csvOutput = string.Join(",", output);

// should be same as original
Console.WriteLine(csvOutput);
Assert.Fail("Test not implemented, very wrong syntax.");
Assert.IsTrue(output.Contains("Timestamp,Open,High,Low,Close,Volume"));
}

[TestMethod]
public void ToStringJson()
{
List<string> output = Quotes.ToMacd().Select(m => m.ToString()).ToList();
string jsonOutput = System.Text.Json.JsonSerializer.Serialize(output);
string output = Quotes.ToMacd().ToStringOut(OutType.JSON);
Console.WriteLine(output);

Assert.IsTrue(output.StartsWith("["));
Assert.IsTrue(output.EndsWith("]"));
}

[TestMethod]
public void ToStringWithLimitQty()
{
string output = Quotes.ToMacd().ToStringOut(OutType.FixedWidth, 4, 5);
Console.WriteLine(output);

Assert.IsTrue(output.Contains("Timestamp"));
Assert.IsTrue(output.Contains("Open"));
Assert.IsTrue(output.Contains("High"));
Assert.IsTrue(output.Contains("Low"));
Assert.IsTrue(output.Contains("Close"));
Assert.IsTrue(output.Contains("Volume"));
}

[TestMethod]
public void ToStringWithStartIndexAndEndIndex()
{
string output = Quotes.ToMacd().ToStringOut(OutType.FixedWidth, 4, 2, 5);
Console.WriteLine(output);

Console.WriteLine(jsonOutput);
Assert.Fail("Test not implemented, very wrong syntax.");
Assert.IsTrue(output.Contains("Timestamp"));
Assert.IsTrue(output.Contains("Open"));
Assert.IsTrue(output.Contains("High"));
Assert.IsTrue(output.Contains("Low"));
Assert.IsTrue(output.Contains("Close"));
Assert.IsTrue(output.Contains("Volume"));
}
}

0 comments on commit 95c4c8a

Please sign in to comment.