Skip to content

Commit

Permalink
Add tests for the rest of the doc examples.
Browse files Browse the repository at this point in the history
  • Loading branch information
grafikrobot committed Jan 18, 2020
1 parent 7efc137 commit 9e31b21
Show file tree
Hide file tree
Showing 18 changed files with 229 additions and 12 deletions.
6 changes: 6 additions & 0 deletions docs/lyra.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,12 @@ Changes:
-- _Rene Rivera_
* Fixed allowing junk extra characters after a non-string option value.
-- _Rene Rivera_
* Support specifying a single value for choices of an argument.
-- _Rene Rivera_
* Fix various problems with the commands example program. Also now the
examples for the documentation are unit tested along with the
regular unit tests.
-- _Rene Rivera_
* Tested with
Visual Studio 2015, VS 2017, VS 2019,
MinGW-64 (gcc 8.1),
Expand Down
29 changes: 23 additions & 6 deletions docs/lyra.html
Original file line number Diff line number Diff line change
Expand Up @@ -660,10 +660,11 @@ <h3 id="_single_option"><a class="anchor" href="#_single_option"></a>2.1. Single
<span class="tok-k">if</span> <span class="tok-p">(</span><span class="tok-o">!</span><span class="tok-n">result</span><span class="tok-p">)</span>
<span class="tok-p">{</span>
<span class="tok-n">std</span><span class="tok-o">::</span><span class="tok-n">cerr</span> <span class="tok-o">&lt;&lt;</span> <span class="tok-s">&quot;Error in command line: &quot;</span> <span class="tok-o">&lt;&lt;</span> <span class="tok-n">result</span><span class="tok-p">.</span><span class="tok-n">errorMessage</span><span class="tok-p">()</span> <span class="tok-o">&lt;&lt;</span> <span class="tok-n">std</span><span class="tok-o">::</span><span class="tok-n">endl</span><span class="tok-p">;</span>
<span class="tok-n">std</span><span class="tok-o">::</span><span class="tok-n">exit</span><span class="tok-p">(</span><span class="tok-mi">1</span><span class="tok-p">);</span>
<span class="tok-k">return</span> <span class="tok-mi">1</span><span class="tok-p">;</span>
<span class="tok-p">}</span>

<span class="tok-n">std</span><span class="tok-o">::</span><span class="tok-n">cout</span> <span class="tok-o">&lt;&lt;</span> <span class="tok-s">&quot;width = &quot;</span> <span class="tok-o">&lt;&lt;</span> <span class="tok-n">width</span> <span class="tok-o">&lt;&lt;</span> <span class="tok-s">&quot;</span><span class="tok-se">\n</span><span class="tok-s">&quot;</span><span class="tok-p">;</span>
<span class="tok-k">return</span> <span class="tok-mi">0</span><span class="tok-p">;</span>
<span class="tok-p">}</span></code></pre>
</div>
</div>
Expand Down Expand Up @@ -724,10 +725,11 @@ <h3 id="_multiple_options"><a class="anchor" href="#_multiple_options"></a>2.2.
<span class="tok-k">if</span> <span class="tok-p">(</span><span class="tok-o">!</span><span class="tok-n">result</span><span class="tok-p">)</span>
<span class="tok-p">{</span>
<span class="tok-n">std</span><span class="tok-o">::</span><span class="tok-n">cerr</span> <span class="tok-o">&lt;&lt;</span> <span class="tok-s">&quot;Error in command line: &quot;</span> <span class="tok-o">&lt;&lt;</span> <span class="tok-n">result</span><span class="tok-p">.</span><span class="tok-n">errorMessage</span><span class="tok-p">()</span> <span class="tok-o">&lt;&lt;</span> <span class="tok-n">std</span><span class="tok-o">::</span><span class="tok-n">endl</span><span class="tok-p">;</span>
<span class="tok-n">std</span><span class="tok-o">::</span><span class="tok-n">exit</span><span class="tok-p">(</span><span class="tok-mi">1</span><span class="tok-p">);</span>
<span class="tok-k">return</span> <span class="tok-mi">1</span><span class="tok-p">;</span>
<span class="tok-p">}</span>

