简体   繁体   English

不知道如何在C#中测试一行代码

[英]Not sure how to test a line of code in C#

I am having touble testing the throw command after the if (command[0] == '(')). 我在if(command [0] =='('))之后测试了throw命令。 The if statment I think means that if the first char in command does not equel ) throw an error. 我认为if语句意味着如果命令中的第一个char不等于抛出错误。 I have tried a several statments without a ) but have still not been able to execute the throw command. 我已经尝试了几个没有a)但仍然无法执行throw命令的语句。 Any ideas. 有任何想法吗。

private double ParseTerm(ref string command)
    {
        double returnValue=0;
        if (command.Length != 0)
        {
         if (command[0] == '('))
            {
                command = command.Substring(1,command.Length -1);   // skip the open paren
                returnValue= ParseExpr(ref command);
                if (command[0] != ')')                              // make sure there is a close paren for each open parenthesis
                    throw new System.FormatException();
                command = command.Substring(1,command.Length -1);   // skip the close paren
            } 
        return returnValue;
    }

Here is ParseExpr 这是ParseExpr

private double ParseExpr(ref string command)
    {
        double op, op2;

        if (command == "")                              // Handle the empty expression case
            return 0;

        op = ParseFactor(ref command);                  // parse left side of expression

        if (command != "")                              // if a right side exists, parse it
        {               

            if (command[0] == '+')                      // test for '+'
            {           
                command = command.Substring(1,command.Length -1);   // skip to +

                if (command.Length == 0)    
                    throw new System.FormatException();     // no right hand side operator

                op2 = ParseExpr(ref command);               // parse remainder of the expression
                op +=  op2;
            } 
            else if (command[0] == '-')
            {                   
                command = command.Substring(1,command.Length -1);
                if (command.Length == 0)
                    throw new System.FormatException();
                op2 = ParseExpr(ref command);           
                op -=  op2;
            } 
        }
        return op;
    }


    private double ParseFactor(ref string command)
    {
        double op, op2;
        op = ParseExp(ref command);
        if (command != "")
        {               
            if (command[0] == '*')
            {                   
                command = command.Substring(1,command.Length -1);
                if (command.Length == 0)
                    throw new System.FormatException();
                op2 = ParseFactor(ref command);         
                op *=  op2;
            } 
            else if (command[0] == '/' || command[0] == '\\')
            {                   
                command = command.Substring(1,command.Length -1);
                if (command.Length == 0)
                    throw new System.FormatException();
                op2 = ParseFactor(ref command);     

                if (op2 == 0)                                   // don't allow divide 0
                    throw new System.DivideByZeroException();   // the division operation won't return
                op /=  op2;                                     // throw the exception since we are using doubles
            }               
            else if (command[0] == '%')
            {                   
                command = command.Substring(1,command.Length -1);
                if (command.Length == 0)
                    throw new System.FormatException();
                op2 = ParseFactor(ref command);                             
                op = (int)op % (int)op2;
            } 
        }
        return op;
    }

You mean a UnitTest? 你的意思是UnitTest? If yes: 如是:

[TestMethod]
[ExpectedException(typeof(FormatException))]
public void ParseTerm_when_the_last_char_is_not_a_close_parenthesis_should_throw_FormatException()
{
    //Call the method here:
    ParseTerm("(some string without close parenthesis");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM