chr
2026-04-05 fe750b791d5b517cc4e9bc8e99a9a75139a0cfba
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
//            Copyright Keysight Technologies 2012-2019
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, you can obtain one at http://mozilla.org/MPL/2.0/.
using System;
using System.Collections.Generic;
using System.Linq;
 
namespace OpenTap
{
    
    /// <summary>
    /// Converts a macro to its expanded string format. 
    /// </summary>
    class MacroExpansion
    {
        /// <summary>
        /// Name of macro.
        /// </summary>
        public string MacroName { get; set; }
        /// <summary>
        /// Description read from attribute.
        /// </summary>
        public string Description { get; set; }
        /// <summary>
        /// String to identify where the macro comes from. 
        /// </summary>
        public string Origin { get; set; }
    }
    
    /// <summary> a string that can be expanded with macros.</summary>
    public class MacroString
    {
        /// <summary> Optional context for macro strings that refers to a test step. This is used to find additional macro definitions such as TestPlanDir.</summary>
        public ITestStepParent Context { get; set; }
 
        string text = "";
 
        /// <summary> The text that can be expanded. </summary>
        public string Text
        {
            get { return text; }
            set
            {
                if (value == null)
                    throw new ArgumentNullException("value");
                if (text == value) return;
                text = value;
            }
        }
        
        /// <summary> Creates a new instance of MacroString with a context. </summary>
        /// <param name="context"></param>
        public MacroString(ITestStepParent context)
        {
            Context = context;
        }
 
        /// <summary> Creates a new MacroString without a context. If a TestStep is used use that as context to get access to TestPlan related macros.</summary>
        public MacroString()
        {
 
        }
 
        /// <summary> Expands the text. Macros are harvested from the optional TestPlanRun or the test step.</summary>
        /// <param name="run">A place to find additional metadata for macro expansion.</param>
        /// <param name="date">If no date was found in the metadata, this date will be used. If date is not supplied, DateTime.Now will be used.</param>
        /// <param name="testPlanDir">If no TestPlanDir was found in the metata, this TestPlanDir will be used.</param>
        /// <returns>The expanded string.</returns>
        public string Expand(TestPlanRun run = null, DateTime? date = null, string testPlanDir = null)
        {
            return Expand(run, date, testPlanDir, null);
        }
        
        /// <summary> Expands the text. Macros are harvested from the optional TestPlanRun or the test step.</summary>
        /// <param name="run">A place to find additional metadata for macro expansion.</param>
        /// <param name="date">If no date was found in the metadata, this date will be used. If date is not supplied, DateTime.Now will be used.</param>
        /// <param name="testPlanDir">If no TestPlanDir was found in the metata, this TestPlanDir will be used.</param>
        /// <param name="replacements">Overrides other macro parameters.</param>
        /// <returns>The expanded string.</returns>
        public string Expand(TestPlanRun run, DateTime? date, string testPlanDir, Dictionary<string, object> replacements)
        {
            ITestStepParent context = Context;
            
            IEnumerable<(string, object)> getMacro()
            {
                // note: macros are case-insensitive.
 
                if(testPlanDir != null)
                    yield return ("TestPlanDir", testPlanDir);
 
                if (date != null)
                    yield return ("date", date);
                
                if(replacements != null)
                    foreach (var elem in replacements)
                        yield return (elem.Key, elem.Value);
 
                if (run != null) {
                    var runparams = run.Parameters.Concat(ResultParameters.GetMetadataFromObject(run)).Where(y => y.IsMetaData);
                    
                    foreach(var v in runparams)
                    {
                        var path = v.Value;
                        yield return (v.Name, path);
                        yield return (v.MacroName, path);
                    }
                }
                
                ITestStepParent ctx = context;
                while (ctx != null)
                {
                    var p = ResultParameters.GetMetadataFromObject(ctx);
                    foreach (var v in p)
                    {
                        if (v.IsMetaData == false)
                            continue;
                        var path = v.Value;
                        yield return (v.Name, path);
                        yield return (v.MacroName, path);
                    }
                    ctx = ctx.Parent;
                }
                yield return ("date", DateTime.Now);
                yield return ("Verdict", Verdict.NotSet);
 
                var met = ResultParameters.GetComponentSettingsMetadataLazy(false);
                
                foreach (var ps in met)
                {
                    foreach (var v in ps)
                    {
                        if (v.IsMetaData == false) continue;
 
                        var path = v.Value;
                        yield return (v.Name, path);
                        yield return (v.MacroName, path);
                    }
                }
 
                
            }
            
            return ReplaceMacros(Text, getMacro().Select(x => (x.Item1, StringConvertProvider.GetString(x.Item2))));
        }
 
