示例#1
0
        private void VerifyExceptionAtOpen(SerialPort com, int readTimeout, ThrowAt throwAt, Type expectedException)
        {
            int origReadTimeout = com.ReadTimeout;

            SerialPortProperties serPortProp = new SerialPortProperties();

            serPortProp.SetAllPropertiesToDefaults();
            serPortProp.SetProperty("PortName", TCSupport.LocalMachineSerialInfo.FirstAvailablePortName);

            if (ThrowAt.Open == throwAt)
            {
                serPortProp.SetProperty("ReadTimeout", readTimeout);
            }

            try
            {
                com.ReadTimeout = readTimeout;

                if (ThrowAt.Open == throwAt)
                {
                    com.Open();
                }

                if (null != expectedException)
                {
                    Fail("ERROR!!! Expected Open() to throw {0} and nothing was thrown", expectedException);
                }
            }
            catch (Exception e)
            {
                if (null == expectedException)
                {
                    Fail("ERROR!!! Expected Open() NOT to throw an exception and {0} was thrown", e.GetType());
                }
                else if (e.GetType() != expectedException)
                {
                    Fail("ERROR!!! Expected Open() throw {0} and {1} was thrown", expectedException, e.GetType());
                }
            }

            serPortProp.VerifyPropertiesAndPrint(com);
            com.ReadTimeout = origReadTimeout;
        }
示例#2
0
文件: ctor_str.cs 项目: jnm2/corefx
        private void VerifyCtor(string portName, Type expectedException, ThrowAt throwAt)
        {
            SerialPortProperties serPortProp = new SerialPortProperties();

            Debug.WriteLine($"Verifying properties where PortName={portName}");
            try
            {
                using (SerialPort com = new SerialPort(portName))
                {
                    if (null != expectedException && throwAt == ThrowAt.Set)
                    {
                        Assert.True(false, $"Err_7212ahsdj Expected Ctor to throw {expectedException}");
                    }

                    serPortProp.SetAllPropertiesToDefaults();
                    serPortProp.SetProperty("PortName", portName);

                    serPortProp.VerifyPropertiesAndPrint(com);
                }
            }
            catch (TrueException)
            {
                // This is an inner failure
                throw;
            }
            catch (Exception e)
            {
                if (null == expectedException)
                {
                    Assert.True(false, $"Err_07081hadnh Did not expect exception to be thrown and the following was thrown: \n{e}");
                }
                else if (throwAt == ThrowAt.Open)
                {
                    Assert.True(false, $"Err_88916adfa Expected {expectedException} to be thrown at Open and the following was thrown at Set: \n{e}");
                }
                else if (e.GetType() != expectedException)
                {
                    Assert.True(false, $"Err_90282ahwhp Expected {expectedException} to be thrown and the following was thrown: \n{e}");
                }
            }
        }
示例#3
0
    private bool VerifyException(int stopBits, ThrowAt throwAt, System.Type expectedException)
    {
        SerialPort com      = new SerialPort(TCSupport.LocalMachineSerialInfo.FirstAvailablePortName);
        bool       retValue = true;

        retValue &= VerifyExceptionAtOpen(com, stopBits, throwAt, expectedException);

        if (com.IsOpen)
        {
            com.Close();
        }

        retValue &= VerifyExceptionAfterOpen(com, stopBits, expectedException);

        if (com.IsOpen)
        {
            com.Close();
        }

        return(retValue);
    }
示例#4
0
        private void VerifyCtor(string portName, int baudRate, int parity, int dataBits, Type expectedException, ThrowAt throwAt)
        {
            SerialPortProperties serPortProp = new SerialPortProperties();

            Debug.WriteLine("Verifying properties where PortName={0},BaudRate={1},Parity={2},DatBits={3}", portName, baudRate, parity, dataBits);
            try
            {
                using (SerialPort com = new SerialPort(portName, baudRate, (Parity)parity, dataBits))
                {
                    if (null != expectedException && throwAt == ThrowAt.Set)
                    {
                        Fail("Err_7212ahsdj Expected Ctor to throw {0}", expectedException);
                    }

                    serPortProp.SetAllPropertiesToDefaults();

                    serPortProp.SetProperty("PortName", portName);
                    serPortProp.SetProperty("BaudRate", baudRate);
                    serPortProp.SetProperty("Parity", (Parity)parity);
                    serPortProp.SetProperty("DataBits", dataBits);

                    serPortProp.VerifyPropertiesAndPrint(com);
                }
            }
            catch (Exception e)
            {
                if (null == expectedException)
                {
                    Fail("Err_07081hadnh Did not expect exception to be thrown and the following was thrown: \n{0}", e);
                }
                else if (throwAt == ThrowAt.Open)
                {
                    Fail("Err_88916adfa Expected {0} to be thrown at Open and the following was thrown at Set: \n{1}", expectedException, e);
                }
                else if (e.GetType() != expectedException)
                {
                    Fail("Err_90282ahwhp Expected {0} to be thrown and the following was thrown: \n{1}", expectedException, e);
                }
            }
        }
