Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions src/AutoLazy.Tests/GenericTypeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,16 @@ public class GenericTypeTests
public void MockGeneric_with_int_should_be_lazy()
{
var target = new MockGeneric<int>(() => 123);

Assert.AreEqual(0, target.GetValueCount);

var first = target.GetValue();

Assert.AreEqual(123, first);
Assert.AreEqual(1, target.GetValueCount);

var second = target.GetValue();

Assert.AreEqual(first, second);
Assert.AreEqual(1, target.GetValueCount);
}
Expand All @@ -24,13 +29,81 @@ public void MockGeneric_with_int_should_be_lazy()
public void MockGeneric_with_string_should_be_lazy()
{
var target = new MockGeneric<string>(() => Guid.NewGuid().ToString("n"));

Assert.AreEqual(0, target.GetValueCount);

var first = target.GetValue();

Assert.AreNotEqual(null, first);
Assert.AreEqual(1, target.GetValueCount);

var second = target.GetValue();

Assert.AreEqual(first, second);
Assert.AreEqual(1, target.GetValueCount);
}

[Test]
public void MockGeneric_with_null_string_should_be_lazy()
{
var target = new MockGeneric<string>(() => null);

Assert.AreEqual(0, target.GetValueCount);

var first = target.GetValue();

Assert.AreEqual(1, target.GetValueCount);

var second = target.GetValue();

Assert.AreEqual(first, second);
Assert.AreEqual(1, target.GetValueCount);
}

[Test]
public void MockGeneric_with_exception_string_should_be_lazy()
{
var target = new MockGeneric<object>(() => throw new ExpectedException());

Assert.AreEqual(0, target.GetValueCount);

object first = null;
ExpectedException firstException = null;
try
{
first = target.GetValue();
}
catch (ExpectedException e )
{
firstException = e;
}

Assert.IsNotNull(firstException);
Assert.AreEqual(1, target.GetValueCount);

object second = null;
ExpectedException secondException = null;
try
{
second = target.GetValue();
}
catch (ExpectedException e)
{
secondException = e;
}

Assert.IsNotNull(secondException);
Assert.AreSame(firstException, secondException );
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[AutoLazy] will not save an exception if one is encountered so I would expect this assertion to fail.

Assert.AreEqual(1, target.GetValueCount);
Assert.AreEqual(first, second);

}

}

public class ExpectedException:Exception
{

}

}