aboutsummaryrefslogtreecommitdiffstats
path: root/tools/SputnikConverter/SputnikTestCase.cs
blob: af9621bc3937b9ff8b03bc1e2640247d8b5ad06d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;
using System.Xml;
using System.Configuration;
using System.Xml.Linq;
using System.Runtime.Serialization;

namespace Microsoft.Sputnik.Interop.ParserEngine
{
    [DataContract]
    public class SputnikTestScript
    {
        private string testScriptID = string.Empty;
        private string testScriptSection = string.Empty;
        private string testScriptDescription = string.Empty;
        private string testScriptAssertion = string.Empty;
        private string replicationCode = string.Empty;
        private int actualFileConvertedCount = 0;
        public bool negativeTest = false;
        private bool strictModeNegativeTest = false;
        private const string xmlNode = "format";
        private const string xmlAttribute = "sequence";
        private string[] checkSections;
        private SortedDictionary<string, string> testScriptFormats = new SortedDictionary<string, string>();

        public string Header = string.Empty;
        public string Body = string.Empty;
        public string InitialComment = string.Empty;
        public string pathFromRoot = string.Empty;

        /// <summary>
        /// Gets or sets the ID.
        /// </summary>
        /// <value>The ID.</value>       
        [DataMember]
        public string id
        {
            get
            {
                return testScriptID;
            }
            set
            {
                if (value != null)
                testScriptID = value.Trim();
            }
        }
        public bool IsNegativeInStrictMode 
        {
            get
            {
                return strictModeNegativeTest;
            }
            set
            {
                strictModeNegativeTest = value;
            }
        }


        /// <summary>
        /// Gets or sets the netative test flag.
        /// </summary>
        /// <value>true if this is a @negative test</value>
        public bool IsNegative
        {
            get
            {
                return negativeTest;
            }
            set
            {
                negativeTest=value;
            }
        }

        /// <summary>
        /// Gets or sets the testScriptSection
        /// </summary>
        [DataMember]
        public string path
        {
            get
            {
                return testScriptSection;
            }
            set
            {
                if(value!=null)
                testScriptSection = value.Trim();
            }
        }

        /// <summary>
        /// Actual number of input files which got converted
        /// </summary>
        public int ConvertedFileCount
        {
            get
            {
                return actualFileConvertedCount;
            }
            set
            {
                actualFileConvertedCount = value;
            }
        }


        /// <summary>
        /// Gets or sets the testScriptDescription
        /// </summary>
        [DataMember]
        public string description
        {
            get
            {
                return testScriptDescription;
            }
            set
            {
                if (value != null)
                testScriptDescription = value.Trim();
            }
        }

        /// <summary>
        /// Gets or sets the testScriptAssersion
        /// </summary>
        [DataMember]
        public string assertion
        {
            get
            {
                return testScriptAssertion;
            }
            set
            {
                if (value != null)
                    testScriptAssertion = value.Trim();
            }
        }

        /// <summary>
        /// Gets or sets the value for checkSections
        /// </summary>
        public string[] Checks
        {
            get
            {
                return checkSections;
            }
            set
            {
                checkSections = value;
            }
        }

        /// <summary>
        /// Gets or sets the full path.
        /// </summary>
        /// <value>The full path.</value>
        public string FullPath { get; set; }

        /// <summary>
        /// Gets or sets the replication code.
        /// </summary>
        /// <value>The replication code.</value>
        public string ReplicationCode
        {
            get
            {
                return replicationCode;
            }
            set 
            {
                if (value != null)
                replicationCode = value.Trim();
            }
        }

        /// <summary>
        /// Gets or sets the possible checks count.
        /// </summary>
        /// <value>The possible checks count.</value>
        public int PossibleChecksCount { get; set; }
        
        /// <summary>
        /// Gets or sets the pre condition code.
        /// </summary>
        /// <value>The pre condition code.</value>
        public string PreConditionCode { get; set; }

        /// <summary>
        /// Constructor which reads the regular expression formats from InputFormats.xml file
        /// </summary>
        public SputnikTestScript()
        {
            ReadInputXml();
        }

