1
+ using NUnit . Framework ;
2
+
3
+ namespace Noesis . Javascript . Tests
4
+ {
5
+ [ TestFixture ]
6
+ public class AccessorInterceptorTests
7
+ {
8
+ private JavascriptContext _context ;
9
+
10
+ [ SetUp ]
11
+ public void SetUp ( )
12
+ {
13
+ _context = new JavascriptContext ( ) ;
14
+ }
15
+
16
+ [ TearDown ]
17
+ public void TearDown ( )
18
+ {
19
+ _context . Dispose ( ) ;
20
+ }
21
+
22
+ [ Test ]
23
+ public void AccessAnElementInAManagedArray ( )
24
+ {
25
+ int [ ] myArray = new int [ ] { 151515 , 666 , 2555 , 888 , 99 } ;
26
+ _context . SetParameter ( "myArray" , myArray ) ;
27
+
28
+ Assert . That ( _context . Run ( "myArray[2] == 2555" ) , Is . True ) ;
29
+ }
30
+
31
+ class ClassWithIndexer
32
+ {
33
+ public int Index { get ; set ; }
34
+ public string Value { get ; set ; }
35
+
36
+ public string this [ int iIndex ]
37
+ {
38
+ get { return ( Value + " " + iIndex ) ; }
39
+ set {
40
+ Value = value ;
41
+ Index = iIndex ;
42
+ }
43
+ }
44
+ }
45
+
46
+ [ Test ]
47
+ public void AccessingByIndexAPropertyInAManagedObject ( )
48
+ {
49
+ _context . SetParameter ( "myObject" , new ClassWithIndexer { Value = "Value" } ) ;
50
+
51
+ Assert . That ( _context . Run ( "myObject[99] == 'Value 99'" ) , Is . True ) ;
52
+ }
53
+
54
+ class ClassWithProperty
55
+ {
56
+ public string MyProperty { get ; set ; }
57
+ }
58
+
59
+ [ Test ]
60
+ public void AccessingByNameAPropertyInManagedObject ( )
61
+ {
62
+ _context . SetParameter ( "myObject" , new ClassWithProperty { MyProperty = "This is the string return by \" MyProperty\" " } ) ;
63
+
64
+ Assert . That ( _context . Run ( "myObject.MyProperty == 'This is the string return by \" MyProperty\" '" ) , Is . True ) ;
65
+ }
66
+
67
+ [ Test ]
68
+ public void SetValueByIndexAValueInNetArray ( )
69
+ {
70
+ int [ ] ints = new [ ] { 1 , 2 , 3 } ;
71
+ _context . SetParameter ( "myArray" , ints ) ;
72
+
73
+ _context . Run ( "myArray[1] = 17" ) ;
74
+
75
+ Assert . That ( ints [ 1 ] , Is . EqualTo ( 17 ) ) ;
76
+ }
77
+
78
+ [ Test ]
79
+ public void SetValueByIndexerInManagedObject ( )
80
+ {
81
+ var classWithIndexer = new ClassWithIndexer ( ) ;
82
+ _context . SetParameter ( "myObject" , classWithIndexer ) ;
83
+
84
+ _context . Run ( "myObject[20] = 'The value is now set'" ) ;
85
+
86
+ Assert . That ( classWithIndexer . Value , Is . EqualTo ( "The value is now set" ) ) ;
87
+ Assert . That ( classWithIndexer . Index , Is . EqualTo ( 20 ) ) ;
88
+ }
89
+
90
+ [ Test ]
91
+ public void SetPropertyByNameInManagedObject ( )
92
+ {
93
+ var classWithProperty = new ClassWithProperty ( ) ;
94
+ _context . SetParameter ( "myObject" , classWithProperty ) ;
95
+
96
+ _context . Run ( "myObject.MyProperty = 'hello'" ) ;
97
+
98
+ Assert . That ( classWithProperty . MyProperty , Is . EqualTo ( "hello" ) ) ;
99
+ }
100
+
101
+ [ Test ]
102
+ public void SettingUnknownPropertiesIsAllowed ( )
103
+ {
104
+ _context . SetParameter ( "myObject" , new ClassWithProperty ( ) ) ;
105
+
106
+ _context . Run ( "myObject.UnknownProperty = 77" ) ;
107
+
108
+ Assert . That ( _context . Run ( "myObject.UnknownProperty" ) , Is . EqualTo ( 77 ) ) ;
109
+ }
110
+
111
+ [ Test ]
112
+ public void SettingUnknownPropertiesIsDisallowedIfRejectUnknownPropertiesIsSet ( )
113
+ {
114
+ _context . SetParameter ( "myObject" , new ClassWithProperty ( ) , SetParameterOptions . RejectUnknownProperties ) ;
115
+
116
+ Assert . Throws < JavascriptException > ( ( ) => _context . Run ( "myObject.UnknownProperty = 77" ) ) ;
117
+ }
118
+ }
119
+ }
0 commit comments