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
//            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.IO;
using System.Text;
using OpenTap.EngineUnitTestUtils;
using NUnit.Framework;
using OpenTap.Plugins.BasicSteps;
 
namespace OpenTap.Engine.UnitTests
{
    [TestFixture]
    public class LogResultListenerTest 
    {
        
        public class TestStepEmit : TestStep
        {
            public TestStepEmit()
            {
                Name = "Emit";
            }
            public override void Run()
            {
                Log.Debug("This was called");
            }
        }
 
 
        [Test]
        public void MacroExpandTest()
        {
            EngineSettings.Current.StationName = "__station_name__";
            var macro = new MacroString() { Text = "test<Date> <Station>" };
            var result = macro.Expand();
            Assert.IsTrue(result.EndsWith(EngineSettings.Current.StationName));
 
            macro.Text = macro.Text + "<ThisIsTBD>";
            var result2 = macro.Expand(); // Since ThisIsTBD does not exist, we have to run through all possible expansions.
            Assert.IsTrue(result2.EndsWith("TBD"));
 
            // Check that when chars that are invalid path characters are used it still works.
            var prevStationName = EngineSettings.Current.StationName;
            EngineSettings.Current.StationName = "esc!@#%^&*(~)_+{{}\":?>><,m\"\"''";
 
            macro.Text = "<Station>";
            var r = macro.Expand();
            Assert.AreEqual(EngineSettings.Current.StationName, r);
            EngineSettings.Current.StationName = prevStationName;
 
        }
 
        [Test]
        public void LogResultListenerBasicTest()
        {
            LogResultListener log = new LogResultListener { FilePath = new MacroString { Text = "logResult.txt" } };
            var expanded = log.FilePath.Expand();
 
            // If debugging the LogResultListener will just rename the file since it might exist
            if (System.IO.File.Exists(expanded))
                System.IO.File.Delete(expanded);
 
            ResourceTest.TestConformance(log);
            var testPlan = TestStepTest.CreateGenericTestPlan();
            testPlan.Steps.Add(new TestStepEmit());
            ResultSettings.Current.Add(log);
            var run = testPlan.Execute();
 
            var logText = System.IO.File.ReadAllText(expanded, System.Text.Encoding.ASCII);
            StringAssert.Contains("This was called", logText);
 
            ResultSettings.Current.Remove(log);
        }
 
        [Test]
        public void SimulateTestPlan()
        {
            var rl = new LogResultListener();
            var planrun = new TestPlanRun();
            planrun.StartTime = DateTime.Now;
            planrun.Duration = TimeSpan.FromSeconds(1);
            planrun.Parameters["TestPlanName", ""] = "test";
            
            // An issue was found where first setting planrun.Parameters["Verdict"] to a string and
            // then getting it as a Verdict.
            planrun.Parameters["Verdict", ""] = Verdict.Pass;
            Assert.AreEqual(Verdict.Pass, planrun.Verdict);
            rl.OnTestPlanRunStart(planrun);
 
            string testString = "Test Test Test";
            
            var ms = new MemoryStream(Encoding.UTF8.GetBytes(testString));
            rl.OnTestPlanRunCompleted(planrun, ms);
            var file = rl.FilePath.Expand(planrun);
            try
            {
                Assert.AreEqual(testString, File.ReadAllText(file));
            }
            finally
            {
                File.Delete(file);
            }
        }
        
        
        [Test]
        public void LogResultListenerBugTest()
        {
            var name = Guid.NewGuid() + ".txt";
            var log = new LogResultListener
            {
                FilterOptions = LogResultListener.FilterOptionsType.Verbose,
                FilePath =
                {
                    Text = name
                }
            };
            try
            {
                var plan = new TestPlan();
                plan.ChildTestSteps.Add(new SequenceStep());
                var ex = plan.Execute(new[]
                {
                    log
                });
                Assert.AreNotEqual(Verdict.Error, ex.Verdict);
 
                var logContent = File.ReadAllText(name);
                Assert.IsNotNull(logContent);
            }
            finally
            {
                File.Delete(name);
            }
        }
    }
}