Skip to content

Commit

Permalink
Add lifetime test case.
Browse files Browse the repository at this point in the history
  • Loading branch information
inversionhourglass committed Dec 1, 2024
1 parent ba48f34 commit 4079d4e
Show file tree
Hide file tree
Showing 3 changed files with 361 additions and 0 deletions.
63 changes: 63 additions & 0 deletions test/Rougamo.Fody.Tests/BasicTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,7 @@ public async Task LifetimeTest()

var list = new List<object>();

#region Singleton
list.Clear();
instance.SingletonNested(list);
Assert.Equal(2, list.Count);
Expand All @@ -616,6 +617,24 @@ public async Task LifetimeTest()
Assert.Equal(3, list.Count);
Assert.Same(list[0], list[2]);

list.Clear();
((IEnumerable<int>)instance.SingletonNestedIterator(list)).ToArray();
Assert.Equal(2, list.Count);
Assert.Same(list[0], list[1]);
((IEnumerable<int>)instance.SingletonIterator(list)).ToArray();
Assert.Equal(3, list.Count);
Assert.Same(list[0], list[2]);

list.Clear();
await ((IAsyncEnumerable<int>)instance.SingletonNestedAiterator(list)).ToArrayAsync();
Assert.Equal(2, list.Count);
Assert.Same(list[0], list[1]);
await ((IAsyncEnumerable<int>)instance.SingletonAiterator(list)).ToArrayAsync();
Assert.Equal(3, list.Count);
Assert.Same(list[0], list[2]);
#endregion Singleton

#region Pooled
list.Clear();
instance.PooledNested(list);
Assert.Equal(2, list.Count);
Expand All @@ -632,6 +651,24 @@ public async Task LifetimeTest()
Assert.Equal(3, list.Count);
Assert.True(list[0] == list[2] || list[1] == list[2]);

list.Clear();
((IEnumerable<int>)instance.PooledNestedIterator(list)).ToArray();
Assert.Equal(2, list.Count);
Assert.NotSame(list[0], list[1]);
((IEnumerable<int>)instance.PooledIterator(list)).ToArray();
Assert.Equal(3, list.Count);
Assert.True(list[0] == list[2] || list[1] == list[2]);

list.Clear();
await ((IAsyncEnumerable<int>)instance.PooledNestedAiterator(list)).ToArrayAsync();
Assert.Equal(2, list.Count);
Assert.NotSame(list[0], list[1]);
await ((IAsyncEnumerable<int>)instance.PooledAiterator(list)).ToArrayAsync();
Assert.Equal(3, list.Count);
Assert.True(list[0] == list[2] || list[1] == list[2]);
#endregion Pooled

#region Transient
list.Clear();
instance.TransientNested(list);
Assert.Equal(2, list.Count);
Expand All @@ -649,6 +686,32 @@ public async Task LifetimeTest()
Assert.Equal(3, list.Count);
Assert.NotSame(list[0], list[2]);
Assert.NotSame(list[1], list[2]);

list.Clear();
((IEnumerable<int>)instance.TransientNestedIterator(list)).ToArray();
Assert.Equal(2, list.Count);
Assert.NotSame(list[0], list[1]);
((IEnumerable<int>)instance.TransientIterator(list)).ToArray();
Assert.Equal(3, list.Count);
Assert.NotSame(list[0], list[2]);
Assert.NotSame(list[1], list[2]);

list.Clear();
await ((IAsyncEnumerable<int>)instance.TransientNestedAiterator(list)).ToArrayAsync();
Assert.Equal(2, list.Count);
Assert.NotSame(list[0], list[1]);
await ((IAsyncEnumerable<int>)instance.TransientAiterator(list)).ToArrayAsync();
Assert.Equal(3, list.Count);
Assert.NotSame(list[0], list[2]);
Assert.NotSame(list[1], list[2]);
#endregion Transient

list.Clear();
instance.GenericPooled(list);
Assert.Single(list);
await (Task)instance.GenericPooledAsync(list);
Assert.Equal(2, list.Count);
Assert.Same(list[0], list[1]);
}
}
}
44 changes: 44 additions & 0 deletions test/TestAssemblies/BasicUsage/Attributes/TryLifetimeAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using Rougamo;
using Rougamo.Context;
using Rougamo.Metadatas;
using System.Collections.Generic;

