Skip to content

Commit

Permalink
tests #36
Browse files Browse the repository at this point in the history
  • Loading branch information
GSerjo committed Aug 16, 2017
1 parent 1d5a34b commit 11df16b
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 18 deletions.
17 changes: 12 additions & 5 deletions Source/TinyMapper/Bindings/BindingConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Nelibur.ObjectMapper.Bindings
internal class BindingConfig
{
private readonly Dictionary<string, string> _oneToOneBindFields = new Dictionary<string, string>();
private readonly Dictionary<string, BindingFieldPath> _bindFieldsPath = new Dictionary<string, BindingFieldPath>();
private readonly Dictionary<string, List<BindingFieldPath>> _bindFieldsPath = new Dictionary<string, List<BindingFieldPath>>();
private readonly Dictionary<string, Type> _bindTypes = new Dictionary<string, Type>();
private readonly Dictionary<string, Func<object, object>> _customTypeConverters = new Dictionary<string, Func<object, object>>();
private readonly HashSet<string> _ignoreFields = new HashSet<string>();
Expand All @@ -28,7 +28,14 @@ internal void BindFields(List<string> sourcePath, List<string> targetPath)
}
else
{
_bindFieldsPath[bindingFieldPath.SourceHead] = bindingFieldPath;
if (_bindFieldsPath.ContainsKey(bindingFieldPath.SourceHead))
{
_bindFieldsPath[bindingFieldPath.SourceHead].Add(bindingFieldPath);
}
else
{
_bindFieldsPath[bindingFieldPath.SourceHead] = new List<BindingFieldPath> { bindingFieldPath };
}
}
}

Expand All @@ -44,11 +51,11 @@ internal Option<string> GetBindField(string sourceName)
return new Option<string>(result, exsist);
}

internal Option<BindingFieldPath> GetBindFieldPath(string fieldName)
internal Option<List<BindingFieldPath>> GetBindFieldPath(string fieldName)
{
BindingFieldPath result;
List<BindingFieldPath> result;
bool exsist = _bindFieldsPath.TryGetValue(fieldName, out result);
return new Option<BindingFieldPath>(result, exsist);
return new Option<List<BindingFieldPath>>(result, exsist);
}

internal Option<Type> GetBindType(string targetName)
Expand Down
21 changes: 17 additions & 4 deletions Source/TinyMapper/Mappers/Classes/Members/MappingMemberBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ private List<MappingMemberPath> ParseMappingTypes(TypePair typePair)
MemberInfo targetMember = targetMembers.FirstOrDefault(x => _config.NameMatching(targetName, x.Name));
if (targetMember.IsNull())
{
result.AddRange(GetBindMappingMemberPath(typePair, bindingConfig, sourceMember));
continue;
}
Option<Type> concreteBindingType = bindingConfig.Map(x => x.GetBindType(targetName));
Expand All @@ -191,13 +192,25 @@ private List<MappingMemberPath> ParseMappingTypes(TypePair typePair)
result.Add(new MappingMemberPath(sourceMember, targetMember));
}

var bindFieldPath = bindingConfig.Map(x => x.GetBindFieldPath(sourceMember.Name));
result.AddRange(GetBindMappingMemberPath(typePair, bindingConfig, sourceMember));
}
return result;
}

