123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 |
- // 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"
- " (mymapping (a 1) (b 2))\n"
- ")\n");
- auto doc = ReaderDocument::parse(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);
- }
- {
- 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);
- }
- {
- 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::parse(in);
- auto root = doc.get_root();
- ASSERT_EQ("supertux-test", root.get_name());
- auto mapping = root.get_mapping();
- bool mybool;
- int myint;
- float myfloat;
- 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 */
|