@@ -88,6 +88,116 @@ describe("XML Patcher", () => {
8888 } ) ;
8989 } ) ;
9090
91+ describe ( "Array operation tests" , ( ) => {
92+ var source : string ;
93+
94+ beforeEach ( function ( ) {
95+ source = '<rootNode><leaf color="#aaaaaa"/><leaf color="#000000"/></rootNode>' ;
96+ } ) ;
97+
98+ describe ( "Add" , ( ) => {
99+ it ( ": should prepend" , ( ) => {
100+ var patcher = new xmlatcher . XmlPatcher ( [
101+ {
102+ op : "add" , path : "/rootNode/0" , value : "leaf"
103+ } , {
104+ op : "replace" , path : "/rootNode/leaf:first()/@color" , value : "#bbbbbb"
105+ }
106+ ] , { } ) ;
107+ var result = patcher . apply ( source ) ;
108+
109+ expect ( result ) . toEqual ( '<rootNode><leaf color="#bbbbbb"/><leaf color="#aaaaaa"/><leaf color="#000000"/></rootNode>' ) ;
110+ } ) ;
111+
112+ it ( ": should add at index" , ( ) => {
113+ var patcher = new xmlatcher . XmlPatcher ( [
114+ {
115+ op : "add" , path : "/rootNode/1" , value : "leaf"
116+ } , {
117+ op : "replace" , path : "/rootNode/leaf[1]/@color" , value : "#bbbbbb"
118+ }
119+ ] , { } ) ;
120+ var result = patcher . apply ( source ) ;
121+
122+ expect ( result ) . toEqual ( '<rootNode><leaf color="#aaaaaa"/><leaf color="#bbbbbb"/><leaf color="#000000"/></rootNode>' ) ;
123+ } ) ;
124+
125+ it ( ": should append" , ( ) => {
126+ var patcher = new xmlatcher . XmlPatcher ( [
127+ {
128+ op : "add" , path : "/rootNode/-" , value : "leaf"
129+ } , {
130+ op : "replace" , path : "/rootNode/leaf:last()/@color" , value : "#bbbbbb"
131+ }
132+ ] , { } ) ;
133+ var result = patcher . apply ( source ) ;
134+
135+ expect ( result ) . toEqual ( '<rootNode><leaf color="#aaaaaa"><leaf color="#000000"/><leaf color="#bbbbbb"/></rootNode>' ) ;
136+ } ) ;
137+ } ) ;
138+
139+ describe ( "Replace" , ( ) => {
140+ it ( ": replace at index" , ( ) => {
141+ var patcher = new xmlatcher . XmlPatcher ( [
142+ {
143+ op : "replace" , path : "/rootNode/1" , value : "otherleaf"
144+ }
145+ ] , { } ) ;
146+ var result = patcher . apply ( source ) ;
147+
148+ expect ( result ) . toEqual ( '<rootNode><leaf color="#aaaaaa"/><otherleaf/></rootNode>' ) ;
149+ } ) ;
150+ } ) ;
151+
152+ describe ( "Move" , ( ) => {
153+ it ( ": move at index" , ( ) => {
154+ var patcher = new xmlatcher . XmlPatcher ( [
155+ {
156+ op : "move" , from : "/rootNode/0" , path : "/rootNode/1" , value : "otherleaf"
157+ }
158+ ] , { } ) ;
159+ var result = patcher . apply ( source ) ;
160+
161+ expect ( result ) . toEqual ( '<rootNode><leaf color="#000000"/><leaf color="#aaaaaa"/></rootNode>' ) ;
162+ } ) ;
163+ } ) ;
164+
165+ describe ( "Delete" , ( ) => {
166+ it ( ": delete at index" , ( ) => {
167+ var patcher = new xmlatcher . XmlPatcher ( [
168+ {
169+ op : "delete" , path : "/rootNode/1"
170+ }
171+ ] , { } ) ;
172+ var result = patcher . apply ( source ) ;
173+
174+ expect ( result ) . toEqual ( '<rootNode><leaf color="#000000"/></rootNode>' ) ;
175+ } ) ;
176+
177+ it ( ": delete first" , ( ) => {
178+ var patcher = new xmlatcher . XmlPatcher ( [
179+ {
180+ op : "delete" , path : "/rootNode/0"
181+ }
182+ ] , { } ) ;
183+ var result = patcher . apply ( source ) ;
184+
185+ expect ( result ) . toEqual ( '<rootNode><leaf color="#aaaaaa"/></rootNode>' ) ;
186+ } ) ;
187+
188+ it ( ": delete last" , ( ) => {
189+ var patcher = new xmlatcher . XmlPatcher ( [
190+ {
191+ op : "delete" , path : "/rootNode/-"
192+ }
193+ ] , { } ) ;
194+ var result = patcher . apply ( source ) ;
195+
196+ expect ( result ) . toEqual ( '<rootNode><leaf color="#000000"/></rootNode>' ) ;
197+ } ) ;
198+ } ) ;
199+ } ) ;
200+
91201 describe ( "Regression tests" , ( ) => {
92202 it ( ": test #22 find by attribute value is working properly" , ( ) => {
93203 var source = '<?xml version="1.0" encoding="utf-8"?> \
0 commit comments