-
Notifications
You must be signed in to change notification settings - Fork 280
/
cata_variant_test.cpp
128 lines (115 loc) · 4.64 KB
/
cata_variant_test.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include "catch/catch.hpp"
#include <sstream>
#include <string>
#include <utility>
#include "cata_variant.h"
#include "character_id.h"
#include "mutation.h"
#include "enum_conversions.h"
#include "item.h"
#include "json.h"
#include "string_id.h"
#include "type_id.h"
TEST_CASE( "variant_construction", "[variant]" )
{
SECTION( "itype_id" ) {
cata_variant v = cata_variant::make<cata_variant_type::itype_id>( itype_id( "anvil" ) );
CHECK( v.type() == cata_variant_type::itype_id );
CHECK( v.get<cata_variant_type::itype_id>() == itype_id( "anvil" ) );
CHECK( v.get<itype_id>() == itype_id( "anvil" ) );
cata_variant v2 = cata_variant( itype_id( "anvil" ) );
CHECK( v2.type() == cata_variant_type::itype_id );
CHECK( v2.get<cata_variant_type::itype_id>() == itype_id( "anvil" ) );
CHECK( v2.get<itype_id>() == itype_id( "anvil" ) );
itype_id anvil( "anvil" );
cata_variant v3( anvil );
CHECK( v3.type() == cata_variant_type::itype_id );
CHECK( v3.get<cata_variant_type::itype_id>() == itype_id( "anvil" ) );
CHECK( v3.get<itype_id>() == itype_id( "anvil" ) );
}
SECTION( "mtype_id" ) {
cata_variant v = cata_variant::make<cata_variant_type::mtype_id>( mtype_id( "zombie" ) );
CHECK( v.type() == cata_variant_type::mtype_id );
CHECK( v.get<cata_variant_type::mtype_id>() == mtype_id( "zombie" ) );
CHECK( v.get<mtype_id>() == mtype_id( "zombie" ) );
cata_variant v2 = cata_variant( mtype_id( "zombie" ) );
CHECK( v2.type() == cata_variant_type::mtype_id );
CHECK( v2.get<cata_variant_type::mtype_id>() == mtype_id( "zombie" ) );
CHECK( v2.get<mtype_id>() == mtype_id( "zombie" ) );
}
SECTION( "construction_from_const_lvalue" ) {
const character_id i;
cata_variant v( i );
CHECK( v.type() == cata_variant_type::character_id );
CHECK( v.get<character_id>() == i );
}
}
TEST_CASE( "variant_copy_move", "[variant]" )
{
cata_variant v = cata_variant( mtype_id( "zombie" ) );
cata_variant v2 = v;
CHECK( v2.get<mtype_id>() == mtype_id( "zombie" ) );
cata_variant v3( v );
CHECK( v3.get<mtype_id>() == mtype_id( "zombie" ) );
cata_variant v4( std::move( v ) );
CHECK( v4.get<mtype_id>() == mtype_id( "zombie" ) );
cata_variant v5( std::move( v2 ) );
CHECK( v5.get<mtype_id>() == mtype_id( "zombie" ) );
}
TEST_CASE( "variant_type_name_round_trip", "[variant]" )
{
int num_types = static_cast<int>( cata_variant_type::num_types );
for( int i = 0; i < num_types; ++i ) {
cata_variant_type type = static_cast<cata_variant_type>( i );
std::string type_as_string = io::enum_to_string( type );
CHECK( io::string_to_enum<cata_variant_type>( type_as_string ) == type );
}
}
TEST_CASE( "variant_default_constructor", "[variant]" )
{
cata_variant v;
CHECK( v.type() == cata_variant_type::void_ );
CHECK( v.get_string().empty() );
}
TEST_CASE( "variant_serialization", "[variant]" )
{
cata_variant v = cata_variant( mtype_id( "zombie" ) );
std::ostringstream os;
JsonOut jsout( os );
v.serialize( jsout );
CHECK( os.str() == R"(["mtype_id","zombie"])" );
}
TEST_CASE( "variant_deserialization", "[variant]" )
{
std::istringstream is( R"(["mtype_id","zombie"])" );
JsonIn jsin( is );
cata_variant v;
v.deserialize( jsin );
CHECK( v == cata_variant( mtype_id( "zombie" ) ) );
}
TEST_CASE( "variant_from_string" )
{
cata_variant v = cata_variant::from_string( cata_variant_type::mtype_id, "mon_zombie" );
CHECK( v == cata_variant( mtype_id( "mon_zombie" ) ) );
}
TEST_CASE( "variant_type_for", "[variant]" )
{
CHECK( cata_variant_type_for<bool>() == cata_variant_type::bool_ );
CHECK( cata_variant_type_for<int>() == cata_variant_type::int_ );
CHECK( cata_variant_type_for<skill_id>() == cata_variant_type::skill_id );
CHECK( cata_variant_type_for<trait_id>() == cata_variant_type::trait_id );
CHECK( cata_variant_type_for<ter_id>() == cata_variant_type::ter_id );
}
TEST_CASE( "variant_is_valid", "[variant]" )
{
// A string_id
CHECK( cata_variant( mtype_id( "mon_zombie" ) ).is_valid() );
CHECK_FALSE( cata_variant( mtype_id( "This is not a valid id" ) ).is_valid() );
// An int_id
CHECK( cata_variant( ter_id( "t_grass" ) ).is_valid() );
CHECK_FALSE( cata_variant::from_string( cata_variant_type::ter_id, "invalid id" ).is_valid() );
// An enum
CHECK( cata_variant( mutagen_technique::consumed_purifier ).is_valid() );
CHECK_FALSE( cata_variant::from_string(
cata_variant_type::mutagen_technique, "invalid enum" ).is_valid() );
}