namespace BasicUsage.Attributes
{
[Advice(Feature.OnEntry)]
public abstract class TryLifetimeAttribute : MoAttribute
{
public override void OnEntry(MethodContext context)
{
var list = (List<object>)context.Arguments[0];
list.Add(this);
}
}

[Lifetime(Lifetime.Singleton)]
public class SingletonAttribute : TryLifetimeAttribute
{
}

[Lifetime(Lifetime.Pooled)]
public class PooledAttribute : TryLifetimeAttribute
{
}

[Lifetime(Lifetime.Pooled)]
public class PooledAttribute<T> : TryLifetimeAttribute, IResettable
{
public T Value { get; set; }

public bool TryReset()
{
Value = default;
return true;
}
}

[Lifetime(Lifetime.Transient)]
public class TransientAttribute : TryLifetimeAttribute
{
}
}
254 changes: 254 additions & 0 deletions test/TestAssemblies/BasicUsage/LifetimeUseCase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,254 @@
using BasicUsage.Attributes;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace BasicUsage
{
public class LifetimeUseCase
{
[Singleton]
public void Singleton(List<object> list)
{
}

[Singleton]
public void SingletonNested(List<object> list)
{
SingletonNestedCore(list);
}

[Singleton]
private void SingletonNestedCore(List<object> list)
{
}

[Singleton]
public async Task SingletonAsync(List<object> list)
{
await Task.CompletedTask;
}

[Singleton]
public async Task SingletonNestedAsync(List<object> list)
{
await SingletonNestedCoreAsync(list);
}

[Singleton]
private async Task SingletonNestedCoreAsync(List<object> list)
{
await Task.CompletedTask;
}

[Singleton]
public IEnumerable<int> SingletonIterator(List<object> list)
{
yield return 1;
}

[Singleton]
public IEnumerable<int> SingletonNestedIterator(List<object> list)
{
foreach (var item in SingletonNestedIteratorCore(list))
{
yield return item;
}
}

[Singleton]
private IEnumerable<int> SingletonNestedIteratorCore(List<object> list)
{
yield return 1;
}

[Singleton]
public async IAsyncEnumerable<int> SingletonAiterator(List<object> list)
{
await Task.Yield();
yield return 1;
}

[Singleton]
public async IAsyncEnumerable<int> SingletonNestedAiterator(List<object> list)
{
await foreach (var item in SingletonNestedAiteratorCore(list))
{
yield return item;
}
}

[Singleton]
private async IAsyncEnumerable<int> SingletonNestedAiteratorCore(List<object> list)
{
await Task.Yield();
yield return 1;
}

[Pooled]
public void Pooled(List<object> list)
{
}

[Pooled]
public void PooledNested(List<object> list)
{
PooledNestedCore(list);
}

[Pooled]
private void PooledNestedCore(List<object> list)
{
}

[Pooled]
public async Task PooledAsync(List<object> list)
{
await Task.CompletedTask;
}

[Pooled]
public async Task PooledNestedAsync(List<object> list)
{
await PooledNestedCoreAsync(list);
}

[Pooled]
private async Task PooledNestedCoreAsync(List<object> list)
{
await Task.CompletedTask;
}

[Pooled]
public IEnumerable<int> PooledIterator(List<object> list)
{
yield return 1;
}

[Pooled]
public IEnumerable<int> PooledNestedIterator(List<object> list)
{
foreach (var item in PooledNestedIteratorCore(list))
{
yield return item;
}
}

[Pooled]
private IEnumerable<int> PooledNestedIteratorCore(List<object> list)
{
yield return 1;
}

[Pooled]
public async IAsyncEnumerable<int> PooledAiterator(List<object> list)
{
await Task.Yield();
yield return 1;
}

[Pooled]
public async IAsyncEnumerable<int> PooledNestedAiterator(List<object> list)
{
await foreach (var item in PooledNestedAiteratorCore(list))
{
yield return item;
}
}

[Pooled]
private async IAsyncEnumerable<int> PooledNestedAiteratorCore(List<object> list)
{
await Task.Yield();
yield return 1;
}

[Pooled<int>(Value = 1)]
public void GenericPooled(List<object> list)
{
}

[Pooled<int>(Value = 2)]
public async Task GenericPooledAsync(List<object> list)
{
await Task.Yield();
}

[Transient]
public void Transient(List<object> list)
{
}

[Transient]
public void TransientNested(List<object> list)
{
TransientNestedCore(list);
}

[Transient]
private void TransientNestedCore(List<object> list)
{
}

[Transient]
public async Task TransientAsync(List<object> list)
{
await Task.CompletedTask;
}

[Transient]
public async Task TransientNestedAsync(List<object> list)
{
await TransientNestedCoreAsync(list);
}

[Transient]
private async Task TransientNestedCoreAsync(List<object> list)
{
await Task.CompletedTask;
}

[Transient]
public IEnumerable<int> TransientIterator(List<object> list)
{
yield return 1;
}

[Transient]
public IEnumerable<int> TransientNestedIterator(List<object> list)
{
foreach (var item in TransientNestedIteratorCore(list))
{
yield return item;
}
}

[Transient]
private IEnumerable<int> TransientNestedIteratorCore(List<object> list)
{
yield return 1;
}

[Transient]
public async IAsyncEnumerable<int> TransientAiterator(List<object> list)
{
await Task.Yield();
yield return 1;
}

[Transient]
public async IAsyncEnumerable<int> TransientNestedAiterator(List<object> list)
{
await foreach (var item in TransientNestedAiteratorCore(list))
{
yield return item;
}
}

[Transient]
private async IAsyncEnumerable<int> TransientNestedAiteratorCore(List<object> list)
{
await Task.Yield();
yield return 1;
}
}
}

0 comments on commit 4079d4e

Please sign in to comment.