Reputation: 426
I've got a simple test suite in .Net core, I would like to test wether a static non-virtual method is recursive. How could I do that?
This is the class I have:
using System;
namespace Fibonacci
{
public class Fibonacci
{
public static int GetNumber(int n)
{
if (n < 0) throw new ArgumentException("Number mustn't be negative");
var previousValue = 1;
var currentValue = 0;
var result = currentValue;
for (var i = 0; i < n; i++)
{
result += previousValue;
previousValue = currentValue;
currentValue = result;
}
return result;
}
}
}
The purpose is to test if a method of that class, with the exact same signature, but a different implementation, is recursive.
Upvotes: 1
Views: 253
Reputation: 84
If you're able to add an attribute the method you want to call, you could use an interceptor pattern. Check out TinyInterceptor for an example: https://github.com/Jalalx/TinyInterceptor With it you can get before and after method calls and use a global count to detect any calls > 1.
If you can't add an attribute, you may be able to use reflection.emit to add the attribute dynamically. See https://learn.microsoft.com/en-us/dotnet/api/system.reflection.emit
Hope this helps, Paul
Upvotes: 1