<span class="tok-n">std</span><span class="tok-o">::</span><span class="tok-n">cout</span> <span class="tok-o">&lt;&lt;</span> <span class="tok-s">&quot;width = &quot;</span> <span class="tok-o">&lt;&lt;</span> <span class="tok-n">width</span> <span class="tok-o">&lt;&lt;</span> <span class="tok-s">&quot;, name = &quot;</span> <span class="tok-o">&lt;&lt;</span> <span class="tok-n">name</span> <span class="tok-o">&lt;&lt;</span> <span class="tok-s">&quot;, doIt = &quot;</span> <span class="tok-o">&lt;&lt;</span> <span class="tok-n">doIt</span> <span class="tok-o">&lt;&lt;</span> <span class="tok-s">&quot;</span><span class="tok-se">\n</span><span class="tok-s">&quot;</span><span class="tok-p">;</span>
<span class="tok-k">return</span> <span class="tok-mi">0</span><span class="tok-p">;</span>
<span class="tok-p">}</span></code></pre>
</div>
</div>
Expand Down Expand Up @@ -834,10 +836,11 @@ <h2 id="_alternate_specification"><a class="anchor" href="#_alternate_specificat
<span class="tok-k">if</span> <span class="tok-p">(</span><span class="tok-o">!</span><span class="tok-n">result</span><span class="tok-p">)</span>
<span class="tok-p">{</span>
<span class="tok-n">std</span><span class="tok-o">::</span><span class="tok-n">cerr</span> <span class="tok-o">&lt;&lt;</span> <span class="tok-s">&quot;Error in command line: &quot;</span> <span class="tok-o">&lt;&lt;</span> <span class="tok-n">result</span><span class="tok-p">.</span><span class="tok-n">errorMessage</span><span class="tok-p">()</span> <span class="tok-o">&lt;&lt;</span> <span class="tok-n">std</span><span class="tok-o">::</span><span class="tok-n">endl</span><span class="tok-p">;</span>
<span class="tok-n">std</span><span class="tok-o">::</span><span class="tok-n">exit</span><span class="tok-p">(</span><span class="tok-mi">1</span><span class="tok-p">);</span>
<span class="tok-k">return</span> <span class="tok-mi">1</span><span class="tok-p">;</span>
<span class="tok-p">}</span>

<span class="tok-n">std</span><span class="tok-o">::</span><span class="tok-n">cout</span> <span class="tok-o">&lt;&lt;</span> <span class="tok-s">&quot;width = &quot;</span> <span class="tok-o">&lt;&lt;</span> <span class="tok-n">width</span> <span class="tok-o">&lt;&lt;</span> <span class="tok-s">&quot;</span><span class="tok-se">\n</span><span class="tok-s">&quot;</span><span class="tok-p">;</span>
<span class="tok-k">return</span> <span class="tok-mi">0</span><span class="tok-p">;</span>
<span class="tok-p">}</span></code></pre>
</div>
</div>
Expand Down Expand Up @@ -874,10 +877,11 @@ <h2 id="_alternate_specification"><a class="anchor" href="#_alternate_specificat
<span class="tok-k">if</span> <span class="tok-p">(</span><span class="tok-o">!</span><span class="tok-n">result</span><span class="tok-p">)</span>
<span class="tok-p">{</span>
<span class="tok-n">std</span><span class="tok-o">::</span><span class="tok-n">cerr</span> <span class="tok-o">&lt;&lt;</span> <span class="tok-s">&quot;Error in command line: &quot;</span> <span class="tok-o">&lt;&lt;</span> <span class="tok-n">result</span><span class="tok-p">.</span><span class="tok-n">errorMessage</span><span class="tok-p">()</span> <span class="tok-o">&lt;&lt;</span> <span class="tok-n">std</span><span class="tok-o">::</span><span class="tok-n">endl</span><span class="tok-p">;</span>
<span class="tok-n">std</span><span class="tok-o">::</span><span class="tok-n">exit</span><span class="tok-p">(</span><span class="tok-mi">1</span><span class="tok-p">);</span>
<span class="tok-k">return</span> <span class="tok-mi">1</span><span class="tok-p">;</span>
<span class="tok-p">}</span>

<span class="tok-n">std</span><span class="tok-o">::</span><span class="tok-n">cout</span> <span class="tok-o">&lt;&lt;</span> <span class="tok-s">&quot;width = &quot;</span> <span class="tok-o">&lt;&lt;</span> <span class="tok-n">width</span> <span class="tok-o">&lt;&lt;</span> <span class="tok-s">&quot;, name = &quot;</span> <span class="tok-o">&lt;&lt;</span> <span class="tok-n">name</span> <span class="tok-o">&lt;&lt;</span> <span class="tok-s">&quot;, doIt = &quot;</span> <span class="tok-o">&lt;&lt;</span> <span class="tok-n">doIt</span> <span class="tok-o">&lt;&lt;</span> <span class="tok-s">&quot;</span><span class="tok-se">\n</span><span class="tok-s">&quot;</span><span class="tok-p">;</span>
<span class="tok-k">return</span> <span class="tok-mi">0</span><span class="tok-p">;</span>
<span class="tok-p">}</span></code></pre>
</div>
</div>
Expand Down Expand Up @@ -940,17 +944,18 @@ <h2 id="_help_option"><a class="anchor" href="#_help_option"></a>4. Help Option<
<span class="tok-p">{</span>
<span class="tok-n">std</span><span class="tok-o">::</span><span class="tok-n">cerr</span> <span class="tok-o">&lt;&lt;</span> <span class="tok-s">&quot;Error in command line: &quot;</span> <span class="tok-o">&lt;&lt;</span> <span class="tok-n">result</span><span class="tok-p">.</span><span class="tok-n">errorMessage</span><span class="tok-p">()</span> <span class="tok-o">&lt;&lt;</span> <span class="tok-n">std</span><span class="tok-o">::</span><span class="tok-n">endl</span><span class="tok-p">;</span>
<span class="tok-n">std</span><span class="tok-o">::</span><span class="tok-n">cerr</span> <span class="tok-o">&lt;&lt;</span> <span class="tok-n">cli</span> <span class="tok-o">&lt;&lt;</span> <span class="tok-s">&quot;</span><span class="tok-se">\n</span><span class="tok-s">&quot;</span><span class="tok-p">;</span> <i class="conum" data-value="1"></i><b>(1)</b>
<span class="tok-n">exit</span><span class="tok-p">(</span><span class="tok-mi">1</span><span class="tok-p">);</span>
<span class="tok-k">return</span> <span class="tok-mi">1</span><span class="tok-p">;</span>
<span class="tok-p">}</span>

<span class="tok-c1">// Show the help when asked for.</span>
<span class="tok-k">if</span> <span class="tok-p">(</span><span class="tok-n">show_help</span><span class="tok-p">)</span> <i class="conum" data-value="2"></i><b>(2)</b>
<span class="tok-p">{</span>
<span class="tok-n">std</span><span class="tok-o">::</span><span class="tok-n">cout</span> <span class="tok-o">&lt;&lt;</span> <span class="tok-n">cli</span> <span class="tok-o">&lt;&lt;</span> <span class="tok-s">&quot;</span><span class="tok-se">\n</span><span class="tok-s">&quot;</span><span class="tok-p">;</span>
<span class="tok-n">exit</span><span class="tok-p">(</span><span class="tok-mi">0</span><span class="tok-p">);</span>
<span class="tok-k">return</span> <span class="tok-mi">0</span><span class="tok-p">;</span>
<span class="tok-p">}</span>

<span class="tok-n">std</span><span class="tok-o">::</span><span class="tok-n">cout</span> <span class="tok-o">&lt;&lt;</span> <span class="tok-s">&quot;width = &quot;</span> <span class="tok-o">&lt;&lt;</span> <span class="tok-n">width</span> <span class="tok-o">&lt;&lt;</span> <span class="tok-s">&quot;, name = &quot;</span> <span class="tok-o">&lt;&lt;</span> <span class="tok-n">name</span> <span class="tok-o">&lt;&lt;</span> <span class="tok-s">&quot;, doIt = &quot;</span> <span class="tok-o">&lt;&lt;</span> <span class="tok-n">doIt</span> <span class="tok-o">&lt;&lt;</span> <span class="tok-s">&quot;</span><span class="tok-se">\n</span><span class="tok-s">&quot;</span><span class="tok-p">;</span>
<span class="tok-k">return</span> <span class="tok-mi">0</span><span class="tok-p">;</span>
<span class="tok-p">}</span></code></pre>
</div>
</div>
Expand Down Expand Up @@ -992,10 +997,12 @@ <h2 id="_value_choices"><a class="anchor" href="#_value_choices"></a>5. Value Ch
<span class="tok-k">if</span> <span class="tok-p">(</span><span class="tok-n">result</span><span class="tok-p">)</span>
<span class="tok-p">{</span>
<span class="tok-n">std</span><span class="tok-o">::</span><span class="tok-n">cout</span> <span class="tok-o">&lt;&lt;</span> <span class="tok-s">&quot;Your preferred color is &quot;</span> <span class="tok-o">&lt;&lt;</span> <span class="tok-n">choice</span> <span class="tok-o">&lt;&lt;</span> <span class="tok-s">&quot;</span><span class="tok-se">\n</span><span class="tok-s">&quot;</span><span class="tok-p">;</span>
<span class="tok-k">return</span> <span class="tok-mi">0</span><span class="tok-p">;</span>
<span class="tok-p">}</span>
<span class="tok-k">else</span>
<span class="tok-p">{</span>
<span class="tok-n">std</span><span class="tok-o">::</span><span class="tok-n">cerr</span> <span class="tok-o">&lt;&lt;</span> <span class="tok-n">result</span><span class="tok-p">.</span><span class="tok-n">errorMessage</span><span class="tok-p">()</span> <span class="tok-o">&lt;&lt;</span> <span class="tok-s">&quot;</span><span class="tok-se">\n</span><span class="tok-s">&quot;</span><span class="tok-p">;</span>
<span class="tok-k">return</span> <span class="tok-mi">1</span><span class="tok-p">;</span>
<span class="tok-p">}</span>
<span class="tok-p">}</span></code></pre>
</div>
Expand All @@ -1020,10 +1027,12 @@ <h2 id="_value_choices"><a class="anchor" href="#_value_choices"></a>5. Value Ch
<span class="tok-k">if</span> <span class="tok-p">(</span><span class="tok-n">result</span><span class="tok-p">)</span>
<span class="tok-p">{</span>
<span class="tok-n">std</span><span class="tok-o">::</span><span class="tok-n">cout</span> <span class="tok-o">&lt;&lt;</span> <span class="tok-s">&quot;Your number between one and ten is &quot;</span> <span class="tok-o">&lt;&lt;</span> <span class="tok-n">choice</span> <span class="tok-o">&lt;&lt;</span> <span class="tok-s">&quot;</span><span class="tok-se">\n</span><span class="tok-s">&quot;</span><span class="tok-p">;</span>
<span class="tok-k">return</span> <span class="tok-mi">0</span><span class="tok-p">;</span>
<span class="tok-p">}</span>
<span class="tok-k">else</span>
<span class="tok-p">{</span>
<span class="tok-n">std</span><span class="tok-o">::</span><span class="tok-n">cerr</span> <span class="tok-o">&lt;&lt;</span> <span class="tok-n">result</span><span class="tok-p">.</span><span class="tok-n">errorMessage</span><span class="tok-p">()</span> <span class="tok-o">&lt;&lt;</span> <span class="tok-s">&quot;</span><span class="tok-se">\n</span><span class="tok-s">&quot;</span><span class="tok-p">;</span>
<span class="tok-k">return</span> <span class="tok-mi">1</span><span class="tok-p">;</span>
<span class="tok-p">}</span>
<span class="tok-p">}</span></code></pre>
</div>
Expand Down Expand Up @@ -1647,6 +1656,14 @@ <h3 id="_1_3"><a class="anchor" href="#_1_3"></a>9.1. 1.3</h3>
<p>Fixed allowing junk extra characters after a non-string option value.&#8201;&#8212;&#8201;<em>Rene Rivera</em></p>
</li>
<li>
<p>Support specifying a single value for choices of an argument.&#8201;&#8212;&#8201;<em>Rene Rivera</em></p>
</li>
<li>
<p>Fix various problems with the commands example program. Also now the
examples for the documentation are unit tested along with the
regular unit tests.&#8201;&#8212;&#8201;<em>Rene Rivera</em></p>
</li>
<li>
<p>Tested with
Visual Studio 2015, VS 2017, VS 2019,
MinGW-64 (gcc 8.1),
Expand Down
3 changes: 2 additions & 1 deletion examples/doc_example1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@ int main(int argc, const char** argv)
if (!result)
{
std::cerr << "Error in command line: " << result.errorMessage() << std::endl;
std::exit(1);
return 1;
}

std::cout << "width = " << width << "\n";
return 0;
}
// end::part2[]
3 changes: 2 additions & 1 deletion examples/doc_example1_alt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@ int main(int argc, const char** argv)
if (!result)
{
std::cerr << "Error in command line: " << result.errorMessage() << std::endl;
std::exit(1);
return 1;
}

std::cout << "width = " << width << "\n";
return 0;
}
// end::doc[]
3 changes: 2 additions & 1 deletion examples/doc_example2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,10 @@ int main(int argc, const char** argv)
if (!result)
{
std::cerr << "Error in command line: " << result.errorMessage() << std::endl;
std::exit(1);
return 1;
}

std::cout << "width = " << width << ", name = " << name << ", doIt = " << doIt << "\n";
return 0;
}
// end::part2[]
3 changes: 2 additions & 1 deletion examples/doc_example2_alt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,10 @@ int main(int argc, const char** argv)
if (!result)
{
std::cerr << "Error in command line: " << result.errorMessage() << std::endl;
std::exit(1);
return 1;
}

std::cout << "width = " << width << ", name = " << name << ", doIt = " << doIt << "\n";
return 0;
}
// end::doc[]
5 changes: 3 additions & 2 deletions examples/doc_example3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,17 @@ int main(int argc, const char** argv)
{
std::cerr << "Error in command line: " << result.errorMessage() << std::endl;
std::cerr << cli << "\n"; // <1>
exit(1);
return 1;
}

// Show the help when asked for.
if (show_help) // <2>
{
std::cout << cli << "\n";
exit(0);
return 0;
}

