Last active
June 12, 2018 18:43
-
-
Save Brar/260a670863121bb0da616976d45681c4 to your computer and use it in GitHub Desktop.
Better array formatting for NUnit
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
using System; | |
using System.Text; | |
namespace FormatArray | |
{ | |
class Program | |
{ | |
private const string THREE_DOTS = "..."; | |
private const int StringLen30 = 30; | |
private const int StringLenUnlimited = 0; | |
static void Main(string[] args) | |
{ | |
int[] test = new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; | |
int[,] testMulti2 = new[,] { { 1, 2, 3, 4, 5 }, { 6, 7, 8, 9, 10 } }; | |
int[,,] testMulti3 = new[,,] { { { 1, 2, 3 }, { 4, 5, 6 } }, { { 7, 8, 9 }, { 10, 11, 12 } } }; | |
int[][] testJagged2 = { new[] { 1, 2, 3, 4, 5 }, new[] { 6, 7, 8, 9, 10 } }; | |
int[][][] testJagged3 = { new[] { new[] { 1, 2, 3 }, new[] { 4, 5, 6 } }, new[] { new[] { 7, 8, 9 }, new[] { 10, 11, 12 } } }; | |
int[][,] testJaggedOfMulti = { new[,] { { 1, 2, 3 }, { 4, 5, 6 } }, new[,] { { 7, 8, 9 }, { 10, 11, 12 } } }; | |
int[,][] testMultiOfJagged = new[,] { { new[] { 1, 2, 3 }, new[] { 4, 5, 6 } }, { new[] { 7, 8, 9 }, new[] { 10, 11, 12 } } }; | |
int[,][,] myBrainHurts = new[,] { { new[,] { { 1, 2, 3 }, { 4, 5, 6 } } }, { new[,] { { 7, 8, 9 }, { 10, 11, 12 } } } }; | |
Console.WriteLine(GetDisplayString(test, StringLen30)); | |
Console.WriteLine(GetDisplayString(test, StringLenUnlimited)); | |
Console.WriteLine(GetDisplayString(testMulti2, StringLen30)); | |
Console.WriteLine(GetDisplayString(testMulti2, StringLenUnlimited)); | |
Console.WriteLine(GetDisplayString(testMulti3, StringLen30)); | |
Console.WriteLine(GetDisplayString(testMulti3, StringLenUnlimited)); | |
Console.WriteLine(GetDisplayString(testJagged2, StringLen30)); | |
Console.WriteLine(GetDisplayString(testJagged2, StringLenUnlimited)); | |
Console.WriteLine(GetDisplayString(testJagged3, StringLen30)); | |
Console.WriteLine(GetDisplayString(testJagged3, StringLenUnlimited)); | |
Console.WriteLine(GetDisplayString(testJaggedOfMulti, StringLen30)); | |
Console.WriteLine(GetDisplayString(testJaggedOfMulti, StringLenUnlimited)); | |
Console.WriteLine(GetDisplayString(testMultiOfJagged, StringLen30)); | |
Console.WriteLine(GetDisplayString(testMultiOfJagged, StringLenUnlimited)); | |
Console.WriteLine(GetDisplayString(myBrainHurts, StringLen30)); | |
Console.WriteLine(GetDisplayString(myBrainHurts, StringLenUnlimited)); | |
// Results: | |
// A[1,2,3,4,5,6,7,8,9,10] | |
// A[1,2,3,4,5,6,7,8,9,10] | |
// A[[1,2,3,4,5],[6,7,8,9,10]] | |
// A[[1,2,3,4,5],[6,7,8,9,10]] | |
// A[[[1,2,3],[4,5,6]],[[7,8,9... | |
// A[[[1,2,3],[4,5,6]],[[7,8,9],[10,11,12]]] | |
// A[A[1,2,3,4,5],A[6,7,8,9,10]] | |
// A[A[1,2,3,4,5],A[6,7,8,9,10]] | |
// A[A[A[1,2,3],A[4,5,6]],A[A[... | |
// A[A[A[1,2,3],A[4,5,6]],A[A[7,8,9],A[10,11,12]]] | |
// A[A[[1,2,3],[4,5,6]],A[[7,8... | |
// A[A[[1,2,3],[4,5,6]],A[[7,8,9],[10,11,12]]] | |
// A[[A[1,2,3],A[4,5,6]],[A[7,... | |
// A[[A[1,2,3],A[4,5,6]],[A[7,8,9],A[10,11,12]]] | |
// A[[A[[1,2,3],[4,5,6]]],[A[[... | |
// A[[A[[1,2,3],[4,5,6]]],[A[[7,8,9],[10,11,12]]]] | |
} | |
protected static string GetDisplayString(object arg, int stringMax) | |
{ | |
string display = arg == null | |
? "null" | |
: Convert.ToString(arg, System.Globalization.CultureInfo.InvariantCulture); | |
if (arg is Array argArray) | |
{ | |
var builder = new StringBuilder("A"); | |
var indices = new int[argArray.Rank]; | |
var currentDimIndex = 0; | |
AppendValues(); | |
if (stringMax > 0 && builder.Length - 1 > stringMax) | |
{ | |
builder.Length = stringMax - THREE_DOTS.Length; | |
builder.Append(THREE_DOTS); | |
} | |
else | |
{ | |
builder.Length = builder.Length - 1; | |
} | |
display = builder.ToString(); | |
void AppendValues() | |
{ | |
builder.Append("["); | |
for (var i = argArray.GetLowerBound(currentDimIndex); i <= argArray.GetUpperBound(currentDimIndex); i++) | |
{ | |
indices[currentDimIndex] = i; | |
if (currentDimIndex < argArray.Rank - 1) | |
{ | |
currentDimIndex++; | |
AppendValues(); | |
} | |
else | |
{ | |
builder.Append(GetDisplayString(argArray.GetValue(indices), stringMax)); | |
builder.Append(','); | |
} | |
} | |
currentDimIndex--; | |
builder.Length = builder.Length - 1; | |
builder.Append("],"); | |
} | |
} | |
else if (arg is int) | |
{ | |
if (arg.Equals(int.MaxValue)) | |
display = "int.MaxValue"; | |
else if (arg.Equals(int.MinValue)) | |
display = "int.MinValue"; | |
} | |
// Other cases removed for brevity | |
return display; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment