question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add ability to dynamically limit graph depth in `Formatter`

See original GitHub issue

Add ability to limit graph depth in Formatter

Currently it is hardcoded to 5, which makes error messages for assertions on collections containing complex object graphs unreadable.

 private static string FormatChild(string path, object childValue, bool useLineBreaks, ObjectGraph graph) {
    /// ...
                else if (graph.Depth > 5)
                {
                    return "{Maximum recursion depth was reached…}";
                }
}

Would be nice to have an option to pass depth to collection assertion e.g.

result.Should().Contain(p => p.Foo == "Bar", maxDepth: 1);

Current behavior

For list of 2 complex objects, I’m getting message Expected result { ... containing almost 200 lines of text

Versions

  • FluentAssertions: 5.10.2
  • .NET Core 2.2.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:9 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
pgolinskicommented, May 19, 2020

Yes. It would be easy to configure for simple cases.

I must confess I used your workaround to cut off ILog from log4net 😉. Thank you.

private class LogFormatter : IValueFormatter
{
	public bool CanHandle(object value) => value is ILog;

	public string Format(object value, FormattingContext context, FormatChild formatChild)
		=> value.ToString();
}
1reaction
jnyrupcommented, Jul 22, 2020

How about adding a ConcurrentDictionary<Type, int> to Formatter to allow specifying the maxDepth per type?

private static ConcurrentDictionary<Type, int> MaxDepths { get; }

public static int MaxDepth { get; set; } = 5;

public static void SetMaxDepth<T>(int maxDepth) =>
    MaxDepths[typeof(T)] = maxDepth;

private static int GetMaxDepth(object obj) => 
    MaxDepths.TryGetValue(obj.GetType(), out int maxDepth)
    ? maxDepth
    : MaxDepth;

...
else if (graph.Depth > GetMaxDepth(childValue))
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Create Conditional and Dynamic Queries in ...
Consider adding a LIMIT clause to dynamic queries generated using the technique described in this post that matches Bloom's “Node query limit” ...
Read more >
Securing GraphQL API endpoints using rate limits and ...
See how to secure your GraphQL API endpoints to prevent API spam and query attacks with rate and depth limiting.
Read more >
Conditional Formatting of Excel Charts
As in worksheet conditional formatting, the only limit is your own ability to construct formulas.
Read more >
Dynamically Formatting Graphs
Wire dynamic data or waveform data to a graph to automatically format the plot legend and x-scale time stamp for the graph.
Read more >
How to Create Interactive Charts with Dynamic Elements in ...
An interactive chart is a graph that allows the user to control which data series should be illustrated in the chart, making it...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found