TEST(ParameterReassignmentRuleTest, MultipleParameterReassignments)
{
    testRuleOnCode(new ParameterReassignmentRule(), "void m(int a) { a = 1; a = 2; }",
        0, 1, 17, 1, 21);
    testRuleOnCode(new ParameterReassignmentRule(), "void m(int a) { a = 1; a = 2; }",
        1, 1, 24, 1, 28);
}
TEST_F(NestedBlockDepthRuleTest, TwoLevelBlock)
{
    testRuleOnCode(new NestedBlockDepthRule(), "void m() {{}}",
        0, 1, 10, 1, 13, "Block depth of 2 exceeds limit of 0");
    testRuleOnCode(new NestedBlockDepthRule(), "void m() {{}}",
        1, 1, 11, 1, 12, "Block depth of 1 exceeds limit of 0");
}
TEST(GotoStatementRuleTest, TwoGotos)
{
    testRuleOnCode(new GotoStatementRule(), "void a(); void m() { goto A; goto B; A:\na();\nB:\na(); }",
        0, 1, 22, 1, 27);
    testRuleOnCode(new GotoStatementRule(), "void a(); void m() { goto A; goto B; A:\na();\nB:\na(); }",
        1, 1, 30, 1, 35);
}
TEST(ParameterReassignmentRuleTest, MultipleParametersBeingReassigned)
{
    testRuleOnCode(new ParameterReassignmentRule(), "void m(int a, int b, int c) { a = 1; c = 3; }",
        0, 1, 31, 1, 35);
    testRuleOnCode(new ParameterReassignmentRule(), "void m(int a, int b, int c) { a = 1; c = 3; }",
        1, 1, 38, 1, 42);
}
Beispiel #5
0
TEST_F(LongLineRuleTest, GetNumberOfCharactersForThreeLines)
{
    testRuleOnCode(new LongLineRule(), "void m() {\n  \n}",
        0, 1, 1, 1, 10, "Line with 10 characters exceeds limit of 0");
    testRuleOnCode(new LongLineRule(), "void m() {\n  \n}",
        1, 2, 1, 2, 2, "Line with 2 characters exceeds limit of 0");
    testRuleOnCode(new LongLineRule(), "void m() {\n  \n}",
        2, 3, 1, 3, 1, "Line with 1 characters exceeds limit of 0");
}
TEST_F(PreferEarlyExitRuleTest, LongIfAndReturnInsideBlock)
{
    std::string code = "int test(int a) {\n";
    code += "  int i = 2;\n";
    code += "  if (a) {\n";
    code += filler("i *= 2;\n", 2);
    code += "  }\n";
    code += "  return i;\n";
    code += "}\n";
    testRuleOnCode(new PreferEarlyExitRule(), code, 0, 3, 3, 6, 3,
                   PreferEarlyExitRule::getMessage());
}
TEST_F(LongMethodRuleTest, TweLines)
{
    testRuleOnCode(new LongMethodRule(), "void aMethod() {\n}",
                   0, 1, 1, 2, 1, "Method with 2 lines exceeds limit of 0");
}
TEST_F(NestedBlockDepthRuleTest, DoStatement)
{
    testRuleOnCode(new NestedBlockDepthRule(), "void m() { do {} while(1); }",
        0, 1, 10, 1, 28, "Block depth of 2 exceeds limit of 1");
}
TEST_F(LongMethodRuleTest, OneLine)
{
    testRuleOnCode(new LongMethodRule(), "void aMethod() { }",
                   0, 1, 1, 1, 18, "Method with 1 lines exceeds limit of 0");
}
TEST_F(NestedBlockDepthRuleTest, ForStatement)
{
    testRuleOnCode(new NestedBlockDepthRule(), "void m() { for(;;) {} }",
        0, 1, 10, 1, 23, "Block depth of 2 exceeds limit of 1");
}
TEST_F(NestedBlockDepthRuleTest, WhileStatement)
{
    testRuleOnCode(new NestedBlockDepthRule(), "void m() { while(1) {} }",
        0, 1, 10, 1, 24, "Block depth of 2 exceeds limit of 1");
}
TEST(RedundantIfStatementRuleTest, CIntDeclaration)
{
    testRuleOnCode(new RedundantIfStatementRule(),
        "void aMethod() { int b; if (1) { b = 6; } else b = 0; }");
}
TEST_F(NestedBlockDepthRuleTest, IfStatementWithElse)
{
    testRuleOnCode(new NestedBlockDepthRule(), "void m() { if (1) {} else {{{}}} }",
        0, 1, 10, 1, 34, "Block depth of 4 exceeds limit of 1");
}
TEST(ParameterReassignmentRuleTest, ReassignParameter)
{
    testRuleOnCode(new ParameterReassignmentRule(), "void m(int a) { a = 1; }",
        0, 1, 17, 1, 21);
}
TEST(RedundantIfStatementRuleTest, ZeroThenAndOneElse)
{
    testRuleOnCode(new RedundantIfStatementRule(),
        "int aMethod() { if (1) return 0; else { return 1; } }");
}
TEST(UnusedMethodParameterRuleTest, FunctionDeclarationWithoutDefincationShouldBeIgnored)
{
    testRuleOnCode(new UnusedMethodParameterRule(), "int aMethod(int a);");
}
TEST_F(NestedBlockDepthRuleTest, OneCaseStatement)
{
    testRuleOnCode(new NestedBlockDepthRule(), "void m() { int i = 1; switch (i) { case 1: break; } }",
        0, 1, 10, 1, 53, "Block depth of 2 exceeds limit of 1");
}
TEST(UnusedMethodParameterRuleTest, MethodWithUnusedParameter)
{
    testRuleOnCode(new UnusedMethodParameterRule(), "void aMethod(int a) { }",
        0, 1, 14, 1, 18);
}
TEST(UnusedMethodParameterRuleTest, MethodWithUsedParameter)
{
    testRuleOnCode(new UnusedMethodParameterRule(), "void aMethod() { int a; a = 1; }");
}
TEST(RedundantIfStatementRuleTest, OneThenAndZeroElse)
{
    testRuleOnCode(new RedundantIfStatementRule(),
        "int aMethod() { if (1) { return 1; } else return 0; }");
}
TEST(RedundantConditionalOperatorRuleTest, GoodConditionalOperator)
{
    testRuleOnCode(new RedundantConditionalOperatorRule(), "void m(int a, int b) { int c = a > b ? a : b; }");
}
TEST(ConstantIfExpressionRuleTest, testOnlyEvaluateTheNecessaryCondition)
{
    testRuleOnCode(new ConstantIfExpressionRule(), "int foo() { return 1; } void aMethod() { if (1 ? 0 : foo()) {;} }",
        0, 1, 46, 1, 58);
}
TEST(ParameterReassignmentRuleTest, UseParameter)
{
    testRuleOnCode(new ParameterReassignmentRule(), "void useInt(int i); void m(int a) { useInt(a); }");
}
TEST(RedundantIfStatementRuleTest, GoodIfStatement)
{
    testRuleOnCode(new RedundantIfStatementRule(), "void aMethod() { if (1) {;} }");
}
TEST_F(NestedBlockDepthRuleTest, TwoCaseStatementsAndDefault)
{
    testRuleOnCode(new NestedBlockDepthRule(), "void m() { int i = 1; switch (i) { case 1: i = 2; break; case 2: break; default: break; } }",
        0, 1, 10, 1, 91, "Block depth of 2 exceeds limit of 1");
}
TEST(EmptyForStatementRuleTest, ForStatementWithEmptyComponent)
{
    testRuleOnCode(new EmptyForStatementRule(), "void aMethod() { for (;;) {} }",
        0, 1, 27, 1, 28);
}
TEST(EmptyForStatementRuleTest, GoodForStatement)
{
    testRuleOnCode(new EmptyForStatementRule(), "void aMethod() { for (;;) {;} }");
}
TEST_F(NestedBlockDepthRuleTest, EmptyFunction)
{
    testRuleOnCode(new NestedBlockDepthRule(), "void m() {}");
}
TEST(EmptyForStatementRuleTest, ForStatementWithNullStmt)
{
    testRuleOnCode(new EmptyForStatementRule(), "void aMethod() { for (;;) \n; }",
        0, 2, 1, 2, 1);
}
TEST_F(NestedBlockDepthRuleTest, ChooseTheDeepest)
{
    testRuleOnCode(new NestedBlockDepthRule(), "void m() {{}{{{}}}{{}}}",
        0, 1, 10, 1, 23, "Block depth of 4 exceeds limit of 1");
}