⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 money_example.html

📁 c++开发的一个不错的工具
💻 HTML
📖 第 1 页 / 共 2 页
字号:
<span class="preprocessor"></span><span class="preprocessor">#define MONEY_H</span><span class="preprocessor"></span><span class="preprocessor">#include &lt;string&gt;</span><span class="keyword">class </span>Money{<span class="keyword">public</span>:  Money( <span class="keywordtype">double</span> amount, std::string currency )    : m_amount( amount )    , m_currency( m_currency )  {  }  <span class="keywordtype">double</span> getAmount()<span class="keyword"> const</span><span class="keyword">  </span>{    <span class="keywordflow">return</span> m_amount;  }  std::string getCurrency()<span class="keyword"> const</span><span class="keyword">  </span>{    <span class="keywordflow">return</span> m_currency;  }<span class="keyword">private</span>:  <span class="keywordtype">double</span> m_amount;  std::string m_currency;};<span class="preprocessor">#endif</span></div></pre><p>Include <code>Money.h</code> in MoneyTest.cpp and compile.<p>Hum, an assertion failed! Press F4, and we jump to the assertion that checks the currency of the constructed money object. The report indicates that string is not equal to expected value. There is only two ways for this to happen: the member was badly initialized or we returned the wrong value. After a quick check, we fin out it is the former. Let's fix that:<p><code>Money.h</code> <pre><div class="fragment">  Money( <span class="keywordtype">double</span> amount, std::string currency )    : m_amount( amount )    , m_currency( currency )  {  }</div></pre><p>Compile. Our test finally pass! Let's add some functionnalities to our Money class.<h2><a class="anchor" name="sec_more_tests">Adding more tests</a></h2><h3><a class="anchor" name="sec_equal">Testing for equality</a></h3>We want to check if to Money object are equal. Let's start by adding a new test to the suite, then add our method:<p><code>MoneyTest.h</code> <pre><div class="fragment">  <a class="code" href="group___writing_test_fixture.html#ga0">CPPUNIT_TEST_SUITE</a>( MoneyTest );  <a class="code" href="group___writing_test_fixture.html#ga5">CPPUNIT_TEST</a>( testConstructor );  <a class="code" href="group___writing_test_fixture.html#ga5">CPPUNIT_TEST</a>( testEqual );  <a class="code" href="group___writing_test_fixture.html#ga2">CPPUNIT_TEST_SUITE_END</a>();<span class="keyword">public</span>:  ...  <span class="keywordtype">void</span> testEqual();</div></pre><p><code>MoneyTest.cpp</code> <pre><div class="fragment"><span class="keywordtype">void</span>MoneyTest::testEqual(){  <span class="comment">// Set up</span>  <span class="keyword">const</span> Money money123FF( 123, <span class="stringliteral">"FF"</span> );  <span class="keyword">const</span> Money money123USD( 123, <span class="stringliteral">"USD"</span> );  <span class="keyword">const</span> Money money12FF( 12, <span class="stringliteral">"FF"</span> );  <span class="keyword">const</span> Money money12USD( 12, <span class="stringliteral">"USD"</span> );  <span class="comment">// Process &amp; Check</span>  <a class="code" href="group___assertions.html#ga0">CPPUNIT_ASSERT</a>( money123FF == money123FF );     <span class="comment">// ==</span>  <a class="code" href="group___assertions.html#ga0">CPPUNIT_ASSERT</a>( money12FF != money123FF );      <span class="comment">// != amount</span>  <a class="code" href="group___assertions.html#ga0">CPPUNIT_ASSERT</a>( money123USD != money123FF );    <span class="comment">// != currency</span>  <a class="code" href="group___assertions.html#ga0">CPPUNIT_ASSERT</a>( money12USD != money123FF );    <span class="comment">// != currency and != amount</span>}</div></pre><p>Let's implements <code>operator</code> <code>==</code> and <code>operator</code> <code>!=</code> in Money.h:<p><code>Money.h</code> <pre><div class="fragment"><span class="keyword">class </span>Money{<span class="keyword">public</span>:...  <span class="keywordtype">bool</span> operator ==( <span class="keyword">const</span> Money &amp;other )<span class="keyword"> const</span><span class="keyword">  </span>{    <span class="keywordflow">return</span> m_amount == other.m_amount  &amp;&amp;             m_currency == other.m_currency;  }  <span class="keywordtype">bool</span> operator !=( <span class="keyword">const</span> Money &amp;other )<span class="keyword"> const</span><span class="keyword">  </span>{    <span class="keywordflow">return</span> (*<span class="keyword">this</span> == other);  }};</div></pre><p>Compile, run... Ooops... Press F4, it seems we're having trouble with <code>operator</code> <code>!=</code>. Let's fix that: <pre><div class="fragment">  <span class="keywordtype">bool</span> operator !=( <span class="keyword">const</span> Money &amp;other )<span class="keyword"> const</span><span class="keyword">  </span>{    <span class="keywordflow">return</span> !(*<span class="keyword">this</span> == other);  }</div></pre><p>Compile, run. Finaly got it working!<h3><a class="anchor" name="sec_opadd">Adding moneys</a></h3>Let's add our test 'testAdd' to MoneyTest. You know the routine...<p><code>MoneyTest.cpp</code> <pre><div class="fragment"><span class="keywordtype">void</span> MoneyTest::testAdd(){  <span class="comment">// Set up</span>  <span class="keyword">const</span> Money money12FF( 12, <span class="stringliteral">"FF"</span> );  <span class="keyword">const</span> Money expectedMoney( 135, <span class="stringliteral">"FF"</span> );  <span class="comment">// Process</span>  Money money( 123, <span class="stringliteral">"FF"</span> );  money += money12FF;  <span class="comment">// Check</span>  <a class="code" href="group___assertions.html#ga3">CPPUNIT_ASSERT_EQUAL</a>( expectedMoney == money.getAmount() );  <span class="comment">// += works</span>  <a class="code" href="group___assertions.html#ga0">CPPUNIT_ASSERT</a>( &amp;money == &amp;(money += money12FF) );           <span class="comment">// += returns ref. on 'this'.</span>}</div></pre><p>While writing that test case, you ask yourself, what is the result of adding money of currencies. Obviously this is an error and it should be reported, say let throw an exception, say <code>IncompatibleMoneyError</code>, when the currencies are not equal. We will write another test case for this later. For now let get our testAdd() case working:<p><code>Money.h</code> <pre><div class="fragment"><span class="keyword">class </span>Money{<span class="keyword">public</span>:...  Money &amp;operator +=( <span class="keyword">const</span> Money &amp;other )  {    m_amount += other.m_amount;    <span class="keywordflow">return</span> *<span class="keyword">this</span>;  }}; </div></pre><p>Compile, run. Miracle, everything is fine! Just to be sure the test is indeed working, in the above code, change <code>m_amount</code> <code>+=</code> to <code>-=</code>. Build and check that it fails (always be suspicious of test that work the first time: you may have forgotten to add it to the suite for example)! Change the code back so that all the tests are working.<p>Let's the incompatible money test case before we forget about it... That test case expect an <code>IncompatibleMoneyError</code> exception to be thrown. CppUnit can test that for us, you need to specify that the test case expect an exception when you add it to the suite:<p><code>MoneyTest.h</code> <pre><div class="fragment"><span class="keyword">class </span>MoneyTest : <span class="keyword">public</span> CppUnit::<a class="code" href="class_test_fixture.html">TestFixture</a>{  <a class="code" href="group___writing_test_fixture.html#ga0">CPPUNIT_TEST_SUITE</a>( MoneyTest );  <a class="code" href="group___writing_test_fixture.html#ga5">CPPUNIT_TEST</a>( testConstructor );  <a class="code" href="group___writing_test_fixture.html#ga5">CPPUNIT_TEST</a>( testEqual );  <a class="code" href="group___writing_test_fixture.html#ga5">CPPUNIT_TEST</a>( testAdd );  <a class="code" href="group___writing_test_fixture.html#ga6">CPPUNIT_TEST_EXCEPTION</a>( testAddThrow, IncompatibleMoneyError );  <a class="code" href="group___writing_test_fixture.html#ga2">CPPUNIT_TEST_SUITE_END</a>();<span class="keyword">public</span>:  ...  <span class="keywordtype">void</span> testAddThrow();};</div></pre><p>By convention, you ends the name of such tests with <code>'Throw'</code>, that way, you know that the test expect an exception to be thrown. Let's write our test case:<p><code>MoneyTest.cpp</code> <pre><div class="fragment"><span class="keywordtype">void</span> MoneyTest::testAddThrow(){  <span class="comment">// Set up</span>  <span class="keyword">const</span> Money money123FF( 123, <span class="stringliteral">"FF"</span> );  <span class="comment">// Process</span>  Money money( 123, <span class="stringliteral">"USD"</span> );  money += money123FF;        <span class="comment">// should throw an exception</span>}</div></pre><p>Compile... Ooops, forgot to declare the exception class. Let's do that:<p><code>Money.h</code> <pre><div class="fragment"><span class="preprocessor">#include &lt;string&gt;</span><span class="preprocessor">#include &lt;stdexcept&gt;</span><span class="keyword">class </span>IncompatibleMoneyError : <span class="keyword">public</span> std::runtime_error{<span class="keyword">public</span>:  IncompatibleMoneyError() : runtime_error( "Incompatible moneys" )  {  }};</div></pre><p>Compile. As expected testAddThrow() fail... Let's fix that:<p><code>Money.h</code> <pre><div class="fragment">  Money &amp;operator +=( <span class="keyword">const</span> Money &amp;other )  {    <span class="keywordflow">if</span> ( m_currency != other.m_currency )      <span class="keywordflow">throw</span> IncompatibleMoneyError();    m_amount += other.m_amount;    <span class="keywordflow">return</span> *<span class="keyword">this</span>;  }</div></pre><p>Compile. Our test finaly pass!<p>TODO:<ul><li>Copy constructor/Assigment operator</li><li>Introducing fixtures</li><li>?</li></ul><h2><a class="anchor" name="sec_credits">Credits</a></h2>This article was written by Baptiste Lepilleur. Unix configuration &amp; set up by Phil Verghese. Inspired from many others (JUnit, Phil's cookbook...), and all the newbies around that keep asking me for the 'Hello world' example ;-) <hr><table width="100%">  <tr>    <td width="10%" align="left" valign="center">      <a href="http://sourceforge.net">       <img      src="http://sourceforge.net/sflogo.php?group_id=11795"      width="88" height="31" border="0" alt="SourceForge Logo"></a>    </td>    <td width="20%" align="left" valign="center">      hosts this site.    </td>    <td>    </td>    <td align="right" valign="center">      Send comments to:<br>      <a href="mailto:cppunit-devel@lists.sourceforge.net">CppUnit Developers</a>    </td>  </tr></table></body> </html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -