diff --git a/src/JsonMergePatch.Abstractions/ITypeRepositoryExntesions.cs b/src/JsonMergePatch.Abstractions/ITypeRepositoryExntesions.cs deleted file mode 100644 index 8061fc7..0000000 --- a/src/JsonMergePatch.Abstractions/ITypeRepositoryExntesions.cs +++ /dev/null @@ -1,13 +0,0 @@ -namespace LaDeak.JsonMergePatch.Abstractions -{ - public static class ITypeRepositoryExntesions - { - public static ITypeRepository Extend(this ITypeRepository repository, ITypeRepository other) - { - foreach (var item in other.GetAll()) - if (!repository.TryGet(item.Key, out _)) - repository.Add(item.Key, item.Value); - return repository; - } - } -} \ No newline at end of file diff --git a/src/JsonMergePatch.Abstractions/ITypeRepositoryExtensions.cs b/src/JsonMergePatch.Abstractions/ITypeRepositoryExtensions.cs new file mode 100644 index 0000000..ba70e9f --- /dev/null +++ b/src/JsonMergePatch.Abstractions/ITypeRepositoryExtensions.cs @@ -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>()) + if (!target.TryGet(item.Key, out _)) + target.Add(item.Key, item.Value); + return target; + } + } +} \ No newline at end of file diff --git a/src/JsonMergePatch.SourceGenerator/NameBuilder.cs b/src/JsonMergePatch.SourceGenerator/NameBuilder.cs index 32042b2..fbff545 100644 --- a/src/JsonMergePatch.SourceGenerator/NameBuilder.cs +++ b/src/JsonMergePatch.SourceGenerator/NameBuilder.cs @@ -4,7 +4,7 @@ namespace LaDeak.JsonMergePatch.SourceGenerator { public static class NameBuilder { - internal const string Namespace = "LaDeak.JsonMergePatch.Generated"; + private const string Namespace = "LaDeak.JsonMergePatch.Generated"; public static string GetName(ITypeSymbol typeInfo) => $"{typeInfo.Name}Wrapped"; diff --git a/test/JsonMergePatch.SourceGenerator.Abstractions.Tests/ITypeRepositoryExtensionTests.cs b/test/JsonMergePatch.SourceGenerator.Abstractions.Tests/ITypeRepositoryExtensionTests.cs new file mode 100644 index 0000000..83f057c --- /dev/null +++ b/test/JsonMergePatch.SourceGenerator.Abstractions.Tests/ITypeRepositoryExtensionTests.cs @@ -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(typeof(object), typeof(string)), new KeyValuePair(typeof(object), typeof(int)) }; + var targetRespository = Substitute.For(); + var sourceRepository = Substitute.For(); + 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>(); + var targetRespository = Substitute.For(); + var sourceRepository = Substitute.For(); + 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(); + var sourceRepository = Substitute.For(); + sourceRepository.GetAll().Returns((IEnumerable>)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(() => targetRespository.Extend(Substitute.For())); + } + + [Fact] + public void NullSource_ThrowsArgumetNullExcepion() + { + ITypeRepository targetRespository = Substitute.For(); + Assert.Throws(() => targetRespository.Extend(null)); + } + } +} diff --git a/test/JsonMergePatch.SourceGenerator.Abstractions.Tests/NameBuilderTests.cs b/test/JsonMergePatch.SourceGenerator.Abstractions.Tests/NameBuilderTests.cs new file mode 100644 index 0000000..59ec057 --- /dev/null +++ b/test/JsonMergePatch.SourceGenerator.Abstractions.Tests/NameBuilderTests.cs @@ -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(); + typeSymbol.Name.Returns("TestName"); + var result = NameBuilder.GetName(typeSymbol); + Assert.Equal("TestNameWrapped", result); + } + + [Fact] + public void GetNamespaceExtension_Returns_SafeExtension() + { + var typeSymbol = Substitute.For(); + 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(); + 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(); + typeSymbol.Name.Returns("TestName"); + typeSymbol.ContainingNamespace.ToDisplayString().Returns("Test.Namespace"); + var result = NameBuilder.GetFullTypeName(typeSymbol); + Assert.Equal("LaDeak.JsonMergePatch.Generated.SafeTest.Namespace.TestNameWrapped", result); + } + } +}