Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect coverage for statement lambda function #343

Closed
driseley opened this issue Feb 18, 2019 · 3 comments
Closed

Incorrect coverage for statement lambda function #343

driseley opened this issue Feb 18, 2019 · 3 comments
Assignees
Labels
bug Something isn't working tenet-coverage Issue related to possible incorrect coverage

Comments

@driseley
Copy link

driseley commented Feb 18, 2019

Coverlet appears to report incorrect coverage for anonymous statement lambda functions.

For the following class:

using System;
using System.IO;
using System.Text;

namespace DemoLibrary
{
	public class DemoClass
	{
		protected T WriteToStream<T>(Func<Stream, bool, T> getResultFunction)
		{
			using (var stream = new MemoryStream())
			{
				var result = getResultFunction(stream, false);
				stream.Close();
				Console.WriteLine(Encoding.Default.GetString(stream.ToArray()));
				return result;
			}
		}

		public bool WriteHelloWorldToStream(Stream stream , bool condition)
		{
			if (condition) stream.Write(Encoding.Default.GetBytes("Hello World"));
			else stream.Write(Encoding.Default.GetBytes("Goodbye World"));        
			return condition;
		}

		public bool InvokeFunction()
		{
			return WriteToStream((stream,condition) => WriteHelloWorldToStream(stream, condition));
		}

		public bool InvokeAnonymous()
		{
			return WriteToStream((stream, condition) =>
			{
				if (condition) stream.Write(Encoding.Default.GetBytes("Hello World"));
				else stream.Write(Encoding.Default.GetBytes("Goodbye World"));               
				return condition;
			}
			);
		}
	}
}

The following tests:

using DemoLibrary;
using System;
using System.IO;
using Xunit;

namespace DemoLibraryTests
{
	public class DemoClassTests
	{
		[Fact]
		public void InvokeFunction_Test()
		{
			DemoClass demoClass = new DemoClass();
			bool result = demoClass.InvokeFunction();
			Assert.False(result);
		}

		[Fact]
		public void InvokeAnonymous_Test()
		{
			DemoClass demoClass = new DemoClass();
			bool result = demoClass.InvokeAnonymous();
			Assert.False(result);
		}
	}
}

Report the following results:
image

Notice that the coverage for the InvokeAnonymous function shows all lines and all branches covered ( which clearly cannot be correct ). For the named function, the coverage is correct.

driseley pushed a commit to driseley/coverlet-issue-343 that referenced this issue Feb 18, 2019
@driseley
Copy link
Author

driseley commented Feb 18, 2019

I have created an example project that demonstrates the issue:
https://github.com/driseley/coverlet-issue-343

@driseley
Copy link
Author

Coverlet version 2.5.1, Project .Net Core 2.1

@tonerdo
Copy link
Collaborator

tonerdo commented Feb 18, 2019

@driseley thanks for providing the repro project. @MarcoRossignoli when you do get some time can you take a look at this. You've got more experience with compiler generated code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working tenet-coverage Issue related to possible incorrect coverage
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants