The Battle for Wesnoth  1.17.0-dev
test_make_enum.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014 - 2021
3  by Chris Beck <render787@gmail.com>
4  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
16 #define GETTEXT_DOMAIN "wesnoth-test"
17 
18 #include <boost/test/unit_test.hpp>
19 
20 #include "config.hpp"
21 #include "lexical_cast.hpp"
22 #include "utils/make_enum.hpp"
23 
24 namespace foo {
25 
26  MAKE_ENUM(enumname,
27  (con1, "name1")
28  (con2, "name2")
29  (con3, "name3")
30  )
31 }
32 
33 
34 //generates an enum foo::enumname with lexical casts
35 /*
36 namespace foo {
37 enum enumname {con1, con2 ,con3}
38 }
39 
40 foo::enumname lexical_cast<std::string> ( std::string str ) throws bad_lexical_cast
41 {
42  ...
43 }
44 */
45 
46 class bar {
47 public:
48  MAKE_ENUM(another,
49  (val1, "name1")
50  (val2, "name2")
51  (val3, "name3")
52  )
53 };
54 
55 
56 /** Tests begin **/
57 
58 BOOST_AUTO_TEST_SUITE ( test_make_enum )
59 
60 BOOST_AUTO_TEST_CASE ( test_make_enum_namespace )
61 {
62  foo::enumname e = foo::enumname::string_to_enum("name2", foo::enumname::con1); //returns con2
63 
64  BOOST_CHECK_EQUAL ( e, foo::enumname::con2 );
65 
66  std::string str = foo::enumname::enum_to_string(foo::enumname::con1); //returns "name1"
67 
68  BOOST_CHECK_EQUAL ( str, "name1" );
69 
70  std::string str2 = lexical_cast<std::string> (e); //returns "name2" since e is con2
71 
72  BOOST_CHECK_EQUAL ( str2, "name2" );
73 
74  bool threw_exception_when_it_wasnt_supposed_to = false;
75 
76  try {
77  e = lexical_cast<foo::enumname> ("name3"); //returns con3
78  } catch (const bad_lexical_cast & /*e*/) {
79  //std::cerr << "enum lexical cast didn't work!" << std::endl;
80  threw_exception_when_it_wasnt_supposed_to = true;
81  }
82 
83  BOOST_CHECK( !threw_exception_when_it_wasnt_supposed_to );
84 
85  bool threw_exception_when_it_was_supposed_to = false;
86 
87  try {
88  e = lexical_cast<foo::enumname> ("name4"); //throw bad_lexical_cast
89  } catch (const bad_lexical_cast & /*e*/) {
90  //std::cerr << "enum lexical cast worked!" << std::endl;
91  threw_exception_when_it_was_supposed_to = true;
92  }
93 
94  BOOST_CHECK( threw_exception_when_it_was_supposed_to );
95 
96  std::stringstream ss;
97  ss << e;
98  BOOST_CHECK_EQUAL (ss.str(), "name3");
99 }
100 
101 BOOST_AUTO_TEST_CASE ( test_make_enum_class )
102 {
103  bar::another e = bar::another::string_to_enum("name2", bar::another::val1); //returns val2
104 
105  BOOST_CHECK_EQUAL ( e, bar::another::val2 );
106 
107  std::string str = bar::another::enum_to_string(bar::another::val1); //returns "name1"
108 
109  BOOST_CHECK_EQUAL ( str, "name1" );
110 
111  std::string str2 = lexical_cast<std::string> (e); //returns "name2" since e is val2
112 
113  BOOST_CHECK_EQUAL ( str2, "name2" );
114 
115  bool threw_exception_when_it_wasnt_supposed_to = false;
116 
117  try {
118  e = lexical_cast<bar::another> ("name3"); //returns val3
119  } catch(const bad_lexical_cast & /*e*/) {
120  //std::cerr << "enum lexical cast didn't work!" << std::endl;
121  threw_exception_when_it_wasnt_supposed_to = true;
122  }
123 
124  BOOST_CHECK( !threw_exception_when_it_wasnt_supposed_to );
125 
126  bool threw_exception_when_it_was_supposed_to = false;
127 
128  try {
129  e = lexical_cast<bar::another> ("name4"); //throw bad_lexical_cast
130  } catch (const bad_lexical_cast & /*e*/) {
131  //std::cerr << "enum lexical cast worked!" << std::endl;
132  threw_exception_when_it_was_supposed_to = true;
133  }
134 
135  BOOST_CHECK( threw_exception_when_it_was_supposed_to );
136 
137  std::stringstream ss;
138  ss << e;
139  BOOST_CHECK_EQUAL (ss.str(), "name3");
140 }
141 
142 BOOST_AUTO_TEST_CASE ( test_make_enum_config )
143 {
144  config cfg;
145  foo::enumname e1 = foo::enumname::con1;
146  foo::enumname e2 = foo::enumname::con2;
147  foo::enumname e3 = foo::enumname::con3;
148 
149  cfg["t1"] = e2;
150  cfg["t2"] = foo::enumname::enum_to_string(e2);
151  cfg["t3"] = "name2";
152  cfg["t4"] = "345646";
153 
154  BOOST_CHECK_EQUAL(cfg["t1"].to_enum<foo::enumname>(foo::enumname::con1) , e2);
155  BOOST_CHECK_EQUAL(cfg["t2"].to_enum<foo::enumname>(foo::enumname::con1) , e2);
156  BOOST_CHECK_EQUAL(cfg["t3"].to_enum<foo::enumname>(foo::enumname::con1) , e2);
157 
158  BOOST_CHECK_EQUAL(cfg["t1"].to_enum(e1) , e2);
159  BOOST_CHECK_EQUAL(cfg["t2"].to_enum(e1) , e2);
160  BOOST_CHECK_EQUAL(cfg["t3"].to_enum(e1) , e2);
161 
162 
163  BOOST_CHECK_EQUAL(cfg["t4"].to_enum<foo::enumname>(foo::enumname::con3) , e3);
164 
165  BOOST_CHECK_EQUAL(cfg["t1"].str() , "name2");
166 }
167 
168 
169 BOOST_AUTO_TEST_CASE ( test_make_enum_parse )
170 {
171  config cfg;
172  foo::enumname e1 = foo::enumname::con1;
173  foo::enumname e2 = foo::enumname::con2;
174  foo::enumname e3 = foo::enumname::con3;
175 
176  cfg["t1"] = e2;
177  cfg["t2"] = foo::enumname::enum_to_string(e1);
178  cfg["t3"] = e1.to_string();
179  cfg["t4"] = "name3";
180  cfg["t5"] = "345646";
181 
182  foo::enumname dummy = foo::enumname::con1;
183 
184  BOOST_CHECK_EQUAL(dummy, e1);
185  BOOST_CHECK_EQUAL(dummy.parse("name1"), true);
186  BOOST_CHECK_EQUAL(dummy, e1);
187  BOOST_CHECK_EQUAL(dummy.parse("name2"), true);
188  BOOST_CHECK_EQUAL(dummy, e2);
189  BOOST_CHECK_EQUAL(dummy.parse("name3"), true);
190  BOOST_CHECK_EQUAL(dummy, e3);
191  BOOST_CHECK_EQUAL(dummy.parse("name2 "), false);
192  BOOST_CHECK_EQUAL(dummy, e3);
193  BOOST_CHECK_EQUAL(dummy.parse("kdfg89"), false);
194  BOOST_CHECK_EQUAL(dummy, e3);
195  BOOST_CHECK_EQUAL(dummy.parse("NAME2"), false);
196  BOOST_CHECK_EQUAL(dummy, e3);
197  BOOST_CHECK_EQUAL(dummy.parse(""), false);
198  BOOST_CHECK_EQUAL(dummy, e3);
199  BOOST_CHECK_EQUAL(dummy.parse("name2"), true);
200  BOOST_CHECK_EQUAL(dummy, e2);
201 
202  BOOST_CHECK_EQUAL(foo::enumname::name(), "enumname");
203 
204 
205 }
206 
207 
208 BOOST_AUTO_TEST_SUITE_END()
int dummy
Definition: lstrlib.cpp:1347
#define MAKE_ENUM(NAME, CONTENT)
Definition: make_enum.hpp:157
New lexcical_cast header.
To lexical_cast(From value)
Lexical cast converts one type to another.
Definitions for the interface to Wesnoth Markup Language (WML).
BOOST_AUTO_TEST_SUITE(filesystem)
BOOST_AUTO_TEST_CASE(test_make_enum_namespace)
Tests begin.
#define e
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:61
Thrown when a lexical_cast fails.
Defines the MAKE_ENUM macro.