kbilsted / StatePrinter

Automating unit testing and ToString() coding
Apache License 2.0
93 stars 32 forks source link

StatePrinter throws if GetEnumerator() throws (e.g. on default ImmutableArray) #62

Closed DanielSchuessler closed 3 years ago

DanielSchuessler commented 5 years ago

StatePrinter version: 3.0.1

class Foo
{
  public readonly ImmutableArray<int> arr = default;
}

[TestMethod]
public void Test()
{
  var printer = new Stateprinter(ConfigurationHelper.GetStandardConfiguration());

  Console.WriteLine(printer.PrintObject(new Foo()));
}

Throws:

System.InvalidOperationException: This operation cannot be performed on a default instance of ImmutableArray<T>.  Consider initializing the array, or checking the ImmutableArray<T>.IsDefault property.
   at System.Collections.Immutable.ImmutableArray`1.ThrowInvalidOperationIfNotInitialized()
   at System.Collections.Immutable.ImmutableArray`1.System.Collections.IEnumerable.GetEnumerator()
   at StatePrinting.Introspection.IntroSpector.IntrospectIEnumerable(Object source, Field field)
   at StatePrinting.Introspection.IntroSpector.Introspect(Object source, Field field)
   at StatePrinting.Introspection.IntroSpector.IntrospectComplexType(Object source, Field field, Type sourceType)
   at StatePrinting.Introspection.IntroSpector.Introspect(Object source, Field field)
   at StatePrinting.Introspection.IntroSpector.PrintObject(Object objectToPrint, String rootname)
   at StatePrinting.Stateprinter.PrintObject(Object objectToPrint, String rootname)

Possible fixes (not mutually exclusive):

kbilsted commented 5 years ago

Many thanks for reporting

kbilsted commented 5 years ago

have you given any thoughts on what to present in the output? perhaps a general exception filter can be provided so you have a fighting chance of either failing (as the current behavior) or implement your own kind of output given knowledge of the field type.

kbilsted commented 3 years ago

This has been fixed in ReassureTest - the next generation StatePrinter https://github.com/kbilsted/ReassureTest.Net/commit/22322d72581b810410c8ea2eb6ffeede9380423b