From 3bfa7f09ba9bbf2f10d4f5e260ce7c013679fb0b Mon Sep 17 00:00:00 2001 From: ToruNiina Date: Mon, 18 Mar 2019 01:36:43 +0900 Subject: [PATCH 1/2] test: use the test suite in the effective way add tests/check_toml_test.cpp to compare json object --- .circleci/config.yml | 20 +++++- tests/check_toml_test.cpp | 142 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 160 insertions(+), 2 deletions(-) create mode 100644 tests/check_toml_test.cpp diff --git a/.circleci/config.yml b/.circleci/config.yml index 421b8d1..6d84573 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -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 diff --git a/tests/check_toml_test.cpp b/tests/check_toml_test.cpp new file mode 100644 index 0000000..aeaf54a --- /dev/null +++ b/tests/check_toml_test.cpp @@ -0,0 +1,142 @@ +#include "toml.hpp" +#include +#include + +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::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 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::max_digits10); + toml::visit(json_serializer(), data); + return 0; + } + catch(const toml::syntax_error& err) + { + std::cout << "what(): " << err.what() << std::endl; + return 1; + } +} From 1e6f30f6fa783788d538a8b87a60b9480f0f3f4e Mon Sep 17 00:00:00 2001 From: ToruNiina Date: Mon, 18 Mar 2019 01:50:23 +0900 Subject: [PATCH 2/2] chore: update README.md --- README.md | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 03d602d..05be666 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,9 @@ toml11 ====== -[![Build Status](https://travis-ci.org/ToruNiina/toml11.svg?branch=master)](https://travis-ci.org/ToruNiina/toml11) -[![Build status](https://ci.appveyor.com/api/projects/status/m2n08a926asvg5mg/branch/master?svg=true)](https://ci.appveyor.com/project/ToruNiina/toml11/branch/master) +[![Build Status on TravisCI](https://travis-ci.org/ToruNiina/toml11.svg?branch=master)](https://travis-ci.org/ToruNiina/toml11) +[![Build status on Appveyor](https://ci.appveyor.com/api/projects/status/m2n08a926asvg5mg/branch/master?svg=true)](https://ci.appveyor.com/project/ToruNiina/toml11/branch/master) +[![Build status on CircleCI](https://circleci.com/gh/ToruNiina/toml11/tree/master.svg?style=svg)](https://circleci.com/gh/ToruNiina/toml11/tree/master) [![Version](https://img.shields.io/github/release/ToruNiina/toml11.svg?style=flat)](https://github.com/ToruNiina/toml11/releases) [![License](https://img.shields.io/github/license/ToruNiina/toml11.svg?style=flat)](LICENSE) [![DOI](https://zenodo.org/badge/DOI/10.5281/zenodo.1209136.svg)](https://doi.org/10.5281/zenodo.1209136) @@ -13,9 +14,14 @@ compatible to the latest version of [TOML v0.5.0](https://github.com/toml-lang/toml/blob/master/versions/en/toml-v0.5.0.md) after version 2.0.0. +It passes [the language agnostic test suite for TOML parsers by BurntSushi](https://github.com/BurntSushi/toml-test). +Not only the test suite itself, tiny TOML reader/encoder also runs on [CircleCI](https://circleci.com/gh/ToruNiina/toml11). +You can see the error messages about invalid files and serialization results of valid files at +[CircleCI](https://circleci.com/gh/ToruNiina/toml11). + Are you looking for pre-C++11 compatible toml parser? Try [Boost.toml](https://github.com/ToruNiina/Boost.toml)! -It has almost the same functionality as this library and works with C++98 & Boost. +It has a bit less functionality than this library but works with C++98 + Boost. ## How to use