if (bindFieldPath.HasValue)
private List<MappingMemberPath> GetBindMappingMemberPath(TypePair typePair, Option<BindingConfig> bindingConfig, MemberInfo sourceMember)
{
var result = new List<MappingMemberPath>();

var bindFieldPath = bindingConfig.Map(x => x.GetBindFieldPath(sourceMember.Name));

if (bindFieldPath.HasValue)
{
foreach (var item in bindFieldPath.Value)
{
var sourceMemperPath = GetSourceMemberPath(bindFieldPath.Value.SourcePath, typePair.Source);
var targetMemperPath = GetSourceMemberPath(bindFieldPath.Value.TargetPath, typePair.Target);
var sourceMemperPath = GetSourceMemberPath(item.SourcePath, typePair.Source);
var targetMemperPath = GetSourceMemberPath(item.TargetPath, typePair.Target);
result.Add(new MappingMemberPath(sourceMemperPath, targetMemperPath));

}
}
return result;
Expand Down
14 changes: 7 additions & 7 deletions Source/TinyMapper/Reflection/DynamicAssemblyBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ internal class DynamicAssemblyBuilder
{
internal const string AssemblyName = "DynamicTinyMapper";
#if !COREFX
private const string AssemblyNameFileName = AssemblyName + ".dll";
private static AssemblyBuilder _assemblyBuilder;
// private const string AssemblyNameFileName = AssemblyName + ".dll";
// private static AssemblyBuilder _assemblyBuilder;
#endif
private static readonly DynamicAssembly _dynamicAssembly = new DynamicAssembly();

Expand All @@ -32,11 +32,11 @@ public DynamicAssembly()
_moduleBuilder = assemblyBuilder.DefineDynamicModule(assemblyName.Name);

#else
// AssemblyBuilder assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run);
_assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.RunAndSave);
AssemblyBuilder assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run);
// _assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.RunAndSave);

// _moduleBuilder = assemblyBuilder.DefineDynamicModule(assemblyName.Name);
_moduleBuilder = _assemblyBuilder.DefineDynamicModule(assemblyName.Name, AssemblyNameFileName, true);
_moduleBuilder = assemblyBuilder.DefineDynamicModule(assemblyName.Name);
// _moduleBuilder = _assemblyBuilder.DefineDynamicModule(assemblyName.Name, AssemblyNameFileName, true);
#endif

}
Expand All @@ -49,7 +49,7 @@ public TypeBuilder DefineType(string typeName, Type parentType)
public void Save()
{
#if !COREFX
_assemblyBuilder.Save(AssemblyNameFileName);
// _assemblyBuilder.Save(AssemblyNameFileName);
#endif
}
}
Expand Down
4 changes: 2 additions & 2 deletions Source/UnitTests/ForTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ namespace UnitTests
{
public sealed class ForTests
{
// [Fact(Skip = "ForTests")]
[Fact]
[Fact(Skip = "ForTests")]
// [Fact]
public void Test()
{
// TinyMapper.Bind<TagSource, TagSource1>();
Expand Down
64 changes: 64 additions & 0 deletions Source/UnitTests/Mappings/MappingWithComplexConfigTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using Nelibur.ObjectMapper;
using Xunit;

namespace UnitTests.Mappings
{
public class MappingWithComplexConfigTests
{
[Fact]
public void Map_ComplexBind_Success()
{
TinyMapper.Bind<PersonDto, Person>(
config =>
{
config.Bind(source => source.Address.Street, target => target.Street);
config.Bind(source => source.Address.Phone, target => target.Phone);
}
);

var dto = new PersonDto
{
Address = new AddressDto
{
Street = "Street",
Phone = "123123"
},
Code = "Code",
Identity = 1,
Name = "Alex"
};

var person = TinyMapper.Map<Person>(dto);

Assert.Equal(dto.Identity, person.Identity);
Assert.Equal(dto.Code, person.Code);
Assert.Equal(dto.Name, person.Name);
Assert.Equal(dto.Address.Street, person.Street);
Assert.Equal(dto.Address.Phone, person.Phone);
}


public class Person
{
public int Identity { get; set; }
public string Code { get; set; }
public string Name { get; set; }
public string Street { get; set; }
public string Phone { get; set; }
}

public class PersonDto
{
public int Identity { get; set; }
public string Code { get; set; }
public string Name { get; set; }
public AddressDto Address { get; set; }
}

public class AddressDto
{
public string Street { get; set; }
public string Phone { get; set; }
}
}
}

0 comments on commit 11df16b

Please sign in to comment.