From 234d1a9c979f4f449be56abfcc38474d3731b143 Mon Sep 17 00:00:00 2001 From: Konstantin Kondr Date: Fri, 22 Jul 2022 21:24:45 +0300 Subject: [PATCH] Fix Aspects.Cache for nullable values (#189) --- samples/src/Cache/CacheAspect.cs | 2 +- samples/tests/Aspests.Tests/CacheTests.cs | 32 +++++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/samples/src/Cache/CacheAspect.cs b/samples/src/Cache/CacheAspect.cs index 8267681a..da11f9c1 100644 --- a/samples/src/Cache/CacheAspect.cs +++ b/samples/src/Cache/CacheAspect.cs @@ -52,7 +52,7 @@ public object Handle( cacheTrigger.Set(key, result, retType, instance); } } - + if (result == NullMarker) return null; return result; } diff --git a/samples/tests/Aspests.Tests/CacheTests.cs b/samples/tests/Aspests.Tests/CacheTests.cs index c65e340b..32439ed5 100644 --- a/samples/tests/Aspests.Tests/CacheTests.cs +++ b/samples/tests/Aspests.Tests/CacheTests.cs @@ -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) { @@ -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() {