Skip to content

Commit 7ebe922

Browse files
committed
Added a unit test to verify the correctness of embedding of anonymous type instance
1 parent cb90a82 commit 7ebe922

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

test/JavaScriptEngineSwitcher.Tests/InteropTestsBase.cs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,70 @@ public virtual void EmbeddingOfInstanceOfCustomReferenceTypeWithPropertiesIsCorr
241241
Assert.Equal(targetOutput2, output2);
242242
}
243243

244+
[Fact]
245+
public virtual void EmbeddingOfInstanceOfAnonymousTypeWithPropertiesIsCorrect()
246+
{
247+
// Arrange
248+
var person = new
249+
{
250+
FirstName = "John",
251+
LastName = "Doe",
252+
Address = new
253+
{
254+
StreetAddress = "103 Elm Street",
255+
City = "Atlanta",
256+
State = "GA",
257+
PostalCode = 30339
258+
}
259+
};
260+
261+
const string input1 = "person.FirstName";
262+
const string targetOutput1 = "John";
263+
264+
const string input2 = "person.LastName";
265+
const string targetOutput2 = "Doe";
266+
267+
const string input3 = "person.Address.StreetAddress";
268+
const string targetOutput3 = "103 Elm Street";
269+
270+
const string input4 = "person.Address.City";
271+
const string targetOutput4 = "Atlanta";
272+
273+
const string input5 = "person.Address.State";
274+
const string targetOutput5 = "GA";
275+
276+
const string input6 = "person.Address.PostalCode";
277+
const int targetOutput6 = 30339;
278+
279+
// Act
280+
string output1;
281+
string output2;
282+
string output3;
283+
string output4;
284+
string output5;
285+
int output6;
286+
287+
using (var jsEngine = CreateJsEngine())
288+
{
289+
jsEngine.EmbedHostObject("person", person);
290+
291+
output1 = jsEngine.Evaluate<string>(input1);
292+
output2 = jsEngine.Evaluate<string>(input2);
293+
output3 = jsEngine.Evaluate<string>(input3);
294+
output4 = jsEngine.Evaluate<string>(input4);
295+
output5 = jsEngine.Evaluate<string>(input5);
296+
output6 = jsEngine.Evaluate<int>(input6);
297+
}
298+
299+
// Assert
300+
Assert.Equal(targetOutput1, output1);
301+
Assert.Equal(targetOutput2, output2);
302+
Assert.Equal(targetOutput3, output3);
303+
Assert.Equal(targetOutput4, output4);
304+
Assert.Equal(targetOutput5, output5);
305+
Assert.Equal(targetOutput6, output6);
306+
}
307+
244308
#endregion
245309

246310
#region Objects with methods

test/JavaScriptEngineSwitcher.Tests/Jurassic/InteropTests.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,17 @@ protected override string EngineName
88
get { return "JurassicJsEngine"; }
99
}
1010

11+
#region Embedding of objects
12+
13+
#region Objects with properties
14+
15+
public override void EmbeddingOfInstanceOfAnonymousTypeWithPropertiesIsCorrect()
16+
{ }
17+
18+
#endregion
19+
20+
#endregion
21+
1122
#region Embedding of types
1223

1324
#region Types with constants

0 commit comments

Comments
 (0)