Skip to content

Commit

Permalink
Adding Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam Renaud committed Jan 7, 2022
1 parent 46a7345 commit aa5b89a
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 17 deletions.
102 changes: 85 additions & 17 deletions tests/Mapr.DependencyInjection.Tests/MapperConfigTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,24 @@ public void ShouldAdd_Defaults()
serviceCollection.AddMapr(_ => { });

serviceCollection.Should()
.Contain(s => s.ServiceType == typeof(IMapper) && s.ImplementationType == typeof(Mapper));
.Contain(
s => s.ServiceType == typeof(IMapper) &&
s.ImplementationType == typeof(Mapper) &&
s.Lifetime == ServiceLifetime.Singleton
);

serviceCollection.Should().Contain(
s => s.ServiceType == typeof(IMapLocator) && s.ImplementationType == typeof(MapLocator));
serviceCollection.Should()
.Contain(
s => s.ServiceType == typeof(IMapLocator) &&
s.ImplementationType == typeof(MapLocator) &&
s.Lifetime == ServiceLifetime.Singleton
);

serviceCollection.Should().Contain(s => s.ServiceType == typeof(MapFactory));
serviceCollection.Should()
.Contain(
s => s.ServiceType == typeof(MapFactory) &&
s.Lifetime == ServiceLifetime.Singleton
);
}

[Fact]
Expand All @@ -32,11 +44,26 @@ public void Scan_ShouldAddAllClassesFromAssemblyThat_InheritFromITypeMap()
config.Scan(typeof(TestMap).Assembly);
});

serviceCollection.Should().Contain(
s => s.ImplementationType == typeof(TestMap) && s.ServiceType == typeof(IMap<string, int>));
serviceCollection.Should()
.Contain(
s => s.ImplementationType == typeof(TestMap) &&
s.ServiceType == typeof(IMap<string, int>) &&
s.Lifetime == ServiceLifetime.Transient
);

serviceCollection.Should().Contain(
s => s.ImplementationType == typeof(TestMap) && s.ServiceType == typeof(IMap<int, string>));
serviceCollection.Should()
.Contain(
s => s.ImplementationType == typeof(TestMap) &&
s.ServiceType == typeof(IMap<int, string>) &&
s.Lifetime == ServiceLifetime.Transient
);

serviceCollection.Should()
.Contain(
s => s.ImplementationType == typeof(TestSingletonMap) &&
s.ServiceType == typeof(IMap<string, string>) &&
s.Lifetime == ServiceLifetime.Singleton
);
}

[Fact]
Expand All @@ -49,15 +76,30 @@ public void Scan_WithT_ShouldAddAllClassesFromAssemblyThat_InheritFromITypeMap()
config.Scan<TestMap>();
});

serviceCollection.Should().Contain(
s => s.ImplementationType == typeof(TestMap) && s.ServiceType == typeof(IMap<string, int>));
serviceCollection.Should()
.Contain(
s => s.ImplementationType == typeof(TestMap) &&
s.ServiceType == typeof(IMap<string, int>) &&
s.Lifetime == ServiceLifetime.Transient
);

serviceCollection.Should().Contain(
s => s.ImplementationType == typeof(TestMap) && s.ServiceType == typeof(IMap<int, string>));
serviceCollection.Should()
.Contain(
s => s.ImplementationType == typeof(TestMap) &&
s.ServiceType == typeof(IMap<int, string>) &&
s.Lifetime == ServiceLifetime.Transient
);

serviceCollection.Should()
.Contain(
s => s.ImplementationType == typeof(TestSingletonMap) &&
s.ServiceType == typeof(IMap<string, string>) &&
s.Lifetime == ServiceLifetime.Singleton
);
}

[Fact]
public void AddMap_ShouldAppMap()
public void AddMap_ShouldAddMap()
{
var serviceCollection = new ServiceCollection();

Expand All @@ -67,10 +109,36 @@ public void AddMap_ShouldAppMap()
.AddMap<int, string, TestMap>();
});

serviceCollection.Should().Contain(
s => s.ImplementationType == typeof(TestMap) && s.ServiceType == typeof(IMap<string, int>));
serviceCollection.Should()
.Contain(
s => s.ImplementationType == typeof(TestMap) &&
s.ServiceType == typeof(IMap<string, int>) &&
s.Lifetime == ServiceLifetime.Transient
);

serviceCollection.Should()
.Contain(
s => s.ImplementationType == typeof(TestMap) &&
s.ServiceType == typeof(IMap<int, string>) &&
s.Lifetime == ServiceLifetime.Transient
);
}

[Fact]
public void AddMap_ShouldAddSingletonMap_WhenAttributeIsPresent()
{
var serviceCollection = new ServiceCollection();

serviceCollection.Should().Contain(
s => s.ImplementationType == typeof(TestMap) && s.ServiceType == typeof(IMap<int, string>));
serviceCollection.AddMapr(config =>
{
config.AddMap<string, string, TestSingletonMap>();
});

serviceCollection.Should()
.Contain(
s => s.ImplementationType == typeof(TestSingletonMap) &&
s.ServiceType == typeof(IMap<string, string>) &&
s.Lifetime == ServiceLifetime.Singleton
);
}
}
11 changes: 11 additions & 0 deletions tests/Mapr.DependencyInjection.Tests/TestSingletonMap.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace Mapr.DependencyInjection.Tests;

[Map(Lifetime = MapLifetime.Singleton)]
public class TestSingletonMap: IMap<string, string>
{
/// <inheritdoc />
public string Map(string source)
{
return source;
}
}

0 comments on commit aa5b89a

Please sign in to comment.