-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
DiscardInBuffer_Generic.cs
98 lines (81 loc) · 3.08 KB
/
DiscardInBuffer_Generic.cs
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Diagnostics;
using System.IO.PortsTests;
using Legacy.Support;
using Xunit;
namespace System.IO.Ports.Tests
{
public class DiscardInBuffer_Generic : PortsTest
{
#region Test Cases
[Fact]
public void DiscardWithoutOpen()
{
using (SerialPort com = new SerialPort())
{
Debug.WriteLine("Verifying Discard method throws exception without a call to Open()");
VerifyDiscardException(com, typeof(InvalidOperationException));
}
}
[Fact]
public void DiscardAfterFailedOpen()
{
using (SerialPort com = new SerialPort("BAD_PORT_NAME"))
{
Debug.WriteLine("Verifying read Discard throws exception with a failed call to Open()");
//Since the PortName is set to a bad port name Open will thrown an exception
//however we don't care what it is since we are verifying a read method
Assert.ThrowsAny<Exception>(() => com.Open());
VerifyDiscardException(com, typeof(InvalidOperationException));
}
}
[ConditionalFact(nameof(HasOneSerialPort))]
public void DiscardAfterClose()
{
using (SerialPort com = new SerialPort(TCSupport.LocalMachineSerialInfo.FirstAvailablePortName))
{
Debug.WriteLine("Verifying Discard method throws exception after a call to Close()");
com.Open();
com.Close();
VerifyDiscardException(com, typeof(InvalidOperationException));
}
}
[ConditionalFact(nameof(HasOneSerialPort))]
public void DiscardAfterOpen()
{
using (SerialPort com = new SerialPort(TCSupport.LocalMachineSerialInfo.FirstAvailablePortName))
{
Debug.WriteLine("Verifying Discard method does not throw an exception after a call to Open()");
com.Open();
VerifyDiscardException(com, null);
Assert.Equal(0, com.BytesToRead);
}
}
#endregion
#region Verification for Test Cases
private void VerifyDiscardException(SerialPort com, Type expectedException)
{
try
{
com.DiscardInBuffer();
if (null != expectedException)
{
Fail("ERROR!!!: No Exception was thrown");
}
}
catch (Exception e)
{
if (null == expectedException)
{
Fail("ERROR!!!: No Exception was expected and {0} was thrown", e.GetType());
}
if (e.GetType() != expectedException)
{
Fail("ERROR!!!: {0} exception was thrown expected {1}", e.GetType(), expectedException);
}
}
}
#endregion
}
}