~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/mono-addins/Test/UnitTests/TestConditions.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
using System;
 
3
using System.Collections;
 
4
using NUnit.Framework;
 
5
using Mono.Addins;
 
6
using SimpleApp;
 
7
 
 
8
namespace UnitTests
 
9
{
 
10
        [TestFixture()]
 
11
        public class TestConditions: TestBase
 
12
        {
 
13
                ParameterInfoCondition pinfo;
 
14
                ExtensionContext ctx;
 
15
                Hashtable added;
 
16
                Hashtable removed;
 
17
                Hashtable oldwriters;
 
18
                
 
19
                public override void Setup ()
 
20
                {
 
21
                        base.Setup ();
 
22
                        
 
23
                        pinfo = new ParameterInfoCondition ();
 
24
                        pinfo.Value = "res";
 
25
                        GlobalInfoCondition.Value = "res";
 
26
                        
 
27
                        ctx = AddinManager.CreateExtensionContext ();
 
28
                        ctx.RegisterCondition ("InputParameter", pinfo);
 
29
                }
 
30
                
 
31
                void StartListenerCheck (string tid)
 
32
                {
 
33
                        // Get the current writers
 
34
                        
 
35
                        added = new Hashtable ();
 
36
                        removed = new Hashtable ();
 
37
                        
 
38
                        ctx.AddExtensionNodeHandler ("/SimpleApp/ExtraWriters", OnExtensionAddRemove);
 
39
                        Assert.AreEqual (0, removed.Count, tid + ": RegisterExtensionListener should not remove");
 
40
                        
 
41
                        oldwriters = added;
 
42
                        added = new Hashtable ();
 
43
                }
 
44
                
 
45
                void EndListenerCheck (string tid)
 
46
                {
 
47
                        IWriter[] writers = (IWriter[]) ctx.GetExtensionObjects ("/SimpleApp/ExtraWriters", typeof(IWriter));
 
48
                        
 
49
                        Hashtable newwriters = new Hashtable ();
 
50
                        for (int n=0; n<writers.Length; n++) {
 
51
                                string nwrit = writers[n].Id;
 
52
                                
 
53
                                // Check added events
 
54
                                if (oldwriters.Contains (nwrit)) {
 
55
                                        Assert.IsFalse (added.Contains (nwrit), tid + ": incorrect Add event for node: " + writers[n].Write ());
 
56
                                        Assert.IsFalse (removed.Contains (nwrit), tid + ": incorrect Remove event for node: " + writers[n].Write ());
 
57
                                } else {
 
58
                                        Assert.IsTrue (added.Contains (nwrit), tid + ": Add event not sent for node: " + writers[n].Write ());
 
59
                                }
 
60
                                newwriters [nwrit] = nwrit;
 
61
                        }
 
62
                        
 
63
                        // Check remove events
 
64
                        foreach (string old in oldwriters.Keys) {
 
65
                                if (!newwriters.Contains (old))
 
66
                                        Assert.IsTrue (removed.Contains (old), tid + ": Remove event not sent for node: " + old);
 
67
                        }
 
68
                        
 
69
                        ctx.RemoveExtensionNodeHandler ("/SimpleApp/ExtraWriters", OnExtensionAddRemove);
 
70
                }
 
71
                
 
72
                void CheckWriters (string tid, string gval, string pval, params string[] result)
 
73
                {
 
74
                        // Do the change
 
75
                        
 
76
                        StartListenerCheck (tid + " set InputParameter");
 
77
                        pinfo.Value = pval;
 
78
                        EndListenerCheck (tid + " set InputParameter");
 
79
                        
 
80
                        StartListenerCheck (tid + " set GlobalInfo");
 
81
                        GlobalInfoCondition.Value = gval;
 
82
                        EndListenerCheck (tid + " set GlobalInfo");
 
83
                        
 
84
                        // Get the new writers
 
85
                        
 
86
                        IWriter[] writers = (IWriter[]) ctx.GetExtensionObjects ("/SimpleApp/ExtraWriters", typeof(IWriter));
 
87
                        Assert.AreEqual (result.Length, writers.Length, tid + ": result count");
 
88
                        
 
89
                        for (int n=0; n<result.Length; n++) {
 
90
                                string nwrit = writers[n].Write ();
 
91
                                Assert.AreEqual (result[n], nwrit, tid + ": result #" + n);
 
92
                        }
 
93
                }
 
94
                
 
95
                void OnExtensionAddRemove (object s, ExtensionNodeEventArgs args)
 
96
                {
 
97
                        IWriter w = (IWriter) ((TypeExtensionNode)args.ExtensionNode).CreateInstance ();
 
98
                        if (args.Change == ExtensionChange.Add)
 
99
                                added [w.Id] = w;
 
100
                        else
 
101
                                removed [w.Id] = w;
 
102
                }
 
103
                
 
104
                [Test()]
 
105
                public void TestAllFalse ()
 
106
                {
 
107
                        // All conditions evaluate to false
 
108
                        CheckWriters (
 
109
                                "t1",
 
110
                                "", "",
 
111
                                "cmd:ca1",
 
112
                                "cmd:ca2"
 
113
                        );
 
114
                }
 
115
                        
 
116
                [Test()]
 
117
                public void TestSimpleCondition ()
 
118
                {
 
119
                        // Simple condition is true
 
120
                        
 
121
                        CheckWriters (
 
122
                                "t1",
 
123
                                "no", "",
 
124
                                "cmd:ca1",
 
125
                                "cmd:ca2",
 
126
                                "cmd:cn1",
 
127
                                "cmd:cn2",
 
128
                                "cmd:cn3"
 
129
                        );
 
130
                }
 
131
                        
 
132
                [Test()]
 
133
                public void TestOr ()
 
134
                {
 
135
                        string[] istrue = new string [] { "cmd:ca1", "cmd:ca2", "cmd:c1 x or y or yes", "cmd:c2 x or y or yes" };
 
136
                        
 
137
                        CheckWriters ("t1", "", "x", istrue);
 
138
                        
 
139
                        CheckWriters ("t2", "", "y", istrue);
 
140
                        
 
141
                        CheckWriters ("t3", "yes", "", istrue);
 
142
                        
 
143
                        CheckWriters ("t3", "yes", "x", istrue);
 
144
                        
 
145
                        CheckWriters ("t3", "yes", "y", istrue);
 
146
                }
 
147
 
 
148
                [Test()]
 
149
                public void TestAnd ()
 
150
                {
 
151
                        string[] isfalse = new string [] { "cmd:ca1", "cmd:ca2" };
 
152
                        string[] istrue = new string [] { "cmd:ca1", "cmd:ca2", "cmd:c3 x1 and yes1", "cmd:c4 x1 and yes1" };
 
153
                        
 
154
                        CheckWriters ("t1", "", "x1", isfalse);
 
155
                        
 
156
                        CheckWriters ("t2", "yes1", "", isfalse);
 
157
                        
 
158
                        CheckWriters ("t3", "yes1", "x1", istrue);
 
159
                }
 
160
 
 
161
                [Test()]
 
162
                public void NestedTestOrAnd ()
 
163
                {
 
164
                        string[] isfalse = new string [] { "cmd:ca1", "cmd:ca2" };
 
165
                        string[] istrue = new string [] { "cmd:ca1", "cmd:ca2", "cmd:cc5", "cmd:cc6" };
 
166
                        
 
167
                        // First or
 
168
                        
 
169
                        CheckWriters ("t1", "", "nx", isfalse);
 
170
                        
 
171
                        CheckWriters ("t2", "nx1", "nx", istrue);
 
172
                        
 
173
                        CheckWriters ("t3", "nx2", "nx", istrue);
 
174
                        
 
175
                        CheckWriters ("t4", "nx1", "", isfalse);
 
176
                        
 
177
                        CheckWriters ("t5", "nx2", "", isfalse);
 
178
                        
 
179
                        // Second or
 
180
                        
 
181
                        CheckWriters ("t6", "", "ny", isfalse);
 
182
                        
 
183
                        CheckWriters ("t7", "ny1", "ny", istrue);
 
184
                        
 
185
                        CheckWriters ("t8", "ny2", "ny", istrue);
 
186
                        
 
187
                        CheckWriters ("t9", "ny1", "", isfalse);
 
188
                        
 
189
                        CheckWriters ("t10", "ny2", "", isfalse);
 
190
                }
 
191
                
 
192
                [Test()]
 
193
                public void InnerCondition ()
 
194
                {
 
195
                        CheckWriters ("t1", "", "ines1", "cmd:ca1", "cmd:ca2");
 
196
                        
 
197
                        CheckWriters ("t2", "cnes", "", "cmd:ca1", "cmd:ca2", "cmd:cnes1", "cmd:cnes2");
 
198
                        
 
199
                        CheckWriters ("t3", "cnes", "ines1", "cmd:ca1", "cmd:ca2", "cmd:cnes1", "cmd:cnes2", "cmd:ines1", "cmd:ines2");
 
200
                }
 
201
                
 
202
                [Test()]
 
203
                public void InnerOrCondition ()
 
204
                {
 
205
                        CheckWriters ("t1", "", "inesOr", "cmd:ca1", "cmd:ca2");
 
206
                        
 
207
                        CheckWriters ("t2", "cnesOr", "", "cmd:ca1", "cmd:ca2", "cmd:cnesOr1", "cmd:cnesOr2");
 
208
                        
 
209
                        CheckWriters ("t3", "cnesOr", "inesOr", "cmd:ca1", "cmd:ca2", "cmd:cnesOr1", "cmd:cnesOr2", "cmd:inesOr1", "cmd:inesOr2");
 
210
                }
 
211
                
 
212
                [Test()]
 
213
                public void InnerAndCondition ()
 
214
                {
 
215
                        CheckWriters ("t1", "", "inesAnd", "cmd:ca1", "cmd:ca2");
 
216
                        
 
217
                        CheckWriters ("t2", "cnesAnd", "", "cmd:ca1", "cmd:ca2", "cmd:cnesAnd1", "cmd:cnesAnd2");
 
218
                        
 
219
                        CheckWriters ("t3", "cnesAnd", "inesAnd", "cmd:ca1", "cmd:ca2", "cmd:cnesAnd1", "cmd:cnesAnd2", "cmd:inesAnd1", "cmd:inesAnd2");
 
220
                }
 
221
                
 
222
                [Test()]
 
223
                public void ChildNodeConditions ()
 
224
                {
 
225
                        CheckWriters ("t1", "testChildren", "tc1", "cmd:ca1", "cmd:ca2", "file:someFile1[child1][child2]");
 
226
                        
 
227
                        pinfo.Value = "tc2";
 
228
                        GlobalInfoCondition.Value = "testChildren";
 
229
                        
 
230
                        string[] result = new string [] {"cmd:ca1", "cmd:ca2", "file:someFile1[child1][child3]"};
 
231
                        
 
232
                        IWriter[] writers = (IWriter[]) ctx.GetExtensionObjects ("/SimpleApp/ExtraWriters", typeof(IWriter));
 
233
                        Assert.AreEqual (result.Length, writers.Length, "t2: result count");
 
234
                        for (int n=0; n<result.Length; n++) {
 
235
                                string nwrit = writers[n].Write ();
 
236
                                Assert.AreEqual (result[n], nwrit, "t2: result #" + n);
 
237
                        }
 
238
                }
 
239
        }
 
240
}