示例#5
0
    private bool VerifyCtor(string portName, int baudRate, int parity, Type expectedException, ThrowAt throwAt)
    {
        SerialPortProperties serPortProp = new SerialPortProperties();

        Console.WriteLine("Verifying properties where PortName={0},BaudRate={1},Parity={2}", portName, baudRate, parity);
        try
        {
            SerialPort com = new SerialPort(portName, baudRate, (Parity)parity);

            if (null != expectedException && throwAt == ThrowAt.Set)
            {
                Console.WriteLine("Err_7212ahsdj Expected Ctor to throw {0}", expectedException);
                return(false);
            }

            serPortProp.SetAllPropertiesToDefaults();

            serPortProp.SetProperty("PortName", portName);
            serPortProp.SetProperty("BaudRate", baudRate);
            serPortProp.SetProperty("Parity", (Parity)parity);

            return(serPortProp.VerifyPropertiesAndPrint(com));
        }
        catch (Exception e)
        {
            if (null == expectedException)
            {
                Console.WriteLine("Err_07081hadnh Did not expect exception to be thrown and the following was thrown: \n{0}", e);
                return(false);
            }
            else if (throwAt == ThrowAt.Open)
            {
                Console.WriteLine("Err_88916adfa Expected {0} to be thrown at Open and the following was thrown at Set: \n{1}", expectedException, e);
                return(false);
            }
            else if (e.GetType() != expectedException)
            {
                Console.WriteLine("Err_90282ahwhp Expected {0} to be thrown and the following was thrown: \n{1}", expectedException, e);
                return(false);
            }

            return(true);
        }
    }
示例#6
0
        private void VerifyExceptionAtOpen(SerialPort com, string portName, ThrowAt throwAt, Type[] expectedExceptions)
        {
            string origPortName = com.PortName;

            SerialPortProperties serPortProp = new SerialPortProperties();

            if (null != expectedExceptions && 0 < expectedExceptions.Length)
            {
                serPortProp.SetAllPropertiesToDefaults();
            }
            else
            {
                serPortProp.SetAllPropertiesToOpenDefaults();
            }

            if (ThrowAt.Open == throwAt)
            {
                serPortProp.SetProperty("PortName", portName);
            }
            else
            {
                serPortProp.SetProperty("PortName", TCSupport.LocalMachineSerialInfo.FirstAvailablePortName);
            }

            try
            {
                com.PortName = portName;

                if (ThrowAt.Open == throwAt)
                {
                    com.Open();
                }

                if (null != expectedExceptions && 0 < expectedExceptions.Length)
                {
                    Fail("ERROR!!! Expected Open() to throw ");
                    for (int i = 0; i < expectedExceptions.Length; ++i)
                    {
                        Console.Write(expectedExceptions[i] + " ");
                    }
                    Debug.WriteLine(" and nothing was thrown");
                }
            }
            catch (Exception e)
            {
                if (null == expectedExceptions || 0 == expectedExceptions.Length)
                {
                    Fail("ERROR!!! Expected Open() NOT to throw an exception and the following was thrown:\n{0}", e);
                }
                else
                {
                    bool exceptionFound      = false;
                    Type actualExceptionType = e.GetType();

                    for (int i = 0; i < expectedExceptions.Length; ++i)
                    {
                        if (actualExceptionType == expectedExceptions[i])
                        {
                            exceptionFound = true;
                            break;
                        }
                    }

                    if (exceptionFound)
                    {
                        Debug.WriteLine("Caught expected exception:\n{0}", e.GetType());
                    }
                    else
                    {
                        Fail("ERROR!!! Expected Open() throw ");
                        for (int i = 0; i < expectedExceptions.Length; ++i)
                        {
                            Console.Write(expectedExceptions[i] + " ");
                        }
                        Debug.WriteLine(" and  the following was thrown:\n{0}", e);
                    }
                }
            }

            serPortProp.VerifyPropertiesAndPrint(com);
            com.PortName = origPortName;
        }