std::cout << "width = " << width << ", name = " << name << ", doIt = " << doIt << "\n";
return 0;
}
// end::part2[]
1 change: 1 addition & 0 deletions examples/doc_example4.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@ int main(int argc, const char** argv)
for (int count = 0; count < repeat; ++count)
std::cout << message << "\n";
}
return 0;
}
// end::part1[]
2 changes: 2 additions & 0 deletions examples/doc_example5.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ int main(int argc, const char** argv)
if (result)
{
std::cout << "Your preferred color is " << choice << "\n";
return 0;
}
else
{
std::cerr << result.errorMessage() << "\n";
return 1;
}
}
// end::part1[]
2 changes: 2 additions & 0 deletions examples/doc_example6.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ int main(int argc, const char** argv)
if (result)
{
std::cout << "Your number between one and ten is " << choice << "\n";
return 0;
}
else
{
std::cerr << result.errorMessage() << "\n";
return 1;
}
}
// end::part1[]
23 changes: 23 additions & 0 deletions tests/doc_example1_alt_run_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
Copyright Rene Rivera 2020
Distributed under the Boost Software License, Version 1.0.
(See accompanying file LICENSE.txt or copy at
http://www.boost.org/LICENSE_1_0.txt)
*/

#include "main_test.hpp"

#include "doc_example1_alt.cpp"

#include "main_test.hpp"

int main()
{
bfg::mini_test::scope test;
{
TEST_MAIN(test, "doc_example1_alt", "-w", "100");
TEST_MAIN(test, "doc_example1_alt", "--width=100");
TEST_MAIN_FAIL(test, "doc_example1_alt", "what");
}
return test;
}
23 changes: 23 additions & 0 deletions tests/doc_example1_run_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
Copyright Rene Rivera 2020
Distributed under the Boost Software License, Version 1.0.
(See accompanying file LICENSE.txt or copy at
http://www.boost.org/LICENSE_1_0.txt)
*/

#include "main_test.hpp"

#include "doc_example1.cpp"

#include "main_test.hpp"

int main()
{
bfg::mini_test::scope test;
{
TEST_MAIN(test, "doc_example1", "-w", "100");
TEST_MAIN(test, "doc_example1", "--width=100");
TEST_MAIN_FAIL(test, "doc_example1", "what");
}
return test;
}
24 changes: 24 additions & 0 deletions tests/doc_example2_alt_run_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
Copyright Rene Rivera 2020
Distributed under the Boost Software License, Version 1.0.
(See accompanying file LICENSE.txt or copy at
http://www.boost.org/LICENSE_1_0.txt)
*/

#include "main_test.hpp"

#include "doc_example2_alt.cpp"

#include "main_test.hpp"

int main()
{
bfg::mini_test::scope test;
{
TEST_MAIN(test, "doc_example2_alt", "-w", "100");
TEST_MAIN(test, "doc_example2_alt", "--width=100");
TEST_MAIN(test, "doc_example2_alt", "-d", "-n", "thing");
TEST_MAIN_FAIL(test, "doc_example2_alt", "what");
}
return test;
}
24 changes: 24 additions & 0 deletions tests/doc_example2_run_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
Copyright Rene Rivera 2020
Distributed under the Boost Software License, Version 1.0.
(See accompanying file LICENSE.txt or copy at
http://www.boost.org/LICENSE_1_0.txt)
*/

#include "main_test.hpp"

#include "doc_example2.cpp"

#include "main_test.hpp"

int main()
{
bfg::mini_test::scope test;
{
TEST_MAIN(test, "doc_example2", "-w", "100");
TEST_MAIN(test, "doc_example2", "--width=100");
TEST_MAIN(test, "doc_example2", "-d", "-n", "thing");
TEST_MAIN_FAIL(test, "doc_example2", "what");
}
return test;
}
24 changes: 24 additions & 0 deletions tests/doc_example3_run_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
Copyright Rene Rivera 2020
Distributed under the Boost Software License, Version 1.0.
(See accompanying file LICENSE.txt or copy at
http://www.boost.org/LICENSE_1_0.txt)
*/

#include "main_test.hpp"

#include "doc_example3.cpp"

#include "main_test.hpp"

int main()
{
bfg::mini_test::scope test;
{
TEST_MAIN(test, "doc_example3", "-w", "100");
TEST_MAIN(test, "doc_example3", "--width=100");
TEST_MAIN(test, "doc_example3", "-d", "-n", "thing");
TEST_MAIN_FAIL(test, "doc_example3", "what");
}
return test;
}
22 changes: 22 additions & 0 deletions tests/doc_example4_run_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
Copyright Rene Rivera 2020
Distributed under the Boost Software License, Version 1.0.
(See accompanying file LICENSE.txt or copy at
http://www.boost.org/LICENSE_1_0.txt)
*/

#include "main_test.hpp"

#include "doc_example4.cpp"

#include "main_test.hpp"

int main()
{
bfg::mini_test::scope test;
{
TEST_MAIN(test, "doc_example4", "--repeat=100", "qwerty");
TEST_MAIN(test, "doc_example4", "qwerty");
}
return test;
}
Loading

0 comments on commit 9e31b21

Please sign in to comment.