Projekt

Obecné

Profil

« Předchozí | Další » 

Revize cbf3b0c6

Přidáno uživatelem Pultak před asi 2 roky(ů)

re #9443 Added implementation of InfoFetcher tests

Zobrazit rozdíly:

ld_client/LDClientTests/LDClientTests.csproj
9 9

  
10 10
  <ItemGroup>
11 11
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
12
    <PackageReference Include="Moq" Version="4.17.2" />
12 13
    <PackageReference Include="NUnit" Version="3.13.2" />
13 14
    <PackageReference Include="NUnit3TestAdapter" Version="4.0.0" />
14 15
    <PackageReference Include="coverlet.collector" Version="3.1.0" />
15 16
  </ItemGroup>
16 17

  
18
  <ItemGroup>
19
    <ProjectReference Include="..\LDClient\LDClient.csproj" />
20
  </ItemGroup>
21

  
17 22
</Project>
ld_client/LDClientTests/UnitTest1.cs
1
using NUnit.Framework;
2

  
3
namespace LDClientTests {
4
    public class Tests {
5
        [SetUp]
6
        public void Setup() {
7

  
8
        }
9

  
10
        [Test]
11
        public void Test1() {
12
            Assert.Pass();
13
        }
14
    }
15
}
ld_client/LDClientTests/detection/DebuggerInfoParserTests.cs
2 2
using LDClient.detection;
3 3
using NUnit.Framework;
4 4

  
5
namespace LDClientTests.detection {
6
    public class DebuggerInfoParserTests {
5
namespace LDClientTests.detection; 
7 6

  
8
        public static readonly string CorrectFileContent =
9
            "B::version.hardware \r\n\r\n" +
10
            "PowerDebug USB 3.0 via USB 3.0 \r\n\r\n" +
11
            "   Serial Number: C12345678912 \r\n\r\n" +
12
            "   Firmware R.2021.02 (136263) \r\n\r\n" +
13
            "   Instance: 1. \r\n\r\n" +
14
            "   Automotive Debug Cable \r\n\r\n" +
15
            "      Serial Number: C98765432198 ";
7
public class DebuggerInfoParserTests {
16 8

  
17
        public static readonly string HeadSerialNumber = "C98765432198";
18
        public static readonly string BodySerialNumber = "C12345678912";
9
    public static readonly string CorrectFileContent =
10
        "B::version.hardware \r\n\r\n" +
11
        "PowerDebug USB 3.0 via USB 3.0 \r\n\r\n" +
12
        "   Serial Number: C12345678912 \r\n\r\n" +
13
        "   Firmware R.2021.02 (136263) \r\n\r\n" +
14
        "   Instance: 1. \r\n\r\n" +
15
        "   Automotive Debug Cable \r\n\r\n" +
16
        "      Serial Number: C98765432198 ";
19 17

  
18
    public static readonly string HeadSerialNumber = "C98765432198";
19
    public static readonly string BodySerialNumber = "C12345678912";
20 20

  
21
        [Test]
22
        [TestCase("B::version.hardware \r\n\r\n" +
23
                  "PowerDebug USB 3.0 via USB 3.0 \r\n\r\n" +
24
                  "   Serial Number: C12345678912 \r\n\r\n" +
25
                  "   Firmware R.2021.02 (136263) \r\n\r\n" +
26
                  "   Instance: 1. \r\n\r\n" +
27
                  "   Automotive Debug Cable \r\n\r\n" +
28
                  "      Serial Number: C12345678912 ", "C12345678912", "C12345678912")]
29
        [TestCase("B::version.hardware \r\n\r\n" +
30
                  "PowerDebug USB 3.0 via USB 3.0 \r\n\r\n" +
31
                  "   Serial Number: C1awfaw484 \r\n\r\n" +
32
                  "   Firmware R.2021.02 (136263) \r\n\r\n" +
33
                  "   Instance: 1. \r\n\r\n" +
34
                  "   Automotive Debug Cable \r\n\r\n" +
35
                  "      Serial Number: C16468551", "C16468551", "C1awfaw484")]
36
        public void Parse_CorrectValues_ReturnSerials(string file, string expectedHead, string expectedBody) {
37
            var (headResult, bodyResult) = DebuggerInfoParser.Parse(file);
38 21

  
39
            Assert.AreEqual(expectedHead, headResult);
40
            Assert.AreEqual(expectedBody, bodyResult);
41
        }
22
    [Test]
23
    [TestCase("B::version.hardware \r\n\r\n" +
24
              "PowerDebug USB 3.0 via USB 3.0 \r\n\r\n" +
25
              "   Serial Number: C12345678912 \r\n\r\n" +
26
              "   Firmware R.2021.02 (136263) \r\n\r\n" +
27
              "   Instance: 1. \r\n\r\n" +
28
              "   Automotive Debug Cable \r\n\r\n" +
29
              "      Serial Number: C12345678912 ", "C12345678912", "C12345678912")]
30
    [TestCase("B::version.hardware \r\n\r\n" +
31
              "PowerDebug USB 3.0 via USB 3.0 \r\n\r\n" +
32
              "   Serial Number: C1awfaw484 \r\n\r\n" +
33
              "   Firmware R.2021.02 (136263) \r\n\r\n" +
34
              "   Instance: 1. \r\n\r\n" +
35
              "   Automotive Debug Cable \r\n\r\n" +
36
              "      Serial Number: C16468551", "C16468551", "C1awfaw484")]
37
    public void Parse_CorrectValues_ReturnSerials(string file, string expectedHead, string expectedBody) {
38
        var (headResult, bodyResult) = DebuggerInfoParser.Parse(file);
42 39

  
43

  
44
        [Test]
45
        [TestCase("B::version.hardware \r\n\r\n" +
46
                  "PowerDebug USB 3.0 via USB 3.0 \r\n\r\n" +
47
                  "   Serial Number: C12345678912 \r\n\r\n" +
48
                  "   Firmware R.2021.02 (136263) \r\n\r\n" +
49
                  "   Instance: 1. \r\n\r\n" +
50
                  "   Automotive Debug Cable \r\n\r\n" +
51
                  "      Serial Number: C12345678912 \n" +
52
                  "      Serial Number: C12345678912 ")]
53
        [TestCase("B::version.hardware \r\n\r\n" +
54
                  "PowerDebug USB 3.0 via USB 3.0 \r\n\r\n" +
55
                  "   Serial Number: C1awfaw484 \r\n\r\n" +
56
                  "   Firmware R.2021.02 (136263) \r\n\r\n" +
57
                  "   Instance: 1. \r\n\r\n" +
58
                  "   Automotive Debug Cable \r\n\r\n" +
59
                  "      Serial Numbeeeer: C16468551")]
60
        public void Parse_IncorrectValues_ThrowException(string file) {
61
            Assert.Throws<ArgumentException>(() => DebuggerInfoParser.Parse(file));
62
        }
40
        Assert.AreEqual(expectedHead, headResult);
41
        Assert.AreEqual(expectedBody, bodyResult);
42
    }
63 43

  
64 44

  
45
    [Test]
46
    [TestCase("B::version.hardware \r\n\r\n" +
47
              "PowerDebug USB 3.0 via USB 3.0 \r\n\r\n" +
48
              "   Serial Number: C12345678912 \r\n\r\n" +
49
              "   Firmware R.2021.02 (136263) \r\n\r\n" +
50
              "   Instance: 1. \r\n\r\n" +
51
              "   Automotive Debug Cable \r\n\r\n" +
52
              "      Serial Number: C12345678912 \n" +
53
              "      Serial Number: C12345678912 ")]
54
    [TestCase("B::version.hardware \r\n\r\n" +
55
              "PowerDebug USB 3.0 via USB 3.0 \r\n\r\n" +
56
              "   Serial Number: C1awfaw484 \r\n\r\n" +
57
              "   Firmware R.2021.02 (136263) \r\n\r\n" +
58
              "   Instance: 1. \r\n\r\n" +
59
              "   Automotive Debug Cable \r\n\r\n" +
60
              "      Serial Numbeeeer: C16468551")]
61
    public void Parse_IncorrectValues_ThrowException(string file) {
62
        Assert.Throws<ArgumentException>(() => DebuggerInfoParser.Parse(file));
65 63
    }
66
}
64

  
65

  
66
}
ld_client/LDClientTests/detection/InfoFetcherTests.cs
1
using System.IO;
2
using System.Threading.Tasks;
3
using LDClient.detection;
4
using LDClient.utils;
5
using Moq;
6
using NUnit.Framework;
7

  
8
namespace LDClientTests.detection; 
9

  
10
internal class InfoFetcherTests {
11
    private InfoFetcher _defaultFetcher;
12
    private InfoFetcher _fetcherWithoutPars;
13
        
14

  
15
    private readonly string[] _defaultArguments = new[] { "argument 1", "argument 2" , "argument 3"};
16
    private const uint DefaultMaxAttempts = 5;
17

  
18
    private Mock<IProcessUtils> _mockProcessUtils;
19
    private Mock<IFileUtils> _mockFileUtils;
20

  
21
    [SetUp]
22
    public void Setup() {
23
        _mockProcessUtils = new Mock<IProcessUtils>(MockBehavior.Strict);
24

  
25
        _mockFileUtils = new Mock<IFileUtils>(MockBehavior.Strict);
26
    
27

  
28
        _defaultFetcher = new InfoFetcher(DefaultMaxAttempts, 50, "info.txt", "executable.exe",
29
            _defaultArguments, 0, 50) {
30
            FileUtils = _mockFileUtils.Object,
31
            ProcessUtils = _mockProcessUtils.Object
32
        };
33

  
34

  
35
        _fetcherWithoutPars = new InfoFetcher(DefaultMaxAttempts, 50, "info.txt", "executable.exe",
36
            null, 0, 50) {
37
            FileUtils = _mockFileUtils.Object,
38
            ProcessUtils = _mockProcessUtils.Object
39
        };
40
    }
41

  
42
    [Test]
43
    public async Task FetchDataAsync_ExecuteAll_ExecutedAndFetched() {
44

  
45
        _mockProcessUtils.Setup(x => x.ExecuteNewProcess(It.IsAny<string>(), It.IsAny<string>(),
46
            It.IsAny<int>(), It.IsAny<int>())).Returns(true);
47
        _mockFileUtils.Setup(x => x.ReadFileAllLines(It.IsAny<string>())).
48
            Returns(DebuggerInfoParserTests.CorrectFileContent.Split("\n"));
49

  
50

  
51
        var result = await _defaultFetcher.FetchDataAsync();
52

  
53
        Assert.IsTrue(result);
54
        _mockProcessUtils.Verify(x => x.ExecuteNewProcess(It.IsAny<string>(), It.IsAny<string>(),
55
            It.IsAny<int>(), It.IsAny<int>()), Times.Exactly(_defaultArguments.Length));
56

  
57
        Assert.AreEqual(DebuggerInfoParserTests.BodySerialNumber, _defaultFetcher.BodySerialNumber);
58
        Assert.AreEqual(DebuggerInfoParserTests.HeadSerialNumber, _defaultFetcher.HeadSerialNumber);
59

  
60
    }
61

  
62

  
63

  
64
    [Test]
65
    public async Task FetchDataAsync_ExecuteNonExistentProgram_ExecutionFailed() {
66

  
67
        _mockProcessUtils.Setup(x => x.ExecuteNewProcess(It.IsAny<string>(), It.IsAny<string>(),
68
            It.IsAny<int>(), It.IsAny<int>())).Returns(false);
69
        _mockFileUtils.Setup(x => x.ReadFileAllLines(It.IsAny<string>())).
70
            Returns(new []{""});
71

  
72

  
73
        var result = await _defaultFetcher.FetchDataAsync();
74

  
75

  
76
        Assert.IsFalse(result);
77
        _mockProcessUtils.Verify(x => x.ExecuteNewProcess(It.IsAny<string>(), It.IsAny<string>(),
78
            It.IsAny<int>(), It.IsAny<int>()), Times.Exactly(1));
79
        _mockFileUtils.Verify(x => x.ReadFileAllLines(It.IsAny<string>()), Times.Never);
80
    }
81

  
82
    [Test]
83
    public async Task FetchDataAsync_ExecuteWithoutParameters_NotExecuted() {
84
        _mockProcessUtils.Setup(x => x.ExecuteNewProcess(It.IsAny<string>(), It.IsAny<string>(),
85
            It.IsAny<int>(), It.IsAny<int>())).Returns(false);
86
        _mockFileUtils.Setup(x => x.ReadFileAllLines(It.IsAny<string>())).
87
            Returns(new[] { "" });
88

  
89
        var result = await _fetcherWithoutPars.FetchDataAsync();
90
            
91
        Assert.IsFalse(result);
92
        _mockProcessUtils.Verify(x => x.ExecuteNewProcess(It.IsAny<string>(), It.IsAny<string>(),
93
            It.IsAny<int>(), It.IsAny<int>()), Times.Never);
94
        _mockFileUtils.Verify(x => x.ReadFileAllLines(It.IsAny<string>()), Times.Never);
95

  
96
    }
97

  
98

  
99
    [Test]
100
    public async Task FetchDataAsync_ExecuteInfoNotCreated_FetchFailed() {
101
        _mockProcessUtils.Setup(x => x.ExecuteNewProcess(It.IsAny<string>(), It.IsAny<string>(),
102
            It.IsAny<int>(), It.IsAny<int>())).Returns(true);
103
        _mockFileUtils.Setup(x => x.ReadFileAllLines(It.IsAny<string>())).Throws(new FileNotFoundException());
104

  
105
        var result = await _defaultFetcher.FetchDataAsync();
106
        Assert.IsFalse(result);
107

  
108
        _mockProcessUtils.Verify(x => x.ExecuteNewProcess(It.IsAny<string>(), It.IsAny<string>(),
109
            It.IsAny<int>(), It.IsAny<int>()), Times.Exactly(_defaultArguments.Length));
110
        _mockFileUtils.Verify(x => x.ReadFileAllLines(It.IsAny<string>()), Times.Exactly((int)DefaultMaxAttempts));
111

  
112
    }
113

  
114

  
115

  
116

  
117
}

Také k dispozici: Unified diff