示例#7
0
 private void VerifyExceptionAtOpen(SerialPort com, string portName, ThrowAt throwAt, Type expectedException)
 {
     VerifyExceptionAtOpen(com, portName, throwAt, new[] { expectedException });
 }
示例#8
0
 private void VerifyException(string portName, ThrowAt throwAt, Type expectedExceptionAtOpen, Type expectedExceptionAfterOpen)
 {
     VerifyException(portName, throwAt, new[] { expectedExceptionAtOpen }, expectedExceptionAfterOpen);
 }
示例#9
0
    private void VerifyExceptionAtOpen(SerialPort com, System.Text.Encoding encoding, ThrowAt throwAt, Type expectedException)
    {
        System.Text.Encoding origEncoding = com.Encoding;
        SerialPortProperties serPortProp  = new SerialPortProperties();

        serPortProp.SetAllPropertiesToDefaults();
        serPortProp.SetProperty("PortName", TCSupport.LocalMachineSerialInfo.FirstAvailablePortName);

        if (ThrowAt.Open == throwAt)
        {
            serPortProp.SetProperty("Encoding", encoding);
        }

        try
        {
            com.Encoding = encoding;

            if (ThrowAt.Open == throwAt)
            {
                com.Open();
            }

            object myEncoding = com.Encoding;

            com.Encoding = origEncoding;

            if (null != expectedException)
            {
                Fail("ERROR!!! Expected Open() to throw {0} and nothing was thrown", expectedException);
            }
        }
        catch (Exception e)
        {
            if (null == expectedException)
            {
                Fail("ERROR!!! Expected Open() NOT to throw an exception and {0} was thrown", e.GetType());
            }
            else if (e.GetType() != expectedException)
            {
                Fail("ERROR!!! Expected Open() throw {0} and {1} was thrown", expectedException, e.GetType());
            }
        }

        serPortProp.VerifyPropertiesAndPrint(com);
        com.Encoding = origEncoding;
    }
示例#10
0
    private bool VerifyExceptionAtOpen(SerialPort com, System.Text.Encoding encoding, ThrowAt throwAt, System.Type expectedException)
    {
        System.Text.Encoding origEncoding = com.Encoding;
        bool retValue = true;
        SerialPortProperties serPortProp = new SerialPortProperties();

        serPortProp.SetAllPropertiesToDefaults();
        serPortProp.SetProperty("PortName", TCSupport.LocalMachineSerialInfo.FirstAvailablePortName);

        if (ThrowAt.Open == throwAt)
        {
            serPortProp.SetProperty("Encoding", encoding);
        }

        try
        {
            com.Encoding = encoding;

            if (ThrowAt.Open == throwAt)
            {
                com.Open();
            }

            Object myEncoding = com.Encoding;

            com.Encoding = origEncoding;

            if (null != expectedException)
            {
                Console.WriteLine("ERROR!!! Expected Open() to throw {0} and nothing was thrown", expectedException);
                retValue = false;
            }
        }
        catch (System.Exception e)
        {
            if (null == expectedException)
            {
                Console.WriteLine("ERROR!!! Expected Open() NOT to throw an exception and {0} was thrown", e.GetType());
                retValue = false;
            }
            else if (e.GetType() != expectedException)
            {
                Console.WriteLine("ERROR!!! Expected Open() throw {0} and {1} was thrown", expectedException, e.GetType());
                retValue = false;
                Console.WriteLine(e);
            }
        }

        retValue    &= serPortProp.VerifyPropertiesAndPrint(com);
        com.Encoding = origEncoding;

        return(retValue);
    }
示例#11
0
 private bool VerifyExceptionAtOpen(SerialPort com, string portName, ThrowAt throwAt, System.Type expectedException)
 {
     return(VerifyExceptionAtOpen(com, portName, throwAt, new Type[] { expectedException }));
 }
示例#12
0
 private bool VerifyException(string portName, ThrowAt throwAt, System.Type expectedExceptionAtOpen, System.Type expectedExceptionAfterOpen)
 {
     return(VerifyException(portName, throwAt, new Type[] { expectedExceptionAtOpen }, expectedExceptionAfterOpen));
 }