Skip to content

Commit b61fdbd

Browse files
committed
add Test for RequireExplicitMappingPrimitive
1 parent a8d3f09 commit b61fdbd

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

src/Mapster.Tests/WhenExplicitMappingRequired.cs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public class WhenExplicitMappingRequired
1313
public void TestCleanup()
1414
{
1515
TypeAdapterConfig.GlobalSettings.RequireExplicitMapping = false;
16+
TypeAdapterConfig.GlobalSettings.RequireExplicitMappingPrimitive = false;
1617
TypeAdapterConfig.GlobalSettings.Clear();
1718
}
1819

@@ -140,8 +141,60 @@ public void UnmappedChildPocoShouldFailed()
140141
setter.Compile(); // Should fail here
141142
}
142143

144+
[TestMethod]
145+
public void RequireExplicitMappingPrimitiveWork()
146+
{
147+
TypeAdapterConfig.GlobalSettings.RequireExplicitMappingPrimitive = true;
148+
149+
TypeAdapterConfig<Source783, Destination783>.NewConfig();
150+
151+
Should.Throw<CompileException>(() =>
152+
{
153+
TypeAdapterConfig.GlobalSettings.Compile(); // throw CompileException
154+
});
155+
156+
byte byteSource = 10;
157+
158+
byteSource.Adapt<byte>(); // Should work when the type is mapped to itself
159+
160+
Should.Throw<CompileException>(() =>
161+
{
162+
byteSource.Adapt<int>(); // throw CompileException, Do not map to another primitive type without registering the configuration
163+
});
164+
165+
Should.NotThrow(() =>
166+
{
167+
TypeAdapterConfig<byte, int>.NewConfig();
168+
169+
byteSource.Adapt<int>(); // Not throw CompileException when config is registering
170+
});
171+
172+
Should.NotThrow(() =>
173+
{
174+
TypeAdapterConfig<Source783, Destination783>.NewConfig()
175+
.Map(dest=> dest.MyProperty, src=> int.Parse(src.MyProperty));
176+
// it work works because int.Parse return Type Int. Type is mapped to itself (int -> int) without config.
177+
178+
var sourceMapconfig = new Source783() { MyProperty = "128" };
179+
var resultMapconfig = sourceMapconfig.Adapt<Destination783>();
180+
181+
resultMapconfig.MyProperty.ShouldBe(128);
182+
});
183+
184+
}
185+
186+
143187
#region TestClasses
188+
189+
public class Source783
190+
{
191+
public string MyProperty { get; set; } = "";
192+
}
144193

194+
public class Destination783
195+
{
196+
public int MyProperty { get; set; }
197+
}
145198

146199
public enum NameEnum
147200
{

0 commit comments

Comments
 (0)