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