Skip to content

Commit

Permalink
Fix Aspects.Cache for nullable values (#189)
Browse files Browse the repository at this point in the history
  • Loading branch information
kondr1 authored Jul 22, 2022
1 parent 384caee commit 234d1a9
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
2 changes: 1 addition & 1 deletion samples/src/Cache/CacheAspect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public object Handle(
cacheTrigger.Set(key, result, retType, instance);
}
}

if (result == NullMarker) return null;
return result;
}

Expand Down
32 changes: 32 additions & 0 deletions samples/tests/Aspests.Tests/CacheTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,20 @@ public class CacheTests
{
class TestClass
{
[MemoryCache(3, PerInstanceCache = false)]
public int? Nullable(bool ok)
{
if (ok) return 1;
return null;
}

[MemoryCache(3, PerInstanceCache = true)]
public int? NullablePerInstance(bool ok)
{
if (ok) return 1;
return null;
}

[MemoryCache(3, PerInstanceCache = false)]
public void Do(ref int a)
{
Expand Down Expand Up @@ -164,6 +178,24 @@ public async Task Cache_Aspect_Caches_AsyncTaskMethod_Result()
Assert.NotEqual(result4, result);
}

[Fact]
public void Cache_Nullable_Method()
{
var target = new TestClass();

var i = target.Nullable(true);
Assert.Equal(1, i);

i = target.Nullable(false);
Assert.Null(i);

i = target.NullablePerInstance(true);
Assert.Equal(1, i);

i = target.NullablePerInstance(false);
Assert.Null(i);
}

[Fact]
public void Cache_Void_Method()
{
Expand Down

0 comments on commit 234d1a9

Please sign in to comment.