Create  Edit  Diff  FrontPage  Index  Search  Changes  History  RSS  Source  Login

SpecForC++ Diff - ひらくの工房

  • Added parts are displayed like this.
  • Deleted parts are displayed like this.

{{toc}}

!概要
C++でRSpec{{fn 'http://rspec.info/

http://jp.rubyist.net/magazine/?0021-Rspec'}}っぽいテストを書くためのツールの試作版です。

現時点で、次のようなテストが書けます。

!!サンプル
\<.<<<
DESCRIBE(int_vector)DESCRIBE("int_vector")
{
    std::vector<int> *v;
    BEFORE{
        v=new std::vector<int>;
    }
    AFTER{
        delete v;
    }

    VERIFY{
        THAT{
            IT(v->empty())
                SHOULD(BE_TRUE);
            v->push_back(1);
            IT(v->at(0))SHOULD(==1);
        }

        AND_THAT{
            IT(v->empty())
                SHOULD(BE_TRUE);
            v->push_back(2);
            IT(v->at(0))SHOULD(==3);
        }
        OVER;
    }
};
REGISTER(int_vector);
} END_DESCRIBE;

DESCRIBE(int)DESCRIBE("int")
{
    VERIFY{
        THAT{
            IT(1+1)SHOULD(==2);
            IT(2*3)SHOULD(==6);
            IT(1/2)SHOULD(==0.5);
        }
        AND_THAT{
            IT(!)SHOULD("This is executed and be failed");
        }
        AND_THAT{
            IT(!)SHOULD("...and this is executed too and be failed");
        }
        OVER

    }
};
REGISTER(int)
} END_DESCRIBE;
>>>.>

!全ソース
{{attach_anchor 'spec.cpp'}}

!実行結果
\<.<<<
Test(109) //// int_vector ////
Test(135)
OK : v->empty()BE_TRUE
Test(112) Test(138) OK : v->at(0)==1
Test(116) Test(142) OK : v->empty()BE_TRUE
Test(119) Test(145) NG : v->at(0)==3
Test(129) //// int ////
Test(155)
OK : 1+1==2
Test(130) Test(156) OK : 2*3==6
Test(131) Test(157) NG : 1/2==0.5

Failure at 119
    by v->at(0)==3

Failure at 131
    by 1/2==0.5
Test(160) NG : !"This is executed and be failed"
Test(163) NG : !"...and this is executed too and be failed"

>>>.>