c# - 不确定如何在 C# 中测试一行代码

标签 c# unit-testing testing

我无法在 if (command[0] == '(')) 之后测试 throw 命令。我认为 if 语句意味着如果命令中的第一个字符不等于 ) 抛出错误。我已经尝试了几个没有 ) 的语句,但仍然无法执行 throw 命令。任何想法。

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;
    }

这里是 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;
    }

最佳答案

你是说单元测试?如果是:

[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");
}

关于c# - 不确定如何在 C# 中测试一行代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7862825/

相关文章:

android - 验证匿名内部类中的模拟交互

javascript - 在 chai.js 中,实际调用传递给 `expect()` 的函数的代码在哪里?

unit-testing - 适用于 iOS 和 Android 的移动测试自动化

c# - GetWindowLong(int hWnd, GWL_STYLE) 在 c# 中返回奇怪的数字

c# - 添加更多参数然后在查询中使用?

c# - LINQ 查找一系列连续数字

java - 列表上随机播放方法的单元测试

c# - 在带有 XML 的 C# Windows 窗体中使用 GetElementsByTagName

unit-testing - Visual Studio 测试中的 RowTest?

unit-testing - Mockito 匹配器不工作