Skip to content

Commit

Permalink
Cover CachedExtensions
Browse files Browse the repository at this point in the history
  • Loading branch information
neon-sunset committed Jun 28, 2022
1 parent bbe5c64 commit a87adae
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 2 deletions.
125 changes: 125 additions & 0 deletions tests/FastCache.CachedTests/Cached_Extensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
using FastCache.Extensions;

namespace FastCache.CachedTests;

public sealed class CachedTests_Extensions
{
[Fact]
public void CacheK1_Caches()
{
var key = GetTestKey();
var value = GetRandomString();

value.Cache(key, TimeSpan.MaxValue);

var found = Cached<string>.TryGet(key, out var cached);

Assert.True(found);
Assert.Equal(value, cached.Value);
}

[Fact]
public void CacheK2_Caches()
{
var k1 = GetTestKey();
var k2 = GetTestKey();
var value = GetRandomString();

value.Cache(k1, k2, TimeSpan.MaxValue);

var found = Cached<string>.TryGet(k1, k2, out var cached);

Assert.True(found);
Assert.Equal(value, cached.Value);
}

[Fact]
public void CacheK3_Caches()
{
var k1 = GetTestKey();
var k2 = GetTestKey();
var k3 = GetTestKey();
var value = GetRandomString();

value.Cache(k1, k2, k3, TimeSpan.MaxValue);

var found = Cached<string>.TryGet(k1, k2, k3, out var cached);

Assert.True(found);
Assert.Equal(value, cached.Value);
}

[Fact]
public void CacheK4_Caches()
{
var k1 = GetTestKey();
var k2 = GetTestKey();
var k3 = GetTestKey();
var k4 = GetTestKey();
var value = GetRandomString();

value.Cache(k1, k2, k3, k4, TimeSpan.MaxValue);

var found = Cached<string>.TryGet(k1, k2, k3, k4, out var cached);

Assert.True(found);
Assert.Equal(value, cached.Value);
}

[Fact]
public void CacheK5_Caches()
{
var k1 = GetTestKey();
var k2 = GetTestKey();
var k3 = GetTestKey();
var k4 = GetTestKey();
var k5 = GetTestKey();
var value = GetRandomString();

value.Cache(k1, k2, k3, k4, k5, TimeSpan.MaxValue);

var found = Cached<string>.TryGet(k1, k2, k3, k4, k5, out var cached);

Assert.True(found);
Assert.Equal(value, cached.Value);
}

[Fact]
public void CacheK6_Caches()
{
var k1 = GetTestKey();
var k2 = GetTestKey();
var k3 = GetTestKey();
var k4 = GetTestKey();
var k5 = GetTestKey();
var k6 = GetTestKey();
var value = GetRandomString();

value.Cache(k1, k2, k3, k4, k5, k6, TimeSpan.MaxValue);

var found = Cached<string>.TryGet(k1, k2, k3, k4, k5, k6, out var cached);

Assert.True(found);
Assert.Equal(value, cached.Value);
}

[Fact]
public void CacheK7_Caches()
{
var k1 = GetTestKey();
var k2 = GetTestKey();
var k3 = GetTestKey();
var k4 = GetTestKey();
var k5 = GetTestKey();
var k6 = GetTestKey();
var k7 = GetTestKey();
var value = GetRandomString();

value.Cache(k1, k2, k3, k4, k5, k6, k7, TimeSpan.MaxValue);

var found = Cached<string>.TryGet(k1, k2, k3, k4, k5, k6, k7, out var cached);

Assert.True(found);
Assert.Equal(value, cached.Value);
}
}
4 changes: 2 additions & 2 deletions tests/FastCache.CachedTests/TestHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ public static string GetTestKey<T>(T arg1, [CallerMemberName] string testName =
public static string GetRandomString()
{
#if !NETSTANDARD2_0
var bytes = (stackalloc byte[256]);
var bytes = (stackalloc byte[64]);
#else
var bytes = new byte[256];
var bytes = new byte[64];
#endif

_random.NextBytes(bytes);
Expand Down

0 comments on commit a87adae

Please sign in to comment.