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 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
|
// SuperTux
// Copyright (C) 2015 Ingo Ruhnke <grumbel@gmail.com>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
#include <gtest/gtest.h>
#include "util/reader_document.hpp"
#include "util/reader_mapping.hpp"
TEST(ReaderTest, get)
{
std::istringstream in(
"(supertux-test\n"
" (mybool #t)\r"
" (myint 123456789)\r\n"
" (myfloat 1.125)\n\r"
" (mystring \"Hello World\")\n"
" (mystringtrans (_ \"Hello World\"))\n"
" (myboolarray #t #f #t #f)\n"
" (myintarray 5 4 3 2 1 0)\n"
" (myfloatarray 6.5 5.25 4.125 3.0625 2.0 1.0 0.5 0.25 0.125)\n"
" (mystringarray \"One\" \"Two\" \"Three\")\n"
" (mymapping (a 1) (b 2))\n"
" (mycustom \"1234\")\n"
")\n");
auto doc = ReaderDocument::from_stream(in);
auto root = doc.get_root();
ASSERT_EQ("supertux-test", root.get_name());
auto mapping = root.get_mapping();
{
bool mybool;
mapping.get("mybool", mybool);
ASSERT_EQ(true, mybool);
}
{
int myint;
mapping.get("myint", myint);
ASSERT_EQ(123456789, myint);
}
{
float myfloat;
mapping.get("myfloat", myfloat);
ASSERT_EQ(1.125, myfloat);
}
{
std::string mystring;
mapping.get("mystring", mystring);
ASSERT_EQ("Hello World", mystring);
}
{
std::string mystringtrans;
mapping.get("mystringtrans", mystringtrans);
ASSERT_EQ("Hello World", mystringtrans);
}
{
std::vector<bool> expected{ true, false, true, false };
std::vector<bool> result;
mapping.get("myboolarray", result);
ASSERT_EQ(expected, result);
}
{
std::vector<int> expected{ 5, 4, 3, 2, 1, 0 };
std::vector<int> result;
mapping.get("myintarray", result);
ASSERT_EQ(expected, result);
}
{
std::vector<float> expected({6.5f, 5.25f, 4.125f, 3.0625f, 2.0f, 1.0f, 0.5f, 0.25f, 0.125f});
std::vector<float> result;
mapping.get("myfloatarray", result);
ASSERT_EQ(expected, result);
}
{
std::vector<std::string> expected{"One", "Two", "Three"};
std::vector<std::string> result;
mapping.get("mystringarray", result);
ASSERT_EQ(expected, result);
}
{
boost::optional<ReaderMapping> child_mapping;
mapping.get("mymapping", child_mapping);
int a;
child_mapping->get("a", a);
ASSERT_EQ(1, a);
int b;
child_mapping->get("b", b);
ASSERT_EQ(2, b);
}
{
auto from_string = [](const std::string& text){ return std::stoi(text); };
int value = 0;
mapping.get_custom("mycustom", value, from_string);
ASSERT_EQ(1234, value);
int value2 = 0;
mapping.get_custom("does-not-exist", value2, from_string);
ASSERT_EQ(0, value2);
int value3 = 0;
mapping.get_custom("does-not-exist", value3, from_string, 4321);
ASSERT_EQ(4321, value3);
}
{
bool mybool;
int myint;
float myfloat;
ASSERT_THROW({mapping.get("mybool", myfloat);}, std::runtime_error);
ASSERT_THROW({mapping.get("myint", mybool);}, std::runtime_error);
ASSERT_THROW({mapping.get("myfloat", myint);}, std::runtime_error);
ASSERT_THROW({mapping.get("mymapping", myint);}, std::runtime_error);
}
}
TEST(ReaderTest, syntax_error)
{
std::istringstream in(
"(supertux-test\n"
" (mybool #t err)\r"
" (myint 123456789 err)\r\n"
" (myfloat 1.125 err)\n\r"
" (mystring \"Hello World\" err)\n"
" (mystringtrans (_ \"Hello World\" err))\n"
" (mymapping err (a 1) (b 2))\n"
")\n");
auto doc = ReaderDocument::from_stream(in);
auto root = doc.get_root();
ASSERT_EQ("supertux-test", root.get_name());
auto mapping = root.get_mapping();
bool mybool;
int myint;
float myfloat;
boost::optional<ReaderMapping> mymapping;
ASSERT_THROW({mapping.get("mybool", mybool);}, std::runtime_error);
ASSERT_THROW({mapping.get("myint", myint);}, std::runtime_error);
ASSERT_THROW({mapping.get("myfloat", myfloat);}, std::runtime_error);
mapping.get("mymapping", mymapping);
ASSERT_THROW({mymapping->get("a", myint);}, std::runtime_error);
ASSERT_THROW({mymapping->get("b", myint);}, std::runtime_error);
}
/* EOF */
|