mirror of
https://github.com/ToruNiina/toml11.git
synced 2025-09-17 00:38:08 +08:00
test: use the test suite in the effective way
add tests/check_toml_test.cpp to compare json object
This commit is contained in:
@@ -1,7 +1,22 @@
|
||||
version: 2.1
|
||||
|
||||
jobs:
|
||||
test:
|
||||
test_suite:
|
||||
environment:
|
||||
- GOPATH: /home/circleci/go
|
||||
docker:
|
||||
- image: circleci/golang:1.9
|
||||
steps:
|
||||
- checkout
|
||||
- run:
|
||||
command: |
|
||||
g++ --version
|
||||
cd tests/
|
||||
ls
|
||||
g++ -std=c++11 -O2 -Wall -Wextra -Werror -I../ check_toml_test.cpp -o check_toml_test
|
||||
go get github.com/BurntSushi/toml-test
|
||||
$GOPATH/bin/toml-test ./check_toml_test
|
||||
output_result:
|
||||
docker:
|
||||
- image: circleci/buildpack-deps:bionic
|
||||
steps:
|
||||
@@ -42,4 +57,5 @@ workflows:
|
||||
version: 2.1
|
||||
test:
|
||||
jobs:
|
||||
- test
|
||||
- test_suite
|
||||
- output_result
|
||||
|
142
tests/check_toml_test.cpp
Normal file
142
tests/check_toml_test.cpp
Normal file
@@ -0,0 +1,142 @@
|
||||
#include "toml.hpp"
|
||||
#include <iostream>
|
||||
#include <iomanip>
|
||||
|
||||
struct json_serializer
|
||||
{
|
||||
void operator()(toml::boolean v)
|
||||
{
|
||||
std::cout << "{\"type\":\"bool\",\"value\":\"" << toml::value(v) << "\"}";
|
||||
return ;
|
||||
}
|
||||
void operator()(toml::integer v)
|
||||
{
|
||||
std::cout << "{\"type\":\"integer\",\"value\":\"" << toml::value(v) << "\"}";
|
||||
return ;
|
||||
}
|
||||
void operator()(toml::floating v)
|
||||
{
|
||||
std::cout << "{\"type\":\"float\",\"value\":\"" << toml::value(v) << "\"}";
|
||||
return ;
|
||||
}
|
||||
void operator()(const toml::string& v)
|
||||
{
|
||||
// since toml11 automatically convert string to multiline string that is
|
||||
// valid only in TOML, we need to format the string to make it valid in
|
||||
// JSON.
|
||||
std::cout << "{\"type\":\"string\",\"value\":\""
|
||||
<< this->escape_string(v.str) << "\"}";
|
||||
return ;
|
||||
}
|
||||
void operator()(const toml::local_time& v)
|
||||
{
|
||||
std::cout << "{\"type\":\"local_time\",\"value\":\"" << toml::value(v) << "\"}";
|
||||
return ;
|
||||
}
|
||||
void operator()(const toml::local_date& v)
|
||||
{
|
||||
std::cout << "{\"type\":\"local_date\",\"value\":\"" << toml::value(v) << "\"}";
|
||||
return ;
|
||||
}
|
||||
void operator()(const toml::local_datetime& v)
|
||||
{
|
||||
std::cout << "{\"type\":\"local_datetime\",\"value\":\"" << toml::value(v) << "\"}";
|
||||
return ;
|
||||
}
|
||||
void operator()(const toml::offset_datetime& v)
|
||||
{
|
||||
std::cout << "{\"type\":\"datetime\",\"value\":\"" << toml::value(v) << "\"}";
|
||||
return ;
|
||||
}
|
||||
void operator()(const toml::array& v)
|
||||
{
|
||||
if(!v.empty() && v.front().is_table())
|
||||
{
|
||||
std::cout << '[';
|
||||
bool is_first = true;
|
||||
for(const auto& elem : v)
|
||||
{
|
||||
if(!is_first) {std::cout << ", ";}
|
||||
is_first = false;
|
||||
toml::visit(*this, elem);
|
||||
}
|
||||
std::cout << ']';
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cout << "{\"type\":\"array\",\"value\":[";
|
||||
bool is_first = true;
|
||||
for(const auto& elem : v)
|
||||
{
|
||||
if(!is_first) {std::cout << ", ";}
|
||||
is_first = false;
|
||||
toml::visit(*this, elem);
|
||||
}
|
||||
std::cout << "]}";
|
||||
}
|
||||
return ;
|
||||
}
|
||||
void operator()(const toml::table& v)
|
||||
{
|
||||
std::cout << '{';
|
||||
bool is_first = true;
|
||||
for(const auto& elem : v)
|
||||
{
|
||||
if(!is_first) {std::cout << ", ";}
|
||||
is_first = false;
|
||||
std::cout << toml::format(toml::string(elem.first),
|
||||
std::numeric_limits<std::size_t>::max());
|
||||
std::cout << ':';
|
||||
toml::visit(*this, elem.second);
|
||||
}
|
||||
std::cout << '}';
|
||||
return ;
|
||||
}
|
||||
|
||||
std::string escape_string(const std::string& s) const
|
||||
{
|
||||
std::string retval;
|
||||
for(const char c : s)
|
||||
{
|
||||
switch(c)
|
||||
{
|
||||
case '\\': {retval += "\\\\"; break;}
|
||||
case '\"': {retval += "\\\""; break;}
|
||||
case '\b': {retval += "\\b"; break;}
|
||||
case '\t': {retval += "\\t"; break;}
|
||||
case '\f': {retval += "\\f"; break;}
|
||||
case '\n': {retval += "\\n"; break;}
|
||||
case '\r': {retval += "\\r"; break;}
|
||||
default : {retval += c; break;}
|
||||
}
|
||||
}
|
||||
return retval;
|
||||
}
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
try
|
||||
{
|
||||
std::vector<char> buf;
|
||||
std::cin.peek();
|
||||
while(!std::cin.eof())
|
||||
{
|
||||
buf.push_back(std::cin.get());
|
||||
std::cin.peek();
|
||||
}
|
||||
std::string bufstr(buf.begin(), buf.end());
|
||||
|
||||
std::istringstream ss(bufstr);
|
||||
|
||||
const auto data = toml::parse(ss);
|
||||
std::cout << std::setprecision(std::numeric_limits<double>::max_digits10);
|
||||
toml::visit(json_serializer(), data);
|
||||
return 0;
|
||||
}
|
||||
catch(const toml::syntax_error& err)
|
||||
{
|
||||
std::cout << "what(): " << err.what() << std::endl;
|
||||
return 1;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user