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
//            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 NUnit.Framework;
using System;
using System.Diagnostics;
using System.Text;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Linq;
 
namespace OpenTap.EngineUnitTestUtils
{
    /// <summary>
    /// Trace listener used when running unittests, it checks for error messages in the trace
    /// </summary>
    public class TestTraceListener : OpenTap.TraceListener
    {
        public void ClearErrors()
        {
            ErrorMessage = new List<string>();
            WarningMessage = new List<string>();
        }
 
        public void AssertErrors(IList<string> allowedMessages)
        {
            foreach (string error in ErrorMessage)
            {
                if (allowedMessages == null || !allowedMessages.Contains(error))
                {
                    Assert.Fail("{0}{1}{1}{2}", ErrorMessage, Environment.NewLine, GetLog());
                }
            }
 
            foreach (string warning in WarningMessage)
            {
                if (allowedMessages == null || !allowedMessages.Contains(warning))
                {
                    Assert.Inconclusive("{0}{1}{1}{2}", WarningMessage, Environment.NewLine, GetLog());
                }
            }
        }
 
        public void AssertErrors(List<Regex> allowedMessages)
        {
            foreach (string error in ErrorMessage)
            {
                if (allowedMessages == null || allowedMessages.All(r => !r.IsMatch(error)))
                {
                    Assert.Fail("{0}{1}{1}{2}", ErrorMessage, Environment.NewLine, GetLog());
                }
            }
            foreach (string warning in WarningMessage)
            {
                if (allowedMessages == null || allowedMessages.All(r => !r.IsMatch(warning)))
                {
                    Assert.Inconclusive("{0}{1}{1}{2}", WarningMessage, Environment.NewLine, GetLog());
                }
            }
        }
 
        public void AssertErrors()
        {
            Flush();
            Assert.IsTrue(ErrorMessage.Count == 0, "{0}{1}{1}{2}", string.Join(Environment.NewLine, ErrorMessage), Environment.NewLine, GetLog());
            if (WarningMessage.Count != 0)
                Assert.Inconclusive("{0}{1}{1}{2}", string.Join(Environment.NewLine, WarningMessage), Environment.NewLine, GetLog());
        }
 
        public string GetLog()
        {
            return allLog.ToString();
        }
 
        public StringBuilder allLog = new StringBuilder(1000);
        private Stopwatch globalTimer = Stopwatch.StartNew();
        public List<string> ErrorMessage = new List<string>();
        public List<string> WarningMessage = new List<string>();
        public override void TraceEvent(string source, LogEventType eventType, int id, string message)
        {
            base.TraceEvent(source, eventType, id, message);
            allLog.AppendLine(String.Format("{3:ss\\.fff} : {0,-11} : {1,-13} : {2}", source, eventType, message, globalTimer.Elapsed));
            if (eventType == LogEventType.Error)
                ErrorMessage.Add(message);
            if (eventType == LogEventType.Warning)
                WarningMessage.Add(message);
        }
 
        public override void TraceEvent(string source, LogEventType eventType, int id, string format, params object[] args)
        {
            if (args == null)
            {
                TraceEvent(source, eventType, id, format);
                return;
            }
            base.TraceEvent(source, eventType, id, format, args);
            allLog.AppendLine(String.Format("{3:ss\\.fff} : {0,-11} : {1,-13} : {2}", source, eventType, String.Format(format, args), globalTimer.Elapsed));
            if (eventType == LogEventType.Error)
                ErrorMessage.Add(String.Format(format, args));
            if (eventType == LogEventType.Warning)
                WarningMessage.Add(String.Format(format, args));
        }
 
        public override void Write(string message)
        {
        }
 
        public override void WriteLine(string message)
        {
        }
 
        public void ExpectWarnings(params string[] expectedWarnings)
        {
            foreach (var warning in expectedWarnings)
                Assert.IsTrue(WarningMessage.Any(x => x.Contains(warning)), "Expected warning '{0}' should be present", warning);
        }
        
        public void ExpectErrors(params string[] expectedErrors)
        {
            foreach (var error in expectedErrors)
                Assert.IsTrue(ErrorMessage.Any(x => x.Contains(error)), "Expected error '{0}' should be present", error);
        }
    }
}