Skip to content

Commit

Permalink
Fixing typos adding unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ladeak committed Mar 2, 2021
1 parent 6db85a8 commit 72e0a48
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 14 deletions.
13 changes: 0 additions & 13 deletions src/JsonMergePatch.Abstractions/ITypeRepositoryExntesions.cs

This file was deleted.

20 changes: 20 additions & 0 deletions src/JsonMergePatch.Abstractions/ITypeRepositoryExtensions.cs
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;
}
}
}
2 changes: 1 addition & 1 deletion src/JsonMergePatch.SourceGenerator/NameBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down
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));
}
}
}
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);
}
}
}

0 comments on commit 72e0a48

Please sign in to comment.