-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
148 additions
and
14 deletions.
There are no files selected for viewing
13 changes: 0 additions & 13 deletions
13
src/JsonMergePatch.Abstractions/ITypeRepositoryExntesions.cs
This file was deleted.
Oops, something went wrong.
20 changes: 20 additions & 0 deletions
20
src/JsonMergePatch.Abstractions/ITypeRepositoryExtensions.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,20 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace LaDeak.JsonMergePatch.Abstractions | ||
{ | ||
public static class ITypeRepositoryExtensions | ||
{ | ||
public static ITypeRepository Extend(this ITypeRepository target, ITypeRepository source) | ||
{ | ||
_ = target ?? throw new ArgumentNullException(nameof(target)); | ||
_ = source ?? throw new ArgumentNullException(nameof(source)); | ||
|
||
foreach (var item in source.GetAll() ?? Enumerable.Empty<KeyValuePair<Type, Type>>()) | ||
if (!target.TryGet(item.Key, out _)) | ||
target.Add(item.Key, item.Value); | ||
return target; | ||
} | ||
} | ||
} |
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
68 changes: 68 additions & 0 deletions
68
test/JsonMergePatch.SourceGenerator.Abstractions.Tests/ITypeRepositoryExtensionTests.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,68 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using LaDeak.JsonMergePatch.Abstractions; | ||
using NSubstitute; | ||
using Xunit; | ||
|
||
namespace LaDeak.JsonMergePatch.SourceGenerator | ||
{ | ||
public class ITypeRepositoryExtensionTests | ||
{ | ||
[Fact] | ||
public void WithTypesInOther_ExtendAddsAllTo_TargetTypeRepository() | ||
{ | ||
var testData = new[] { new KeyValuePair<Type, Type>(typeof(object), typeof(string)), new KeyValuePair<Type, Type>(typeof(object), typeof(int)) }; | ||
var targetRespository = Substitute.For<ITypeRepository>(); | ||
var sourceRepository = Substitute.For<ITypeRepository>(); | ||
sourceRepository.GetAll().Returns(testData); | ||
|
||
var result = targetRespository.Extend(sourceRepository); | ||
|
||
Assert.Same(targetRespository, result); | ||
targetRespository.Received().Add(testData[0].Key, testData[0].Value); | ||
targetRespository.Received().Add(testData[1].Key, testData[1].Value); | ||
} | ||
|
||
[Fact] | ||
public void WithNoTypesInOther_ExtendAddsAllTo_TargetTypeRepository() | ||
{ | ||
var testData = Enumerable.Empty<KeyValuePair<Type, Type>>(); | ||
var targetRespository = Substitute.For<ITypeRepository>(); | ||
var sourceRepository = Substitute.For<ITypeRepository>(); | ||
sourceRepository.GetAll().Returns(testData); | ||
|
||
var result = targetRespository.Extend(sourceRepository); | ||
|
||
Assert.Same(targetRespository, result); | ||
targetRespository.DidNotReceiveWithAnyArgs().Add(null, null); | ||
} | ||
|
||
[Fact] | ||
public void WithNullTypesInOther_ExtendAddsAllTo_TargetTypeRepository() | ||
{ | ||
var targetRespository = Substitute.For<ITypeRepository>(); | ||
var sourceRepository = Substitute.For<ITypeRepository>(); | ||
sourceRepository.GetAll().Returns((IEnumerable<KeyValuePair<Type, Type>>)null); | ||
|
||
var result = targetRespository.Extend(sourceRepository); | ||
|
||
Assert.Same(targetRespository, result); | ||
targetRespository.DidNotReceiveWithAnyArgs().Add(null, null); | ||
} | ||
|
||
[Fact] | ||
public void NullTarget_ThrowsArgumetNullExcepion() | ||
{ | ||
ITypeRepository targetRespository = null; | ||
Assert.Throws<ArgumentNullException>(() => targetRespository.Extend(Substitute.For<ITypeRepository>())); | ||
} | ||
|
||
[Fact] | ||
public void NullSource_ThrowsArgumetNullExcepion() | ||
{ | ||
ITypeRepository targetRespository = Substitute.For<ITypeRepository>(); | ||
Assert.Throws<ArgumentNullException>(() => targetRespository.Extend(null)); | ||
} | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
test/JsonMergePatch.SourceGenerator.Abstractions.Tests/NameBuilderTests.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,59 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Microsoft.CodeAnalysis; | ||
using NSubstitute; | ||
using Xunit; | ||
|
||
namespace LaDeak.JsonMergePatch.SourceGenerator.Tests | ||
{ | ||
public class NameBuilderTests | ||
{ | ||
[Fact] | ||
public void GetName_ReturnsTypeName_WithWrappedSuffix() | ||
{ | ||
var typeSymbol = Substitute.For<ITypeSymbol>(); | ||
typeSymbol.Name.Returns("TestName"); | ||
var result = NameBuilder.GetName(typeSymbol); | ||
Assert.Equal("TestNameWrapped", result); | ||
} | ||
|
||
[Fact] | ||
public void GetNamespaceExtension_Returns_SafeExtension() | ||
{ | ||
var typeSymbol = Substitute.For<ITypeSymbol>(); | ||
typeSymbol.ContainingNamespace.ToDisplayString().Returns("Test.Namespace"); | ||
var result = NameBuilder.GetNamespaceExtension(typeSymbol); | ||
Assert.Equal("SafeTest.Namespace", result); | ||
} | ||
|
||
[Fact] | ||
public void GetNamespace_Returns_Namespace_SafeExtension() | ||
{ | ||
var typeSymbol = Substitute.For<ITypeSymbol>(); | ||
typeSymbol.ContainingNamespace.ToDisplayString().Returns("Test.Namespace"); | ||
var result = NameBuilder.GetNamespace(typeSymbol); | ||
Assert.Equal("LaDeak.JsonMergePatch.Generated.SafeTest.Namespace", result); | ||
} | ||
|
||
|
||
[Fact] | ||
public void GetNamespaceOnString_Returns_Namespace_SafeExtension() | ||
{ | ||
var result = NameBuilder.GetNamespace("Test.Namespace"); | ||
Assert.Equal("LaDeak.JsonMergePatch.Generated.SafeTest.Namespace", result); | ||
} | ||
|
||
[Fact] | ||
public void GetGetFullTypeName_Returns_Namespace_SafeExtension_TypeNameWrapped() | ||
{ | ||
var typeSymbol = Substitute.For<ITypeSymbol>(); | ||
typeSymbol.Name.Returns("TestName"); | ||
typeSymbol.ContainingNamespace.ToDisplayString().Returns("Test.Namespace"); | ||
var result = NameBuilder.GetFullTypeName(typeSymbol); | ||
Assert.Equal("LaDeak.JsonMergePatch.Generated.SafeTest.Namespace.TestNameWrapped", result); | ||
} | ||
} | ||
} |