        /// <summary>
        /// Reads the regular expression formats from inputformats.xml file
        /// </summary>
        public void ReadInputXml()
        {
            string inputXmlPath = Path.GetFullPath(ConfigurationManager.AppSettings[ResourceClass.InputXMLPath].ToString());
            XmlTextReader reader = new XmlTextReader(inputXmlPath);
            try
            {
                if (File.Exists(inputXmlPath))
                {
                    while (reader.Read())
                    {
                        switch (reader.NodeType)
                        {
                            case XmlNodeType.Element:
                                if (!Convert.ToBoolean(string.Compare(reader.Name.ToLower(), xmlNode)))
                                {
                                    testScriptFormats.Add(reader.GetAttribute(xmlAttribute), reader.ReadString());
                                }
                                break;
                        }
                    }
                }
            }
            catch (XmlException ex)
            {
                Logger.WriteToLog(string.Format(ResourceClass.XMLException_Log, ex.Message));
            }
        }
    
        /// <summary>
        /// Loads the sputnik testscript file and extracts the required details from it
        /// </summary>
        /// <param name="filePath">Path to the source file</param>
        public void Load(string filePath, string root)
        {
            string[] regexTrimDelimiter = { "\n","\r"};            
            String fullFile = string.Empty;

            using (TextReader txtReader = new StreamReader(filePath))
            {
                fullFile = txtReader.ReadToEnd();
            }
            this.FullPath = filePath;
            int indexOfRoot = this.FullPath.IndexOf(root, StringComparison.InvariantCulture) + root.Length + 1;
            this.pathFromRoot = this.FullPath.Substring(indexOfRoot, this.FullPath.Length - indexOfRoot);


            ReadSimpleTestCase(fullFile);
        }

        /// <summary>
        /// Read a basic sputnik test file and create an object for it. Assume just a header comment and a test body
        /// </summary>
        /// <param name="fullFile">Input file content</param>
        private void ReadSimpleTestCase(string fullFile)
        {
            char[] delimiter = { ':' };
            char[] trimDelimit = { ';' ,'\r','\n'};
            string holdStr = string.Empty;
            string[] arrComments;

            Regex commentTailRegex = new Regex("\\s*\\*\\/\\s*");
            Match matchCommentTail = commentTailRegex.Match(fullFile);

            this.Header = fullFile.Substring(0,matchCommentTail.Index+matchCommentTail.Length);

            Regex csharpCommentRegx = new Regex("\\/\\*");
            Match matchCSharpCommentHead = csharpCommentRegx.Match(this.Header);
            this.InitialComment = this.Header.Substring(0, matchCSharpCommentHead.Index);

            this.Body = fullFile.Substring(matchCommentTail.Index+matchCommentTail.Length);

            string commentFormat = "@[a-zA-Z0-9_]+(:\\s*[^\\r\\n]*)?;?\\s*(\\r|\\n)";
            Regex regx = new Regex(commentFormat);
            MatchCollection matchComments = regx.Matches(this.Header);

            foreach (Match comment in matchComments)
            {
                holdStr = comment.Value;
                arrComments = holdStr.Trim(trimDelimit).Trim().Split(delimiter,2);

                string commentKey = arrComments[0].ToLower();
                if (commentKey.Contains(ResourceClass.LookFor_Name))
                {
                    this.id = this.pathFromRoot.Substring(this.pathFromRoot.LastIndexOf("\\") + 1);
                    this.id = GetRealId(this.id.Remove(this.id.Length - 3));
                }
                if (commentKey.Contains(ResourceClass.LookFor_Section))
                {
                    this.path = GetRealSectionName(this.pathFromRoot);
                }
                if (commentKey.Contains(ResourceClass.LookFor_Assertion))
                {
                    this.assertion = arrComments[arrComments.Length - 1].Trim(trimDelimit);
                }
                if (commentKey.Contains(ResourceClass.LookFor_Description))
                {
                    this.description = arrComments[arrComments.Length - 1].Trim(trimDelimit);
                }
                if (commentKey.Contains(ResourceClass.LookFor_NegativeStrictMode))
                {
                    this.IsNegativeInStrictMode = true;
                }
                if (commentKey.Contains(ResourceClass.LookFor_Negative))
                {
                    if (!arrComments[0].Contains(ResourceClass.LookFor_NegativeStrictMode)) this.IsNegative = true;
                }
            }


            this.PossibleChecksCount = 1;
        }

        

        private static string GetRealId(string id)
        {
            Regex regx = new Regex("^ S([0-9]+)_");
            return regx.Replace(id, " S$1.0_", 1);
        }
        private static string GetRealSectionName(string sectionName)
        {
            Regex regx = new Regex("^ ([0-9]+)$");
            if (! regx.IsMatch(sectionName)) {
                return sectionName;
            }

            return regx.Replace(sectionName, " $1.0", 1);
        }
    }
}