        /// <summary> Expands the text.</summary>
        public override string ToString()
        {
            return Expand(date: DateTime.Now);
        }
        
        /// <summary> Implicit to string conversion that expands the text of the macroString. This makes it possible to seamlessly switch between string and MacroString in implementation.</summary>
        public static implicit operator string(MacroString macroString)
        {
            return macroString.Expand();
        }
        /// <summary>
        /// Replaces macro strings with the strings in the macroDef dictionary.
        /// If the macro name does not exist in the expanders dictionary.
        /// </summary>
        /// <param name="userString">The string to replace macros in.</param>
        /// <param name="keyvaluepairs">The macro definitions.</param>
        /// <param name="macroDefault">Default value if MacroName is not in macroDef.</param>
        /// <returns>A string with macros expanded.</returns>
        internal static string ReplaceMacros(string userString, IEnumerable<(string,string)> keyvaluepairs, string macroDefault = "TBD")
        {
            var cmp = StringComparer.OrdinalIgnoreCase;
            Dictionary<string, string> cache = new Dictionary<string, string>(cmp);
            IEnumerator<(string, string)> valueiterator = keyvaluepairs.GetEnumerator();
            string getMacroDef(string key)
            {
                if (cache.TryGetValue(key, out string val))
                    return val;
                while(valueiterator != null)
                {
                    if (valueiterator.MoveNext() == false)
                        return macroDefault;
                    var kv = valueiterator.Current;
 
                    if (kv.Item1 == null) continue;
                    if (cache.ContainsKey(kv.Item1))
                        continue;
                    cache[kv.Item1] = kv.Item2;
                    if (cmp.Compare(kv.Item1, key) == 0)
                        return kv.Item2;
                }
                return macroDefault;
            }
            List<macroLocation> macroLocations = macroLocation.GetMacroLocations(userString);
            int removed = 0;
            foreach (var mloc in macroLocations)
            {
 
                int taglen = mloc.MacroTagLength;
                string inserted = getMacroDef(mloc.MacroName);
 
                userString = userString.Remove(mloc.MacroTagBegin - removed, mloc.MacroTagLength);
                userString = userString.Insert(mloc.MacroTagBegin - removed, inserted);
                removed += taglen - inserted.Length;
            }
            if(userString.Contains('%'))
                userString = Environment.ExpandEnvironmentVariables(userString);
 
            return userString;
        }
 
        /// <summary>
        /// To keep track of a macro in the user string.
        /// </summary>
        class macroLocation
        {
            /// <summary>
            /// Name of the macro.
            /// </summary>
            public readonly string MacroName;
 
            /// <summary>
            /// Index of the first character of the macro.
            /// </summary>
            public readonly int MacroBegin;
 
            /// <summary>
            /// The location of the first macro delimiter.
            /// </summary>
            public int MacroTagBegin
            {
                get
                {
                    return MacroBegin - 1;
                }
            }
 
            /// <summary>
            /// Location of the last macro delimiter.
            /// </summary>
            public readonly int MacroEnd;
 
            public int MacroTagEnd
            {
                get { return MacroEnd + 1; }
            }
 
            /// <summary>
            /// Length from delimiter to delimiter.
            /// </summary>
            public int MacroTagLength
            {
                get { return MacroTagEnd - MacroTagBegin + 1; }
            }
 
 
            public macroLocation(string macroName, int macroStart, int macroStop)
            {
                MacroName = macroName;
                MacroBegin = macroStart;
                MacroEnd = macroStop;
            }
            
            /// <summary>
            /// Extract macro information from a string.
            /// </summary>
            /// <param name="suppliedString"></param>
            /// <returns></returns>
            public static List<macroLocation> GetMacroLocations(string suppliedString)
            {
                List<macroLocation> locations = new List<macroLocation>();
 
                // search for things delimited by '<' and '>'.
                for(int i = 0; i < suppliedString.Length; i++)
                {
                    if(suppliedString[i] == '<')
                    {
                        i += 1; // skip to first char.
                        int start = i;
                        for (; i < suppliedString.Length; i++)
                        {
                            if(suppliedString[i] == '>')
                            {
                                int stop = i - 1; // last char thats not a '>'.
                                locations.Add(new macroLocation(suppliedString.Substring(start, stop - start + 1).Trim(), start, stop));
                                break;
                            }
                        }
                    }
                }
                return locations;
            }
        }
    }
}