Skip to content

Commit 381b81f

Browse files
committed
Added a unit test to verify the correctness of embedding of anonymous type instance
1 parent 6774432 commit 381b81f

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
@@ -240,6 +240,70 @@ public virtual void EmbeddingOfInstanceOfCustomReferenceTypeWithPropertiesIsCorr
240240
Assert.Equal(targetOutput2, output2);
241241
}
242242

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

245309
#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)