55using Xunit ;
66
77using JavaScriptEngineSwitcher . Core ;
8+ using JavaScriptEngineSwitcher . Jint ;
89
910using JavaScriptEngineSwitcher . Tests . Interop ;
1011using JavaScriptEngineSwitcher . Tests . Interop . Animals ;
@@ -24,31 +25,51 @@ protected override string EngineName
2425 #region Objects with methods
2526
2627 [ Fact ]
27- public override void EmbeddingOfInstanceOfCustomReferenceTypeAndCallingOfItsGetTypeMethod ( )
28+ public override void EmbeddingOfInstanceOfCustomValueTypeAndCallingOfItsGetTypeMethod ( )
2829 {
2930 // Arrange
30- var cat = new Cat ( ) ;
31+ static string TestAllowReflectionSetting ( bool allowReflection )
32+ {
33+ var date = new Date ( ) ;
3134
32- const string input = "cat .GetType();" ;
35+ const string input = "date .GetType();" ;
3336
34- // Act
35- JsRuntimeException exception = null ;
37+ using ( var jsEngine = new JintJsEngine ( new JintSettings { AllowReflection = allowReflection } ) )
38+ {
39+ jsEngine . EmbedHostObject ( "date" , date ) ;
40+ return jsEngine . Evaluate < string > ( input ) ;
41+ }
42+ }
3643
37- using ( var jsEngine = CreateJsEngine ( ) )
44+ // Act and Assert
45+ Assert . Equal ( typeof ( Date ) . FullName , TestAllowReflectionSetting ( true ) ) ;
46+
47+ var exception = Assert . Throws < JsRuntimeException > ( ( ) => TestAllowReflectionSetting ( false ) ) ;
48+ Assert . Equal ( "Runtime error" , exception . Category ) ;
49+ Assert . Equal ( "Property 'GetType' of object is not a function" , exception . Description ) ;
50+ }
51+
52+ [ Fact ]
53+ public override void EmbeddingOfInstanceOfCustomReferenceTypeAndCallingOfItsGetTypeMethod ( )
54+ {
55+ // Arrange
56+ static string TestAllowReflectionSetting ( bool allowReflection )
3857 {
39- try
58+ var cat = new Cat ( ) ;
59+
60+ const string input = "cat.GetType();" ;
61+
62+ using ( var jsEngine = new JintJsEngine ( new JintSettings { AllowReflection = allowReflection } ) )
4063 {
4164 jsEngine . EmbedHostObject ( "cat" , cat ) ;
42- jsEngine . Evaluate < string > ( input ) ;
43- }
44- catch ( JsRuntimeException e )
45- {
46- exception = e ;
65+ return jsEngine . Evaluate < string > ( input ) ;
4766 }
4867 }
4968
50- // Assert
51- Assert . NotNull ( exception ) ;
69+ // Act and Assert
70+ Assert . Equal ( typeof ( Cat ) . FullName , TestAllowReflectionSetting ( true ) ) ;
71+
72+ var exception = Assert . Throws < JsRuntimeException > ( ( ) => TestAllowReflectionSetting ( false ) ) ;
5273 Assert . Equal ( "Runtime error" , exception . Category ) ;
5374 Assert . Equal ( "Property 'GetType' of object is not a function" , exception . Description ) ;
5475 }
@@ -343,28 +364,23 @@ public void MappingHostErrorDuringRecursiveExecutionOfFiles()
343364 public override void CreatingAnInstanceOfEmbeddedCustomExceptionAndCallingOfItsGetTypeMethod ( )
344365 {
345366 // Arrange
346- Type loginFailedExceptionType = typeof ( LoginFailedException ) ;
347-
348- const string input = "new LoginFailedError( \" Wrong password entered! \" ).GetType();" ;
367+ static string TestAllowReflectionSetting ( bool allowReflection )
368+ {
369+ Type loginFailedExceptionType = typeof ( LoginFailedException ) ;
349370
350- // Act
351- JsRuntimeException exception = null ;
371+ const string input = "new LoginFailedError(\" Wrong password entered!\" ).GetType();" ;
352372
353- using ( var jsEngine = CreateJsEngine ( ) )
354- {
355- try
373+ using ( var jsEngine = new JintJsEngine ( new JintSettings { AllowReflection = allowReflection } ) )
356374 {
357375 jsEngine . EmbedHostType ( "LoginFailedError" , loginFailedExceptionType ) ;
358- jsEngine . Evaluate < string > ( input ) ;
359- }
360- catch ( JsRuntimeException e )
361- {
362- exception = e ;
376+ return jsEngine . Evaluate < string > ( input ) ;
363377 }
364378 }
365379
366- // Assert
367- Assert . NotNull ( exception ) ;
380+ // Act and Assert
381+ Assert . Equal ( typeof ( LoginFailedException ) . FullName , TestAllowReflectionSetting ( true ) ) ;
382+
383+ var exception = Assert . Throws < JsRuntimeException > ( ( ) => TestAllowReflectionSetting ( false ) ) ;
368384 Assert . Equal ( "Runtime error" , exception . Category ) ;
369385 Assert . Equal ( "Property 'GetType' of object is not a function" , exception . Description ) ;
370386 }
0 commit comments