-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adding priority for fluent decorators issue #255
- Loading branch information
Showing
4 changed files
with
141 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
tests/Grace.Tests/DependencyInjection/Decorator/PriorityDecoratorTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using Grace.DependencyInjection; | ||
using Grace.Tests.Classes.Simple; | ||
using Xunit; | ||
|
||
namespace Grace.Tests.DependencyInjection.Decorator | ||
{ | ||
public class PriorityDecoratorTests | ||
{ | ||
[Fact] | ||
public void DecoratorInOrderTest() | ||
{ | ||
var container = new DependencyInjectionContainer{ | ||
_ => | ||
{ | ||
_.ExportFactory<IBasicService>(() => new BasicService {Count = 5}); | ||
_.ExportDecorator(typeof(DecoratorOne)).As(typeof(IBasicService)); | ||
_.ExportDecorator(typeof(DecoratorTwo)).As(typeof(IBasicService)); | ||
} | ||
}; | ||
|
||
var instance = container.Locate<IBasicService>(); | ||
|
||
Assert.IsType<DecoratorTwo>(instance); | ||
Assert.Equal(55, instance.Count); | ||
} | ||
|
||
|
||
[Fact] | ||
public void DecoratorPriorityInOrderTest() | ||
{ | ||
var container = new DependencyInjectionContainer{ | ||
_ => | ||
{ | ||
_.ExportFactory<IBasicService>(() => new BasicService {Count = 5}); | ||
_.ExportDecorator(typeof(DecoratorOne)).As(typeof(IBasicService)); | ||
_.ExportDecorator(typeof(DecoratorTwo)).As(typeof(IBasicService)).Priority(10); | ||
} | ||
}; | ||
|
||
var instance = container.Locate<IBasicService>(); | ||
|
||
Assert.IsType<DecoratorTwo>(instance); | ||
Assert.Equal(55, instance.Count); | ||
} | ||
|
||
|
||
[Fact] | ||
public void DecoratorPriorityOutOfOrderTest() | ||
{ | ||
var container = new DependencyInjectionContainer{ | ||
_ => | ||
{ | ||
_.ExportFactory<IBasicService>(() => new BasicService {Count = 5}); | ||
_.ExportDecorator(typeof(DecoratorOne)).As(typeof(IBasicService)).Priority(10); | ||
_.ExportDecorator(typeof(DecoratorTwo)).As(typeof(IBasicService)); | ||
} | ||
}; | ||
|
||
var instance = container.Locate<IBasicService>(); | ||
|
||
Assert.IsType<DecoratorOne>(instance); | ||
Assert.Equal(100, instance.Count); | ||
} | ||
|
||
|
||
public class DecoratorOne : IBasicService | ||
{ | ||
private IBasicService _basicService; | ||
|
||
public DecoratorOne(IBasicService basicService) | ||
{ | ||
_basicService = basicService; | ||
} | ||
|
||
public int Count | ||
{ | ||
get => _basicService.Count * 10; | ||
set => _basicService.Count = value; | ||
} | ||
|
||
public int TestMethod() | ||
{ | ||
return _basicService.TestMethod(); | ||
} | ||
} | ||
|
||
|
||
public class DecoratorTwo : IBasicService | ||
{ | ||
private IBasicService _basicService; | ||
|
||
public DecoratorTwo(IBasicService basicService) | ||
{ | ||
_basicService = basicService; | ||
} | ||
|
||
public int Count | ||
{ | ||
get => _basicService.Count + 5; | ||
set => _basicService.Count = value; | ||
} | ||
|
||
public int TestMethod() | ||
{ | ||
return _basicService.TestMethod(); | ||
} | ||
} | ||
|
||
} | ||
} |