Compare commits

...

155 Commits

Author SHA1 Message Date
Toru Niina
abb6ae517d Merge pull request #11 from ToruNiina/toml-v050
WIP: major update: support TOML v0.5.0
2018-12-13 21:38:03 +09:00
ToruNiina
fd21d5dd95 add simplest copyright notice 2018-12-13 20:44:10 +09:00
ToruNiina
57de57a1de improve error message for empty value 2018-12-13 20:37:40 +09:00
ToruNiina
e86777d19c improve error message for conflicting tables 2018-12-13 20:17:57 +09:00
ToruNiina
e79069cc47 enable to show err msg for invalid insertion
like, with the following (invalid) toml file

> a.b = "value"
> a.b.c = 42

The error message becomes

> terminate called after throwing an instance of 'toml::syntax_error'
>   what():  [error] toml::insert_value: target (a.b) is neither table nor
> an array of tables
>  --> example.toml
>  1 | a.b = "value"
>    |       ~~~~~~~ actual type is string
>  ...
>  2 | a.b.c = 42
>    |         ~~ inserting this
2018-12-13 17:09:38 +09:00
ToruNiina
f60e93c36f enable to assign value keeping region info 2018-12-13 17:07:26 +09:00
ToruNiina
e5c29c2870 enable to show err msg for 2 different location 2018-12-13 17:07:03 +09:00
ToruNiina
acc7b7870f remove format_error_for_value and add get_region instead
it is convenient to have get_region function that can access region_info
in toml::value. get_region is placed in toml::detail and made friend of
toml::value because I don't want to make toml::value::region_info public
and keep it internal use only.
2018-12-13 16:13:05 +09:00
ToruNiina
06f04af375 fix typoes in README 2018-12-13 14:58:42 +09:00
ToruNiina
5125287ac7 fix datetime conversion
use internal duration type in std::chrono::system_clock::time_point
2018-12-13 14:57:42 +09:00
ToruNiina
56287803e7 update README a bit 2018-12-13 13:21:26 +09:00
ToruNiina
95d73a290f add test case for reading dotted-keys 2018-12-13 13:07:48 +09:00
ToruNiina
26e0d87d3b enable nanoseconds in datetimes 2018-12-13 12:49:53 +09:00
ToruNiina
fb6d51954a turn test_parse_unicode on; no change required 2018-12-13 02:28:52 +09:00
ToruNiina
4d7cfc9d1d turn test_parse_file on
the required change is;
- change Datetime -> offset_datetime and construct correctly.
2018-12-13 02:26:55 +09:00
ToruNiina
514f3c773f set test_from_toml 2018-12-13 02:24:03 +09:00
ToruNiina
77b237c53a update README 2018-12-13 02:00:42 +09:00
ToruNiina
27a80b1214 rename get(table, key) to find() 2018-12-13 02:00:13 +09:00
ToruNiina
f62bcb3077 update cmakelists 2018-12-13 01:30:25 +09:00
ToruNiina
be1a310ae5 move test for find to get_related 2018-12-13 01:30:06 +09:00
ToruNiina
affa159c82 add get_or(value, key, opt) 2018-12-13 01:29:23 +09:00
ToruNiina
901c299c40 add unwrap_or to result 2018-12-13 01:28:55 +09:00
ToruNiina
2080b30110 add test cases for test_to_toml 2018-12-13 00:38:36 +09:00
ToruNiina
c15cb15c4c simplify to_toml implementation 2018-12-13 00:38:04 +09:00
ToruNiina
d370ae7d0d set tm_isdst as negative value 2018-12-13 00:35:43 +09:00
ToruNiina
83b588a8c8 rename test code 2018-12-13 00:35:05 +09:00
ToruNiina
5bfbbe35a6 update README 2018-12-12 23:23:59 +09:00
ToruNiina
c69969733f use carriage return depending on env 2018-12-12 23:22:31 +09:00
ToruNiina
47cd6f5a41 remove redundant error message 2018-12-12 23:17:28 +09:00
ToruNiina
a19c9b4a39 add test case for find & get 2018-12-12 21:55:11 +09:00
ToruNiina
f64430af92 remove old test; individual test cases are added
test_parse_* is now available, old test_parser is not needed now
2018-12-12 20:55:58 +09:00
ToruNiina
8e154cdd74 add test case for parsing datetime 2018-12-12 20:55:30 +09:00
ToruNiina
65cfa9d06b remove unused file because its not stable
since toml::format is not stable now, remove once for the next release.
2018-12-12 20:35:21 +09:00
ToruNiina
cfd82c95f0 add test case for getting converted map 2018-12-12 20:32:26 +09:00
ToruNiina
18a22eb3c4 add test cases for datetime variants 2018-12-12 20:28:31 +09:00
ToruNiina
03be08a2e6 fix conversion from offset_datetime to system_clock::time_point 2018-12-12 20:28:11 +09:00
ToruNiina
27ad4e2d8f cleanup headers 2018-12-12 19:39:04 +09:00
ToruNiina
0924164f51 add a note about the error message 2018-12-12 19:36:57 +09:00
ToruNiina
d4a4865217 fix some errors in README 2018-12-12 19:35:33 +09:00
ToruNiina
1bc66f6c28 Merge branch 'toml-v050' of github.com:ToruNiina/toml11 into toml-v050 2018-12-12 19:33:13 +09:00
ToruNiina
b015e1ac5b update README for the next version 2018-12-12 19:33:01 +09:00
ToruNiina
5aae0b17c8 change error message; require unicode codepoint
before this, it recommends the range that can be represented by utf-8
but the range of valid unicode codepoint is narrower than that. for
error message, it is good to recommend valid unicode codepoint.
2018-12-12 19:14:27 +09:00
ToruNiina
0f83ee6039 change temporaly loc from token to copy of loc
location constructed from token string does not has correct line number
information. to show an informative error message about UTF-8 and escape
sequences, parse_(ml_)basic_string requires those information that can
only be given from root location<Container>.
2018-12-12 19:12:23 +09:00
ToruNiina
879b7d3bff improve format of error message for utf-8 2018-12-12 19:01:22 +09:00
ToruNiina
c33ad31981 split lexer for escape sequence for unicode 2018-12-12 18:59:20 +09:00
ToruNiina
5d29509d98 remove duplicated default argument for SFINAE 2018-12-12 18:58:54 +09:00
ToruNiina
717e03cd4a add find-get overload functions 2018-12-12 17:55:34 +09:00
ToruNiina
5a20d55dd0 add test for toml::get 2018-12-12 17:23:06 +09:00
ToruNiina
dc060ba840 add explicit std::move to toml::get(&&) 2018-12-12 17:22:41 +09:00
ToruNiina
fcbfbd3a26 supress warning about comparison between signed and unsigned 2018-12-12 16:12:10 +09:00
ToruNiina
0c9b785969 add missing include file 2018-12-12 16:11:37 +09:00
ToruNiina
bcaf5baf88 fix parse_array_of_table_key
allow whitespace before and after [[ and ]] (like, [[ a.b ]])
2018-12-12 12:14:11 +09:00
ToruNiina
5dea88001e add test case for array-of-tables 2018-12-12 12:13:49 +09:00
ToruNiina
b63dc1f370 remove extra comma 2018-12-12 01:36:20 +09:00
ToruNiina
d3e88b3082 add test case for table key 2018-12-12 01:31:36 +09:00
ToruNiina
a1a81089c5 skip whitespace before/inside/after dotted-keys 2018-12-12 01:30:47 +09:00
ToruNiina
765ab97d8b add whitespace between [] and key
[ a.b.c ] is allowed. also, [[ a . b ]] is allowed.
dotted key matches `a.b.c` only, so the explicit whitespace is needed.
2018-12-12 01:27:10 +09:00
ToruNiina
9f8e86524a add a lot of dotted keys to test_lex 2018-12-12 01:26:56 +09:00
ToruNiina
4d64c0d8af add tests for inline table 2018-12-11 23:38:57 +09:00
ToruNiina
e810c3d35e add tests for equivalent operator 2018-12-11 23:33:32 +09:00
ToruNiina
fc3471434f add test for array 2018-12-11 23:31:24 +09:00
ToruNiina
be8600abfa add test for key and string 2018-12-11 23:25:44 +09:00
ToruNiina
e79e6150f2 update testing macro 2018-12-11 23:25:23 +09:00
ToruNiina
fc6a15440d enable format_underline to print hint with region 2018-12-11 22:22:07 +09:00
ToruNiina
d7bba10fa3 improve error message in parse_key_value_pair 2018-12-11 22:21:22 +09:00
ToruNiina
247bcb0714 show error message for inhomogenous array 2018-12-11 22:17:28 +09:00
ToruNiina
3055645323 remove old tests 2018-12-11 21:52:53 +09:00
ToruNiina
0253f49101 add a test for parser (WIP) 2018-12-11 21:51:39 +09:00
ToruNiina
27b9334f10 skip BOM if exists 2018-12-11 21:40:48 +09:00
ToruNiina
38135940e9 add expect<T>(toml::value) 2018-12-11 11:36:40 +09:00
ToruNiina
d75a977066 improve error message for bad unwrap a bit 2018-12-11 11:35:07 +09:00
ToruNiina
75c136924b add datetimes to chrono 2018-12-10 22:06:06 +09:00
ToruNiina
0759e757ae move is_chrono_duration from types to traits 2018-12-10 22:05:42 +09:00
ToruNiina
4e57c5f5df improve error message for invalid line
like a = 12 = true, newline is expected after 12
2018-12-10 21:43:02 +09:00
ToruNiina
ff83a6a477 remove redundant part of error messages 2018-12-10 21:42:56 +09:00
ToruNiina
3f991c4759 improve power of get 2018-12-10 15:58:20 +09:00
ToruNiina
bf2158ae98 add map from exact toml type -> toml::value_t 2018-12-10 15:57:44 +09:00
ToruNiina
ff19c9f492 add corresponding region to each value 2018-12-10 15:57:17 +09:00
ToruNiina
28ba2713ee fix initialization of region in value 2018-12-10 15:25:27 +09:00
ToruNiina
129ea81f66 remove redundant words in error message 2018-12-10 15:23:46 +09:00
ToruNiina
8dfe187d59 add a function to show a better error message 2018-12-10 15:06:28 +09:00
ToruNiina
8078c719fe remove old code 2018-12-10 00:15:41 +09:00
ToruNiina
8e18aa9b16 add toml::parse 2018-12-10 00:14:46 +09:00
ToruNiina
ed155a5040 remove help msgs in parse_value
because the error message becomes too long
2018-12-09 21:54:47 +09:00
ToruNiina
cf03a08632 re-write parser using result and new value
wip.
2018-12-09 19:32:30 +09:00
ToruNiina
34c3d33936 use vector instead of initializer_list 2018-12-09 19:30:46 +09:00
ToruNiina
c04b75b2e3 consider LF in the range when writing error msg 2018-12-09 18:08:04 +09:00
ToruNiina
e24039f4ef update toml::value and improve test_value
- enable to store new types
- store source string if possible
- refactoring
2018-12-09 18:03:20 +09:00
ToruNiina
dc8ccdc458 construct much more tmp variables
std::chrono::seconds -= std::chrono::milliseconds cannot be done bc
it represents the duration as integer value and milliseconds are less
than seconds. it causes compilation error when we pass a duration to
toml::local_time. to avoid this, we need to type-cast the values to
smaller duration, like sec -> msec
2018-12-09 18:00:46 +09:00
ToruNiina
2696e4e6ba split storage from value
also, quit inheritance in storage class
2018-12-09 16:41:45 +09:00
ToruNiina
d1d5ca6bf8 add toml::string to have basic/literal flag 2018-12-09 16:40:57 +09:00
ToruNiina
80eafd1424 add datetime variants and rearrange type-related functions 2018-12-09 16:34:47 +09:00
ToruNiina
9fadf71a10 add constructor from duration to local_time 2018-12-09 16:27:47 +09:00
ToruNiina
ac3025d92f change default return value of region_base 2018-12-09 13:39:13 +09:00
ToruNiina
b0e7efa1e0 make some constructors explicit 2018-12-09 13:38:57 +09:00
ToruNiina
84676eab0b improve quality of error message 2018-12-09 13:05:09 +09:00
ToruNiina
2b3a4d49a5 add region_base to contain it in toml::value
to make toml::get and toml::value::cast return better error messages
2018-12-09 12:41:38 +09:00
ToruNiina
f834e0d142 cosmetic: sort value_t in types.h 2018-12-09 11:06:19 +09:00
ToruNiina
a1aa780a60 Merge branch 'toml-v050' of github.com 2018-12-09 11:05:26 +09:00
ToruNiina
48f3b73b91 add ctor(local_datetime, time_offset) to offset_datetime 2018-12-09 00:11:07 +09:00
ToruNiina
04854f9d21 stop having begin/end iterator in region/location 2018-12-09 00:00:15 +09:00
ToruNiina
8388664fc6 add map_err_or_else to result 2018-12-08 22:44:15 +09:00
ToruNiina
bb215836dc add missing header files 2018-12-08 20:39:37 +09:00
ToruNiina
2b2a05148e add from_string to utility 2018-12-08 20:21:15 +09:00
ToruNiina
66807d19d1 add specializations 2018-12-08 19:40:58 +09:00
ToruNiina
861444a02b disable some of the tests once 2018-12-08 19:23:53 +09:00
ToruNiina
25789d1450 set Datetime as offset_datetime
prepare for TOML v0.5.0
2018-12-08 19:23:09 +09:00
ToruNiina
1e28cb2d13 add test for concat_string 2018-12-08 19:22:41 +09:00
ToruNiina
ae564bd814 change include file of test_traits 2018-12-08 19:22:31 +09:00
ToruNiina
366f72bbdd Merge branch 'datetime' into combinator 2018-12-08 19:06:19 +09:00
ToruNiina
3ef33c1637 change almost everything about datetime 2018-12-08 19:04:41 +09:00
ToruNiina
e05d0bdb84 stop using distance(next(iter), last)
under some condition, it causes serious error.
2018-12-06 20:13:06 +09:00
ToruNiina
5dbbc1fb1a add escaped newline to lexer for multiline string
to use it in parse_ml_basic_string
2018-12-06 19:53:49 +09:00
ToruNiina
b3b5682cc0 add message to bad_unwrap 2018-12-06 17:15:19 +09:00
ToruNiina
df314da751 change error message considering context
combinators are used with other parser-combinators. in such cases, empty
input means `not enough character`.
2018-12-06 17:03:57 +09:00
ToruNiina
3d1783e12a Merge 'types' into combinator 2018-12-06 16:06:25 +09:00
ToruNiina
f8aa604959 Merge branch 'result' into combinator
- fix some of the constructors of result
- add some utility member functions to boost
2018-12-06 12:57:58 +09:00
ToruNiina
e3f6805629 add conversion members to result 2018-12-06 12:47:14 +09:00
ToruNiina
1dddc6e26c add missing std::move for ctors 2018-12-06 12:46:32 +09:00
ToruNiina
5e052237ba add alias for snake_case types 2018-12-06 01:20:11 +09:00
ToruNiina
a995bd515b Merge branch 'master' into combinator
split typedefs from value.hpp
2018-12-05 21:42:14 +09:00
ToruNiina
00619c1c85 Merge branch 'types'; split typedefs from value 2018-12-05 21:41:22 +09:00
ToruNiina
532457345c split type definitions from value.hpp 2018-12-05 20:55:25 +09:00
ToruNiina
f9a018b5ea add source_name to test_lex_aux macros 2018-12-05 17:19:37 +09:00
ToruNiina
aa05858de3 add source_name to location/region to show filename
now error message prints the filename
2018-12-05 16:55:31 +09:00
ToruNiina
2b3c8887d6 add comment to confusing implementation 2018-12-04 22:17:20 +09:00
ToruNiina
932a0646ce force clamping character code in [0,256) 2018-12-04 21:58:47 +09:00
ToruNiina
c3a2cd8c1e use hexcode instead of u8 string on windows 2018-12-04 21:43:43 +09:00
ToruNiina
c0ce5a2d7d remove debug message from test code 2018-12-04 21:00:34 +09:00
ToruNiina
c3e1f68ef6 add u8 to the front of UTF-8 string literal
explicitly set the character encoding for them, for compatibility
2018-12-04 20:59:44 +09:00
ToruNiina
1a2fa6d53a add test for lexers 2018-12-04 20:30:21 +09:00
ToruNiina
17f3d96766 add lexers 2018-12-04 20:29:59 +09:00
ToruNiina
1f564ec047 add combinators to scan content 2018-12-04 20:29:39 +09:00
ToruNiina
679e282e23 make variables in region/location read-only
to avoid modifying mistakenly
2018-12-03 00:10:26 +09:00
ToruNiina
59588e3a10 add static_assert and useful member funcs 2018-12-02 23:22:27 +09:00
ToruNiina
f83a8b450e add concat_to_string to utility for error messges 2018-12-02 23:05:15 +09:00
ToruNiina
8bf97d8a00 add constructors that receive range to region 2018-12-02 23:04:49 +09:00
ToruNiina
2ee8ffab21 add begin/end to region 2018-12-02 21:54:39 +09:00
ToruNiina
9c1bfbd5eb make region::source immutable 2018-12-02 21:03:08 +09:00
ToruNiina
c38b9b7dc7 add region and location to represent tokens
location is almost same as an Iterator, but having shared_ptr that points
the content. region is almost same as a range. by adding pointer to the
content source, utility function to show the error message can be
implemented easier. it is expected that this also makes easy to show
error messages after parse (e.g., in the case of bad_get)
2018-12-02 20:52:04 +09:00
ToruNiina
4791088106 Merge branch 'result' into combinator 2018-12-02 18:30:39 +09:00
ToruNiina
b53d11ce79 ci: downgrade test modules, use BOOST_CHECK
Because travis.CI installs old, out-of-date version of Boost,
BOOST_TEST is not supported. we need to use BOOST_CHECK to link
with it.
2018-12-02 18:25:09 +09:00
ToruNiina
00c3c2f773 Merge branch 'result' into combinator 2018-12-02 18:15:58 +09:00
ToruNiina
6c0a12148b add result<T, E> struct to handle errors
aiming later updates and refactoring of parsers
2018-12-02 18:01:37 +09:00
ToruNiina
a32ccd82f3 fix UB in test_value (use after move) 2018-10-22 19:00:16 +09:00
ToruNiina
f326334147 add definition of constexpr static value to avoid linker error 2018-07-08 18:58:38 +09:00
ToruNiina
b1a55b1331 simplify SFINAE in to_toml 2018-05-05 13:09:40 +09:00
ToruNiina
170b0d6b3f use constexpr value instead of call constexpr func directory 2018-05-05 12:06:06 +09:00
ToruNiina
433636a06f simplify the implementation of from_toml 2018-05-05 11:59:34 +09:00
ToruNiina
9555817901 add get<pair>, get<tuple> 2018-05-05 11:46:09 +09:00
ToruNiina
e54deacf1a simplify SFINAE expressions in toml::get 2018-05-05 11:42:11 +09:00
ToruNiina
b6f53cae7a update test_traits
now is_container returns false when map-like class is passed
2018-05-05 11:40:13 +09:00
ToruNiina
f953a9cf23 add conjunction, disjunction, negation, index_seq 2018-05-05 11:37:18 +09:00
ToruNiina
117549bf70 change is_(map|container) and remove needless trait 2018-05-05 11:36:47 +09:00
ToruNiina
4287160254 add a badge 2018-03-28 19:21:17 +09:00
52 changed files with 8757 additions and 5524 deletions

483
README.md
View File

@@ -4,200 +4,210 @@ 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?svg=true)](https://ci.appveyor.com/project/ToruNiina/toml11)
[![MIT License](http://img.shields.io/badge/license-MIT-blue.svg?style=flat)](LICENSE)
[![DOI](https://zenodo.org/badge/DOI/10.5281/zenodo.1209136.svg)](https://doi.org/10.5281/zenodo.1209136)
c++11 header-only toml parser depending only on c++11 standard library.
c++11 header-only toml parser depending only on c++ standard library.
compatible to TOML v0.4.0.
compatible to the latest version of TOML v0.5.0 after version 2.0.0.
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.
## How to use
Just include the file after adding correct include path.
## Installation
Just include the file after adding it to the include path.
```cpp
#include <toml11/toml.hpp>
#include <toml11/toml.hpp> // that's all! now you can use it.
int main()
{
/* do something ... */
const auto data = toml::parse("example.toml");
const auto title = toml::get<std::string>(data.at("title"));
std::cout << "the title is " << title << std::endl;
}
```
### decoding toml file
### Decoding toml file
The only thing you have to do is passing filename to `toml::parse` function.
The only thing you have to do is to pass a filename to the `toml::parse` function.
```cpp
const std::string fname("sample.toml");
const auto data = toml::parse(fname);
const toml::table data = toml::parse(fname);
```
In the case of file open error, it will throw `std::runtime_error`.
You can pass also `stream` to `toml::parse` function.
You can also pass a `stream` to the `toml::parse` function after checking the status.
```cpp
std::ifstream ifs("sample.toml");
assert(ifs.good());
const auto data = toml::parse(ifs);
const auto data = toml::parse(ifs /*, "filename" (optional)*/);
```
If there are syntax error in the toml file,
`toml::parse` will throw `toml::syntax_error`.
To show a better error message, it is recommended to pass a filename with `istream`. See also [in the case of syntax error](#in-the-case-of-syntax-error) and [passing invalid type to toml::get](#passing-invalid-type-to-tomlget).
#### toml::get()
### In the case of syntax error
Then you can obtain the various value from the `data` using `toml::get` function.
If there is a syntax error in a toml file, `toml::parse` will throw `toml::syntax_error`.
toml11 now has clean and informative error messages inspired by Rust and it looks like the following (comment after hash sign are actually not shown).
```console
terminate called after throwing an instance of 'toml::syntax_error'
what(): [error] toml::parse_table: invalid line format # error description
--> example.toml # file name
3 | a = 42 = true # line num and content
| ^------ expected newline, but got '='. # error reason
```
Since the error message generation is generally a difficult task, the current status is not ideal. toml11 needs your help. If you encounter a weird error message, please let us know and contribute to improve the quality!
### Getting a toml value
After parsing successfully, you can obtain the values from the result of `toml::parse` (here, `data`) using `toml::get` function.
```toml
answer = 42
pi = 3.14
answer = 42
pi = 3.14
numbers = [1,2,3]
time = 1979-05-27T07:32:00Z
time = 1979-05-27T07:32:00Z
[tab]
key = "value"
```
``` cpp
const auto answer = toml::get<std::int64_t>(data.at("answer"));
const auto pi = toml::get<double>(data.at("pi"));
const auto answer = toml::get<std::int64_t >(data.at("answer"));
const auto pi = toml::get<double >(data.at("pi"));
const auto numbers = toml::get<std::vector<int>>(data.at("numbers"));
const auto timepoint = toml::get<std::chrono::system_clock::time_point>(data.at("time"));
const auto tab = toml::get<toml::Table>(data.at("tab"));
const auto key = toml::get<std::string>(tab.at("key"));
const auto key = toml::get<std::string>( tab.at("key"));
```
You can set any kind of `container` class to obtain `toml::Array` except for
`map`-like class.
When you pass an exact TOML type that does not require type conversion, `toml::get` returns also a reference through which you can modify the content.
```cpp
toml::get<toml::integer>(data["answer"]) = 6 * 9;
std::cout << toml::get<int>(data.at("answer")) << std::endl; // 54
```
If the specified type requires conversion, you can't take a reference to the value. See also [underlying types](#underlying-types).
#### Passing invalid type to toml::get
If you choose the invalid type, `toml::type_error` will be thrown. Similar to the `syntax_error`, toml11 also displays informative error messages. The error message when you choose `int` to get `string` value would be like this.
```console
terminate called after throwing an instance of 'toml::type_error'
what(): [error] toml::value bad_cast to integer
--> example.toml
3 | title = "TOML Example"
| ~~~~~~~~~~~~~~ the actual type is string
```
NOTE: In order to show this kind of error message, all the toml values have 1 shared_ptr that points the corresponding byte sequence and 2 iterators that point the range. It is recommended to destruct all the `toml::value` classes after configuring your application to save memory resources.
### Getting arrays
You can set any kind of `container` class to obtain a `toml::array` except for `map`-like classes.
``` cpp
const auto vc = toml::get<std::vector<int>>(data.at("numbers"));
const auto ls = toml::get<std::list<int>>(data.at("numbers"));
const auto dq = toml::get<std::deque<int>>(data.at("numbers"));
const auto vc = toml::get<std::vector<int> >(data.at("numbers"));
const auto ls = toml::get<std::list<int> >(data.at("numbers"));
const auto dq = toml::get<std::deque<int> >(data.at("numbers"));
const auto ar = toml::get<std::array<int, 3>>(data.at("numbers"));
// if size of data.at("numbers") is larger than 3, it will throw toml::type_error
// because std::array is not resizable.
// if the size of data.at("numbers") is larger than that of std::array,
// it will throw toml::type_error because std::array is not resizable.
```
If the type you passed as a template parameter is incorrect,
it will throw `toml::type_error`.
``` cpp
const auto wrong1 = toml::get<bool>(data.at("integer")); // exception thrown!
const auto wrong2 = toml::get<float>(data.at("integer")); // ditto
const auto wrong3 = toml::get<toml::Datetime>(data.at("array")); // ditto
```
Although `toml::get` is convenient, it has additional copy-cost because it
copies data contained in `toml::value` to user-specified type.
Of course in some case this overhead is not ignorable.
You can get reference pointing to contained value using `toml::value::cast()` like this.
``` cpp
const auto& pi = data.at("pi").cast<toml::value_t::Float>();
const auto& tab = data.at("tab").cast<toml::value_t::Table>();
const auto& numbers = data.at("numbers").cast<toml::value_t::Array>();
```
Unfortunately, if you use `toml::value::cast` to get an array, you would need to
`cast` each element in `toml::Array` because `toml::Array` is represented as
an array of `toml::value`.
Surprisingly, you can also get a `toml::array` as `std::pair` and `std::tuple.`
```cpp
const auto& num0 = numbers.at(0).cast<toml::value_t::Integer>();
const auto& num1 = numbers.at(1).cast<toml::value_t::Integer>();
const auto& num2 = numbers.at(2).cast<toml::value_t::Integer>();
const auto tp = toml::get<std::tuple<short, int, unsigned int>>(data.at("numbers"));
```
#### toml::get\_or
The case when you need this functionality is to get an array of arrays.
You can also set default value for `toml::get`.
```cpp
toml::Table data; // empty table!
const auto value1 = toml::get_or(data, "key1", 42); // value1 => int 42.
toml::Integer i(123);
const auto value2 = toml::get_or(data, "key1", i); // value2 => toml::Integer 42.
```toml
aofa = [[1,2,3], ["foo", "bar", "baz"]] # toml allows this
```
#### toml::value\_t
When you don't know the exact type of toml-value, you can get `enum` type from
`toml::value`.
What is the corresponding C++ type? Obviously, it is a `std::pair` of `std::vector`s.
```cpp
int i;
double d;
std::string s;
std::vector<int> a;
const auto t = data.at("something").type();
switch(t)
const auto aofa = toml::get<
std::pair<std::vector<int>, std::vector<std::string>>
>(data.at("aofa"));
```
If you don't know what the type is inside the array, you can use `toml::array`, which is a `std::vector` of `toml::value`, instead.
```cpp
const auto aofa = toml::get<toml::array>(data.at("aofa"));
const auto first = toml::get<toml::array>(aofa.at(0));
```
See also [expecting conversion](#expecting-conversion) and [checking-value-type](#checking-value-type).
### Getting tables
`toml::table` is a key component of this library, which is an alias of a `std::unordered_map` from `toml::key (a.k.a. std::string)` to `toml::value`. `toml::parse` returns this as a result.
Since it is just an alias of `std::unordered_map`, it has all the functionalities that `std::unordered_map` has, e.g. `operator[]`, `count`, and `find`.
```cpp
toml::table data = toml::parse("example.toml");
if(data.count("title") != 0)
{
case toml::value_t::Integer: i = toml::get<int>(data.at("something")); break;
case toml::value_t::Float : d = toml::get<double>(data.at("something")); break;
case toml::value_t::String : s = toml::get<std::string>(data.at("something")); break;
case toml::value_t::Array : a = toml::get<std::vector<int>>(data.at("something")); break;
default : throw std::runtime_error("unexpected type : " + toml::stringize(t));
data["title"] = std::string("TOML example");
}
```
Okey, but it is painful to write `switch-case` for many time.
#### toml::from\_toml()
The more sophisticated way is using `toml::from_toml` and `std::tie`.
```cpp
int i = 0;
double d = 0.;
std::string s;
std::vector<int> a;
toml::from_toml(std::tie(i, d, s, a), data.at("something"));
```
Here, only matched value will be filled.
The others are left intact after calling `from_toml`.
It should be noted that `toml::from_toml` returns as usual even if there are no
matched type.
`from_toml` can be used also for single type.
```cpp
int i;
toml::from_toml(i, data.at("something"));
```
Unlike `toml::get`, `toml::from_toml` does not require to specify the type
through the template argument because the type can be deduced from argument.
#### toml::value
In toml, `Array` is capable of having `Array` s and each of them possibly have
different types like this.
When all the values of the table have the same type, toml11 allows you to convert a `toml::table` to a `map` that contains the convertible type.
```toml
array_of_array = [[1,2,3,4,5], ["foo", "bar", "baz"]]
[tab]
key1 = "foo" # all the values are
key2 = "bar" # toml String
```
In this case, you can use `toml::value` directly.
```cpp
// use toml::value in a container
const auto a = toml::get<std::vector<toml::value>>(data.at("array_of_array"));
// or you can use default toml::Array.
const auto a_ = toml::get<toml::Array>(data.at("array_of_array"));
// you can obtain values from toml::value in the same way as described above.
const auto ns = toml::get<std::vector<std::int64_t>>(a.at(0));
const auto ss = toml::get<std::vector<std::string>>(a.at(1));
const auto tab = toml::get<std::map<std::string, std::string>>(data.at("tab"));
std::cout << tab["key1"] << std::endl; // foo
std::cout << tab["key2"] << std::endl; // bar
```
#### Array of Table
### Dotted keys
Of course, you can obtain `array of table` in the same way.
TOML v0.5.0 has a new feature named "dotted keys". You can chain keys to represent the structure of the data.
```toml
physical.color = "orange"
physical.shape = "round"
```
This is equivalent to the following.
```toml
[physical]
color = "orange"
shape = "round"
```
You can get both of the above formats with the same c++ code.
```cpp
const auto physical = toml::get<toml::table>(data.at("physical"));
const auto color = toml::get<std::string>(physical.at("color"));
```
### An array of tables
An array of tables is just an array of tables. You can get it completely in the same way as the other arrays and tables.
```toml
array_of_inline_table = [{key = "value1"}, {key = "value2"}, {key = "value3"}]
@@ -211,61 +221,196 @@ key = "value6"
```
```cpp
const auto aot1 = toml::get<std::vector<toml::Table>>(data.at("array_of_inline_table"))
const auto aot2 = toml::get<std::vector<toml::Table>>(data.at("array_of_table"))
const auto aot1 = toml::get<std::vector<toml::table>>(data.at("array_of_inline_table"));
const auto aot2 = toml::get<std::vector<toml::table>>(data.at("array_of_table"));
```
## Documentation
### Cost of conversion
The toml types and corresponding `enum` name are listed in the table below.
`value_t` is a scoped-enum defined in the namespace toml.
Although `toml::get` is convenient, it has additional copy-cost because it copies data contained in `toml::value` to the user-specified type. Of course in some case this overhead is not ignorable.
| toml-type | c++ type | enum |
| --------- | ---------------------------------------------- | ------------------------- |
| Boolean | `bool` | `toml::value_t::Boolean` |
| Integer | `std::int64_t` | `toml::value_t::Integer` |
| Float | `double` | `toml::value_t::Float` |
| String | `std::string` | `toml::value_t::String` |
| Datetime | `toml::Datetime` | `toml::value_t::Datetime` |
| Array | `std::vector<toml::value>` | `toml::value_t::Array` |
| Table | `std::unordered_map<std::string, toml::value>` | `toml::value_t::Table` |
By passing the exact types, `toml::get` returns reference that has nealy zero overhead.
`Datetime` is the `struct` that is defined in this library.
Because `std::chrono::system_clock::time_point` is a __time point__, not capable
of representing a Local Time independent from a specific day.
``` cpp
const auto& tab = toml::get<toml::array>(data.at("tab"));
const auto& numbers = toml::get<toml::table>(data.at("numbers"));
```
For user-convenience, `toml::Datetime` is _implicitly_ convertible to
`std::chrono::system_clock::time_point`. If `toml::Datetime` does not have any
Date information, the information will be generated from
`std::chrono::system_clock::now()` when cast is performed.
The definition of Datetime struct is below.
Unfortunately, in this case you need to call `toml::get` each time you access to the element of `toml::array` because `toml::array` is an array of `toml::value`.
```cpp
namespace toml
{
template<typename uintT, typename intT>
struct basic_datetime
{
uintT year; // since 0.
uintT month; // [1-12]
uintT day; // [1-31]
uintT hour; // [0-23]
uintT minite; // [0-59]
uintT second; // [0-59]
uintT millisecond // [0-999]
uintT microsecond // [0-999]
intT offset_hour; // [-12 - +12]
intT offset_minute; // [-59 - +59]
};
const auto& num0 = toml::get<toml::integer>(numbers.at(0));
const auto& num1 = toml::get<toml::integer>(numbers.at(1));
const auto& num2 = toml::get<toml::integer>(numbers.at(2));
```
typedef basic_datetime<unsigned, int> Datetime;
### Datetime and its variants
TOML v0.5.0 has 4 different datetime objects, `local_date`, `local_time`, `local_datetime`, and `offset_datetime`. With toml11, you can convert `local_time` to your favorite `std::chrono::duration` and others to `std::chrono::system_clock::time_point`.
```toml
time = 12:30:00
date = 2018-12-23
```
```cpp
const auto dur = toml::get<std::chrono::minutes>(data.at("time")); // 12 * 60 + 30 min
const auto tp = toml::get<std::chrono::system_clock::time_point>(data.at("date"));
```
### Getting with a fallback
`toml::get_or` returns a default value if `toml::get<T>` failed.
```cpp
toml::table data; // empty table!
const auto value = toml::get_or(data, "key", 42); // value => int 42.
```
`toml::get_or` automatically deduces what type you want to get from the default value you passed.
### Expecting conversion
By using `toml::expect`, you will get your expected value or an error message without throwing `toml::type_error`.
```cpp
const auto value = toml::expect<std::string>(data.at("title"));
if(value.is_ok()) {
std::cout << value.unwrap() << std::endl;
} else {
std::cout << value.unwrap_err() << std::endl;
}
```
It must be noted that the range of some values in `basic_datetime` is different
from `std::tm`. For example, month is in the range of `[1,12]` and year starts
from 0 (not 1900).
Also, you can pass a function object to modify the expected value.
```cpp
const auto value = toml::expect<int>(data.at("number"))
.map(// function that receives expected type (here, int)
[](const int number) -> double {
return number * 1.5 + 1.0;
}).unwrap_or(/*default value =*/ 3.14);
```
### Finding value from table
toml11 provides utility function to find a value from `toml::table`. Of course, you can do this in your own way with `toml::get` because it just searches an `unordered_map` and returns a value if it exists.
```cpp
const auto data = toml::parse("example.toml");
const auto num = toml::find<int>(data, "num", /*for err msg*/"example.toml");
```
If the value does not exist, it throws `std::out_of_range` with informative error message.
```console
terminate called after throwing an instance of 'std::out_of_range'
what(): [error] key "num" not found in example.toml
```
You can use this with a `toml::value` that is expected to be a `toml::table`. It automatically casts the value to table.
```cpp
const auto data = toml::parse("example.toml");
const auto num = toml::find<int>(data.at("table"), "num");
// expecting the following example.toml
// [table]
// num = 42
```
In this case, because the value `data.at("table")` knows the locatoin of itself, you don't need to pass where you find the value. `toml::find` will show you a great error message.
```console
terminate called after throwing an instance of 'std::out_of_range'
what(): [error] key "num" not found
--> example.toml
3 | [table]
| ~~~~~~~ in this table
```
If it's not a `toml::table`, the same error as "invalid type" would be thrown.
### Checking value type
When you don't know the exact type of toml-value, you can get `enum` type from `toml::value`.
```cpp
switch(data.at("something").type())
{
case toml::value_t::Integer: /* do some stuff */; break;
case toml::value_t::Float : /* do some stuff */; break;
case toml::value_t::String : /* do some stuff */; break;
default : throw std::runtime_error(
"unexpected type : " + toml::stringize(data.at("something").type()));
}
```
### Fill only the matched value
The more sophisticated way is using `toml::from_toml` and `std::tie`.
```cpp
toml::table data{{"something", toml::value("foo")}};
int i = 0;
double d = 0.;
std::string s;
toml::from_toml(std::tie(i, d, s), data.at("something"));
std::cout << i << ", " << d << ", " << s << std::endl; // 0, 0, foo
```
Here, only matched value will be filled. The others are left intact after calling `from_toml`.
It should be noted that `toml::from_toml` returns as usual even if there are no matched type.
`from_toml` can be used also for single type.
```cpp
int i = 0;
toml::from_toml(i, data.at("something"));
```
### Sanitizing UTF-8 codepoints
toml11 shows warning if a value of an escape sequence used to represent unicode character exceeds the unicode range.
```console
[warning] input codepoint (0011FFFF) is too large to decode as a unicode character. The result may not be able to render to your screen.
--> example.toml
3 | exceeds_unicode = "\U0011FFFF example"
| ~~~~~~~~~ should be in [0x00..0x10FFFF]
```
Also, toml11 throws `std::domain_error` if the code point exceeds the range that can be represented by utf-8.
```console
terminate called after throwing an instance of 'std::range_error'
what(): [error] input codepoint (0020FFFF) is too large to encode as utf-8.
--> example.toml
3 | exceeds_utf8 = "\U0020FFFF example"
| ~~~~~~~~~ should be in [0x00..0x10FFFF]
```
## Underlying types
The toml types (can be used as `toml::*` in this library) and corresponding `enum` names are listed in the table below.
| toml::type | underlying c++ type | enum |
| -------------- | -------------------------------------------- | ------------------------------- |
| Boolean | `bool` | `toml::value_t::Boolean` |
| Integer | `std::int64_t` | `toml::value_t::Integer` |
| Float | `double` | `toml::value_t::Float` |
| String | `toml::string` | `toml::value_t::String` |
| LocalDate | `toml::local_date` | `toml::value_t::LocalDate` |
| LocalTime | `toml::local_time` | `toml::value_t::LocalTime` |
| LocalDatetime | `toml::local_datetime` | `toml::value_t::LocalDatetime` |
| OffsetDatetime | `toml::offset_datetime` | `toml::value_t::offsetDatetime` |
| Array | `std::vector<toml::value>` | `toml::value_t::Array` |
| Table | `std::unordered_map<std::string, toml::key>` | `toml::value_t::Table` |
`toml::string` is effectively the same as `std::string` but has an additional flag that represents a kind of a string, `string_t::basic` and `string_t::literal`. Although `std::string` is not an exact toml type, still you can get a reference that points to internal `std::string` by using `toml::get<std::string>()` for convenience.
`Datetime` variants are `struct` that are defined in this library. Because `std::chrono::system_clock::time_point` is a __time point__, not capable of representing a Local Time independent from a specific day.
It is recommended to get `Datetime`s as `std::chrono` classes through `toml::get`.
## Contributors

View File

@@ -1,15 +1,29 @@
set(TEST_NAMES
test_traits
test_datetime
test_utility
test_result
test_traits
test_value
test_lex_boolean
test_lex_integer
test_lex_floating
test_lex_datetime
test_lex_string
test_lex_key_comment
test_parse_boolean
test_parse_integer
test_parse_floating
test_parse_string
test_parse_datetime
test_parse_array
test_parse_table
test_parse_inline_table
test_parse_key
test_parse_table_key
test_get
test_get_related_func
test_to_toml
test_from_toml
test_get
test_get_or
test_value_operator
test_datetime
test_acceptor
test_parser
test_parse_file
test_parse_unicode
)

View File

@@ -1,592 +0,0 @@
#define BOOST_TEST_MODULE "test_acceptor"
#ifdef UNITTEST_FRAMEWORK_LIBRARY_EXIST
#include <boost/test/unit_test.hpp>
#else
#define BOOST_TEST_NO_LIB
#include <boost/test/included/unit_test.hpp>
#endif
#include <toml/acceptor.hpp>
#include <locale>
#include <iostream>
BOOST_AUTO_TEST_CASE(test_conditions)
{
{
const std::string tmp(" ");
const std::string dummy("dummy");
BOOST_CHECK(toml::is_space<char>::invoke(tmp.begin(), tmp.end()) == tmp.end());
BOOST_CHECK(toml::is_space<char>::invoke(dummy.begin(), dummy.end()) == dummy.begin());
}
{
const std::string tmp("\t");
const std::string dummy("dummy");
BOOST_CHECK(toml::is_tab<char>::invoke(tmp.begin(), tmp.end()) == tmp.end());
BOOST_CHECK(toml::is_tab<char>::invoke(dummy.begin(), dummy.end()) == dummy.begin());
}
{
for(int i=0; i<10; ++i)
{
const std::string tmp = std::to_string(i);
const std::string dummy("dummy");
BOOST_CHECK(toml::is_number<char>::invoke(tmp.begin(), tmp.end()) == tmp.end());
BOOST_CHECK(toml::is_number<char>::invoke(dummy.begin(), dummy.end()) == dummy.begin());
}
}
{
for(char c='a'; c <= 'z'; ++c)
{
const std::string tmp(1, c);
const std::string dummy(1, std::toupper(c));
BOOST_CHECK(toml::is_lowercase<char>::invoke(tmp.begin(), tmp.end()) == tmp.end());
BOOST_CHECK(toml::is_lowercase<char>::invoke(dummy.begin(), dummy.end()) == dummy.begin());
}
}
{
for(char c='A'; c <= 'Z'; ++c)
{
const std::string tmp(1, c);
const std::string dummy(1, std::tolower(c));
BOOST_CHECK(toml::is_uppercase<char>::invoke(tmp.begin(), tmp.end()) == tmp.end());
BOOST_CHECK(toml::is_uppercase<char>::invoke(dummy.begin(), dummy.end()) == dummy.begin());
}
}
{
const std::string tmp(" ");
const std::string dummy("a");
BOOST_CHECK(toml::is_whitespace<char>::invoke(tmp.begin(), tmp.end()) == tmp.end());
BOOST_CHECK(toml::is_whitespace<char>::invoke(dummy.begin(), dummy.end()) == dummy.begin());
}
{
const std::string tmp("\t");
const std::string dummy("a");
BOOST_CHECK(toml::is_whitespace<char>::invoke(tmp.begin(), tmp.end()) == tmp.end());
BOOST_CHECK(toml::is_whitespace<char>::invoke(dummy.begin(), dummy.end()) == dummy.begin());
}
{
const std::string tmp("hoge1-piyo2_fuga3");
const std::string dummy(" \t");
BOOST_CHECK(toml::is_barekey<char>::invoke(tmp.begin(), tmp.end()) != tmp.begin());
BOOST_CHECK(toml::is_barekey<char>::invoke(dummy.begin(), dummy.end()) == dummy.begin());
}
}
BOOST_AUTO_TEST_CASE(test_basic_inline_string)
{
using is_valid = toml::is_basic_inline_string<char>;
{
const std::string simple("\"hoge1-piyo2_fuga3\"");
BOOST_CHECK(is_valid::invoke(simple.cbegin(), simple.cend()) == simple.cend());
}
{
const std::string quote("\"hoge1-\\\"piyo2\\\"_fuga3\"");
BOOST_CHECK(is_valid::invoke(quote.cbegin(), quote.cend()) == quote.cend());
}
{
const std::string escape("\"I'm a string. \\\"You can quote me\\\". Name\\tJos\\u00E9\\nLocation\\tSF.\"");
BOOST_CHECK(is_valid::invoke(escape.cbegin(), escape.cend()) == escape.cend());
}
{
const std::string empty("\"\"");
BOOST_CHECK(is_valid::invoke(empty.cbegin(), empty.cend()) == empty.cend());
}
{
const std::string newline("\"newline\r\nhoge\"");
BOOST_CHECK(is_valid::invoke(newline.cbegin(), newline.cend()) == newline.cbegin());
}
{
const std::string invalid_escape("\"foo\\abar\"");
BOOST_CHECK(is_valid::invoke(invalid_escape.cbegin(), invalid_escape.cend()) == invalid_escape.cbegin());
}
{
const std::string invalid_character("\"foo\10bar\"");
BOOST_CHECK(is_valid::invoke(invalid_character.cbegin(), invalid_character.cend()) == invalid_character.cbegin());
}
{
const std::string multi("\"\"\"multiline\"\"\"");
BOOST_CHECK(is_valid::invoke(multi.cbegin(), multi.cend()) == multi.cbegin());
}
}
BOOST_AUTO_TEST_CASE(test_basic_multiline_string)
{
using is_valid = toml::is_basic_multiline_string<char>;
{
const std::string simple("\"\"\"foobar\"\"\"");
BOOST_CHECK(is_valid::invoke(simple.cbegin(), simple.cend()) == simple.cend());
}
{
const std::string quote("\"\"\"hoge1-\"piyo2\"_fuga3\"\"\"");
BOOST_CHECK(is_valid::invoke(quote.cbegin(), quote.cend()) == quote.cend());
}
{
const std::string newline("\"\"\"hoge1-\npiyo2_\r\nfuga3\"\"\"");
BOOST_CHECK(is_valid::invoke(newline.cbegin(), newline.cend()) == newline.cend());
}
{
const std::string escape("\"\"\"I'm a string. \"You can quote me\". Name\\tJos\\u00E9\\nLocation\\tSF.\"\"\"");
BOOST_CHECK(is_valid::invoke(escape.cbegin(), escape.cend()) == escape.cend());
}
{
const std::string empty("\"\"\"\"\"\"");
BOOST_CHECK(is_valid::invoke(empty.cbegin(), empty.cend()) == empty.cend());
}
{
const std::string ending_backslash("\"\"\"hoge\\\n piyo\\\n\"\"\"");
BOOST_CHECK(is_valid::invoke(ending_backslash.cbegin(), ending_backslash.cend()) == ending_backslash.cend());
}
{
const std::string invalid_escape("\"\"\"foo\\abar\"\"\"");
BOOST_CHECK(is_valid::invoke(invalid_escape.cbegin(), invalid_escape.cend()) == invalid_escape.cbegin());
}
{
const std::string invalid_character("\"\"\"foo\10bar\"\"\"");
BOOST_CHECK(is_valid::invoke(invalid_character.cbegin(), invalid_character.cend()) == invalid_character.cbegin());
}
{
const std::string single("\"singleline\"");
BOOST_CHECK(is_valid::invoke(single.cbegin(), single.cend()) == single.cbegin());
}
}
BOOST_AUTO_TEST_CASE(test_literal_inline_string)
{
using is_valid = toml::is_literal_inline_string<char>;
{
const std::string simple("'foobar'");
BOOST_CHECK(is_valid::invoke(simple.cbegin(), simple.cend()) == simple.cend());
}
{
const std::string nonescape("'C:\\Users\\nodejs\\templates'");
BOOST_CHECK(is_valid::invoke(nonescape.cbegin(), nonescape.cend()) == nonescape.cend());
}
{
const std::string empty("''");
BOOST_CHECK(is_valid::invoke(empty.cbegin(), empty.cend()) == empty.cend());
}
{
const std::string quote("'hoge1-'piyo2'_fuga3'");
BOOST_CHECK(is_valid::invoke(quote.cbegin(), quote.cend()) == quote.cbegin() + 8);
}
{
const std::string newline("'hoge1-\npiyo2_\r\nfuga3'");
BOOST_CHECK(is_valid::invoke(newline.cbegin(), newline.cend()) == newline.cbegin());
}
{
const std::string invalid_character("'foo\10bar'");
BOOST_CHECK(is_valid::invoke(invalid_character.cbegin(), invalid_character.cend()) == invalid_character.cbegin());
}
{
const std::string multi("'''multiline'''");
BOOST_CHECK(is_valid::invoke(multi.cbegin(), multi.cend()) == multi.cbegin());
}
}
BOOST_AUTO_TEST_CASE(test_literal_multiline_string)
{
using is_valid = toml::is_literal_multiline_string<char>;
{
const std::string simple("'''foobar'''");
BOOST_CHECK(is_valid::invoke(simple.cbegin(), simple.cend()) == simple.cend());
}
{
const std::string quote("'''hoge1-'piyo2'_fuga3'''");
BOOST_CHECK(is_valid::invoke(quote.cbegin(), quote.cend()) == quote.cend());
}
{
const std::string nonescape("'''C:\\Users\\nodejs\\templates'''");
BOOST_CHECK(is_valid::invoke(nonescape.cbegin(), nonescape.cend()) == nonescape.cend());
}
{
const std::string newline("'''hoge1-\npiyo2_\r\nfuga3'''");
BOOST_CHECK(is_valid::invoke(newline.cbegin(), newline.cend()) == newline.cend());
}
{
const std::string empty("''''''");
BOOST_CHECK(is_valid::invoke(empty.cbegin(), empty.cend()) == empty.cend());
}
{
const std::string invalid_character("'''foo\10bar'''");
BOOST_CHECK(is_valid::invoke(invalid_character.cbegin(), invalid_character.cend()) == invalid_character.cbegin());
}
{
const std::string single("'singleline'");
BOOST_CHECK(is_valid::invoke(single.cbegin(), single.cend()) == single.cbegin());
}
}
BOOST_AUTO_TEST_CASE(test_integer)
{
using is_valid = toml::is_integer<char>;
{
const std::string simple("1");
BOOST_CHECK(is_valid::invoke(simple.cbegin(), simple.cend()) == simple.cend());
}
{
const std::string psign("+1234");
BOOST_CHECK(is_valid::invoke(psign.cbegin(), psign.cend()) == psign.cend());
const std::string nsign("-1234");
BOOST_CHECK(is_valid::invoke(nsign.cbegin(), nsign.cend()) == nsign.cend());
}
{
const std::string zero("0");
BOOST_CHECK(is_valid::invoke(zero.cbegin(), zero.cend()) == zero.cend());
}
{
const std::string us("1_2_3_4_5");
BOOST_CHECK(is_valid::invoke(us.cbegin(), us.cend()) == us.cend());
}
{
const std::string f("12.34");
BOOST_CHECK(is_valid::invoke(f.cbegin(), f.cend()) == f.cbegin()+2);
}
{
const std::string f("12e34");
BOOST_CHECK(is_valid::invoke(f.cbegin(), f.cend()) == f.cbegin()+2);
}
{
const std::string ascii("1234a");
BOOST_CHECK(is_valid::invoke(ascii.cbegin(), ascii.cend()) == ascii.cbegin()+4);
}
}
BOOST_AUTO_TEST_CASE(test_float)
{
using is_valid = toml::is_float<char>;
{
const std::string simplef("1.0");
BOOST_CHECK(is_valid::invoke(simplef.cbegin(), simplef.cend()) == simplef.cend());
const std::string simplee("1e0");
BOOST_CHECK(is_valid::invoke(simplee.cbegin(), simplee.cend()) == simplee.cend());
const std::string both("6.626e-34");
BOOST_CHECK(is_valid::invoke(both.cbegin(), both.cend()) == both.cend());
}
{
const std::string psign("+1.0");
BOOST_CHECK(is_valid::invoke(psign.cbegin(), psign.cend()) == psign.cend());
const std::string nsign("-1.0");
BOOST_CHECK(is_valid::invoke(nsign.cbegin(), nsign.cend()) == nsign.cend());
}
{
const std::string psmall("+0.001");
BOOST_CHECK(is_valid::invoke(psmall.cbegin(), psmall.cend()) == psmall.cend());
const std::string nsmall("-0.001");
BOOST_CHECK(is_valid::invoke(nsmall.cbegin(), nsmall.cend()) == nsmall.cend());
}
{
const std::string zero("0.0");
BOOST_CHECK(is_valid::invoke(zero.cbegin(), zero.cend()) == zero.cend());
}
{
const std::string us("9_224_617.445_991_228_313");
BOOST_CHECK(is_valid::invoke(us.cbegin(), us.cend()) == us.cend());
}
}
BOOST_AUTO_TEST_CASE(test_boolean)
{
using is_valid = toml::is_boolean<char>;
{
const std::string t("true");
BOOST_CHECK(is_valid::invoke(t.cbegin(), t.cend()) == t.cend());
const std::string f("false");
BOOST_CHECK(is_valid::invoke(f.cbegin(), f.cend()) == f.cend());
}
{
const std::string t("True");
BOOST_CHECK(is_valid::invoke(t.cbegin(), t.cend()) == t.cbegin());
const std::string f("False");
BOOST_CHECK(is_valid::invoke(f.cbegin(), f.cend()) == f.cbegin());
}
}
BOOST_AUTO_TEST_CASE(test_localtime)
{
using is_valid = toml::is_local_time<char>;
{
const std::string t("07:32:00");
BOOST_CHECK(is_valid::invoke(t.cbegin(), t.cend()) == t.cend());
const std::string tf("07:32:00.0000");
BOOST_CHECK(is_valid::invoke(tf.cbegin(), tf.cend()) == tf.cend());
}
{
const std::string d("1907-32-00");
BOOST_CHECK(is_valid::invoke(d.cbegin(), d.cend()) == d.cbegin());
const std::string f("1907:32:00");
BOOST_CHECK(is_valid::invoke(f.cbegin(), f.cend()) == f.cbegin());
}
}
BOOST_AUTO_TEST_CASE(test_localdate)
{
using is_valid = toml::is_local_date<char>;
{
const std::string d("1907-32-00");
BOOST_CHECK(is_valid::invoke(d.cbegin(), d.cend()) == d.cend());
}
{
const std::string t("07:32:00");
BOOST_CHECK(is_valid::invoke(t.cbegin(), t.cend()) == t.cbegin());
const std::string f("1907:32:00");
BOOST_CHECK(is_valid::invoke(f.cbegin(),f.cend()) == f.cbegin());
}
}
BOOST_AUTO_TEST_CASE(test_localdatetime)
{
using is_valid = toml::is_local_date_time<char>;
{
const std::string dt("1907-32-00T07:32:00");
BOOST_CHECK(is_valid::invoke(dt.cbegin(), dt.cend()) == dt.cend());
const std::string dtf("1907-32-00T07:32:00.0000");
BOOST_CHECK(is_valid::invoke(dtf.cbegin(), dtf.cend()) == dtf.cend());
}
{
const std::string d("1907-32-00");
BOOST_CHECK(is_valid::invoke(d.cbegin(), d.cend()) == d.cbegin());
const std::string t("07:32:00");
BOOST_CHECK(is_valid::invoke(t.cbegin(), t.cend()) == t.cbegin());
const std::string f("1907-32-00 07:32:00");
BOOST_CHECK(is_valid::invoke(f.cbegin(), f.cend()) == f.cbegin());
}
}
BOOST_AUTO_TEST_CASE(test_offsetdatetime)
{
using is_valid = toml::is_offset_date_time<char>;
{
const std::string dtZ("1907-32-00T07:32:00Z");
BOOST_CHECK(is_valid::invoke(dtZ.cbegin(), dtZ.cend()) == dtZ.cend());
const std::string dtfZ("1907-32-00T07:32:00.0000Z");
BOOST_CHECK(is_valid::invoke(dtfZ.cbegin(), dtfZ.cend()) == dtfZ.cend());
const std::string dtp("1907-32-00T07:32:00+12:34");
BOOST_CHECK(is_valid::invoke(dtp.cbegin(), dtp.cend()) == dtp.cend());
const std::string dtfp("1907-32-00T07:32:00.0000+12:34");
BOOST_CHECK(is_valid::invoke(dtfp.cbegin(), dtfp.cend()) == dtfp.cend());
const std::string dtn("1907-32-00T07:32:00-12:34");
BOOST_CHECK(is_valid::invoke(dtn.cbegin(), dtn.cend()) == dtn.cend());
const std::string dtfn("1907-32-00T07:32:00.0000-12:34");
BOOST_CHECK(is_valid::invoke(dtfn.cbegin(), dtfn.cend()) == dtfn.cend());
}
{
const std::string d("1907-32-00");
BOOST_CHECK(is_valid::invoke(d.cbegin(), d.cend()) == d.cbegin());
const std::string t("07:32:00");
BOOST_CHECK(is_valid::invoke(t.cbegin(), t.cend()) == t.cbegin());
const std::string l("1907-32-00T07:32:00");
BOOST_CHECK(is_valid::invoke(l.cbegin(), l.cend()) == l.cbegin());
}
}
BOOST_AUTO_TEST_CASE(test_array)
{
using is_valid = toml::is_array<char>;
{
const std::string arr0("[]");
BOOST_CHECK(is_valid::invoke(arr0.cbegin(), arr0.cend()) == arr0.cend());
const std::string arr1("[1,2,3]");
BOOST_CHECK(is_valid::invoke(arr1.cbegin(), arr1.cend()) == arr1.cend());
const std::string arr2("[ 1,2,3 ]");
BOOST_CHECK(is_valid::invoke(arr2.cbegin(), arr2.cend()) == arr2.cend());
const std::string arr3("[ 1, 2, 3 ]");
BOOST_CHECK(is_valid::invoke(arr3.cbegin(), arr3.cend()) == arr3.cend());
const std::string arr4("[ 1, 2, 3, ]");
BOOST_CHECK(is_valid::invoke(arr4.cbegin(), arr4.cend()) == arr4.cend());
const std::string arr5("[ 1, 2, 3,]");
BOOST_CHECK(is_valid::invoke(arr5.cbegin(), arr5.cend()) == arr5.cend());
const std::string arr6("[ 1 , 2 , 3 ,]");
BOOST_CHECK(is_valid::invoke(arr6.cbegin(), arr6.cend()) == arr6.cend());
}
{
const std::string arr1("[\"red\", \"yellow\", \"green\"]");
BOOST_CHECK(is_valid::invoke(arr1.cbegin(), arr1.cend()) == arr1.cend());
const std::string arr2("[\"]\", \"#\", \" \"]");
BOOST_CHECK(is_valid::invoke(arr2.cbegin(), arr2.cend()) == arr2.cend());
const std::string arr3("[[1, 2, 3], ['a', 'b', 'c']]");
BOOST_CHECK(is_valid::invoke(arr3.cbegin(), arr3.cend()) == arr3.cend());
const std::string arr4("[{hoge = 1}, {piyo = 'a'}, {fuga = [1,2,3]}]");
BOOST_CHECK(is_valid::invoke(arr4.cbegin(), arr4.cend()) == arr4.cend());
}
{
const std::string arr1("[1,\n2,#comment\n3]");
BOOST_CHECK(is_valid::invoke(arr1.cbegin(), arr1.cend()) == arr1.cend());
const std::string arr2("[#c\n1,\n2,#comment\r\n3]");
BOOST_CHECK(is_valid::invoke(arr2.cbegin(), arr2.cend()) == arr2.cend());
}
{
const std::string invalid("[1, 3.14, 'string']");
BOOST_CHECK(is_valid::invoke(invalid.cbegin(), invalid.cend()) == invalid.cbegin());
const std::string valid("[[1,2,3], [3.14, 2.71, 1.414], ['foo', 'bar']]");
BOOST_CHECK(is_valid::invoke(valid.cbegin(), valid.cend()) == valid.cend());
}
}
BOOST_AUTO_TEST_CASE(test_inline_table)
{
using is_valid = toml::is_inline_table<char>;
{
const std::string tab0("{}");
BOOST_CHECK(is_valid::invoke(tab0.cbegin(), tab0.cend()) == tab0.cend());
const std::string tab1("{hoge=1,piyo=2,fuga=3}");
BOOST_CHECK(is_valid::invoke(tab1.cbegin(), tab1.cend()) == tab1.cend());
const std::string tab2("{hoge=1, piyo=2, fuga=3}");
BOOST_CHECK(is_valid::invoke(tab2.cbegin(), tab2.cend()) == tab2.cend());
const std::string tab3("{ hoge=1, piyo=2, fuga=3 }");
BOOST_CHECK(is_valid::invoke(tab3.cbegin(), tab3.cend()) == tab3.cend());
const std::string tab4("{ hoge = 1, piyo = 2, fuga = 3 }");
BOOST_CHECK(is_valid::invoke(tab4.cbegin(), tab4.cend()) == tab4.cend());
const std::string tab5("{hoge = 1, piyo = 2, fuga = 3}");
BOOST_CHECK(is_valid::invoke(tab5.cbegin(), tab5.cend()) == tab5.cend());
const std::string tab6("{hoge = 1, piyo = 2, fuga = 3,}");
BOOST_CHECK(is_valid::invoke(tab6.cbegin(), tab6.cend()) == tab6.cend());
const std::string tab7("{hoge = 1, piyo = 2, fuga = 3, }");
BOOST_CHECK(is_valid::invoke(tab7.cbegin(), tab7.cend()) == tab7.cend());
}
{
const std::string tab0("{hoge = 1, piyo = 2.0}");
BOOST_CHECK(is_valid::invoke(tab0.cbegin(), tab0.cend()) == tab0.cend());
const std::string tab1("{hoge = [1,2,3], piyo = {fuga = {}}}");
BOOST_CHECK(is_valid::invoke(tab1.cbegin(), tab1.cend()) == tab1.cend());
const std::string tab2("{hoge = \"}\", piyo = \"#\"}");
BOOST_CHECK(is_valid::invoke(tab2.cbegin(), tab2.cend()) == tab2.cend());
const std::string tab3("{b=true, i=1, f=2.0, d=1907-03-02T07:32:00, s='str', a=[1,2,3], t={foo=1}}");
BOOST_CHECK(is_valid::invoke(tab3.cbegin(), tab3.cend()) == tab3.cend());
}
{
const std::string tab0("{hoge = \"}\",\n piyo = \"#\"}");
BOOST_CHECK(is_valid::invoke(tab0.cbegin(), tab0.cend()) == tab0.cbegin());
}
}
BOOST_AUTO_TEST_CASE(test_table_definition)
{
using is_valid = toml::is_table_definition<char>;
{
const std::string simple("[hoge]");
BOOST_CHECK(is_valid::invoke(simple.cbegin(), simple.cend()) == simple.cend());
const std::string dotted("[hoge.piyo.fuga]");
BOOST_CHECK(is_valid::invoke(dotted.cbegin(), dotted.cend()) == dotted.cend());
const std::string spaced_dotted("[hoge . piyo .fuga. foo]");
BOOST_CHECK(is_valid::invoke(spaced_dotted.cbegin(), spaced_dotted.cend()) == spaced_dotted.cend());
const std::string quoted("[\"hoge\"]");
BOOST_CHECK(is_valid::invoke(quoted.cbegin(), quoted.cend()) == quoted.cend());
const std::string quoted_dot("[\"hoge\".'piyo'.fuga]");
BOOST_CHECK(is_valid::invoke(quoted_dot.cbegin(), quoted_dot.cend()) == quoted_dot.cend());
}
}
BOOST_AUTO_TEST_CASE(test_array_of_table_definition)
{
using is_valid = toml::is_array_of_table_definition<char>;
{
const std::string simple("[[hoge]]");
BOOST_CHECK(is_valid::invoke(simple.cbegin(), simple.cend()) == simple.cend());
const std::string dotted("[[hoge.piyo.fuga]]");
BOOST_CHECK(is_valid::invoke(dotted.cbegin(), dotted.cend()) == dotted.cend());
const std::string spaced_dotted("[[hoge . piyo .fuga. foo]]");
BOOST_CHECK(is_valid::invoke(spaced_dotted.cbegin(), spaced_dotted.cend()) == spaced_dotted.cend());
const std::string quoted("[[\"hoge\"]]");
BOOST_CHECK(is_valid::invoke(quoted.cbegin(), quoted.cend()) == quoted.cend());
const std::string quoted_dot("[[\"hoge\".'piyo'.fuga]]");
BOOST_CHECK(is_valid::invoke(quoted_dot.cbegin(), quoted_dot.cend()) == quoted_dot.cend());
}
}
BOOST_AUTO_TEST_CASE(test_key)
{
using is_valid = toml::is_key<char>;
{
const std::string simple("foobar");
BOOST_CHECK(is_valid::invoke(simple.cbegin(), simple.cend()) == simple.cend());
const std::string quoted("\"foo#bar.baz\\n\"");
BOOST_CHECK(is_valid::invoke(quoted.cbegin(), quoted.cend()) == quoted.cend());
}
}
BOOST_AUTO_TEST_CASE(test_value)
{
using is_valid = toml::is_value<char>;
{
const std::string boolean("true");
BOOST_CHECK(is_valid::invoke(boolean.cbegin(), boolean.cend()) == boolean.cend());
const std::string integer("-42");
BOOST_CHECK(is_valid::invoke(integer.cbegin(), integer.cend()) == integer.cend());
const std::string floating("-42e0");
BOOST_CHECK(is_valid::invoke(floating.cbegin(), floating.cend()) == floating.cend());
const std::string string("\"string\"");
BOOST_CHECK(is_valid::invoke(string.cbegin(), string.cend()) == string.cend());
const std::string datetime("1901-01-01T00:00:00");
BOOST_CHECK(is_valid::invoke(datetime.cbegin(), datetime.cend()) == datetime.cend());
const std::string array("[1,2,3]");
BOOST_CHECK(is_valid::invoke(array.cbegin(), array.cend()) == array.cend());
const std::string table("{foo=1,bar=2.0,baz='3'}");
BOOST_CHECK(is_valid::invoke(table.cbegin(), table.cend()) == table.cend());
}
}
BOOST_AUTO_TEST_CASE(test_key_value_pair)
{
using is_valid = toml::is_key_value_pair<char>;
{
const std::string kv("key=1");
BOOST_CHECK(is_valid::invoke(kv.cbegin(), kv.cend()) == kv.cend());
}
{
const std::string kv("key = 1");
BOOST_CHECK(is_valid::invoke(kv.cbegin(), kv.cend()) == kv.cend());
}
{
const std::string kv(" key = 1");
BOOST_CHECK(is_valid::invoke(kv.cbegin(), kv.cend()) == kv.cend());
}
{
const std::string kv(" key = 1 ");
BOOST_CHECK(is_valid::invoke(kv.cbegin(), kv.cend()) == kv.cend());
}
{
const std::string boolean("key = true");
BOOST_CHECK(is_valid::invoke(boolean.cbegin(), boolean.cend()) == boolean.cend());
const std::string integer("key = -42");
BOOST_CHECK(is_valid::invoke(integer.cbegin(), integer.cend()) == integer.cend());
const std::string floating("key = -42.0");
BOOST_CHECK(is_valid::invoke(floating.cbegin(), floating.cend()) == floating.cend());
const std::string string("key = \"string\"");
BOOST_CHECK(is_valid::invoke(string.cbegin(), string.cend()) == string.cend());
const std::string datetime("key = 1901-01-01T00:00:00");
BOOST_CHECK(is_valid::invoke(datetime.cbegin(), datetime.cend()) == datetime.cend());
const std::string array("key = [1,2,3]");
BOOST_CHECK(is_valid::invoke(array.cbegin(), array.cend()) == array.cend());
const std::string table("key = {foo=1,bar=2.0,baz='3'}");
BOOST_CHECK(is_valid::invoke(table.cbegin(), table.cend()) == table.cend());
}
}
BOOST_AUTO_TEST_CASE(test_empty_line)
{
using is_valid = toml::is_empty_line<char>;
{
const std::string empty("\n");
BOOST_CHECK(is_valid::invoke(empty.cbegin(), empty.cend()) == empty.cend());
}
{
const std::string empty(" \n");
BOOST_CHECK(is_valid::invoke(empty.cbegin(), empty.cend()) == empty.cend());
}
{
const std::string empty("#comment\n");
BOOST_CHECK(is_valid::invoke(empty.cbegin(), empty.cend()) == empty.cend());
}
{
const std::string empty(" #comment\n");
BOOST_CHECK(is_valid::invoke(empty.cbegin(), empty.cend()) == empty.cend());
}
}

View File

@@ -5,26 +5,117 @@
#define BOOST_TEST_NO_LIB
#include <boost/test/included/unit_test.hpp>
#endif
#include <toml.hpp>
#include <thread>
#include <toml/datetime.hpp>
BOOST_AUTO_TEST_CASE(test_datetime_convertible)
BOOST_AUTO_TEST_CASE(test_local_date)
{
const auto now = std::chrono::system_clock::now();
toml::Datetime d1(now);
const std::chrono::system_clock::time_point cvt(d1);
toml::Datetime d2(cvt);
const toml::local_date date(2018, toml::month_t::Jan, 1);
const toml::local_date date1(date);
BOOST_CHECK_EQUAL(date, date1);
BOOST_CHECK_EQUAL(d1, d2);
const std::chrono::system_clock::time_point tp(date);
const toml::local_date date2(tp);
BOOST_CHECK_EQUAL(date, date2);
const auto time = std::chrono::system_clock::to_time_t(now);
toml::Datetime d3(time);
toml::Datetime d4(std::chrono::system_clock::from_time_t(time));
const toml::local_date date3(2017, toml::month_t::Dec, 31);
BOOST_CHECK(date > date3);
BOOST_CHECK_EQUAL(d3, d4);
std::this_thread::sleep_for(std::chrono::seconds(1));
const auto later = std::chrono::system_clock::now();
toml::Datetime d5(later);
BOOST_CHECK(d1 < d5);
std::ostringstream oss;
oss << date;
BOOST_CHECK_EQUAL(oss.str(), std::string("2018-01-01"));
}
BOOST_AUTO_TEST_CASE(test_local_time)
{
const toml::local_time time(12, 30, 45);
const toml::local_time time1(time);
BOOST_CHECK_EQUAL(time, time1);
const std::chrono::nanoseconds dur(time);
std::chrono::nanoseconds ns(0);
ns += std::chrono::hours (12);
ns += std::chrono::minutes(30);
ns += std::chrono::seconds(45);
BOOST_CHECK_EQUAL(dur.count(), ns.count());
const toml::local_time time3(12, 15, 45);
BOOST_CHECK(time > time3);
{
std::ostringstream oss;
oss << time;
BOOST_CHECK_EQUAL(oss.str(), std::string("12:30:45"));
}
{
const toml::local_time time4(12, 30, 45, 123, 456);
std::ostringstream oss;
oss << time4;
BOOST_CHECK_EQUAL(oss.str(), std::string("12:30:45.123456"));
}
}
BOOST_AUTO_TEST_CASE(test_time_offset)
{
const toml::time_offset time(9, 30);
const toml::time_offset time1(time);
BOOST_CHECK_EQUAL(time, time1);
const std::chrono::minutes dur(time);
std::chrono::minutes m(0);
m += std::chrono::hours (9);
m += std::chrono::minutes(30);
BOOST_CHECK_EQUAL(dur.count(), m.count());
const toml::time_offset time2(9, 0);
BOOST_CHECK(time2 < time);
std::ostringstream oss;
oss << time;
BOOST_CHECK_EQUAL(oss.str(), std::string("+09:30"));
}
BOOST_AUTO_TEST_CASE(test_local_datetime)
{
const toml::local_datetime dt(toml::local_date(2018, toml::month_t::Jan, 1),
toml::local_time(12, 30, 45));
const toml::local_datetime dt1(dt);
BOOST_CHECK_EQUAL(dt, dt1);
const std::chrono::system_clock::time_point tp(dt);
const toml::local_datetime dt2(tp);
BOOST_CHECK_EQUAL(dt, dt2);
std::ostringstream oss;
oss << dt;
BOOST_CHECK_EQUAL(oss.str(), std::string("2018-01-01T12:30:45"));
}
BOOST_AUTO_TEST_CASE(test_offset_datetime)
{
const toml::offset_datetime dt(toml::local_date(2018, toml::month_t::Jan, 1),
toml::local_time(12, 30, 45),
toml::time_offset(9, 30));
const toml::offset_datetime dt1(dt);
BOOST_CHECK_EQUAL(dt, dt1);
const std::chrono::system_clock::time_point tp1(dt);
const toml::offset_datetime dt2(tp1);
const std::chrono::system_clock::time_point tp2(dt2);
BOOST_CHECK(tp1 == tp2);
{
std::ostringstream oss;
oss << dt;
BOOST_CHECK_EQUAL(oss.str(), std::string("2018-01-01T12:30:45+09:30"));
}
{
const toml::offset_datetime dt3(
toml::local_date(2018, toml::month_t::Jan, 1),
toml::local_time(12, 30, 45),
toml::time_offset(0, 0));
std::ostringstream oss;
oss << dt3;
BOOST_CHECK_EQUAL(oss.str(), std::string("2018-01-01T12:30:45Z"));
}
}

View File

@@ -12,161 +12,52 @@
#include <deque>
#include <array>
BOOST_AUTO_TEST_CASE(test_from_toml_exact)
BOOST_AUTO_TEST_CASE(test_from_toml)
{
toml::Boolean b(true);
toml::Integer i(42);
toml::Float f(3.14);
toml::String s("hoge");
toml::Datetime d(std::chrono::system_clock::now());
toml::Array a;
a.emplace_back(2);
a.emplace_back(7);
a.emplace_back(1);
a.emplace_back(8);
a.emplace_back(2);
toml::Table t;
t.emplace("val1", true);
t.emplace("val2", 42);
t.emplace("val3", 3.14);
t.emplace("val4", "piyo");
toml::boolean b = false;
toml::integer i = 0;
toml::floating f = 0.;
toml::string s;
toml::local_date dt;
toml::array a;
toml::table t;
{
toml::value v(true);
toml::from_toml(std::tie(b, i, f, s, dt, a, t), v);
BOOST_CHECK_EQUAL(b, true);
}
{
toml::value v(42);
toml::from_toml(std::tie(b, i, f, s, dt, a, t), v);
BOOST_CHECK_EQUAL(i, 42);
}
{
toml::value v(3.14);
toml::from_toml(std::tie(b, i, f, s, dt, a, t), v);
BOOST_CHECK_EQUAL(f, 3.14);
}
{
toml::value v("foo");
toml::value v1(b);
toml::value v2(i);
toml::value v3(f);
toml::value v4(s);
toml::value v5(d);
toml::value v6(a);
toml::value v7(t);
toml::Boolean u1;
toml::Integer u2;
toml::Float u3;
toml::String u4;
toml::Datetime u5;
toml::Array u6;
toml::Table u7;
toml::from_toml(u1, v1);
toml::from_toml(u2, v2);
toml::from_toml(u3, v3);
toml::from_toml(u4, v4);
toml::from_toml(u5, v5);
toml::from_toml(u6, v6);
toml::from_toml(u7, v7);
BOOST_CHECK_EQUAL(u1, b);
BOOST_CHECK_EQUAL(u2, i);
BOOST_CHECK_EQUAL(u3, f);
BOOST_CHECK_EQUAL(u4, s);
BOOST_CHECK_EQUAL(u6.at(0).cast<toml::value_t::Integer>(), a.at(0).cast<toml::value_t::Integer>());
BOOST_CHECK_EQUAL(u6.at(1).cast<toml::value_t::Integer>(), a.at(1).cast<toml::value_t::Integer>());
BOOST_CHECK_EQUAL(u6.at(2).cast<toml::value_t::Integer>(), a.at(2).cast<toml::value_t::Integer>());
BOOST_CHECK_EQUAL(u6.at(3).cast<toml::value_t::Integer>(), a.at(3).cast<toml::value_t::Integer>());
BOOST_CHECK_EQUAL(u6.at(4).cast<toml::value_t::Integer>(), a.at(4).cast<toml::value_t::Integer>());
BOOST_CHECK_EQUAL(u7.at("val1").cast<toml::value_t::Boolean>(), true);
BOOST_CHECK_EQUAL(u7.at("val2").cast<toml::value_t::Integer>(), 42);
BOOST_CHECK_CLOSE_FRACTION(u7.at("val3").cast<toml::value_t::Float>(),3.14, 1e-3);
BOOST_CHECK_EQUAL(u7.at("val4").cast<toml::value_t::String>(), "piyo");
}
BOOST_AUTO_TEST_CASE(test_from_toml_cast)
{
toml::Integer i(42);
toml::Float f(3.14);
toml::Array a{2, 7, 1, 8, 2};
toml::Table t{{"val1", true}, {"val2", 42}, {"val3", 3.14}, {"val4", "piyo"}};
toml::value vi(i);
toml::value vf(f);
toml::value va(a);
toml::value vt(t);
int u1;
std::size_t u2;
float u3;
std::list<int> u4;
std::deque<std::size_t> u5;
std::array<long, 5> u6;
std::map<std::string, toml::value> u7;
std::list<int> expect_list{2,7,1,8,2};
std::deque<std::size_t> expect_deque{2,7,1,8,2};
std::array<long, 5> expect_array{{2,7,1,8,2}};
toml::from_toml(u1, vi);
toml::from_toml(u2, vi);
toml::from_toml(u3, vf);
toml::from_toml(u4, va);
toml::from_toml(u5, va);
toml::from_toml(u6, va);
toml::from_toml(u7, vt);
BOOST_CHECK_EQUAL(u1, 42);
BOOST_CHECK_EQUAL(u2, 42ul);
BOOST_CHECK_CLOSE_FRACTION(u3, 3.14, 1e-3);
const bool same_list = (u4 == expect_list);
const bool same_deque = (u5 == expect_deque);
const bool same_array = (u6 == expect_array);
BOOST_CHECK(same_list);
BOOST_CHECK(same_deque);
BOOST_CHECK(same_array);
BOOST_CHECK_EQUAL(u7["val1"].cast<toml::value_t::Boolean>(), true);
BOOST_CHECK_EQUAL(u7["val2"].cast<toml::value_t::Integer>(), 42);
BOOST_CHECK_CLOSE_FRACTION(u7["val3"].cast<toml::value_t::Float>(), 3.14, 1e-3);
BOOST_CHECK_EQUAL(u7["val4"].cast<toml::value_t::String >(), "piyo");
}
BOOST_AUTO_TEST_CASE(test_from_toml_tie)
{
toml::Boolean b(42);
toml::Integer i(42);
toml::Float f(3.14);
toml::Array a;
a.emplace_back(2);
a.emplace_back(7);
a.emplace_back(1);
a.emplace_back(8);
a.emplace_back(2);
toml::Table t;
t.emplace("val1", true);
t.emplace("val2", 42);
t.emplace("val3", 3.14);
t.emplace("val4", "piyo");
toml::value vb(b);
toml::value vi(i);
toml::value vf(f);
toml::value va(a);
toml::value vt(t);
bool ub;
int ui;
float uf;
std::deque<int> ua;
std::map<std::string, toml::value> ut;
toml::from_toml(std::tie(ub, ui, uf, ua, ut), vb);
toml::from_toml(std::tie(ub, ui, uf, ua, ut), vi);
toml::from_toml(std::tie(ub, ui, uf, ua, ut), vf);
toml::from_toml(std::tie(ub, ui, uf, ua, ut), va);
toml::from_toml(std::tie(ub, ui, uf, ua, ut), va);
toml::from_toml(std::tie(ub, ui, uf, ua, ut), vt);
BOOST_CHECK_EQUAL(ub, true);
BOOST_CHECK_EQUAL(ui, 42);
BOOST_CHECK_CLOSE_FRACTION(uf, 3.14, 1e-3);
BOOST_CHECK_EQUAL(ua.at(0), 2);
BOOST_CHECK_EQUAL(ua.at(1), 7);
BOOST_CHECK_EQUAL(ua.at(2), 1);
BOOST_CHECK_EQUAL(ua.at(3), 8);
BOOST_CHECK_EQUAL(ua.at(4), 2);
BOOST_CHECK_EQUAL(ut["val1"].cast<toml::value_t::Boolean>(), true);
BOOST_CHECK_EQUAL(ut["val2"].cast<toml::value_t::Integer>(), 42);
BOOST_CHECK_CLOSE_FRACTION(ut["val3"].cast<toml::value_t::Float>(), 3.14, 1e-3);
BOOST_CHECK_EQUAL(ut["val4"].cast<toml::value_t::String >(), "piyo");
toml::from_toml(std::tie(b, i, f, s, dt, a, t), v);
BOOST_CHECK_EQUAL(s, "foo");
}
{
toml::value v(toml::local_date(2018, toml::month_t::Apr, 22));
toml::from_toml(std::tie(b, i, f, s, dt, a, t), v);
BOOST_CHECK(dt == toml::local_date(2018, toml::month_t::Apr, 22));
}
{
toml::array ref{toml::value(42), toml::value(54)};
toml::value v(ref);
toml::from_toml(std::tie(b, i, f, s, dt, a, t), v);
BOOST_CHECK(ref == a);
}
{
toml::table ref{{"key1", 42}, {"key2", 3.14}};
toml::value v(ref);
toml::from_toml(std::tie(b, i, f, s, dt, a, t), v);
BOOST_CHECK(ref == t);
}
}

View File

@@ -5,7 +5,8 @@
#define BOOST_TEST_NO_LIB
#include <boost/test/included/unit_test.hpp>
#endif
#include <toml.hpp>
#include <toml/value.hpp>
#include <toml/get.hpp>
#include <map>
#include <unordered_map>
#include <list>
@@ -15,98 +16,350 @@
BOOST_AUTO_TEST_CASE(test_get_exact)
{
toml::Boolean b(true);
toml::Integer i(42);
toml::Float f(3.14);
toml::String s("hoge");
toml::Array a;
a.emplace_back(2);
a.emplace_back(7);
a.emplace_back(1);
a.emplace_back(8);
a.emplace_back(2);
toml::Table t;
t.emplace("val1", true);
t.emplace("val2", 42);
t.emplace("val3", 3.14);
t.emplace("val4", "piyo");
{
toml::value v(true);
BOOST_CHECK_EQUAL(true, toml::get<toml::boolean>(v));
toml::value v1(b);
toml::value v2(i);
toml::value v3(f);
toml::value v4(s);
toml::value v6(a);
toml::value v7(t);
toml::get<toml::boolean>(v) = false;
BOOST_CHECK_EQUAL(false, toml::get<toml::boolean>(v));
}
{
toml::value v(42);
BOOST_CHECK_EQUAL(toml::integer(42), toml::get<toml::integer>(v));
toml::Boolean u1 = toml::get<toml::Boolean >(v1);
toml::Integer u2 = toml::get<toml::Integer >(v2);
toml::Float u3 = toml::get<toml::Float >(v3);
toml::String u4 = toml::get<toml::String >(v4);
toml::Array u6 = toml::get<toml::Array >(v6);
toml::Table u7 = toml::get<toml::Table >(v7);
toml::get<toml::integer>(v) = 54;
BOOST_CHECK_EQUAL(toml::integer(54), toml::get<toml::integer>(v));
}
{
toml::value v(3.14);
BOOST_CHECK_EQUAL(toml::floating(3.14), toml::get<toml::floating>(v));
BOOST_CHECK_EQUAL(u1, b);
BOOST_CHECK_EQUAL(u2, i);
BOOST_CHECK_EQUAL(u3, f);
BOOST_CHECK_EQUAL(u4, s);
BOOST_CHECK_EQUAL(u6.at(0).cast<toml::value_t::Integer>(), a.at(0).cast<toml::value_t::Integer>());
BOOST_CHECK_EQUAL(u6.at(1).cast<toml::value_t::Integer>(), a.at(1).cast<toml::value_t::Integer>());
BOOST_CHECK_EQUAL(u6.at(2).cast<toml::value_t::Integer>(), a.at(2).cast<toml::value_t::Integer>());
BOOST_CHECK_EQUAL(u6.at(3).cast<toml::value_t::Integer>(), a.at(3).cast<toml::value_t::Integer>());
BOOST_CHECK_EQUAL(u6.at(4).cast<toml::value_t::Integer>(), a.at(4).cast<toml::value_t::Integer>());
BOOST_CHECK_EQUAL(u7.at("val1").cast<toml::value_t::Boolean>(), true);
BOOST_CHECK_EQUAL(u7.at("val2").cast<toml::value_t::Integer>(), 42);
BOOST_CHECK_CLOSE_FRACTION(u7.at("val3").cast<toml::value_t::Float>(),3.14, 1e-3);
BOOST_CHECK_EQUAL(u7.at("val4").cast<toml::value_t::String>(), "piyo");
toml::get<toml::floating>(v) = 2.71;
BOOST_CHECK_EQUAL(toml::floating(2.71), toml::get<toml::floating>(v));
}
{
toml::value v("foo");
BOOST_CHECK_EQUAL(toml::string("foo", toml::string_t::basic),
toml::get<toml::string>(v));
toml::get<toml::string>(v).str += "bar";
BOOST_CHECK_EQUAL(toml::string("foobar", toml::string_t::basic),
toml::get<toml::string>(v));
}
{
toml::value v("foo", toml::string_t::literal);
BOOST_CHECK_EQUAL(toml::string("foo", toml::string_t::literal),
toml::get<toml::string>(v));
toml::get<toml::string>(v).str += "bar";
BOOST_CHECK_EQUAL(toml::string("foobar", toml::string_t::literal),
toml::get<toml::string>(v));
}
{
toml::local_date d(2018, toml::month_t::Apr, 22);
toml::value v(d);
BOOST_CHECK(d == toml::get<toml::local_date>(v));
toml::get<toml::local_date>(v).year = 2017;
d.year = 2017;
BOOST_CHECK(d == toml::get<toml::local_date>(v));
}
{
toml::local_time t(12, 30, 45);
toml::value v(t);
BOOST_CHECK(t == toml::get<toml::local_time>(v));
toml::get<toml::local_time>(v).hour = 9;
t.hour = 9;
BOOST_CHECK(t == toml::get<toml::local_time>(v));
}
{
toml::local_datetime dt(toml::local_date(2018, toml::month_t::Apr, 22),
toml::local_time(12, 30, 45));
toml::value v(dt);
BOOST_CHECK(dt == toml::get<toml::local_datetime>(v));
toml::get<toml::local_datetime>(v).date.year = 2017;
dt.date.year = 2017;
BOOST_CHECK(dt == toml::get<toml::local_datetime>(v));
}
{
toml::offset_datetime dt(toml::local_datetime(
toml::local_date(2018, toml::month_t::Apr, 22),
toml::local_time(12, 30, 45)), toml::time_offset(9, 0));
toml::value v(dt);
BOOST_CHECK(dt == toml::get<toml::offset_datetime>(v));
toml::get<toml::offset_datetime>(v).date.year = 2017;
dt.date.year = 2017;
BOOST_CHECK(dt == toml::get<toml::offset_datetime>(v));
}
{
toml::array vec;
vec.push_back(toml::value(42));
vec.push_back(toml::value(54));
toml::value v(vec);
BOOST_CHECK(vec == toml::get<toml::array>(v));
toml::get<toml::array>(v).push_back(toml::value(123));
vec.push_back(toml::value(123));
BOOST_CHECK(vec == toml::get<toml::array>(v));
}
{
toml::table tab;
tab["key1"] = toml::value(42);
tab["key2"] = toml::value(3.14);
toml::value v(tab);
BOOST_CHECK(tab == toml::get<toml::table>(v));
toml::get<toml::table>(v)["key3"] = toml::value(123);
tab["key3"] = toml::value(123);
BOOST_CHECK(tab == toml::get<toml::table>(v));
}
}
BOOST_AUTO_TEST_CASE(test_get_cast)
BOOST_AUTO_TEST_CASE(test_get_integer_type)
{
toml::Integer i(42);
toml::Float f(3.14);
toml::String s("hoge");
toml::Datetime d(std::chrono::system_clock::now());
toml::Array a;
a.emplace_back(2);
a.emplace_back(7);
a.emplace_back(1);
a.emplace_back(8);
a.emplace_back(2);
toml::Table t;
t.emplace("val1", true);
t.emplace("val2", 42);
t.emplace("val3", 3.14);
t.emplace("val4", "piyo");
toml::value v2(i);
toml::value v3(f);
toml::value v4(s);
toml::value v5(d);
toml::value v6(a);
toml::value v7(t);
const auto u2 = toml::get<std::size_t>(v2);
const auto u3 = toml::get<float>(v3);
const auto u4 = toml::get<std::deque<int>>(v6);
const auto u5 = toml::get<std::list<int> >(v6);
const auto u6 = toml::get<std::array<int,5>>(v6);
std::map<std::string, toml::value> u7 = toml::get<std::map<std::string, toml::value>>(v7);
std::deque<int> r4{2,7,1,8,2};
std::list<int> r5{2,7,1,8,2};
std::array<int, 5> r6{{2,7,1,8,2}};
BOOST_CHECK_EQUAL(u2, 42ul);
BOOST_CHECK_CLOSE_FRACTION(u3, 3.14, 1e-3);
const bool dq = r4 == u4;
const bool ls = r5 == u5;
const bool ar = r6 == u6;
BOOST_CHECK(dq);
BOOST_CHECK(ls);
BOOST_CHECK(ar);
BOOST_CHECK_EQUAL(u7.at("val1").cast<toml::value_t::Boolean>(), true);
BOOST_CHECK_EQUAL(u7.at("val2").cast<toml::value_t::Integer>(), 42);
BOOST_CHECK_CLOSE_FRACTION(u7.at("val3").cast<toml::value_t::Float>(),3.14, 1e-3);
BOOST_CHECK_EQUAL(u7.at("val4").cast<toml::value_t::String>(), "piyo");
{
toml::value v(42);
BOOST_CHECK_EQUAL(int(42), toml::get<int >(v));
BOOST_CHECK_EQUAL(short(42), toml::get<short >(v));
BOOST_CHECK_EQUAL(char(42), toml::get<char >(v));
BOOST_CHECK_EQUAL(unsigned(42), toml::get<unsigned >(v));
BOOST_CHECK_EQUAL(long(42), toml::get<long >(v));
BOOST_CHECK_EQUAL(std::int64_t(42), toml::get<std::int64_t >(v));
BOOST_CHECK_EQUAL(std::uint64_t(42), toml::get<std::uint64_t>(v));
BOOST_CHECK_EQUAL(std::int16_t(42), toml::get<std::int16_t >(v));
BOOST_CHECK_EQUAL(std::uint16_t(42), toml::get<std::uint16_t>(v));
}
}
BOOST_AUTO_TEST_CASE(test_get_floating_type)
{
{
toml::value v(3.14);
BOOST_CHECK_EQUAL(static_cast<float >(3.14), toml::get<float >(v));
BOOST_CHECK_EQUAL(static_cast<double >(3.14), toml::get<double >(v));
BOOST_CHECK_EQUAL(static_cast<long double>(3.14), toml::get<long double>(v));
}
}
BOOST_AUTO_TEST_CASE(test_get_string_type)
{
{
toml::value v("foo", toml::string_t::basic);
BOOST_CHECK_EQUAL("foo", toml::get<std::string>(v));
toml::get<std::string>(v) += "bar";
BOOST_CHECK_EQUAL("foobar", toml::get<std::string>(v));
}
{
toml::value v("foo", toml::string_t::literal);
BOOST_CHECK_EQUAL("foo", toml::get<std::string>(v));
toml::get<std::string>(v) += "bar";
BOOST_CHECK_EQUAL("foobar", toml::get<std::string>(v));
}
}
BOOST_AUTO_TEST_CASE(test_get_toml_array)
{
toml::value v(toml::array(0));
toml::get<toml::array>(v).push_back(toml::value(42));
toml::get<toml::array>(v).push_back(toml::value(54));
toml::get<toml::array>(v).push_back(toml::value(69));
toml::get<toml::array>(v).push_back(toml::value(72));
const std::vector<int> vec = toml::get<std::vector<int>>(v);
const std::list<short> lst = toml::get<std::list<short>>(v);
const std::deque<std::int64_t> deq = toml::get<std::deque<std::int64_t>>(v);
BOOST_CHECK_EQUAL(42, vec.at(0));
BOOST_CHECK_EQUAL(54, vec.at(1));
BOOST_CHECK_EQUAL(69, vec.at(2));
BOOST_CHECK_EQUAL(72, vec.at(3));
std::list<short>::const_iterator iter = lst.begin();
BOOST_CHECK_EQUAL(static_cast<short>(42), *(iter++));
BOOST_CHECK_EQUAL(static_cast<short>(54), *(iter++));
BOOST_CHECK_EQUAL(static_cast<short>(69), *(iter++));
BOOST_CHECK_EQUAL(static_cast<short>(72), *(iter++));
BOOST_CHECK_EQUAL(static_cast<std::int64_t>(42), deq.at(0));
BOOST_CHECK_EQUAL(static_cast<std::int64_t>(54), deq.at(1));
BOOST_CHECK_EQUAL(static_cast<std::int64_t>(69), deq.at(2));
BOOST_CHECK_EQUAL(static_cast<std::int64_t>(72), deq.at(3));
std::array<int, 4> ary = toml::get<std::array<int, 4>>(v);
BOOST_CHECK_EQUAL(static_cast<int>(42), ary.at(0));
BOOST_CHECK_EQUAL(static_cast<int>(54), ary.at(1));
BOOST_CHECK_EQUAL(static_cast<int>(69), ary.at(2));
BOOST_CHECK_EQUAL(static_cast<int>(72), ary.at(3));
std::tuple<int, short, unsigned, long> tpl =
toml::get<std::tuple<int, short, unsigned, long>>(v);
BOOST_CHECK_EQUAL(static_cast<int >(42), std::get<0>(tpl));
BOOST_CHECK_EQUAL(static_cast<short >(54), std::get<1>(tpl));
BOOST_CHECK_EQUAL(static_cast<unsigned>(69), std::get<2>(tpl));
BOOST_CHECK_EQUAL(static_cast<long >(72), std::get<3>(tpl));
toml::value p(toml::array{});
toml::get<toml::array>(p).push_back(toml::value(3.14));
toml::get<toml::array>(p).push_back(toml::value(2.71));
std::pair<double, double> pr = toml::get<std::pair<double, double> >(p);
BOOST_CHECK_EQUAL(3.14, pr.first);
BOOST_CHECK_EQUAL(2.71, pr.second);
}
BOOST_AUTO_TEST_CASE(test_get_toml_array_of_array)
{
toml::value v1(toml::array{});
toml::get<toml::array>(v1).push_back(toml::value(42));
toml::get<toml::array>(v1).push_back(toml::value(54));
toml::get<toml::array>(v1).push_back(toml::value(69));
toml::get<toml::array>(v1).push_back(toml::value(72));
toml::value v2(toml::array{});
toml::get<toml::array>(v2).push_back(toml::value("foo"));
toml::get<toml::array>(v2).push_back(toml::value("bar"));
toml::get<toml::array>(v2).push_back(toml::value("baz"));
toml::value v(toml::array(2));
toml::get<toml::array>(v).at(0) = v1;
toml::get<toml::array>(v).at(1) = v2;
std::pair<std::vector<int>, std::vector<std::string>> p =
toml::get<std::pair<std::vector<int>, std::vector<std::string>>>(v);
BOOST_CHECK_EQUAL(p.first.at(0), 42);
BOOST_CHECK_EQUAL(p.first.at(1), 54);
BOOST_CHECK_EQUAL(p.first.at(2), 69);
BOOST_CHECK_EQUAL(p.first.at(3), 72);
BOOST_CHECK_EQUAL(p.second.at(0), "foo");
BOOST_CHECK_EQUAL(p.second.at(1), "bar");
BOOST_CHECK_EQUAL(p.second.at(2), "baz");
std::tuple<std::vector<int>, std::vector<std::string>> t =
toml::get<std::tuple<std::vector<int>, std::vector<std::string>>>(v);
BOOST_CHECK_EQUAL(std::get<0>(t).at(0), 42);
BOOST_CHECK_EQUAL(std::get<0>(t).at(1), 54);
BOOST_CHECK_EQUAL(std::get<0>(t).at(2), 69);
BOOST_CHECK_EQUAL(std::get<0>(t).at(3), 72);
BOOST_CHECK_EQUAL(std::get<1>(t).at(0), "foo");
BOOST_CHECK_EQUAL(std::get<1>(t).at(1), "bar");
BOOST_CHECK_EQUAL(std::get<1>(t).at(2), "baz");
}
BOOST_AUTO_TEST_CASE(test_get_toml_table)
{
toml::value v1(toml::table{
{"key1", 1},
{"key2", 2},
{"key3", 3},
{"key4", 4}
});
const auto v = toml::get<std::map<std::string, int>>(v1);
BOOST_CHECK_EQUAL(v.at("key1"), 1);
BOOST_CHECK_EQUAL(v.at("key2"), 2);
BOOST_CHECK_EQUAL(v.at("key3"), 3);
BOOST_CHECK_EQUAL(v.at("key4"), 4);
}
BOOST_AUTO_TEST_CASE(test_get_toml_local_date)
{
toml::value v1(toml::local_date{2018, toml::month_t::Apr, 1});
const auto date = std::chrono::system_clock::to_time_t(
toml::get<std::chrono::system_clock::time_point>(v1));
std::tm t;
t.tm_year = 2018 - 1900;
t.tm_mon = 4 - 1;
t.tm_mday = 1;
t.tm_hour = 0;
t.tm_min = 0;
t.tm_sec = 0;
t.tm_isdst = -1;
const auto c = std::mktime(&t);
BOOST_CHECK_EQUAL(c, date);
}
BOOST_AUTO_TEST_CASE(test_get_toml_local_time)
{
toml::value v1(toml::local_time{12, 30, 45});
const auto time = toml::get<std::chrono::seconds>(v1);
BOOST_CHECK(time == std::chrono::hours(12) +
std::chrono::minutes(30) + std::chrono::seconds(45));
}
BOOST_AUTO_TEST_CASE(test_get_toml_local_datetime)
{
toml::value v1(toml::local_datetime(
toml::local_date{2018, toml::month_t::Apr, 1},
toml::local_time{12, 30, 45}));
const auto date = std::chrono::system_clock::to_time_t(
toml::get<std::chrono::system_clock::time_point>(v1));
std::tm t;
t.tm_year = 2018 - 1900;
t.tm_mon = 4 - 1;
t.tm_mday = 1;
t.tm_hour = 12;
t.tm_min = 30;
t.tm_sec = 45;
t.tm_isdst = -1;
const auto c = std::mktime(&t);
BOOST_CHECK_EQUAL(c, date);
}
BOOST_AUTO_TEST_CASE(test_get_toml_offset_datetime)
{
{
toml::value v1(toml::offset_datetime(
toml::local_date{2018, toml::month_t::Apr, 1},
toml::local_time{12, 30, 0},
toml::time_offset{9, 0}));
// 2018-04-01T12:30:00+09:00
// == 2018-04-01T03:30:00Z
const auto date = toml::get<std::chrono::system_clock::time_point>(v1);
const auto timet = std::chrono::system_clock::to_time_t(date);
// get time_t as gmtime (2018-04-01T03:30:00Z)
const auto tmp = std::gmtime(std::addressof(timet)); // XXX not threadsafe!
BOOST_CHECK(tmp);
const auto tm = *tmp;
BOOST_CHECK_EQUAL(tm.tm_year + 1900, 2018);
BOOST_CHECK_EQUAL(tm.tm_mon + 1, 4);
BOOST_CHECK_EQUAL(tm.tm_mday, 1);
BOOST_CHECK_EQUAL(tm.tm_hour, 3);
BOOST_CHECK_EQUAL(tm.tm_min, 30);
BOOST_CHECK_EQUAL(tm.tm_sec, 0);
}
{
toml::value v1(toml::offset_datetime(
toml::local_date{2018, toml::month_t::Apr, 1},
toml::local_time{12, 30, 0},
toml::time_offset{-8, 0}));
// 2018-04-01T12:30:00-08:00
// == 2018-04-01T20:30:00Z
const auto date = toml::get<std::chrono::system_clock::time_point>(v1);
const auto timet = std::chrono::system_clock::to_time_t(date);
// get time_t as gmtime (2018-04-01T03:30:00Z)
const auto tmp = std::gmtime(std::addressof(timet)); // XXX not threadsafe!
BOOST_CHECK(tmp);
const auto tm = *tmp;
BOOST_CHECK_EQUAL(tm.tm_year + 1900, 2018);
BOOST_CHECK_EQUAL(tm.tm_mon + 1, 4);
BOOST_CHECK_EQUAL(tm.tm_mday, 1);
BOOST_CHECK_EQUAL(tm.tm_hour, 20);
BOOST_CHECK_EQUAL(tm.tm_min, 30);
BOOST_CHECK_EQUAL(tm.tm_sec, 0);
}
}

View File

@@ -1,88 +0,0 @@
#define BOOST_TEST_MODULE "test_get_or"
#ifdef UNITTEST_FRAMEWORK_LIBRARY_EXIST
#include <boost/test/unit_test.hpp>
#else
#define BOOST_TEST_NO_LIB
#include <boost/test/included/unit_test.hpp>
#endif
#include <toml.hpp>
#include <map>
#include <unordered_map>
#include <list>
#include <deque>
#include <array>
BOOST_AUTO_TEST_CASE(test_get_or_exist)
{
toml::Boolean raw_v1(true);
toml::Integer raw_v2(42);
toml::Float raw_v3(3.14);
toml::String raw_v4("hoge");
toml::Array raw_v5{2,7,1,8,2};
toml::Table raw_v6{{"key", 42}};
toml::value v1(raw_v1);
toml::value v2(raw_v2);
toml::value v3(raw_v3);
toml::value v4(raw_v4);
toml::value v5(raw_v5);
toml::value v6(raw_v6);
toml::Table table{
{"value1", v1},
{"value2", v2},
{"value3", v3},
{"value4", v4},
{"value5", v5},
{"value6", v6}
};
toml::Boolean u1 = toml::get_or(table, "value1", raw_v1);
toml::Integer u2 = toml::get_or(table, "value2", raw_v2);
toml::Float u3 = toml::get_or(table, "value3", raw_v3);
toml::String u4 = toml::get_or(table, "value4", raw_v4);
toml::Array u5 = toml::get_or(table, "value5", raw_v5);
toml::Table u6 = toml::get_or(table, "value6", raw_v6);
BOOST_CHECK_EQUAL(u1, raw_v1);
BOOST_CHECK_EQUAL(u2, raw_v2);
BOOST_CHECK_EQUAL(u3, raw_v3);
BOOST_CHECK_EQUAL(u4, raw_v4);
BOOST_CHECK_EQUAL(u5.at(0).cast<toml::value_t::Integer>(), raw_v5.at(0).cast<toml::value_t::Integer>());
BOOST_CHECK_EQUAL(u5.at(1).cast<toml::value_t::Integer>(), raw_v5.at(1).cast<toml::value_t::Integer>());
BOOST_CHECK_EQUAL(u5.at(2).cast<toml::value_t::Integer>(), raw_v5.at(2).cast<toml::value_t::Integer>());
BOOST_CHECK_EQUAL(u5.at(3).cast<toml::value_t::Integer>(), raw_v5.at(3).cast<toml::value_t::Integer>());
BOOST_CHECK_EQUAL(u5.at(4).cast<toml::value_t::Integer>(), raw_v5.at(4).cast<toml::value_t::Integer>());
BOOST_CHECK_EQUAL(u6.at("key").cast<toml::value_t::Integer>(), 42);
}
BOOST_AUTO_TEST_CASE(test_get_or_empty)
{
toml::Boolean raw_v1(true);
toml::Integer raw_v2(42);
toml::Float raw_v3(3.14);
toml::String raw_v4("hoge");
toml::Array raw_v5{2,7,1,8,2};
toml::Table raw_v6{{"key", 42}};
toml::Table table; // empty!
toml::Boolean u1 = toml::get_or(table, std::string("value1"), raw_v1);
toml::Integer u2 = toml::get_or(table, std::string("value2"), raw_v2);
toml::Float u3 = toml::get_or(table, std::string("value3"), raw_v3);
toml::String u4 = toml::get_or(table, std::string("value4"), raw_v4);
toml::Array u5 = toml::get_or(table, std::string("value5"), raw_v5);
toml::Table u6 = toml::get_or(table, std::string("value6"), raw_v6);
BOOST_CHECK_EQUAL(u1, raw_v1);
BOOST_CHECK_EQUAL(u2, raw_v2);
BOOST_CHECK_EQUAL(u3, raw_v3);
BOOST_CHECK_EQUAL(u4, raw_v4);
BOOST_CHECK_EQUAL(u5.at(0).cast<toml::value_t::Integer>(), raw_v5.at(0).cast<toml::value_t::Integer>());
BOOST_CHECK_EQUAL(u5.at(1).cast<toml::value_t::Integer>(), raw_v5.at(1).cast<toml::value_t::Integer>());
BOOST_CHECK_EQUAL(u5.at(2).cast<toml::value_t::Integer>(), raw_v5.at(2).cast<toml::value_t::Integer>());
BOOST_CHECK_EQUAL(u5.at(3).cast<toml::value_t::Integer>(), raw_v5.at(3).cast<toml::value_t::Integer>());
BOOST_CHECK_EQUAL(u5.at(4).cast<toml::value_t::Integer>(), raw_v5.at(4).cast<toml::value_t::Integer>());
BOOST_CHECK_EQUAL(u6.at("key").cast<toml::value_t::Integer>(), 42);
}

View File

@@ -0,0 +1,76 @@
#define BOOST_TEST_MODULE "test_get_or"
#ifdef UNITTEST_FRAMEWORK_LIBRARY_EXIST
#include <boost/test/unit_test.hpp>
#else
#define BOOST_TEST_NO_LIB
#include <boost/test/included/unit_test.hpp>
#endif
#include <toml.hpp>
#include <map>
#include <unordered_map>
#include <list>
#include <deque>
#include <array>
BOOST_AUTO_TEST_CASE(test_find)
{
{
toml::value v(true);
bool thrown = false;
try
{
toml::find<toml::boolean>(v, "key");
}
catch(toml::type_error const& te)
{
thrown = true;
}
BOOST_CHECK(thrown);
}
{
toml::table v{{"num", 42}};
BOOST_CHECK_EQUAL(42, toml::find<int>(v, "num"));
toml::find<toml::integer>(v, "num") = 54;
BOOST_CHECK_EQUAL(54, toml::find<int>(v, "num"));
}
{
toml::value v = toml::table{{"num", 42}};
BOOST_CHECK_EQUAL(42, toml::find<int>(v, "num"));
toml::find<toml::integer>(v, "num") = 54;
BOOST_CHECK_EQUAL(54, toml::find<int>(v, "num"));
}
}
BOOST_AUTO_TEST_CASE(test_get_or)
{
{
toml::table v{{"num", 42}};
BOOST_CHECK_EQUAL(42, toml::get_or<int>(v, "num", 0));
BOOST_CHECK_EQUAL(0, toml::get_or<int>(v, "foo", 0));
}
{
toml::value v = toml::table{{"num", 42}};
BOOST_CHECK_EQUAL(42, toml::get_or<int>(v, "num", 0));
BOOST_CHECK_EQUAL(0, toml::get_or<int>(v, "foo", 0));
}
{
toml::value v1(42);
toml::value v2(3.14);
BOOST_CHECK_EQUAL(42, toml::get_or<int>(v1, 0));
BOOST_CHECK_EQUAL(0, toml::get_or<int>(v2, 0));
}
}
BOOST_AUTO_TEST_CASE(test_expect)
{
{
toml::value v1(42);
toml::value v2(3.14);
BOOST_CHECK_EQUAL(42, toml::expect<int>(v1).unwrap_or(0));
BOOST_CHECK_EQUAL( 0, toml::expect<int>(v2).unwrap_or(0));
BOOST_CHECK_EQUAL("42", toml::expect<int>(v1).map([](int i){return std::to_string(i);}).unwrap_or(std::string("none")));
BOOST_CHECK_EQUAL("none", toml::expect<int>(v2).map([](int i){return std::to_string(i);}).unwrap_or(std::string("none")));
}
}

35
tests/test_lex_aux.hpp Normal file
View File

@@ -0,0 +1,35 @@
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <toml/region.hpp>
#include <toml/result.hpp>
#define TOML11_TEST_LEX_ACCEPT(lxr, tkn, expct) \
do { \
const std::string token (tkn); \
const std::string expected(expct); \
toml::detail::location<std::string> loc("test", token); \
const auto result = lxr::invoke(loc); \
BOOST_CHECK(result.is_ok()); \
if(result.is_ok()){ \
const auto region = result.unwrap(); \
BOOST_CHECK_EQUAL(region.str(), expected); \
BOOST_CHECK_EQUAL(region.str().size(), expected.size()); \
BOOST_CHECK_EQUAL(static_cast<std::size_t>(std::distance( \
loc.begin(), loc.iter())), region.size()); \
} else { \
std::cerr << "lexer " << lxr::pattern() << " failed with input `"; \
std::cerr << token << "`. expected `" << expected << "`\n"; \
std::cerr << "reason: " << result.unwrap_err() << '\n'; \
} \
} while(false); \
/**/
#define TOML11_TEST_LEX_REJECT(lxr, tkn) \
do { \
const std::string token (tkn); \
toml::detail::location<std::string> loc("test", token); \
const auto result = lxr::invoke(loc); \
BOOST_CHECK(result.is_err()); \
BOOST_CHECK(loc.begin() == loc.iter()); \
} while(false); /**/

View File

@@ -0,0 +1,23 @@
#define BOOST_TEST_MODULE "test_lex_boolean"
#include <boost/test/unit_test.hpp>
#include <toml/lexer.hpp>
#include "test_lex_aux.hpp"
using namespace toml;
using namespace detail;
BOOST_AUTO_TEST_CASE(test_correct)
{
TOML11_TEST_LEX_ACCEPT(lex_boolean, "true", "true");
TOML11_TEST_LEX_ACCEPT(lex_boolean, "false", "false");
TOML11_TEST_LEX_ACCEPT(lex_boolean, "true # trailing", "true");
TOML11_TEST_LEX_ACCEPT(lex_boolean, "false # trailing", "false");
}
BOOST_AUTO_TEST_CASE(test_invalid)
{
TOML11_TEST_LEX_REJECT(lex_boolean, "TRUE");
TOML11_TEST_LEX_REJECT(lex_boolean, "FALSE");
TOML11_TEST_LEX_REJECT(lex_boolean, "True");
TOML11_TEST_LEX_REJECT(lex_boolean, "False");
}

View File

@@ -0,0 +1,57 @@
#define BOOST_TEST_MODULE "test_lex_datetime"
#include <boost/test/unit_test.hpp>
#include <toml/lexer.hpp>
#include "test_lex_aux.hpp"
using namespace toml;
using namespace detail;
BOOST_AUTO_TEST_CASE(test_offset_datetime)
{
TOML11_TEST_LEX_ACCEPT(lex_offset_date_time,
"1979-05-27T07:32:00Z",
"1979-05-27T07:32:00Z");
TOML11_TEST_LEX_ACCEPT(lex_offset_date_time,
"1979-05-27T07:32:00-07:00",
"1979-05-27T07:32:00-07:00");
TOML11_TEST_LEX_ACCEPT(lex_offset_date_time,
"1979-05-27T07:32:00.999999-07:00",
"1979-05-27T07:32:00.999999-07:00");
TOML11_TEST_LEX_ACCEPT(lex_offset_date_time,
"1979-05-27 07:32:00Z",
"1979-05-27 07:32:00Z");
TOML11_TEST_LEX_ACCEPT(lex_offset_date_time,
"1979-05-27 07:32:00-07:00",
"1979-05-27 07:32:00-07:00");
TOML11_TEST_LEX_ACCEPT(lex_offset_date_time,
"1979-05-27 07:32:00.999999-07:00",
"1979-05-27 07:32:00.999999-07:00");
}
BOOST_AUTO_TEST_CASE(test_local_datetime)
{
TOML11_TEST_LEX_ACCEPT(lex_local_date_time,
"1979-05-27T07:32:00",
"1979-05-27T07:32:00");
TOML11_TEST_LEX_ACCEPT(lex_local_date_time,
"1979-05-27T07:32:00.999999",
"1979-05-27T07:32:00.999999");
TOML11_TEST_LEX_ACCEPT(lex_local_date_time,
"1979-05-27 07:32:00",
"1979-05-27 07:32:00");
TOML11_TEST_LEX_ACCEPT(lex_local_date_time,
"1979-05-27 07:32:00.999999",
"1979-05-27 07:32:00.999999");
}
BOOST_AUTO_TEST_CASE(test_local_date)
{
TOML11_TEST_LEX_ACCEPT(lex_local_date, "1979-05-27", "1979-05-27");
}
BOOST_AUTO_TEST_CASE(test_local_time)
{
TOML11_TEST_LEX_ACCEPT(lex_local_time, "07:32:00", "07:32:00");
TOML11_TEST_LEX_ACCEPT(lex_local_time, "07:32:00.999999", "07:32:00.999999");
}

View File

@@ -0,0 +1,84 @@
#define BOOST_TEST_MODULE "test_lex_floating"
#include <boost/test/unit_test.hpp>
#include <toml/lexer.hpp>
#include <limits>
#include "test_lex_aux.hpp"
using namespace toml;
using namespace detail;
BOOST_AUTO_TEST_CASE(test_fractional_valid)
{
TOML11_TEST_LEX_ACCEPT(lex_float, "1.0", "1.0" );
TOML11_TEST_LEX_ACCEPT(lex_float, "0.1", "0.1" );
TOML11_TEST_LEX_ACCEPT(lex_float, "0.001", "0.001" );
TOML11_TEST_LEX_ACCEPT(lex_float, "0.100", "0.100" );
TOML11_TEST_LEX_ACCEPT(lex_float, "+3.14", "+3.14" );
TOML11_TEST_LEX_ACCEPT(lex_float, "-3.14", "-3.14" );
TOML11_TEST_LEX_ACCEPT(lex_float, "3.1415_9265_3589", "3.1415_9265_3589" );
TOML11_TEST_LEX_ACCEPT(lex_float, "+3.1415_9265_3589", "+3.1415_9265_3589");
TOML11_TEST_LEX_ACCEPT(lex_float, "-3.1415_9265_3589", "-3.1415_9265_3589");
TOML11_TEST_LEX_ACCEPT(lex_float, "123_456.789", "123_456.789" );
TOML11_TEST_LEX_ACCEPT(lex_float, "+123_456.789", "+123_456.789" );
TOML11_TEST_LEX_ACCEPT(lex_float, "-123_456.789", "-123_456.789" );
}
BOOST_AUTO_TEST_CASE(test_fractional_invalid)
{
TOML11_TEST_LEX_REJECT(lex_float, "0.");
TOML11_TEST_LEX_REJECT(lex_float, ".0");
TOML11_TEST_LEX_REJECT(lex_float, "01.0");
TOML11_TEST_LEX_REJECT(lex_float, "3,14");
TOML11_TEST_LEX_REJECT(lex_float, "+-1.0");
TOML11_TEST_LEX_REJECT(lex_float, "1._0");
}
BOOST_AUTO_TEST_CASE(test_exponential_valid)
{
TOML11_TEST_LEX_ACCEPT(lex_float, "1e10", "1e10");
TOML11_TEST_LEX_ACCEPT(lex_float, "1e+10", "1e+10");
TOML11_TEST_LEX_ACCEPT(lex_float, "1e-10", "1e-10");
TOML11_TEST_LEX_ACCEPT(lex_float, "+1e10", "+1e10");
TOML11_TEST_LEX_ACCEPT(lex_float, "+1e+10", "+1e+10");
TOML11_TEST_LEX_ACCEPT(lex_float, "+1e-10", "+1e-10");
TOML11_TEST_LEX_ACCEPT(lex_float, "-1e10", "-1e10");
TOML11_TEST_LEX_ACCEPT(lex_float, "-1e+10", "-1e+10");
TOML11_TEST_LEX_ACCEPT(lex_float, "-1e-10", "-1e-10");
TOML11_TEST_LEX_ACCEPT(lex_float, "123e-10", "123e-10");
TOML11_TEST_LEX_ACCEPT(lex_float, "1E10", "1E10");
TOML11_TEST_LEX_ACCEPT(lex_float, "1E+10", "1E+10");
TOML11_TEST_LEX_ACCEPT(lex_float, "1E-10", "1E-10");
TOML11_TEST_LEX_ACCEPT(lex_float, "123E-10", "123E-10");
TOML11_TEST_LEX_ACCEPT(lex_float, "1_2_3E-10", "1_2_3E-10");
TOML11_TEST_LEX_ACCEPT(lex_float, "1_2_3E-1_0", "1_2_3E-1_0");
}
BOOST_AUTO_TEST_CASE(test_exponential_invalid)
{
TOML11_TEST_LEX_ACCEPT(lex_float, "1e1E0", "1e1");
TOML11_TEST_LEX_ACCEPT(lex_float, "1E1e0", "1E1");
}
BOOST_AUTO_TEST_CASE(test_both_valid)
{
TOML11_TEST_LEX_ACCEPT(lex_float, "6.02e23", "6.02e23");
TOML11_TEST_LEX_ACCEPT(lex_float, "6.02e+23", "6.02e+23");
TOML11_TEST_LEX_ACCEPT(lex_float, "1.112_650_06e-17", "1.112_650_06e-17");
}
BOOST_AUTO_TEST_CASE(test_both_invalid)
{
TOML11_TEST_LEX_ACCEPT(lex_float, "1e1.0", "1e1");
TOML11_TEST_LEX_REJECT(lex_float, "01e1.0");
}
BOOST_AUTO_TEST_CASE(test_special_floating_point)
{
TOML11_TEST_LEX_ACCEPT(lex_float, "inf", "inf");
TOML11_TEST_LEX_ACCEPT(lex_float, "+inf", "+inf");
TOML11_TEST_LEX_ACCEPT(lex_float, "-inf", "-inf");
TOML11_TEST_LEX_ACCEPT(lex_float, "nan", "nan");
TOML11_TEST_LEX_ACCEPT(lex_float, "+nan", "+nan");
TOML11_TEST_LEX_ACCEPT(lex_float, "-nan", "-nan");
}

101
tests/test_lex_integer.cpp Normal file
View File

@@ -0,0 +1,101 @@
#define BOOST_TEST_MODULE "test_lex_integer"
#include <boost/test/unit_test.hpp>
#include <toml/lexer.hpp>
#include "test_lex_aux.hpp"
using namespace toml;
using namespace detail;
BOOST_AUTO_TEST_CASE(test_decimal_correct)
{
TOML11_TEST_LEX_ACCEPT(lex_integer, "1234", "1234" );
TOML11_TEST_LEX_ACCEPT(lex_integer, "+1234", "+1234" );
TOML11_TEST_LEX_ACCEPT(lex_integer, "-1234", "-1234" );
TOML11_TEST_LEX_ACCEPT(lex_integer, "0", "0" );
TOML11_TEST_LEX_ACCEPT(lex_integer, "1_2_3_4", "1_2_3_4" );
TOML11_TEST_LEX_ACCEPT(lex_integer, "+1_2_3_4", "+1_2_3_4" );
TOML11_TEST_LEX_ACCEPT(lex_integer, "-1_2_3_4", "-1_2_3_4" );
TOML11_TEST_LEX_ACCEPT(lex_integer, "123_456_789", "123_456_789");
}
BOOST_AUTO_TEST_CASE(test_decimal_invalid)
{
TOML11_TEST_LEX_ACCEPT(lex_integer, "123+45", "123");
TOML11_TEST_LEX_ACCEPT(lex_integer, "123-45", "123");
TOML11_TEST_LEX_ACCEPT(lex_integer, "01234", "0");
TOML11_TEST_LEX_ACCEPT(lex_integer, "123__45", "123");
TOML11_TEST_LEX_REJECT(lex_integer, "_1234");
}
BOOST_AUTO_TEST_CASE(test_hex_correct)
{
TOML11_TEST_LEX_ACCEPT(lex_integer, "0xDEADBEEF", "0xDEADBEEF" );
TOML11_TEST_LEX_ACCEPT(lex_integer, "0xdeadbeef", "0xdeadbeef" );
TOML11_TEST_LEX_ACCEPT(lex_integer, "0xDEADbeef", "0xDEADbeef" );
TOML11_TEST_LEX_ACCEPT(lex_integer, "0xDEAD_BEEF", "0xDEAD_BEEF");
TOML11_TEST_LEX_ACCEPT(lex_integer, "0xdead_beef", "0xdead_beef");
TOML11_TEST_LEX_ACCEPT(lex_integer, "0xdead_BEEF", "0xdead_BEEF");
TOML11_TEST_LEX_ACCEPT(lex_integer, "0xFF", "0xFF" );
TOML11_TEST_LEX_ACCEPT(lex_integer, "0x00FF", "0x00FF" );
TOML11_TEST_LEX_ACCEPT(lex_integer, "0x0000FF", "0x0000FF");
}
BOOST_AUTO_TEST_CASE(test_hex_invalid)
{
TOML11_TEST_LEX_ACCEPT(lex_integer, "0xAPPLE", "0xA");
TOML11_TEST_LEX_ACCEPT(lex_integer, "0xDEAD+BEEF", "0xDEAD");
TOML11_TEST_LEX_ACCEPT(lex_integer, "0xDEAD__BEEF", "0xDEAD");
TOML11_TEST_LEX_REJECT(lex_hex_int, "0x_DEADBEEF");
TOML11_TEST_LEX_REJECT(lex_hex_int, "0x+DEADBEEF");
TOML11_TEST_LEX_REJECT(lex_hex_int, "-0xFF" );
TOML11_TEST_LEX_REJECT(lex_hex_int, "-0x00FF" );
TOML11_TEST_LEX_ACCEPT(lex_integer, "0x_DEADBEEF", "0" );
TOML11_TEST_LEX_ACCEPT(lex_integer, "0x+DEADBEEF", "0" );
TOML11_TEST_LEX_ACCEPT(lex_integer, "-0xFF" , "-0" );
TOML11_TEST_LEX_ACCEPT(lex_integer, "-0x00FF" , "-0" );
}
BOOST_AUTO_TEST_CASE(test_oct_correct)
{
TOML11_TEST_LEX_ACCEPT(lex_integer, "0o777", "0o777" );
TOML11_TEST_LEX_ACCEPT(lex_integer, "0o7_7_7", "0o7_7_7");
TOML11_TEST_LEX_ACCEPT(lex_integer, "0o007", "0o007" );
}
BOOST_AUTO_TEST_CASE(test_oct_invalid)
{
TOML11_TEST_LEX_ACCEPT(lex_integer, "0o77+7", "0o77");
TOML11_TEST_LEX_ACCEPT(lex_integer, "0o1__0", "0o1");
TOML11_TEST_LEX_REJECT(lex_oct_int, "0o800" );
TOML11_TEST_LEX_REJECT(lex_oct_int, "-0o777");
TOML11_TEST_LEX_REJECT(lex_oct_int, "0o+777");
TOML11_TEST_LEX_REJECT(lex_oct_int, "0o_10" );
TOML11_TEST_LEX_ACCEPT(lex_integer, "0o800", "0");
TOML11_TEST_LEX_ACCEPT(lex_integer, "-0o777", "-0");
TOML11_TEST_LEX_ACCEPT(lex_integer, "0o+777", "0");
TOML11_TEST_LEX_ACCEPT(lex_integer, "0o_10", "0");
}
BOOST_AUTO_TEST_CASE(test_bin_correct)
{
TOML11_TEST_LEX_ACCEPT(lex_integer, "0b10000", "0b10000" );
TOML11_TEST_LEX_ACCEPT(lex_integer, "0b010000", "0b010000" );
TOML11_TEST_LEX_ACCEPT(lex_integer, "0b01_00_00", "0b01_00_00");
TOML11_TEST_LEX_ACCEPT(lex_integer, "0b111111", "0b111111" );
}
BOOST_AUTO_TEST_CASE(test_bin_invalid)
{
TOML11_TEST_LEX_ACCEPT(lex_bin_int, "0b11__11", "0b11");
TOML11_TEST_LEX_ACCEPT(lex_bin_int, "0b11+11" , "0b11");
TOML11_TEST_LEX_REJECT(lex_bin_int, "-0b10000");
TOML11_TEST_LEX_REJECT(lex_bin_int, "0b_1111" );
}

View File

@@ -0,0 +1,53 @@
#define BOOST_TEST_MODULE "lex_key_comment_test"
#include <boost/test/unit_test.hpp>
#include <toml/lexer.hpp>
#include "test_lex_aux.hpp"
using namespace toml;
using namespace detail;
BOOST_AUTO_TEST_CASE(test_bare_key)
{
TOML11_TEST_LEX_ACCEPT(lex_key, "barekey", "barekey");
TOML11_TEST_LEX_ACCEPT(lex_key, "bare-key", "bare-key");
TOML11_TEST_LEX_ACCEPT(lex_key, "bare_key", "bare_key");
TOML11_TEST_LEX_ACCEPT(lex_key, "1234", "1234");
}
BOOST_AUTO_TEST_CASE(test_quoted_key)
{
TOML11_TEST_LEX_ACCEPT(lex_key, "\"127.0.0.1\"", "\"127.0.0.1\"");
TOML11_TEST_LEX_ACCEPT(lex_key, "\"character encoding\"", "\"character encoding\"");
#if defined(_MSC_VER) || defined(__INTEL_COMPILER)
TOML11_TEST_LEX_ACCEPT(lex_key, "\"\xCA\x8E\xC7\x9D\xCA\x9E\"",
"\"\xCA\x8E\xC7\x9D\xCA\x9E\"");
#else
TOML11_TEST_LEX_ACCEPT(lex_key, u8"\"ʎǝʞ\"", u8"\"ʎǝʞ\"");
#endif
TOML11_TEST_LEX_ACCEPT(lex_key, "'key2'", "'key2'");
TOML11_TEST_LEX_ACCEPT(lex_key, "'quoted \"value\"'", "'quoted \"value\"'");
}
BOOST_AUTO_TEST_CASE(test_dotted_key)
{
TOML11_TEST_LEX_ACCEPT(lex_key, "physical.color", "physical.color");
TOML11_TEST_LEX_ACCEPT(lex_key, "physical.shape", "physical.shape");
TOML11_TEST_LEX_ACCEPT(lex_key, "x.y", "x.y");
TOML11_TEST_LEX_ACCEPT(lex_key, "x . y", "x . y");
TOML11_TEST_LEX_ACCEPT(lex_key, "x.y.z", "x.y.z");
TOML11_TEST_LEX_ACCEPT(lex_key, "x. y .z", "x. y .z");
TOML11_TEST_LEX_ACCEPT(lex_key, "x .y. z", "x .y. z");
TOML11_TEST_LEX_ACCEPT(lex_key, "x . y . z", "x . y . z");
TOML11_TEST_LEX_ACCEPT(lex_key, "x.y.z.w", "x.y.z.w");
TOML11_TEST_LEX_ACCEPT(lex_key, "x. y .z. w", "x. y .z. w");
TOML11_TEST_LEX_ACCEPT(lex_key, "x . y . z . w", "x . y . z . w");
TOML11_TEST_LEX_ACCEPT(lex_key, "site.\"google.com\"", "site.\"google.com\"");
}
BOOST_AUTO_TEST_CASE(test_comment)
{
TOML11_TEST_LEX_ACCEPT(lex_comment, "# hoge", "# hoge");
TOML11_TEST_LEX_ACCEPT(lex_comment, "# \n", "# ");
TOML11_TEST_LEX_ACCEPT(lex_comment, "# \r\n", "# ");
TOML11_TEST_LEX_ACCEPT(lex_comment, "# # \n", "# # ");
}

86
tests/test_lex_string.cpp Normal file
View File

@@ -0,0 +1,86 @@
#define BOOST_TEST_MODULE "test_lex_string"
#include <boost/test/unit_test.hpp>
#include <toml/lexer.hpp>
#include "test_lex_aux.hpp"
using namespace toml;
using namespace detail;
BOOST_AUTO_TEST_CASE(test_string)
{
TOML11_TEST_LEX_ACCEPT(lex_string,
"\"The quick brown fox jumps over the lazy dog\"",
"\"The quick brown fox jumps over the lazy dog\"");
TOML11_TEST_LEX_ACCEPT(lex_string,
"\'The quick brown fox jumps over the lazy dog\'",
"\'The quick brown fox jumps over the lazy dog\'");
TOML11_TEST_LEX_ACCEPT(lex_ml_basic_string,
"\"\"\"The quick brown fox \\\njumps over the lazy dog\"\"\"",
"\"\"\"The quick brown fox \\\njumps over the lazy dog\"\"\"");
TOML11_TEST_LEX_ACCEPT(lex_ml_literal_string,
"'''The quick brown fox \njumps over the lazy dog'''",
"'''The quick brown fox \njumps over the lazy dog'''");
}
BOOST_AUTO_TEST_CASE(test_basic_string)
{
TOML11_TEST_LEX_ACCEPT(lex_string,
"\"GitHub Cofounder & CEO\\nLikes tater tots and beer.\"",
"\"GitHub Cofounder & CEO\\nLikes tater tots and beer.\"");
TOML11_TEST_LEX_ACCEPT(lex_string,
"\"192.168.1.1\"",
"\"192.168.1.1\"");
#if defined(_MSC_VER) || defined(__INTEL_COMPILER)
TOML11_TEST_LEX_ACCEPT(lex_string,
"\"\xE4\xB8\xAD\xE5\x9B\xBD\"",
"\"\xE4\xB8\xAD\xE5\x9B\xBD\"");
#else
TOML11_TEST_LEX_ACCEPT(lex_string,
u8"\"中国\"",
u8"\"中国\"");
#endif
TOML11_TEST_LEX_ACCEPT(lex_string,
"\"You'll hate me after this - #\"",
"\"You'll hate me after this - #\"");
TOML11_TEST_LEX_ACCEPT(lex_string,
"\" And when \\\"'s are in the string, along with # \\\"\"",
"\" And when \\\"'s are in the string, along with # \\\"\"");
}
BOOST_AUTO_TEST_CASE(test_ml_basic_string)
{
TOML11_TEST_LEX_ACCEPT(lex_string,
"\"\"\"\nThe quick brown \\\n\n fox jumps over \\\n the lazy dog.\"\"\"",
"\"\"\"\nThe quick brown \\\n\n fox jumps over \\\n the lazy dog.\"\"\"");
TOML11_TEST_LEX_ACCEPT(lex_string,
"\"\"\"\\\n The quick brown \\\n\n fox jumps over \\\n the lazy dog.\\\n \"\"\"",
"\"\"\"\\\n The quick brown \\\n\n fox jumps over \\\n the lazy dog.\\\n \"\"\"");
}
BOOST_AUTO_TEST_CASE(test_literal_string)
{
TOML11_TEST_LEX_ACCEPT(lex_string,
"'C:\\Users\\nodejs\\templates'",
"'C:\\Users\\nodejs\\templates'");
TOML11_TEST_LEX_ACCEPT(lex_string,
"'\\\\ServerX\\admin$\\system32\\'",
"'\\\\ServerX\\admin$\\system32\\'");
TOML11_TEST_LEX_ACCEPT(lex_string,
"'Tom \"Dubs\" Preston-Werner'",
"'Tom \"Dubs\" Preston-Werner'");
TOML11_TEST_LEX_ACCEPT(lex_string,
"'<\\i\\c*\\s*>'",
"'<\\i\\c*\\s*>'");
}
BOOST_AUTO_TEST_CASE(test_ml_literal_string)
{
TOML11_TEST_LEX_ACCEPT(lex_string,
"'''I [dw]on't need \\d{2} apples'''",
"'''I [dw]on't need \\d{2} apples'''");
TOML11_TEST_LEX_ACCEPT(lex_string,
"'''\nThe first newline is\ntrimmed in raw strings.\n All other whitespace\n is preserved.\n'''",
"'''\nThe first newline is\ntrimmed in raw strings.\n All other whitespace\n is preserved.\n'''");
}

130
tests/test_parse_array.cpp Normal file
View File

@@ -0,0 +1,130 @@
#define BOOST_TEST_MODULE "parse_array_test"
#ifdef UNITTEST_FRAMEWORK_LIBRARY_EXIST
#include <boost/test/unit_test.hpp>
#else
#define BOOST_TEST_NO_LIB
#include <boost/test/included/unit_test.hpp>
#endif
#include <toml/parser.hpp>
#include "test_parse_aux.hpp"
using namespace toml;
using namespace detail;
BOOST_AUTO_TEST_CASE(test_oneline_array)
{
TOML11_TEST_PARSE_EQUAL(parse_array, "[]", array());
{
array a(5);
a[0] = toml::value(3); a[1] = toml::value(1); a[2] = toml::value(4);
a[3] = toml::value(1); a[4] = toml::value(5);
TOML11_TEST_PARSE_EQUAL(parse_array, "[3,1,4,1,5]", a);
}
{
array a(3);
a[0] = toml::value("foo"); a[1] = toml::value("bar");
a[2] = toml::value("baz");
TOML11_TEST_PARSE_EQUAL(parse_array, "[\"foo\", \"bar\", \"baz\"]", a);
}
{
array a(5);
a[0] = toml::value(3); a[1] = toml::value(1); a[2] = toml::value(4);
a[3] = toml::value(1); a[4] = toml::value(5);
TOML11_TEST_PARSE_EQUAL(parse_array, "[3,1,4,1,5,]", a);
}
{
array a(3);
a[0] = toml::value("foo"); a[1] = toml::value("bar");
a[2] = toml::value("baz");
TOML11_TEST_PARSE_EQUAL(parse_array, "[\"foo\", \"bar\", \"baz\",]", a);
}
}
BOOST_AUTO_TEST_CASE(test_oneline_array_value)
{
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value, "[]", toml::value(array()));
{
array a(5);
a[0] = toml::value(3); a[1] = toml::value(1); a[2] = toml::value(4);
a[3] = toml::value(1); a[4] = toml::value(5);
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value, "[3,1,4,1,5]", toml::value(a));
}
{
array a(3);
a[0] = toml::value("foo"); a[1] = toml::value("bar");
a[2] = toml::value("baz");
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value, "[\"foo\", \"bar\", \"baz\"]", toml::value(a));
}
{
array a(5);
a[0] = toml::value(3); a[1] = toml::value(1); a[2] = toml::value(4);
a[3] = toml::value(1); a[4] = toml::value(5);
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value, "[3,1,4,1,5,]", toml::value(a));
}
{
array a(3);
a[0] = toml::value("foo"); a[1] = toml::value("bar");
a[2] = toml::value("baz");
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value, "[\"foo\", \"bar\", \"baz\",]", toml::value(a));
}
}
BOOST_AUTO_TEST_CASE(test_multiline_array)
{
TOML11_TEST_PARSE_EQUAL(parse_array, "[\n#comment\n]", array());
{
array a(5);
a[0] = toml::value(3); a[1] = toml::value(1); a[2] = toml::value(4);
a[3] = toml::value(1); a[4] = toml::value(5);
TOML11_TEST_PARSE_EQUAL(parse_array, "[3,\n1,\n4,\n1,\n5]", a);
}
{
array a(3);
a[0] = toml::value("foo"); a[1] = toml::value("bar");
a[2] = toml::value("baz");
TOML11_TEST_PARSE_EQUAL(parse_array, "[\"foo\",\n\"bar\",\n\"baz\"]", a);
}
{
array a(5);
a[0] = toml::value(3); a[1] = toml::value(1); a[2] = toml::value(4);
a[3] = toml::value(1); a[4] = toml::value(5);
TOML11_TEST_PARSE_EQUAL(parse_array, "[3,#comment\n1,#comment\n4,#comment\n1,#comment\n5]", a);
}
{
array a(3);
a[0] = toml::value("foo"); a[1] = toml::value("b#r");
a[2] = toml::value("b#z");
TOML11_TEST_PARSE_EQUAL(parse_array, "[\"foo\",#comment\n\"b#r\",#comment\n\"b#z\"#comment\n]", a);
}
}
BOOST_AUTO_TEST_CASE(test_multiline_array_value)
{
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value, "[\n#comment\n]", toml::value(array()));
{
array a(5);
a[0] = toml::value(3); a[1] = toml::value(1); a[2] = toml::value(4);
a[3] = toml::value(1); a[4] = toml::value(5);
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value, "[3,\n1,\n4,\n1,\n5]", toml::value(a));
}
{
array a(3);
a[0] = toml::value("foo"); a[1] = toml::value("bar");
a[2] = toml::value("baz");
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value, "[\"foo\",\n\"bar\",\n\"baz\"]", toml::value(a));
}
{
array a(5);
a[0] = toml::value(3); a[1] = toml::value(1); a[2] = toml::value(4);
a[3] = toml::value(1); a[4] = toml::value(5);
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value, "[3,#comment\n1,#comment\n4,#comment\n1,#comment\n5]", toml::value(a));
}
{
array a(3);
a[0] = toml::value("foo"); a[1] = toml::value("b#r");
a[2] = toml::value("b#z");
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value, "[\"foo\",#comment\n\"b#r\",#comment\n\"b#z\"#comment\n]", toml::value(a));
}
}

38
tests/test_parse_aux.hpp Normal file
View File

@@ -0,0 +1,38 @@
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <toml/region.hpp>
#include <toml/result.hpp>
// some of the parsers returns not only a value but also a region.
#define TOML11_TEST_PARSE_EQUAL(psr, tkn, expct) \
do { \
const std::string token(tkn); \
toml::detail::location<std::string> loc("test", token); \
const auto result = psr(loc); \
BOOST_CHECK(result.is_ok()); \
if(result.is_ok()){ \
BOOST_CHECK(result.unwrap().first == expct); \
} else { \
std::cerr << "parser " << #psr << " failed with input `"; \
std::cerr << token << "`.\n"; \
std::cerr << "reason: " << result.unwrap_err() << '\n'; \
} \
} while(false); \
/**/
#define TOML11_TEST_PARSE_EQUAL_VALUE(psr, tkn, expct) \
do { \
const std::string token(tkn); \
toml::detail::location<std::string> loc("test", token); \
const auto result = psr(loc); \
BOOST_CHECK(result.is_ok()); \
if(result.is_ok()){ \
BOOST_CHECK(result.unwrap() == expct); \
} else { \
std::cerr << "parse_value failed with input `"; \
std::cerr << token << "`.\n"; \
std::cerr << "reason: " << result.unwrap_err() << '\n'; \
} \
} while(false); \
/**/

View File

@@ -0,0 +1,24 @@
#define BOOST_TEST_MODULE "test_parse_boolean"
#ifdef UNITTEST_FRAMEWORK_LIBRARY_EXIST
#include <boost/test/unit_test.hpp>
#else
#define BOOST_TEST_NO_LIB
#include <boost/test/included/unit_test.hpp>
#endif
#include <toml/parser.hpp>
#include "test_parse_aux.hpp"
using namespace toml;
using namespace detail;
BOOST_AUTO_TEST_CASE(test_boolean)
{
TOML11_TEST_PARSE_EQUAL(parse_boolean, "true", true);
TOML11_TEST_PARSE_EQUAL(parse_boolean, "false", false);
}
BOOST_AUTO_TEST_CASE(test_boolean_value)
{
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value, "true", toml::value( true));
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value, "false", toml::value(false));
}

View File

@@ -0,0 +1,133 @@
#define BOOST_TEST_MODULE "parse_datetime_test"
#ifdef UNITTEST_FRAMEWORK_LIBRARY_EXIST
#include <boost/test/unit_test.hpp>
#else
#define BOOST_TEST_NO_LIB
#include <boost/test/included/unit_test.hpp>
#endif
#include <toml/parser.hpp>
#include "test_parse_aux.hpp"
using namespace toml;
using namespace detail;
BOOST_AUTO_TEST_CASE(test_time)
{
TOML11_TEST_PARSE_EQUAL(parse_local_time, "07:32:00", toml::local_time(7, 32, 0));
TOML11_TEST_PARSE_EQUAL(parse_local_time, "07:32:00.99", toml::local_time(7, 32, 0, 990, 0));
TOML11_TEST_PARSE_EQUAL(parse_local_time, "07:32:00.999", toml::local_time(7, 32, 0, 999, 0));
TOML11_TEST_PARSE_EQUAL(parse_local_time, "07:32:00.999999", toml::local_time(7, 32, 0, 999, 999));
}
BOOST_AUTO_TEST_CASE(test_time_value)
{
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value, "07:32:00", toml::value(toml::local_time(7, 32, 0)));
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value, "07:32:00.99", toml::value(toml::local_time(7, 32, 0, 990, 0)));
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value, "07:32:00.999", toml::value(toml::local_time(7, 32, 0, 999, 0)));
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value, "07:32:00.999999", toml::value(toml::local_time(7, 32, 0, 999, 999)));
}
BOOST_AUTO_TEST_CASE(test_date)
{
TOML11_TEST_PARSE_EQUAL(parse_local_date, "1979-05-27",
toml::local_date(1979, toml::month_t::May, 27));
}
BOOST_AUTO_TEST_CASE(test_date_value)
{
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value, "1979-05-27",
value(toml::local_date(1979, toml::month_t::May, 27)));
}
BOOST_AUTO_TEST_CASE(test_datetime)
{
TOML11_TEST_PARSE_EQUAL(parse_local_datetime, "1979-05-27T07:32:00",
toml::local_datetime(toml::local_date(1979, toml::month_t::May, 27), toml::local_time(7, 32, 0)));
TOML11_TEST_PARSE_EQUAL(parse_local_datetime, "1979-05-27T07:32:00.99",
toml::local_datetime(toml::local_date(1979, toml::month_t::May, 27), toml::local_time(7, 32, 0, 990, 0)));
TOML11_TEST_PARSE_EQUAL(parse_local_datetime, "1979-05-27T07:32:00.999999",
toml::local_datetime(toml::local_date(1979, toml::month_t::May, 27), toml::local_time(7, 32, 0, 999, 999)));
TOML11_TEST_PARSE_EQUAL(parse_local_datetime, "1979-05-27t07:32:00",
toml::local_datetime(toml::local_date(1979, toml::month_t::May, 27), toml::local_time(7, 32, 0)));
TOML11_TEST_PARSE_EQUAL(parse_local_datetime, "1979-05-27t07:32:00.99",
toml::local_datetime(toml::local_date(1979, toml::month_t::May, 27), toml::local_time(7, 32, 0, 990, 0)));
TOML11_TEST_PARSE_EQUAL(parse_local_datetime, "1979-05-27t07:32:00.999999",
toml::local_datetime(toml::local_date(1979, toml::month_t::May, 27), toml::local_time(7, 32, 0, 999, 999)));
TOML11_TEST_PARSE_EQUAL(parse_local_datetime, "1979-05-27 07:32:00",
toml::local_datetime(toml::local_date(1979, toml::month_t::May, 27), toml::local_time(7, 32, 0)));
TOML11_TEST_PARSE_EQUAL(parse_local_datetime, "1979-05-27 07:32:00.99",
toml::local_datetime(toml::local_date(1979, toml::month_t::May, 27), toml::local_time(7, 32, 0, 990, 0)));
TOML11_TEST_PARSE_EQUAL(parse_local_datetime, "1979-05-27 07:32:00.999999",
toml::local_datetime(toml::local_date(1979, toml::month_t::May, 27), toml::local_time(7, 32, 0, 999, 999)));
}
BOOST_AUTO_TEST_CASE(test_datetime_value)
{
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value, "1979-05-27T07:32:00",
toml::value(toml::local_datetime(toml::local_date(1979, toml::month_t::May, 27), toml::local_time(7, 32, 0))));
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value, "1979-05-27T07:32:00.99",
toml::value(toml::local_datetime(toml::local_date(1979, toml::month_t::May, 27), toml::local_time(7, 32, 0, 990, 0))));
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value, "1979-05-27T07:32:00.999999",
toml::value(toml::local_datetime(toml::local_date(1979, toml::month_t::May, 27), toml::local_time(7, 32, 0, 999, 999))));
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value, "1979-05-27t07:32:00",
toml::value(toml::local_datetime(toml::local_date(1979, toml::month_t::May, 27), toml::local_time(7, 32, 0))));
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value, "1979-05-27t07:32:00.99",
toml::value(toml::local_datetime(toml::local_date(1979, toml::month_t::May, 27), toml::local_time(7, 32, 0, 990, 0))));
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value, "1979-05-27t07:32:00.999999",
toml::value(toml::local_datetime(toml::local_date(1979, toml::month_t::May, 27), toml::local_time(7, 32, 0, 999, 999))));
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value, "1979-05-27 07:32:00",
toml::value(toml::local_datetime(toml::local_date(1979, toml::month_t::May, 27), toml::local_time(7, 32, 0))));
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value, "1979-05-27 07:32:00.99",
toml::value(toml::local_datetime(toml::local_date(1979, toml::month_t::May, 27), toml::local_time(7, 32, 0, 990, 0))));
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value, "1979-05-27 07:32:00.999999",
toml::value(toml::local_datetime(toml::local_date(1979, toml::month_t::May, 27), toml::local_time(7, 32, 0, 999, 999))));
}
BOOST_AUTO_TEST_CASE(test_offset_datetime)
{
TOML11_TEST_PARSE_EQUAL(parse_offset_datetime, "1979-05-27T07:32:00Z",
toml::offset_datetime(toml::local_date(1979, toml::month_t::May, 27),
toml::local_time(7, 32, 0), toml::time_offset(0, 0)));
TOML11_TEST_PARSE_EQUAL(parse_offset_datetime, "1979-05-27T07:32:00.99Z",
toml::offset_datetime(toml::local_date(1979, toml::month_t::May, 27),
toml::local_time(7, 32, 0, 990, 0), toml::time_offset(0, 0)));
TOML11_TEST_PARSE_EQUAL(parse_offset_datetime, "1979-05-27T07:32:00.999999Z",
toml::offset_datetime(toml::local_date(1979, toml::month_t::May, 27),
toml::local_time(7, 32, 0, 999, 999), toml::time_offset(0, 0)));
TOML11_TEST_PARSE_EQUAL(parse_offset_datetime, "1979-05-27T07:32:00+09:00",
toml::offset_datetime(toml::local_date(1979, toml::month_t::May, 27),
toml::local_time(7, 32, 0), toml::time_offset(9, 0)));
TOML11_TEST_PARSE_EQUAL(parse_offset_datetime, "1979-05-27T07:32:00.99+09:00",
toml::offset_datetime(toml::local_date(1979, toml::month_t::May, 27),
toml::local_time(7, 32, 0, 990, 0), toml::time_offset(9, 0)));
TOML11_TEST_PARSE_EQUAL(parse_offset_datetime, "1979-05-27T07:32:00.999999+09:00",
toml::offset_datetime(toml::local_date(1979, toml::month_t::May, 27),
toml::local_time(7, 32, 0, 999, 999), toml::time_offset(9, 0)));
}
BOOST_AUTO_TEST_CASE(test_offset_datetime_value)
{
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value, "1979-05-27T07:32:00Z",
toml::value(toml::offset_datetime(toml::local_date(1979, toml::month_t::May, 27),
toml::local_time(7, 32, 0), toml::time_offset(0, 0))));
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value, "1979-05-27T07:32:00.99Z",
toml::value(toml::offset_datetime(toml::local_date(1979, toml::month_t::May, 27),
toml::local_time(7, 32, 0, 990, 0), toml::time_offset(0, 0))));
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value, "1979-05-27T07:32:00.999999Z",
toml::value(toml::offset_datetime(toml::local_date(1979, toml::month_t::May, 27),
toml::local_time(7, 32, 0, 999, 999), toml::time_offset(0, 0))));
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value, "1979-05-27T07:32:00+09:00",
toml::value(toml::offset_datetime(toml::local_date(1979, toml::month_t::May, 27),
toml::local_time(7, 32, 0), toml::time_offset(9, 0))));
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value, "1979-05-27T07:32:00.99+09:00",
toml::value(toml::offset_datetime(toml::local_date(1979, toml::month_t::May, 27),
toml::local_time(7, 32, 0, 990, 0), toml::time_offset(9, 0))));
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value, "1979-05-27T07:32:00.999999+09:00",
toml::value(toml::offset_datetime(toml::local_date(1979, toml::month_t::May, 27),
toml::local_time(7, 32, 0, 999, 999), toml::time_offset(9, 0))));
}

View File

@@ -22,7 +22,8 @@ BOOST_AUTO_TEST_CASE(test_example)
BOOST_CHECK_EQUAL(toml::get<std::string>(owner.at("bio")),
"GitHub Cofounder & CEO\nLikes tater tots and beer.");
BOOST_CHECK_EQUAL(toml::get<toml::Datetime>(owner.at("dob")),
toml::Datetime(1979, 5, 27, 7, 32, 0, 0, 0, 0, 0));
toml::offset_datetime(toml::local_date(1979, toml::month_t::May, 27),
toml::local_time(7, 32, 0), toml::time_offset(0, 0)));
}
toml::Table database = toml::get<toml::Table>(data.at("database"));
@@ -90,7 +91,8 @@ BOOST_AUTO_TEST_CASE(test_example_stream)
BOOST_CHECK_EQUAL(toml::get<std::string>(owner.at("bio")),
"GitHub Cofounder & CEO\nLikes tater tots and beer.");
BOOST_CHECK_EQUAL(toml::get<toml::Datetime>(owner.at("dob")),
toml::Datetime(1979, 5, 27, 7, 32, 0, 0, 0, 0, 0));
toml::offset_datetime(toml::local_date(1979, toml::month_t::May, 27),
toml::local_time(7, 32, 0), toml::time_offset(0, 0)));
}
toml::Table database = toml::get<toml::Table>(data.at("database"));

View File

@@ -0,0 +1,158 @@
#define BOOST_TEST_MODULE "parse_floating_test"
#ifdef UNITTEST_FRAMEWORK_LIBRARY_EXIST
#include <boost/test/unit_test.hpp>
#else
#define BOOST_TEST_NO_LIB
#include <boost/test/included/unit_test.hpp>
#endif
#include <toml/parser.hpp>
#include <cmath>
#include "test_parse_aux.hpp"
using namespace toml;
using namespace detail;
BOOST_AUTO_TEST_CASE(test_fractional)
{
TOML11_TEST_PARSE_EQUAL(parse_floating, "1.0", 1.0);
TOML11_TEST_PARSE_EQUAL(parse_floating, "0.1", 0.1);
TOML11_TEST_PARSE_EQUAL(parse_floating, "0.001", 0.001);
TOML11_TEST_PARSE_EQUAL(parse_floating, "0.100", 0.1);
TOML11_TEST_PARSE_EQUAL(parse_floating, "+3.14", 3.14);
TOML11_TEST_PARSE_EQUAL(parse_floating, "-3.14", -3.14);
TOML11_TEST_PARSE_EQUAL(parse_floating, "3.1415_9265_3589", 3.141592653589);
TOML11_TEST_PARSE_EQUAL(parse_floating, "+3.1415_9265_3589", 3.141592653589);
TOML11_TEST_PARSE_EQUAL(parse_floating, "-3.1415_9265_3589", -3.141592653589);
TOML11_TEST_PARSE_EQUAL(parse_floating, "123_456.789", 123456.789);
TOML11_TEST_PARSE_EQUAL(parse_floating, "+123_456.789", 123456.789);
TOML11_TEST_PARSE_EQUAL(parse_floating, "-123_456.789", -123456.789);
TOML11_TEST_PARSE_EQUAL(parse_floating, "+0.0", 0.0);
TOML11_TEST_PARSE_EQUAL(parse_floating, "-0.0", -0.0);
}
BOOST_AUTO_TEST_CASE(test_fractional_value)
{
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value, "1.0", value( 1.0));
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value, "0.1", value( 0.1));
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value, "0.001", value( 0.001));
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value, "0.100", value( 0.1));
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value, "+3.14", value( 3.14));
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value, "-3.14", value(-3.14));
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value, "3.1415_9265_3589", value( 3.141592653589));
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value, "+3.1415_9265_3589", value( 3.141592653589));
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value, "-3.1415_9265_3589", value(-3.141592653589));
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value, "123_456.789", value( 123456.789));
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value, "+123_456.789", value( 123456.789));
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value, "-123_456.789", value(-123456.789));
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value, "+0.0", value( 0.0));
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value, "-0.0", value(-0.0));
}
BOOST_AUTO_TEST_CASE(test_exponential)
{
TOML11_TEST_PARSE_EQUAL(parse_floating, "1e10", 1e10);
TOML11_TEST_PARSE_EQUAL(parse_floating, "1e+10", 1e10);
TOML11_TEST_PARSE_EQUAL(parse_floating, "1e-10", 1e-10);
TOML11_TEST_PARSE_EQUAL(parse_floating, "+1e10", 1e10);
TOML11_TEST_PARSE_EQUAL(parse_floating, "+1e+10", 1e10);
TOML11_TEST_PARSE_EQUAL(parse_floating, "+1e-10", 1e-10);
TOML11_TEST_PARSE_EQUAL(parse_floating, "-1e10", -1e10);
TOML11_TEST_PARSE_EQUAL(parse_floating, "-1e+10", -1e10);
TOML11_TEST_PARSE_EQUAL(parse_floating, "-1e-10", -1e-10);
TOML11_TEST_PARSE_EQUAL(parse_floating, "123e-10", 123e-10);
TOML11_TEST_PARSE_EQUAL(parse_floating, "1E10", 1e10);
TOML11_TEST_PARSE_EQUAL(parse_floating, "1E+10", 1e10);
TOML11_TEST_PARSE_EQUAL(parse_floating, "1E-10", 1e-10);
TOML11_TEST_PARSE_EQUAL(parse_floating, "123E-10", 123e-10);
TOML11_TEST_PARSE_EQUAL(parse_floating, "1_2_3E-10", 123e-10);
TOML11_TEST_PARSE_EQUAL(parse_floating, "1_2_3E-1_0", 123e-10);
TOML11_TEST_PARSE_EQUAL(parse_floating, "+0e0", 0.0);
TOML11_TEST_PARSE_EQUAL(parse_floating, "-0e0", -0.0);
}
BOOST_AUTO_TEST_CASE(test_exponential_value)
{
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value, "1e10", value(1e10));
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value, "1e+10", value(1e10));
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value, "1e-10", value(1e-10));
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value, "+1e10", value(1e10));
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value, "+1e+10", value(1e10));
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value, "+1e-10", value(1e-10));
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value, "-1e10", value(-1e10));
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value, "-1e+10", value(-1e10));
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value, "-1e-10", value(-1e-10));
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value, "123e-10", value(123e-10));
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value, "1E10", value(1e10));
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value, "1E+10", value(1e10));
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value, "1E-10", value(1e-10));
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value, "123E-10", value(123e-10));
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value, "1_2_3E-10", value(123e-10));
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value, "1_2_3E-1_0", value(123e-10));
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value, "+0e0", value( 0.0));
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value, "-0e0", value(-0.0));
}
BOOST_AUTO_TEST_CASE(test_fe)
{
TOML11_TEST_PARSE_EQUAL(parse_floating, "6.02e23", 6.02e23);
TOML11_TEST_PARSE_EQUAL(parse_floating, "6.02e+23", 6.02e23);
TOML11_TEST_PARSE_EQUAL(parse_floating, "1.112_650_06e-17", 1.11265006e-17);
}
BOOST_AUTO_TEST_CASE(test_fe_vaule)
{
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value, "6.02e23", value(6.02e23));
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value, "6.02e+23", value(6.02e23));
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value, "1.112_650_06e-17", value(1.11265006e-17));
}
BOOST_AUTO_TEST_CASE(test_inf)
{
{
const std::string token("inf");
toml::detail::location<std::string> loc("test", token);
const auto r = parse_floating(loc);
BOOST_CHECK(r.is_ok());
BOOST_CHECK(std::isinf(r.unwrap().first));
BOOST_CHECK(r.unwrap().first > 0.0);
}
{
const std::string token("+inf");
toml::detail::location<std::string> loc("test", token);
const auto r = parse_floating(loc);
BOOST_CHECK(r.is_ok());
BOOST_CHECK(std::isinf(r.unwrap().first));
BOOST_CHECK(r.unwrap().first > 0.0);
}
{
const std::string token("-inf");
toml::detail::location<std::string> loc("test", token);
const auto r = parse_floating(loc);
BOOST_CHECK(r.is_ok());
BOOST_CHECK(std::isinf(r.unwrap().first));
BOOST_CHECK(r.unwrap().first < 0.0);
}
}
BOOST_AUTO_TEST_CASE(test_nan)
{
{
const std::string token("nan");
toml::detail::location<std::string> loc("test", token);
const auto r = parse_floating(loc);
BOOST_CHECK(r.is_ok());
BOOST_CHECK(std::isnan(r.unwrap().first));
}
{
const std::string token("+nan");
toml::detail::location<std::string> loc("test", token);
const auto r = parse_floating(loc);
BOOST_CHECK(r.is_ok());
BOOST_CHECK(std::isnan(r.unwrap().first));
}
{
const std::string token("-nan");
toml::detail::location<std::string> loc("test", token);
const auto r = parse_floating(loc);
BOOST_CHECK(r.is_ok());
BOOST_CHECK(std::isnan(r.unwrap().first));
}
}

View File

@@ -0,0 +1,48 @@
#define BOOST_TEST_MODULE "parse_inline_table_test"
#ifdef UNITTEST_FRAMEWORK_LIBRARY_EXIST
#include <boost/test/unit_test.hpp>
#else
#define BOOST_TEST_NO_LIB
#include <boost/test/included/unit_test.hpp>
#endif
#include <toml/parser.hpp>
#include "test_parse_aux.hpp"
using namespace toml;
using namespace detail;
BOOST_AUTO_TEST_CASE(test_inline_table)
{
TOML11_TEST_PARSE_EQUAL(parse_inline_table, "{}", table());
{
table t;
t["foo"] = toml::value(42);
t["bar"] = toml::value("baz");
TOML11_TEST_PARSE_EQUAL(parse_inline_table, "{foo = 42, bar = \"baz\"}", t);
}
{
table t;
table t_sub;
t_sub["name"] = toml::value("pug");
t["type"] = toml::value(t_sub);
TOML11_TEST_PARSE_EQUAL(parse_inline_table, "{type.name = \"pug\"}", t);
}
}
BOOST_AUTO_TEST_CASE(test_inline_table_value)
{
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value, "{}", value(table()));
{
table t;
t["foo"] = toml::value(42);
t["bar"] = toml::value("baz");
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value, "{foo = 42, bar = \"baz\"}", value(t));
}
{
table t;
table t_sub;
t_sub["name"] = toml::value("pug");
t["type"] = toml::value(t_sub);
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value, "{type.name = \"pug\"}", value(t));
}
}

View File

@@ -0,0 +1,92 @@
#define BOOST_TEST_MODULE "parse_integer_test"
#ifdef UNITTEST_FRAMEWORK_LIBRARY_EXIST
#include <boost/test/unit_test.hpp>
#else
#define BOOST_TEST_NO_LIB
#include <boost/test/included/unit_test.hpp>
#endif
#include <toml/parser.hpp>
#include "test_parse_aux.hpp"
using namespace toml;
using namespace detail;
BOOST_AUTO_TEST_CASE(test_decimal)
{
TOML11_TEST_PARSE_EQUAL(parse_integer, "1234", 1234);
TOML11_TEST_PARSE_EQUAL(parse_integer, "+1234", 1234);
TOML11_TEST_PARSE_EQUAL(parse_integer, "-1234", -1234);
TOML11_TEST_PARSE_EQUAL(parse_integer, "0", 0);
TOML11_TEST_PARSE_EQUAL(parse_integer, "1_2_3_4", 1234);
TOML11_TEST_PARSE_EQUAL(parse_integer, "+1_2_3_4", +1234);
TOML11_TEST_PARSE_EQUAL(parse_integer, "-1_2_3_4", -1234);
TOML11_TEST_PARSE_EQUAL(parse_integer, "123_456_789", 123456789);
}
BOOST_AUTO_TEST_CASE(test_decimal_value)
{
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value, "1234", toml::value( 1234));
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value, "+1234", toml::value( 1234));
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value, "-1234", toml::value( -1234));
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value, "0", toml::value( 0));
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value, "1_2_3_4", toml::value( 1234));
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value, "+1_2_3_4", toml::value( +1234));
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value, "-1_2_3_4", toml::value( -1234));
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value, "123_456_789", toml::value(123456789));
}
BOOST_AUTO_TEST_CASE(test_hex)
{
TOML11_TEST_PARSE_EQUAL(parse_integer, "0xDEADBEEF", 0xDEADBEEF);
TOML11_TEST_PARSE_EQUAL(parse_integer, "0xdeadbeef", 0xDEADBEEF);
TOML11_TEST_PARSE_EQUAL(parse_integer, "0xDEADbeef", 0xDEADBEEF);
TOML11_TEST_PARSE_EQUAL(parse_integer, "0xDEAD_BEEF", 0xDEADBEEF);
TOML11_TEST_PARSE_EQUAL(parse_integer, "0xdead_beef", 0xDEADBEEF);
TOML11_TEST_PARSE_EQUAL(parse_integer, "0xdead_BEEF", 0xDEADBEEF);
TOML11_TEST_PARSE_EQUAL(parse_integer, "0xFF", 0xFF);
TOML11_TEST_PARSE_EQUAL(parse_integer, "0x00FF", 0xFF);
TOML11_TEST_PARSE_EQUAL(parse_integer, "0x0000FF", 0xFF);
}
BOOST_AUTO_TEST_CASE(test_hex_value)
{
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value, "0xDEADBEEF", value(0xDEADBEEF));
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value, "0xdeadbeef", value(0xDEADBEEF));
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value, "0xDEADbeef", value(0xDEADBEEF));
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value, "0xDEAD_BEEF", value(0xDEADBEEF));
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value, "0xdead_beef", value(0xDEADBEEF));
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value, "0xdead_BEEF", value(0xDEADBEEF));
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value, "0xFF", value(0xFF));
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value, "0x00FF", value(0xFF));
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value, "0x0000FF", value(0xFF));
}
BOOST_AUTO_TEST_CASE(test_oct)
{
TOML11_TEST_PARSE_EQUAL(parse_integer, "0o777", 64*7+8*7+7);
TOML11_TEST_PARSE_EQUAL(parse_integer, "0o7_7_7", 64*7+8*7+7);
TOML11_TEST_PARSE_EQUAL(parse_integer, "0o007", 7);
}
BOOST_AUTO_TEST_CASE(test_oct_value)
{
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value, "0o777", value(64*7+8*7+7));
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value, "0o7_7_7", value(64*7+8*7+7));
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value, "0o007", value(7));
}
BOOST_AUTO_TEST_CASE(test_bin)
{
TOML11_TEST_PARSE_EQUAL(parse_integer, "0b10000", 16);
TOML11_TEST_PARSE_EQUAL(parse_integer, "0b010000", 16);
TOML11_TEST_PARSE_EQUAL(parse_integer, "0b01_00_00", 16);
TOML11_TEST_PARSE_EQUAL(parse_integer, "0b111111", 63);
}
BOOST_AUTO_TEST_CASE(test_bin_value)
{
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value, "0b10000", value(16));
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value, "0b010000", value(16));
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value, "0b01_00_00", value(16));
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value, "0b111111", value(63));
}

63
tests/test_parse_key.cpp Normal file
View File

@@ -0,0 +1,63 @@
#define BOOST_TEST_MODULE "parse_key_test"
#ifdef UNITTEST_FRAMEWORK_LIBRARY_EXIST
#include <boost/test/unit_test.hpp>
#else
#define BOOST_TEST_NO_LIB
#include <boost/test/included/unit_test.hpp>
#endif
#include <toml/parser.hpp>
#include "test_parse_aux.hpp"
using namespace toml;
using namespace detail;
BOOST_AUTO_TEST_CASE(test_bare_key)
{
TOML11_TEST_PARSE_EQUAL_VALUE(parse_key, "barekey", std::vector<key>(1, "barekey"));
TOML11_TEST_PARSE_EQUAL_VALUE(parse_key, "bare-key", std::vector<key>(1, "bare-key"));
TOML11_TEST_PARSE_EQUAL_VALUE(parse_key, "bare_key", std::vector<key>(1, "bare_key"));
TOML11_TEST_PARSE_EQUAL_VALUE(parse_key, "1234", std::vector<key>(1, "1234"));
}
BOOST_AUTO_TEST_CASE(test_quoted_key)
{
TOML11_TEST_PARSE_EQUAL_VALUE(parse_key, "\"127.0.0.1\"", std::vector<key>(1, "127.0.0.1" ));
TOML11_TEST_PARSE_EQUAL_VALUE(parse_key, "\"character encoding\"", std::vector<key>(1, "character encoding"));
#if defined(_MSC_VER) || defined(__INTEL_COMPILER)
TOML11_TEST_PARSE_EQUAL_VALUE(parse_key, "\"\xCA\x8E\xC7\x9D\xCA\x9E\"", std::vector<key>(1, "\xCA\x8E\xC7\x9D\xCA\x9E"));
#else
TOML11_TEST_PARSE_EQUAL_VALUE(parse_key, "\"ʎǝʞ\"", std::vector<key>(1, "ʎǝʞ" ));
#endif
TOML11_TEST_PARSE_EQUAL_VALUE(parse_key, "'key2'", std::vector<key>(1, "key2" ));
TOML11_TEST_PARSE_EQUAL_VALUE(parse_key, "'quoted \"value\"'", std::vector<key>(1, "quoted \"value\"" ));
}
BOOST_AUTO_TEST_CASE(test_dotted_key)
{
{
std::vector<key> keys(2);
keys[0] = "physical";
keys[1] = "color";
TOML11_TEST_PARSE_EQUAL_VALUE(parse_key, "physical.color", keys);
}
{
std::vector<key> keys(2);
keys[0] = "physical";
keys[1] = "shape";
TOML11_TEST_PARSE_EQUAL_VALUE(parse_key, "physical.shape", keys);
}
{
std::vector<key> keys(4);
keys[0] = "x";
keys[1] = "y";
keys[2] = "z";
keys[3] = "w";
TOML11_TEST_PARSE_EQUAL_VALUE(parse_key, "x.y.z.w", keys);
}
{
std::vector<key> keys(2);
keys[0] = "site";
keys[1] = "google.com";
TOML11_TEST_PARSE_EQUAL_VALUE(parse_key, "site.\"google.com\"", keys);
}
}

188
tests/test_parse_string.cpp Normal file
View File

@@ -0,0 +1,188 @@
#define BOOST_TEST_MODULE "parse_string_test"
#ifdef UNITTEST_FRAMEWORK_LIBRARY_EXIST
#include <boost/test/unit_test.hpp>
#else
#define BOOST_TEST_NO_LIB
#include <boost/test/included/unit_test.hpp>
#endif
#include <toml/parser.hpp>
#include "test_parse_aux.hpp"
using namespace toml;
using namespace detail;
BOOST_AUTO_TEST_CASE(test_string)
{
TOML11_TEST_PARSE_EQUAL(parse_string,
"\"The quick brown fox jumps over the lazy dog\"",
string("The quick brown fox jumps over the lazy dog", string_t::basic));
TOML11_TEST_PARSE_EQUAL(parse_string,
"\'The quick brown fox jumps over the lazy dog\'",
string("The quick brown fox jumps over the lazy dog", string_t::literal));
TOML11_TEST_PARSE_EQUAL(parse_string,
"\"\"\"The quick brown fox \\\njumps over the lazy dog\"\"\"",
string("The quick brown fox jumps over the lazy dog", string_t::basic));
TOML11_TEST_PARSE_EQUAL(parse_string,
"'''The quick brown fox \njumps over the lazy dog'''",
string("The quick brown fox \njumps over the lazy dog", string_t::literal));
}
BOOST_AUTO_TEST_CASE(test_string_value)
{
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value,
"\"The quick brown fox jumps over the lazy dog\"",
toml::value("The quick brown fox jumps over the lazy dog", string_t::basic));
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value,
"\'The quick brown fox jumps over the lazy dog\'",
toml::value("The quick brown fox jumps over the lazy dog", string_t::literal));
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value,
"\"\"\"The quick brown fox \\\njumps over the lazy dog\"\"\"",
toml::value("The quick brown fox jumps over the lazy dog", string_t::basic));
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value,
"'''The quick brown fox \njumps over the lazy dog'''",
toml::value("The quick brown fox \njumps over the lazy dog", string_t::literal));
}
BOOST_AUTO_TEST_CASE(test_basic_string)
{
TOML11_TEST_PARSE_EQUAL(parse_string,
"\"GitHub Cofounder & CEO\\nLikes tater tots and beer.\"",
string("GitHub Cofounder & CEO\nLikes tater tots and beer.", string_t::basic));
TOML11_TEST_PARSE_EQUAL(parse_string,
"\"192.168.1.1\"",
string("192.168.1.1", string_t::basic));
#if defined(_MSC_VER) || defined(__INTEL_COMPILER)
TOML11_TEST_PARSE_EQUAL(parse_string,
"\"\xE4\xB8\xAD\xE5\x9B\xBD\"",
string("\xE4\xB8\xAD\xE5\x9B\xBD", string_t::basic));
#else
TOML11_TEST_PARSE_EQUAL(parse_string,
"\"中国\"",
string("中国", string_t::basic));
#endif
TOML11_TEST_PARSE_EQUAL(parse_string,
"\"You'll hate me after this - #\"",
string("You'll hate me after this - #", string_t::basic));
TOML11_TEST_PARSE_EQUAL(parse_string,
"\" And when \\\"'s are in the along with # \\\"\"",
string(" And when \"'s are in the along with # \"", string_t::basic));
}
BOOST_AUTO_TEST_CASE(test_basic_string_value)
{
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value,
"\"GitHub Cofounder & CEO\\nLikes tater tots and beer.\"",
value("GitHub Cofounder & CEO\nLikes tater tots and beer.", string_t::basic));
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value,
"\"192.168.1.1\"",
value("192.168.1.1", string_t::basic));
#if defined(_MSC_VER) || defined(__INTEL_COMPILER)
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value,
"\"\xE4\xB8\xAD\xE5\x9B\xBD\"",
value("\xE4\xB8\xAD\xE5\x9B\xBD", string_t::basic));
#else
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value,
"\"中国\"",
value("中国", string_t::basic));
#endif
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value,
"\"You'll hate me after this - #\"",
value("You'll hate me after this - #", string_t::basic));
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value,
"\" And when \\\"'s are in the along with # \\\"\"",
value(" And when \"'s are in the along with # \"", string_t::basic));
}
BOOST_AUTO_TEST_CASE(test_ml_basic_string)
{
TOML11_TEST_PARSE_EQUAL(parse_string,
"\"\"\"\nThe quick brown \\\n\n fox jumps over \\\n the lazy dog.\"\"\"",
string("The quick brown fox jumps over the lazy dog.", string_t::basic));
TOML11_TEST_PARSE_EQUAL(parse_string,
"\"\"\"\\\n The quick brown \\\n\n fox jumps over \\\n the lazy dog.\\\n \"\"\"",
string("The quick brown fox jumps over the lazy dog.", string_t::basic));
}
BOOST_AUTO_TEST_CASE(test_ml_basic_string_value)
{
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value,
"\"\"\"\nThe quick brown \\\n\n fox jumps over \\\n the lazy dog.\"\"\"",
value("The quick brown fox jumps over the lazy dog.", string_t::basic));
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value,
"\"\"\"\\\n The quick brown \\\n\n fox jumps over \\\n the lazy dog.\\\n \"\"\"",
value("The quick brown fox jumps over the lazy dog.", string_t::basic));
}
BOOST_AUTO_TEST_CASE(test_literal_string)
{
TOML11_TEST_PARSE_EQUAL(parse_string,
"'C:\\Users\\nodejs\\templates'",
string("C:\\Users\\nodejs\\templates", string_t::literal));
TOML11_TEST_PARSE_EQUAL(parse_string,
"'\\\\ServerX\\admin$\\system32\\'",
string("\\\\ServerX\\admin$\\system32\\", string_t::literal));
TOML11_TEST_PARSE_EQUAL(parse_string,
"'Tom \"Dubs\" Preston-Werner'",
string("Tom \"Dubs\" Preston-Werner", string_t::literal));
TOML11_TEST_PARSE_EQUAL(parse_string,
"'<\\i\\c*\\s*>'",
string("<\\i\\c*\\s*>", string_t::literal));
}
BOOST_AUTO_TEST_CASE(test_literal_string_value)
{
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value,
"'C:\\Users\\nodejs\\templates'",
value("C:\\Users\\nodejs\\templates", string_t::literal));
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value,
"'\\\\ServerX\\admin$\\system32\\'",
value("\\\\ServerX\\admin$\\system32\\", string_t::literal));
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value,
"'Tom \"Dubs\" Preston-Werner'",
value("Tom \"Dubs\" Preston-Werner", string_t::literal));
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value,
"'<\\i\\c*\\s*>'",
value("<\\i\\c*\\s*>", string_t::literal));
}
BOOST_AUTO_TEST_CASE(test_ml_literal_string)
{
TOML11_TEST_PARSE_EQUAL(parse_string,
"'''I [dw]on't need \\d{2} apples'''",
string("I [dw]on't need \\d{2} apples", string_t::literal));
TOML11_TEST_PARSE_EQUAL(parse_string,
"'''\nThe first newline is\ntrimmed in raw strings.\n All other whitespace\n is preserved.\n'''",
string("The first newline is\ntrimmed in raw strings.\n All other whitespace\n is preserved.\n", string_t::literal));
}
BOOST_AUTO_TEST_CASE(test_ml_literal_string_value)
{
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value,
"'''I [dw]on't need \\d{2} apples'''",
value("I [dw]on't need \\d{2} apples", string_t::literal));
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value,
"'''\nThe first newline is\ntrimmed in raw strings.\n All other whitespace\n is preserved.\n'''",
value("The first newline is\ntrimmed in raw strings.\n All other whitespace\n is preserved.\n", string_t::literal));
}
BOOST_AUTO_TEST_CASE(test_unicode_escape_sequence)
{
#if defined(_MSC_VER) || defined(__INTEL_COMPILER)
TOML11_TEST_PARSE_EQUAL(parse_string,
"\"\\u03B1\\u03B2\\u03B3\"",
string("\xCE\xB1\xCE\xB2\xCE\xB3", string_t::basic));
TOML11_TEST_PARSE_EQUAL(parse_string,
"\"\\U0001D7AA\"",
string("\xF0\x9D\x9E\xAA", string_t::basic));
#else
TOML11_TEST_PARSE_EQUAL(parse_string,
"\"\\u03B1\\u03B2\\u03B3\"",
string("αβγ", string_t::basic));
TOML11_TEST_PARSE_EQUAL(parse_string,
"\"\\U0001D7AA\"",
string("𝞪", string_t::basic));
#endif
}

View File

@@ -0,0 +1,50 @@
#define BOOST_TEST_MODULE "parse_table_test"
#ifdef UNITTEST_FRAMEWORK_LIBRARY_EXIST
#include <boost/test/unit_test.hpp>
#else
#define BOOST_TEST_NO_LIB
#include <boost/test/included/unit_test.hpp>
#endif
#include <toml/parser.hpp>
#include <toml/get.hpp>
#include "test_parse_aux.hpp"
using namespace toml;
using namespace detail;
BOOST_AUTO_TEST_CASE(test_normal_table)
{
std::string table(
"key1 = \"value\"\n"
"key2 = 42\n"
"key3 = 3.14\n"
);
location<std::string> loc("test", table);
const auto result = toml::detail::parse_ml_table(loc);
BOOST_CHECK(result.is_ok());
const auto data = result.unwrap();
BOOST_CHECK_EQUAL(toml::get<std::string >(data.at("key1")), "value");
BOOST_CHECK_EQUAL(toml::get<std::int64_t>(data.at("key2")), 42);
BOOST_CHECK_EQUAL(toml::get<double >(data.at("key3")), 3.14);
}
BOOST_AUTO_TEST_CASE(test_nested_table)
{
std::string table(
"a.b = \"value\"\n"
"a.c.d = 42\n"
);
location<std::string> loc("test", table);
const auto result = toml::detail::parse_ml_table(loc);
BOOST_CHECK(result.is_ok());
const auto data = result.unwrap();
const auto a = toml::get<toml::table>(data.at("a"));
const auto c = toml::get<toml::table>(a.at("c"));
BOOST_CHECK_EQUAL(toml::get<std::string >(a.at("b")), "value");
BOOST_CHECK_EQUAL(toml::get<std::int64_t>(c.at("d")), 42);
}

View File

@@ -0,0 +1,117 @@
#define BOOST_TEST_MODULE "parse_table_key_test"
#ifdef UNITTEST_FRAMEWORK_LIBRARY_EXIST
#include <boost/test/unit_test.hpp>
#else
#define BOOST_TEST_NO_LIB
#include <boost/test/included/unit_test.hpp>
#endif
#include <toml/parser.hpp>
#include "test_parse_aux.hpp"
using namespace toml;
using namespace detail;
BOOST_AUTO_TEST_CASE(test_table_bare_key)
{
TOML11_TEST_PARSE_EQUAL(parse_table_key, "[barekey]", std::vector<key>(1, "barekey"));
TOML11_TEST_PARSE_EQUAL(parse_table_key, "[bare-key]", std::vector<key>(1, "bare-key"));
TOML11_TEST_PARSE_EQUAL(parse_table_key, "[bare_key]", std::vector<key>(1, "bare_key"));
TOML11_TEST_PARSE_EQUAL(parse_table_key, "[1234]", std::vector<key>(1, "1234"));
}
BOOST_AUTO_TEST_CASE(test_table_quoted_key)
{
TOML11_TEST_PARSE_EQUAL(parse_table_key, "[\"127.0.0.1\"]", std::vector<key>(1, "127.0.0.1" ));
TOML11_TEST_PARSE_EQUAL(parse_table_key, "[\"character encoding\"]", std::vector<key>(1, "character encoding"));
TOML11_TEST_PARSE_EQUAL(parse_table_key, "[\"ʎǝʞ\"]", std::vector<key>(1, "ʎǝʞ" ));
TOML11_TEST_PARSE_EQUAL(parse_table_key, "['key2']", std::vector<key>(1, "key2" ));
TOML11_TEST_PARSE_EQUAL(parse_table_key, "['quoted \"value\"']", std::vector<key>(1, "quoted \"value\"" ));
}
BOOST_AUTO_TEST_CASE(test_table_dotted_key)
{
{
std::vector<key> keys(2);
keys[0] = "physical";
keys[1] = "color";
TOML11_TEST_PARSE_EQUAL(parse_table_key, "[physical.color]", keys);
}
{
std::vector<key> keys(2);
keys[0] = "physical";
keys[1] = "shape";
TOML11_TEST_PARSE_EQUAL(parse_table_key, "[physical.shape]", keys);
}
{
std::vector<key> keys(4);
keys[0] = "x";
keys[1] = "y";
keys[2] = "z";
keys[3] = "w";
TOML11_TEST_PARSE_EQUAL(parse_table_key, "[x.y.z.w]", keys);
TOML11_TEST_PARSE_EQUAL(parse_table_key, "[x . y . z . w]", keys);
TOML11_TEST_PARSE_EQUAL(parse_table_key, "[x. y .z. w]", keys);
TOML11_TEST_PARSE_EQUAL(parse_table_key, "[x .y. z .w]", keys);
TOML11_TEST_PARSE_EQUAL(parse_table_key, "[ x. y .z . w ]", keys);
TOML11_TEST_PARSE_EQUAL(parse_table_key, "[ x . y . z . w ]", keys);
}
{
std::vector<key> keys(2);
keys[0] = "site";
keys[1] = "google.com";
TOML11_TEST_PARSE_EQUAL(parse_table_key, "[site.\"google.com\"]", keys);
}
}
BOOST_AUTO_TEST_CASE(test_array_of_table_bare_key)
{
TOML11_TEST_PARSE_EQUAL(parse_array_table_key, "[[barekey]]", std::vector<key>(1, "barekey"));
TOML11_TEST_PARSE_EQUAL(parse_array_table_key, "[[bare-key]]", std::vector<key>(1, "bare-key"));
TOML11_TEST_PARSE_EQUAL(parse_array_table_key, "[[bare_key]]", std::vector<key>(1, "bare_key"));
TOML11_TEST_PARSE_EQUAL(parse_array_table_key, "[[1234]]", std::vector<key>(1, "1234"));
}
BOOST_AUTO_TEST_CASE(test_array_of_table_quoted_key)
{
TOML11_TEST_PARSE_EQUAL(parse_array_table_key, "[[\"127.0.0.1\"]]", std::vector<key>(1, "127.0.0.1" ));
TOML11_TEST_PARSE_EQUAL(parse_array_table_key, "[[\"character encoding\"]]", std::vector<key>(1, "character encoding"));
TOML11_TEST_PARSE_EQUAL(parse_array_table_key, "[[\"ʎǝʞ\"]]", std::vector<key>(1, "ʎǝʞ" ));
TOML11_TEST_PARSE_EQUAL(parse_array_table_key, "[['key2']]", std::vector<key>(1, "key2" ));
TOML11_TEST_PARSE_EQUAL(parse_array_table_key, "[['quoted \"value\"']]", std::vector<key>(1, "quoted \"value\"" ));
}
BOOST_AUTO_TEST_CASE(test_array_of_table_dotted_key)
{
{
std::vector<key> keys(2);
keys[0] = "physical";
keys[1] = "color";
TOML11_TEST_PARSE_EQUAL(parse_array_table_key, "[[physical.color]]", keys);
}
{
std::vector<key> keys(2);
keys[0] = "physical";
keys[1] = "shape";
TOML11_TEST_PARSE_EQUAL(parse_array_table_key, "[[physical.shape]]", keys);
}
{
std::vector<key> keys(4);
keys[0] = "x";
keys[1] = "y";
keys[2] = "z";
keys[3] = "w";
TOML11_TEST_PARSE_EQUAL(parse_array_table_key, "[[x.y.z.w]]", keys);
TOML11_TEST_PARSE_EQUAL(parse_array_table_key, "[[x . y . z . w]]", keys);
TOML11_TEST_PARSE_EQUAL(parse_array_table_key, "[[x. y .z. w]]", keys);
TOML11_TEST_PARSE_EQUAL(parse_array_table_key, "[[x .y. z .w]]", keys);
TOML11_TEST_PARSE_EQUAL(parse_array_table_key, "[[ x. y .z . w ]]", keys);
TOML11_TEST_PARSE_EQUAL(parse_array_table_key, "[[ x . y . z . w ]]", keys);
}
{
std::vector<key> keys(2);
keys[0] = "site";
keys[1] = "google.com";
TOML11_TEST_PARSE_EQUAL(parse_array_table_key, "[[site.\"google.com\"]]", keys);
}
}

View File

@@ -1,935 +0,0 @@
#define BOOST_TEST_MODULE "test_parser"
#ifdef UNITTEST_FRAMEWORK_LIBRARY_EXIST
#include <boost/test/unit_test.hpp>
#else
#define BOOST_TEST_NO_LIB
#include <boost/test/included/unit_test.hpp>
#endif
#include <toml/acceptor.hpp>
#include <toml/parser.hpp>
#include <toml/from_toml.hpp>
#include <iostream>
BOOST_AUTO_TEST_CASE(test_parse_basic_inline_string)
{
typedef toml::parse_basic_inline_string parser;
typedef toml::is_basic_inline_string<toml::character> acceptor;
{
const std::string source("\"simple\"");
const std::string expected("simple");
const auto result = parser::invoke(source.cbegin(), source.cend());
BOOST_CHECK(result.first.ok());
BOOST_CHECK_EQUAL(result.first.get(), expected);
BOOST_CHECK(result.second == acceptor::invoke(source.begin(), source.end()));
}
{
const std::string source("\"I'm a string. \\\"You can quote me\\\". Name\\tJos\\u00E9\\nLocation\\tSF.\"");
const std::string expected("I'm a string. \"You can quote me\". Name\tJosé\nLocation\tSF.");
const auto result = parser::invoke(source.cbegin(), source.cend());
BOOST_CHECK(result.first.ok());
BOOST_CHECK_EQUAL(result.first.get(), expected);
BOOST_CHECK(result.second == acceptor::invoke(source.begin(), source.end()));
}
{
const std::string source("dummy");
const auto result = parser::invoke(source.cbegin(), source.cend());
BOOST_CHECK(!result.first.ok());
BOOST_CHECK(result.second == source.begin());
}
}
BOOST_AUTO_TEST_CASE(test_parse_basic_multiline_string)
{
typedef toml::parse_basic_multiline_string parser;
typedef toml::is_basic_multiline_string<toml::character> acceptor;
{
//XXX ifdef windows platform
const std::string source("\"\"\"\nRoses are red\nViolets are blue\"\"\"");
const std::string expected("Roses are red\nViolets are blue");
const auto result = parser::invoke(source.cbegin(), source.cend());
BOOST_CHECK(result.first.ok());
BOOST_CHECK_EQUAL(result.first.get(), expected);
BOOST_CHECK(result.second == acceptor::invoke(source.begin(), source.end()));
}
{
const std::string source("\"\"\"\nThe quick brown \\\n\n fox jumps over \\\n the lazy dog.\"\"\"");
const std::string expected("The quick brown fox jumps over the lazy dog.");
const auto result = parser::invoke(source.cbegin(), source.cend());
BOOST_CHECK(result.first.ok());
BOOST_CHECK_EQUAL(result.first.get(), expected);
BOOST_CHECK(result.second == acceptor::invoke(source.begin(), source.end()));
}
{
const std::string source("\"\"\"\nThe quick brown \\\n fox jumps over \\\n the lazy dog.\\\n \"\"\"");
const std::string expected("The quick brown fox jumps over the lazy dog.");
const auto result = parser::invoke(source.cbegin(), source.cend());
BOOST_CHECK(result.first.ok());
BOOST_CHECK_EQUAL(result.first.get(), expected);
BOOST_CHECK(result.second == acceptor::invoke(source.begin(), source.end()));
}
{
const std::string source("dummy");
const auto result = parser::invoke(source.cbegin(), source.cend());
BOOST_CHECK(!result.first.ok());
BOOST_CHECK(result.second == source.begin());
}
}
BOOST_AUTO_TEST_CASE(test_parse_literal_inline_string)
{
typedef toml::parse_literal_inline_string parser;
typedef toml::is_literal_inline_string<toml::character> acceptor;
{
const std::string source("'C:\\Users\\nodejs\\templates'");
const std::string expected("C:\\Users\\nodejs\\templates");
const auto result = parser::invoke(source.cbegin(), source.cend());
BOOST_CHECK(result.first.ok());
BOOST_CHECK_EQUAL(result.first.get(), expected);
BOOST_CHECK(result.second == acceptor::invoke(source.begin(), source.end()));
}
{
const std::string source("'\\\\ServerX\\admin$\\system32\\'");
const std::string expected("\\\\ServerX\\admin$\\system32\\");
const auto result = parser::invoke(source.cbegin(), source.cend());
BOOST_CHECK(result.first.ok());
BOOST_CHECK_EQUAL(result.first.get(), expected);
BOOST_CHECK(result.second == acceptor::invoke(source.begin(), source.end()));
}
{
const std::string source("'Tom \"Dubs\" Preston-Werner'");
const std::string expected("Tom \"Dubs\" Preston-Werner");
const auto result = parser::invoke(source.cbegin(), source.cend());
BOOST_CHECK(result.first.ok());
BOOST_CHECK_EQUAL(result.first.get(), expected);
BOOST_CHECK(result.second == acceptor::invoke(source.begin(), source.end()));
}
{
const std::string source("'<\\i\\c*\\s*>'");
const std::string expected("<\\i\\c*\\s*>");
const auto result = parser::invoke(source.cbegin(), source.cend());
BOOST_CHECK(result.first.ok());
BOOST_CHECK_EQUAL(result.first.get(), expected);
BOOST_CHECK(result.second == acceptor::invoke(source.begin(), source.end()));
}
{
const std::string source("dummy");
const auto result = parser::invoke(source.cbegin(), source.cend());
BOOST_CHECK(!result.first.ok());
BOOST_CHECK(result.second == source.begin());
}
}
BOOST_AUTO_TEST_CASE(test_parse_literal_multiline_string)
{
typedef toml::parse_literal_multiline_string parser;
typedef toml::is_literal_multiline_string<toml::character> acceptor;
{
const std::string source("'''I [dw]on't need \\d{2} apples'''");
const std::string expected("I [dw]on't need \\d{2} apples");
const auto result = parser::invoke(source.cbegin(), source.cend());
BOOST_CHECK(result.first.ok());
BOOST_CHECK_EQUAL(result.first.get(), expected);
BOOST_CHECK(result.second == acceptor::invoke(source.begin(), source.end()));
}
{
const std::string source("'''\nThe first newline is \ntrimmed in raw strings.\n All other whitespace\n is preserved.'''");
const std::string expected("The first newline is \ntrimmed in raw strings.\n All other whitespace\n is preserved.");
const auto result = parser::invoke(source.cbegin(), source.cend());
BOOST_CHECK(result.first.ok());
BOOST_CHECK_EQUAL(result.first.get(), expected);
BOOST_CHECK(result.second == acceptor::invoke(source.begin(), source.end()));
}
{
const std::string source("dummy");
const auto result = parser::invoke(source.cbegin(), source.cend());
BOOST_CHECK(!result.first.ok());
BOOST_CHECK(result.second == source.begin());
}
}
BOOST_AUTO_TEST_CASE(test_parse_string)
{
typedef toml::parse_string parser;
typedef toml::is_string<toml::character> acceptor;
{
const std::string source("\"string\"");
const std::string expected("string");
const auto result = parser::invoke(source.cbegin(), source.cend());
BOOST_CHECK(result.first.ok());
BOOST_CHECK_EQUAL(result.first.get(), expected);
BOOST_CHECK(result.second == acceptor::invoke(source.begin(), source.end()));
}
{
const std::string source("\"\"\"string\"\"\"");
const std::string expected("string");
const auto result = parser::invoke(source.cbegin(), source.cend());
BOOST_CHECK(result.first.ok());
BOOST_CHECK_EQUAL(result.first.get(), expected);
BOOST_CHECK(result.second == acceptor::invoke(source.begin(), source.end()));
}
{
const std::string source("'string'");
const std::string expected("string");
const auto result = parser::invoke(source.cbegin(), source.cend());
BOOST_CHECK(result.first.ok());
BOOST_CHECK_EQUAL(result.first.get(), expected);
BOOST_CHECK(result.second == acceptor::invoke(source.begin(), source.end()));
}
{
const std::string source("'''string'''");
const std::string expected("string");
const auto result = parser::invoke(source.cbegin(), source.cend());
BOOST_CHECK(result.first.ok());
BOOST_CHECK_EQUAL(result.first.get(), expected);
BOOST_CHECK(result.second == acceptor::invoke(source.begin(), source.end()));
}
{
const std::string source("dummy");
const auto result = parser::invoke(source.cbegin(), source.cend());
BOOST_CHECK(!result.first.ok());
BOOST_CHECK(result.second == source.begin());
}
}
BOOST_AUTO_TEST_CASE(test_integer)
{
typedef toml::parse_integer parser;
typedef toml::is_integer<toml::character> acceptor;
{
const std::string source("42");
const toml::Integer expected(42);
const auto result = parser::invoke(source.cbegin(), source.cend());
BOOST_CHECK(result.first.ok());
BOOST_CHECK_EQUAL(result.first.get(), expected);
BOOST_CHECK(result.second == acceptor::invoke(source.begin(), source.end()));
}
{
const std::string source("+42");
const toml::Integer expected(42);
const auto result = parser::invoke(source.cbegin(), source.cend());
BOOST_CHECK(result.first.ok());
BOOST_CHECK_EQUAL(result.first.get(), expected);
BOOST_CHECK(result.second == acceptor::invoke(source.begin(), source.end()));
}
{
const std::string source("-42");
const toml::Integer expected(-42);
const auto result = parser::invoke(source.cbegin(), source.cend());
BOOST_CHECK(result.first.ok());
BOOST_CHECK_EQUAL(result.first.get(), expected);
BOOST_CHECK(result.second == acceptor::invoke(source.begin(), source.end()));
}
{
const std::string source("-4_2");
const toml::Integer expected(-42);
const auto result = parser::invoke(source.cbegin(), source.cend());
BOOST_CHECK(result.first.ok());
BOOST_CHECK_EQUAL(result.first.get(), expected);
BOOST_CHECK(result.second == acceptor::invoke(source.begin(), source.end()));
}
{
const std::string source("dummy");
const auto result = parser::invoke(source.cbegin(), source.cend());
BOOST_CHECK(!result.first.ok());
BOOST_CHECK(result.second == source.begin());
}
}
BOOST_AUTO_TEST_CASE(test_float)
{
typedef toml::parse_float parser;
typedef toml::is_float<toml::character> acceptor;
{
const std::string source("42.0");
const toml::Float expected(42.0);
const auto result = parser::invoke(source.cbegin(), source.cend());
BOOST_CHECK(result.first.ok());
BOOST_CHECK_EQUAL(result.first.get(), expected);
BOOST_CHECK(result.second == acceptor::invoke(source.begin(), source.end()));
}
{
const std::string source("+42.0");
const toml::Float expected(42.0);
const auto result = parser::invoke(source.cbegin(), source.cend());
BOOST_CHECK(result.first.ok());
BOOST_CHECK_EQUAL(result.first.get(), expected);
BOOST_CHECK(result.second == acceptor::invoke(source.begin(), source.end()));
}
{
const std::string source("-42.0");
const toml::Float expected(-42.0);
const auto result = parser::invoke(source.cbegin(), source.cend());
BOOST_CHECK(result.first.ok());
BOOST_CHECK_EQUAL(result.first.get(), expected);
BOOST_CHECK(result.second == acceptor::invoke(source.begin(), source.end()));
}
{
const std::string source("-4_2.0");
const toml::Float expected(-42.0);
const auto result = parser::invoke(source.cbegin(), source.cend());
BOOST_CHECK(result.first.ok());
BOOST_CHECK_EQUAL(result.first.get(), expected);
BOOST_CHECK(result.second == acceptor::invoke(source.begin(), source.end()));
}
{
const std::string source("-42e0");
const toml::Float expected(-42.0);
const auto result = parser::invoke(source.cbegin(), source.cend());
BOOST_CHECK(result.first.ok());
BOOST_CHECK_EQUAL(result.first.get(), expected);
BOOST_CHECK(result.second == acceptor::invoke(source.begin(), source.end()));
}
{
const std::string source("-42.0e0");
const toml::Float expected(-42.0);
const auto result = parser::invoke(source.cbegin(), source.cend());
BOOST_CHECK(result.first.ok());
BOOST_CHECK_EQUAL(result.first.get(), expected);
BOOST_CHECK(result.second == acceptor::invoke(source.begin(), source.end()));
}
{
const std::string source("dummy");
const auto result = parser::invoke(source.cbegin(), source.cend());
BOOST_CHECK(!result.first.ok());
BOOST_CHECK(result.second == source.begin());
}
{
const std::string source("42");
const auto result = parser::invoke(source.cbegin(), source.cend());
BOOST_CHECK(!result.first.ok());
BOOST_CHECK(result.second == source.begin());
}
}
BOOST_AUTO_TEST_CASE(test_parse_boolean)
{
typedef toml::parse_boolean parser;
typedef toml::is_boolean<toml::character> acceptor;
{
const std::string source("true");
const toml::Boolean expected(true);
const auto result = parser::invoke(source.cbegin(), source.cend());
BOOST_CHECK(result.first.ok());
BOOST_CHECK_EQUAL(result.first.get(), expected);
BOOST_CHECK(result.second == acceptor::invoke(source.begin(), source.end()));
}
{
const std::string source("false");
const toml::Boolean expected(false);
const auto result = parser::invoke(source.cbegin(), source.cend());
BOOST_CHECK(result.first.ok());
BOOST_CHECK_EQUAL(result.first.get(), expected);
BOOST_CHECK(result.second == acceptor::invoke(source.begin(), source.end()));
}
{
const std::string source("dummy");
const auto result = parser::invoke(source.cbegin(), source.cend());
BOOST_CHECK(!result.first.ok());
BOOST_CHECK(result.second == source.begin());
}
}
BOOST_AUTO_TEST_CASE(test_parse_local_time)
{
typedef toml::parse_local_time parser;
typedef toml::is_local_time<toml::character> acceptor;
{
const std::string source("12:34:56");
const toml::Datetime expected(12, 34, 56, 0, 0);
const auto result = parser::invoke(source.cbegin(), source.cend());
BOOST_CHECK(result.first.ok());
BOOST_CHECK_EQUAL(result.first.get(), expected);
BOOST_CHECK(result.second == acceptor::invoke(source.begin(), source.end()));
}
{
const std::string source("12:34:56.7");
const toml::Datetime expected(12, 34, 56, 700, 0);
const auto result = parser::invoke(source.cbegin(), source.cend());
BOOST_CHECK(result.first.ok());
BOOST_CHECK_EQUAL(result.first.get(), expected);
BOOST_CHECK(result.second == acceptor::invoke(source.begin(), source.end()));
}
{
const std::string source("12:34:56.7891");
const toml::Datetime expected(12, 34, 56, 789, 100);
const auto result = parser::invoke(source.cbegin(), source.cend());
BOOST_CHECK(result.first.ok());
BOOST_CHECK_EQUAL(result.first.get(), expected);
BOOST_CHECK(result.second == acceptor::invoke(source.begin(), source.end()));
}
{
const std::string source("10");
const auto result = parser::invoke(source.cbegin(), source.cend());
BOOST_CHECK(!result.first.ok());
BOOST_CHECK(result.second == source.begin());
}
}
BOOST_AUTO_TEST_CASE(test_parse_local_date)
{
typedef toml::parse_local_date parser;
typedef toml::is_local_date<toml::character> acceptor;
{
const std::string source("1979-09-27");
const toml::Datetime expected(1979, 9, 27);
const auto result = parser::invoke(source.cbegin(), source.cend());
BOOST_CHECK(result.first.ok());
BOOST_CHECK_EQUAL(result.first.get(), expected);
BOOST_CHECK(result.second == acceptor::invoke(source.begin(), source.end()));
}
{
const std::string source("10");
const auto result = parser::invoke(source.cbegin(), source.cend());
BOOST_CHECK(!result.first.ok());
BOOST_CHECK(result.second == source.begin());
}
}
BOOST_AUTO_TEST_CASE(test_parse_local_date_time)
{
typedef toml::parse_local_date_time parser;
typedef toml::is_local_date_time<toml::character> acceptor;
{
const std::string source("1979-09-27T12:34:56");
const toml::Datetime expected(1979, 9, 27, 12, 34, 56, 0, 0);
const auto result = parser::invoke(source.cbegin(), source.cend());
BOOST_CHECK(result.first.ok());
BOOST_CHECK_EQUAL(result.first.get(), expected);
BOOST_CHECK(result.second == acceptor::invoke(source.begin(), source.end()));
}
{
const std::string source("1979-09-27T12:34:56.789000");
const toml::Datetime expected(1979, 9, 27, 12, 34, 56, 789, 0);
const auto result = parser::invoke(source.cbegin(), source.cend());
BOOST_CHECK(result.first.ok());
BOOST_CHECK_EQUAL(result.first.get(), expected);
BOOST_CHECK(result.second == acceptor::invoke(source.begin(), source.end()));
}
{
const std::string source("1000-11-11");
const auto result = parser::invoke(source.cbegin(), source.cend());
BOOST_CHECK(!result.first.ok());
BOOST_CHECK(result.second == source.begin());
}
}
BOOST_AUTO_TEST_CASE(test_parse_offset_date_time)
{
typedef toml::parse_offset_date_time parser;
typedef toml::is_offset_date_time<toml::character> acceptor;
{
const std::string source("1979-09-27T12:34:56Z");
const toml::Datetime expected(1979, 9, 27, 12, 34, 56, 0, 0, 0, 0);
const auto result = parser::invoke(source.cbegin(), source.cend());
BOOST_CHECK(result.first.ok());
BOOST_CHECK_EQUAL(result.first.get(), expected);
BOOST_CHECK(result.second == acceptor::invoke(source.begin(), source.end()));
}
{
const std::string source("1979-09-27T12:34:56.789000Z");
const toml::Datetime expected(1979, 9, 27, 12, 34, 56, 789, 0, 0, 0);
const auto result = parser::invoke(source.cbegin(), source.cend());
BOOST_CHECK(result.first.ok());
BOOST_CHECK_EQUAL(result.first.get(), expected);
BOOST_CHECK(result.second == acceptor::invoke(source.begin(), source.end()));
}
{
const std::string source("1979-09-27T12:34:56+07:30");
const toml::Datetime expected(1979, 9, 27, 12, 34, 56, 0, 0, 7, 30);
const auto result = parser::invoke(source.cbegin(), source.cend());
BOOST_CHECK(result.first.ok());
BOOST_CHECK_EQUAL(result.first.get(), expected);
BOOST_CHECK(result.second == acceptor::invoke(source.begin(), source.end()));
}
{
const std::string source("1979-09-27T12:34:56.789000+07:30");
const toml::Datetime expected(1979, 9, 27, 12, 34, 56, 789, 0, 7, 30);
const auto result = parser::invoke(source.cbegin(), source.cend());
BOOST_CHECK(result.first.ok());
BOOST_CHECK_EQUAL(result.first.get(), expected);
BOOST_CHECK(result.second == acceptor::invoke(source.begin(), source.end()));
}
{
const std::string source("1979-09-27T12:34:56-07:30");
const toml::Datetime expected(1979, 9, 27, 12, 34, 56, 0, 0, -7, -30);
const auto result = parser::invoke(source.cbegin(), source.cend());
BOOST_CHECK(result.first.ok());
BOOST_CHECK_EQUAL(result.first.get(), expected);
BOOST_CHECK(result.second == acceptor::invoke(source.begin(), source.end()));
}
{
const std::string source("1979-09-27T12:34:56.789000-07:30");
const toml::Datetime expected(1979, 9, 27, 12, 34, 56, 789, 0, -7, -30);
const auto result = parser::invoke(source.cbegin(), source.cend());
BOOST_CHECK(result.first.ok());
BOOST_CHECK_EQUAL(result.first.get(), expected);
BOOST_CHECK(result.second == acceptor::invoke(source.begin(), source.end()));
}
{
const std::string source("1000-11-11");
const auto result = parser::invoke(source.cbegin(), source.cend());
BOOST_CHECK(!result.first.ok());
BOOST_CHECK(result.second == source.begin());
}
}
BOOST_AUTO_TEST_CASE(test_parse_datetime)
{
typedef toml::parse_datetime parser;
typedef toml::is_datetime<toml::character> acceptor;
{
const std::string source("1979-09-27T12:34:56Z");
const toml::Datetime expected(1979, 9, 27, 12, 34, 56, 0, 0, 0, 0);
const auto result = parser::invoke(source.cbegin(), source.cend());
BOOST_CHECK(result.first.ok());
BOOST_CHECK_EQUAL(result.first.get(), expected);
BOOST_CHECK(result.second == acceptor::invoke(source.begin(), source.end()));
}
{
const std::string source("1979-09-27T12:34:56");
const toml::Datetime expected(1979, 9, 27, 12, 34, 56, 0, 0);
const auto result = parser::invoke(source.cbegin(), source.cend());
BOOST_CHECK(result.first.ok());
BOOST_CHECK_EQUAL(result.first.get(), expected);
BOOST_CHECK(result.second == acceptor::invoke(source.begin(), source.end()));
}
{
const std::string source("1979-09-27");
const toml::Datetime expected(1979, 9, 27);
const auto result = parser::invoke(source.cbegin(), source.cend());
BOOST_CHECK(result.first.ok());
BOOST_CHECK_EQUAL(result.first.get(), expected);
BOOST_CHECK(result.second == acceptor::invoke(source.begin(), source.end()));
}
{
const std::string source("12:34:56");
const toml::Datetime expected(12, 34, 56, 0, 0);
const auto result = parser::invoke(source.cbegin(), source.cend());
BOOST_CHECK(result.first.ok());
BOOST_CHECK_EQUAL(result.first.get(), expected);
BOOST_CHECK(result.second == acceptor::invoke(source.begin(), source.end()));
}
{
const std::string source("12");
const auto result = parser::invoke(source.cbegin(), source.cend());
BOOST_CHECK(!result.first.ok());
BOOST_CHECK(result.second == source.begin());
}
}
BOOST_AUTO_TEST_CASE(test_parse_array)
{
typedef toml::parse_array<toml::character> parser;
typedef toml::is_array<toml::character> acceptor;
{
const std::string source("[1,2,3]");
const toml::Array expected{1, 2, 3};
const auto result = parser::invoke(source.cbegin(), source.cend());
BOOST_CHECK(result.first.ok());
BOOST_CHECK(result.first.get() == expected);
BOOST_CHECK(result.second == acceptor::invoke(source.begin(), source.end()));
}
{
const std::string source("[1, 2, 3]");
const toml::Array expected{1, 2, 3};
const auto result = parser::invoke(source.cbegin(), source.cend());
BOOST_CHECK(result.first.ok());
BOOST_CHECK(result.first.get() == expected);
BOOST_CHECK(result.second == acceptor::invoke(source.begin(), source.end()));
}
{
const std::string source("[ 1,2,3 ]");
const toml::Array expected{1, 2, 3};
const auto result = parser::invoke(source.cbegin(), source.cend());
BOOST_CHECK(result.first.ok());
BOOST_CHECK(result.first.get() == expected);
BOOST_CHECK(result.second == acceptor::invoke(source.begin(), source.end()));
}
{
const std::string source("[ 1 , 2 , 3 ]");
const toml::Array expected{1, 2, 3};
const auto result = parser::invoke(source.cbegin(), source.cend());
BOOST_CHECK(result.first.ok());
BOOST_CHECK(result.first.get() == expected);
BOOST_CHECK(result.second == acceptor::invoke(source.begin(), source.end()));
}
{
const std::string source("[ 1 \n,#comment\n 2 ,\n 3\n ]");
const toml::Array expected{1, 2, 3};
const auto result = parser::invoke(source.cbegin(), source.cend());
BOOST_CHECK(result.first.ok());
BOOST_CHECK(result.first.get() == expected);
BOOST_CHECK(result.second == acceptor::invoke(source.begin(), source.end()));
}
{
const std::string source("[ # empty array\n ]");
const toml::Array expected{};
const auto result = parser::invoke(source.cbegin(), source.cend());
BOOST_CHECK(result.first.ok());
BOOST_CHECK(result.first.get() == expected);
BOOST_CHECK(result.second == acceptor::invoke(source.begin(), source.end()));
}
{
const std::string source("[ \"] \", ' # ', \n']', # ] \n]");
const toml::Array expected{"] ", " # ", "]"};
const auto result = parser::invoke(source.cbegin(), source.cend());
BOOST_CHECK(result.first.ok());
BOOST_CHECK(result.first.get() == expected);
BOOST_CHECK(result.second == acceptor::invoke(source.begin(), source.end()));
}
{
const std::string source("[ \"Test #11 ]proved that\", 'Experiment #9 was a success' ]");
const toml::Array expected{"Test #11 ]proved that", "Experiment #9 was a success"};
const auto result = parser::invoke(source.cbegin(), source.cend());
BOOST_CHECK(result.first.ok());
BOOST_CHECK(result.first.get() == expected);
BOOST_CHECK(result.second == acceptor::invoke(source.begin(), source.end()));
}
{
const std::string source("[ \"Test #11 ]proved that\", 'Experiment #9 was a success' ]");
const toml::Array expected{"Test #11 ]proved that", "Experiment #9 was a success"};
const auto result = parser::invoke(source.cbegin(), source.cend());
BOOST_CHECK(result.first.ok());
BOOST_CHECK(result.first.get() == expected);
BOOST_CHECK(result.second == acceptor::invoke(source.begin(), source.end()));
}
{
const std::string source("[ [1,2,3] , ['a', 'b', 'c'] ]");
const toml::Array expected{{1,2,3}, {"a", "b", "c"}};
const auto result = parser::invoke(source.cbegin(), source.cend());
BOOST_CHECK(result.first.ok());
BOOST_CHECK(result.first.get() == expected);
BOOST_CHECK(result.second == acceptor::invoke(source.begin(), source.end()));
}
{
const std::string source("[ {foo=1}, {foo=1, bar=2.0}, {foo=1, bar=2.0, baz='str'} ]");
const toml::Array expected{{{"foo", 1}}, {{"foo", 1}, {"bar", 2.0}}, {{"foo", 1}, {"bar", 2.0}, {"baz", "str"}}};
const auto result = parser::invoke(source.cbegin(), source.cend());
BOOST_CHECK(result.first.ok());
BOOST_CHECK(result.first.get() == expected);
BOOST_CHECK(result.second == acceptor::invoke(source.begin(), source.end()));
}
{
const std::string source("[dummy]");
const auto result = parser::invoke(source.cbegin(), source.cend());
BOOST_CHECK(!result.first.ok());
BOOST_CHECK(result.second == source.begin());
}
}
BOOST_AUTO_TEST_CASE(test_parse_inline_table)
{
typedef toml::parse_inline_table<toml::character> parser;
typedef toml::is_inline_table<toml::character> acceptor;
{
const std::string source("{foo=1,bar=2.0,baz='str'}");
const toml::Table expected{{"foo", 1}, {"bar", 2.0}, {"baz", "str"}};
const auto result = parser::invoke(source.cbegin(), source.cend());
BOOST_CHECK(result.first.ok());
BOOST_CHECK(result.first.get() == expected);
BOOST_CHECK(result.second == acceptor::invoke(source.begin(), source.end()));
}
{
const std::string source("{ foo=1, bar=2.0, baz='str' }");
const toml::Table expected{{"foo", 1}, {"bar", 2.0}, {"baz", "str"}};
const auto result = parser::invoke(source.cbegin(), source.cend());
BOOST_CHECK(result.first.ok());
BOOST_CHECK(result.first.get() == expected);
BOOST_CHECK(result.second == acceptor::invoke(source.begin(), source.end()));
}
{
const std::string source("{ foo = 1, bar = 2.0, baz = 'str' }");
const toml::Table expected{{"foo", 1}, {"bar", 2.0}, {"baz", "str"}};
const auto result = parser::invoke(source.cbegin(), source.cend());
BOOST_CHECK(result.first.ok());
BOOST_CHECK(result.first.get() == expected);
BOOST_CHECK(result.second == acceptor::invoke(source.begin(), source.end()));
}
{
const std::string source("{b=true, i=1, f=2.0, d=1907-03-02T07:32:00, s='str', a=[1,2,3], t={foo=1}}");
const toml::Table expected{{"b", true}, {"i", 1}, {"f", 2.0},
{"d", toml::Datetime(1907,3,2,7,32,0,0,0)},
{"s", "str"}, {"a", {1, 2, 3}},
{"t", {{"foo", 1}}}};
const auto result = parser::invoke(source.cbegin(), source.cend());
BOOST_CHECK(result.first.ok());
BOOST_CHECK(result.first.get() == expected);
BOOST_CHECK(result.second == acceptor::invoke(source.begin(), source.end()));
}
{
const std::string source("{dummy}");
const auto result = parser::invoke(source.cbegin(), source.cend());
BOOST_CHECK(!result.first.ok());
BOOST_CHECK(result.second == source.begin());
}
}
BOOST_AUTO_TEST_CASE(test_parse_barekey)
{
typedef toml::parse_barekey parser;
typedef toml::is_barekey<toml::character> acceptor;
{
const std::string source("hoge");
const toml::key expected("hoge");
const auto result = parser::invoke(source.cbegin(), source.cend());
BOOST_CHECK(result.first.ok());
BOOST_CHECK(result.first.get() == expected);
BOOST_CHECK(result.second == acceptor::invoke(source.begin(), source.end()));
}
{
const std::string source("bare-key");
const toml::key expected("bare-key");
const auto result = parser::invoke(source.cbegin(), source.cend());
BOOST_CHECK(result.first.ok());
BOOST_CHECK(result.first.get() == expected);
BOOST_CHECK(result.second == acceptor::invoke(source.begin(), source.end()));
}
{
const std::string source("bare_key");
const toml::key expected("bare_key");
const auto result = parser::invoke(source.cbegin(), source.cend());
BOOST_CHECK(result.first.ok());
BOOST_CHECK(result.first.get() == expected);
BOOST_CHECK(result.second == acceptor::invoke(source.begin(), source.end()));
}
{
const std::string source("42");
const toml::key expected("42");
const auto result = parser::invoke(source.cbegin(), source.cend());
BOOST_CHECK(result.first.ok());
BOOST_CHECK(result.first.get() == expected);
BOOST_CHECK(result.second == acceptor::invoke(source.begin(), source.end()));
}
}
BOOST_AUTO_TEST_CASE(test_key_value_pair)
{
typedef toml::parse_key_value_pair<char> parser;
typedef toml::is_key_value_pair<char> acceptor;
{
const std::string source("key=1");
const std::pair<toml::key, toml::value> expected{"key", 1};
const auto result = parser::invoke(source.cbegin(), source.cend());
BOOST_CHECK(result.first.ok());
BOOST_CHECK(result.first.get() == expected);
BOOST_CHECK(result.second == acceptor::invoke(source.begin(), source.end()));
}
{
const std::string source("key =\t1");
const std::pair<toml::key, toml::value> expected{"key", 1};
const auto result = parser::invoke(source.cbegin(), source.cend());
BOOST_CHECK(result.first.ok());
BOOST_CHECK(result.first.get() == expected);
BOOST_CHECK(result.second == acceptor::invoke(source.begin(), source.end()));
}
{
const std::string source("key = true");
const std::pair<toml::key, toml::value> expected{"key", true};
const auto result = parser::invoke(source.cbegin(), source.cend());
BOOST_CHECK(result.first.ok());
BOOST_CHECK(result.first.get() == expected);
BOOST_CHECK(result.second == acceptor::invoke(source.begin(), source.end()));
}
{
const std::string source("key = -42");
const std::pair<toml::key, toml::value> expected{"key", -42};
const auto result = parser::invoke(source.cbegin(), source.cend());
BOOST_CHECK(result.first.ok());
BOOST_CHECK(result.first.get() == expected);
BOOST_CHECK(result.second == acceptor::invoke(source.begin(), source.end()));
}
{
const std::string source("key = -42.0");
const std::pair<toml::key, toml::value> expected{"key", -42.};
const auto result = parser::invoke(source.cbegin(), source.cend());
BOOST_CHECK(result.first.ok());
BOOST_CHECK(result.first.get() == expected);
BOOST_CHECK(result.second == acceptor::invoke(source.begin(), source.end()));
}
{
const std::string source("key = \"string\"");
const std::pair<toml::key, toml::value> expected{"key", "string"};
const auto result = parser::invoke(source.cbegin(), source.cend());
BOOST_CHECK(result.first.ok());
BOOST_CHECK(result.first.get() == expected);
BOOST_CHECK(result.second == acceptor::invoke(source.begin(), source.end()));
}
{
const std::string source("key = 1901-01-01T00:00:00");
const std::pair<toml::key, toml::value> expected{"key", toml::Datetime(1901, 1,1,0,0,0,0,0)};
const auto result = parser::invoke(source.cbegin(), source.cend());
BOOST_CHECK(result.first.ok());
BOOST_CHECK(result.first.get() == expected);
BOOST_CHECK(result.second == acceptor::invoke(source.begin(), source.end()));
}
{
const std::string source("key = [1,2,3]");
const std::pair<toml::key, toml::value> expected{"key", {1,2,3}};
const auto result = parser::invoke(source.cbegin(), source.cend());
BOOST_CHECK(result.first.ok());
BOOST_CHECK(result.first.get() == expected);
BOOST_CHECK(result.second == acceptor::invoke(source.begin(), source.end()));
}
{
const std::string source("key = {foo=1,bar=2.0,baz='3'}");
const std::pair<toml::key, toml::value> expected{"key",
{{"foo", 1}, {"bar", 2.0}, {"baz", "3"}}};
const auto result = parser::invoke(source.cbegin(), source.cend());
BOOST_CHECK(result.first.ok());
BOOST_CHECK(result.first.get() == expected);
BOOST_CHECK(result.second == acceptor::invoke(source.begin(), source.end()));
}
}
BOOST_AUTO_TEST_CASE(test_table_definition)
{
typedef toml::parse_table_definition parser;
typedef toml::is_table_definition<char> acceptor;
{
const std::string source("[foo]");
const std::vector<toml::key> expected{"foo"};
const auto result = parser::invoke(source.cbegin(), source.cend());
BOOST_CHECK(result.first.ok());
BOOST_CHECK(result.first.get() == expected);
BOOST_CHECK(result.second == acceptor::invoke(source.begin(), source.end()));
}
{
const std::string source("[foo.bar.baz]");
const std::vector<toml::key> expected{"foo", "bar", "baz"};
const auto result = parser::invoke(source.cbegin(), source.cend());
BOOST_CHECK(result.first.ok());
BOOST_CHECK(result.first.get() == expected);
BOOST_CHECK(result.second == acceptor::invoke(source.begin(), source.end()));
}
{
const std::string source("[foo . bar. baz]");
const std::vector<toml::key> expected{"foo", "bar", "baz"};
const auto result = parser::invoke(source.cbegin(), source.cend());
BOOST_CHECK(result.first.ok());
BOOST_CHECK(result.first.get() == expected);
BOOST_CHECK(result.second == acceptor::invoke(source.begin(), source.end()));
}
{
const std::string source("[foo . \"bar\" . baz]");
const std::vector<toml::key> expected{"foo", "bar", "baz"};
const auto result = parser::invoke(source.cbegin(), source.cend());
BOOST_CHECK(result.first.ok());
BOOST_CHECK(result.first.get() == expected);
BOOST_CHECK(result.second == acceptor::invoke(source.begin(), source.end()));
}
{
const std::string source("[foo . \"b\\tar\" . baz]");
const std::vector<toml::key> expected{"foo", "b\tar", "baz"};
const auto result = parser::invoke(source.cbegin(), source.cend());
BOOST_CHECK(result.first.ok());
BOOST_CHECK(result.first.get() == expected);
BOOST_CHECK(result.second == acceptor::invoke(source.begin(), source.end()));
}
}
BOOST_AUTO_TEST_CASE(test_array_of_table_definition)
{
typedef toml::parse_array_of_table_definition parser;
typedef toml::is_array_of_table_definition<char> acceptor;
{
const std::string source("[[foo]]");
const std::vector<toml::key> expected{"foo"};
const auto result = parser::invoke(source.cbegin(), source.cend());
BOOST_CHECK(result.first.ok());
BOOST_CHECK(result.first.get() == expected);
BOOST_CHECK(result.second == acceptor::invoke(source.begin(), source.end()));
}
{
const std::string source("[[foo.bar.baz]]");
const std::vector<toml::key> expected{"foo", "bar", "baz"};
const auto result = parser::invoke(source.cbegin(), source.cend());
BOOST_CHECK(result.first.ok());
BOOST_CHECK(result.first.get() == expected);
BOOST_CHECK(result.second == acceptor::invoke(source.begin(), source.end()));
}
{
const std::string source("[[foo . bar. baz]]");
const std::vector<toml::key> expected{"foo", "bar", "baz"};
const auto result = parser::invoke(source.cbegin(), source.cend());
BOOST_CHECK(result.first.ok());
BOOST_CHECK(result.first.get() == expected);
BOOST_CHECK(result.second == acceptor::invoke(source.begin(), source.end()));
}
{
const std::string source("[[foo . \"bar\" . baz]]");
const std::vector<toml::key> expected{"foo", "bar", "baz"};
const auto result = parser::invoke(source.cbegin(), source.cend());
BOOST_CHECK(result.first.ok());
BOOST_CHECK(result.first.get() == expected);
BOOST_CHECK(result.second == acceptor::invoke(source.begin(), source.end()));
}
{
const std::string source("[[foo . \"b\\tar\" . baz]]");
const std::vector<toml::key> expected{"foo", "b\tar", "baz"};
const auto result = parser::invoke(source.cbegin(), source.cend());
BOOST_CHECK(result.first.ok());
BOOST_CHECK(result.first.get() == expected);
BOOST_CHECK(result.second == acceptor::invoke(source.begin(), source.end()));
}
}
BOOST_AUTO_TEST_CASE(test_parse_data)
{
typedef toml::parse_data parser;
{
const std::string source("#hogehoge");
const toml::Table expected{};
const auto result = parser::invoke(source.cbegin(), source.cend());
BOOST_CHECK(result == expected);
}
{
const std::string source("key = 'value'");
const toml::Table expected{{"key", "value"}};
const auto result = parser::invoke(source.cbegin(), source.cend());
BOOST_CHECK(result == expected);
}
{
const std::string source("key = 'value' #hoge");
const toml::Table expected{{"key", "value"}};
const auto result = parser::invoke(source.cbegin(), source.cend());
BOOST_CHECK(result == expected);
}
{
const std::string source("[table]\nkey = 'value' #hoge");
const toml::Table expected{{"table", {{"key", "value"}} }};
const auto result = parser::invoke(source.cbegin(), source.cend());
BOOST_CHECK(result == expected);
}
{
const std::string source("[table]\n\tkey = 'value'\n\t#hoge");
const toml::Table expected{{"table", {{"key", "value"}} }};
const auto result = parser::invoke(source.cbegin(), source.cend());
BOOST_CHECK(result == expected);
}
{
const std::string source("[table]\n\tkey = 'value'\n\t#hoge");
const toml::Table expected{{"table", {{"key", "value"}} }};
const auto result = parser::invoke(source.cbegin(), source.cend());
BOOST_CHECK(result == expected);
}
{
const std::string source("[nested.table]\n\tkey = 'value'\n\t#hoge");
const toml::Table expected{{"nested", {{"table", {{"key", "value"}}}}}};
const auto result = parser::invoke(source.cbegin(), source.cend());
BOOST_CHECK(result == expected);
}
}

411
tests/test_result.cpp Normal file
View File

@@ -0,0 +1,411 @@
#define BOOST_TEST_MODULE "test_result"
#include <boost/test/unit_test.hpp>
#include <iostream>
#include <toml/result.hpp>
BOOST_AUTO_TEST_CASE(test_construct)
{
{
auto s = toml::ok(42);
toml::result<int, std::string> result(s);
BOOST_CHECK(!!result);
BOOST_CHECK(result.is_ok());
BOOST_CHECK(!result.is_err());
BOOST_CHECK_EQUAL(result.unwrap(), 42);
}
{
const auto s = toml::ok(42);
toml::result<int, std::string> result(s);
BOOST_CHECK(!!result);
BOOST_CHECK(result.is_ok());
BOOST_CHECK(!result.is_err());
BOOST_CHECK_EQUAL(result.unwrap(), 42);
}
{
toml::result<int, std::string> result(toml::ok(42));
BOOST_CHECK(!!result);
BOOST_CHECK(result.is_ok());
BOOST_CHECK(!result.is_err());
BOOST_CHECK_EQUAL(result.unwrap(), 42);
}
{
auto f = toml::err<std::string>("foobar");
toml::result<int, std::string> result(f);
BOOST_CHECK(!result);
BOOST_CHECK(!result.is_ok());
BOOST_CHECK(result.is_err());
BOOST_CHECK_EQUAL(result.unwrap_err(), "foobar");
}
{
const auto f = toml::err<std::string>("foobar");
toml::result<int, std::string> result(f);
BOOST_CHECK(!result);
BOOST_CHECK(!result.is_ok());
BOOST_CHECK(result.is_err());
BOOST_CHECK_EQUAL(result.unwrap_err(), "foobar");
}
{
toml::result<int, std::string> result(toml::err<std::string>("foobar"));
BOOST_CHECK(!result);
BOOST_CHECK(!result.is_ok());
BOOST_CHECK(result.is_err());
BOOST_CHECK_EQUAL(result.unwrap_err(), "foobar");
}
}
BOOST_AUTO_TEST_CASE(test_assignment)
{
{
toml::result<int, std::string> result(toml::err<std::string>("foobar"));
result = toml::ok(42);
BOOST_CHECK(!!result);
BOOST_CHECK(result.is_ok());
BOOST_CHECK(!result.is_err());
BOOST_CHECK_EQUAL(result.unwrap(), 42);
}
{
toml::result<int, std::string> result(toml::err<std::string>("foobar"));
auto s = toml::ok(42);
result = s;
BOOST_CHECK(!!result);
BOOST_CHECK(result.is_ok());
BOOST_CHECK(!result.is_err());
BOOST_CHECK_EQUAL(result.unwrap(), 42);
}
{
toml::result<int, std::string> result(toml::err<std::string>("foobar"));
const auto s = toml::ok(42);
result = s;
BOOST_CHECK(!!result);
BOOST_CHECK(result.is_ok());
BOOST_CHECK(!result.is_err());
BOOST_CHECK_EQUAL(result.unwrap(), 42);
}
{
toml::result<int, std::string> result(toml::err<std::string>("foobar"));
result = toml::err<std::string>("hoge");
BOOST_CHECK(!result);
BOOST_CHECK(!result.is_ok());
BOOST_CHECK(result.is_err());
BOOST_CHECK_EQUAL(result.unwrap_err(), "hoge");
}
{
toml::result<int, std::string> result(toml::err<std::string>("foobar"));
auto f = toml::err<std::string>("hoge");
result = f;
BOOST_CHECK(!result);
BOOST_CHECK(!result.is_ok());
BOOST_CHECK(result.is_err());
BOOST_CHECK_EQUAL(result.unwrap_err(), "hoge");
}
{
toml::result<int, std::string> result(toml::err<std::string>("foobar"));
const auto f = toml::err<std::string>("hoge");
result = f;
BOOST_CHECK(!result);
BOOST_CHECK(!result.is_ok());
BOOST_CHECK(result.is_err());
BOOST_CHECK_EQUAL(result.unwrap_err(), "hoge");
}
}
BOOST_AUTO_TEST_CASE(test_map)
{
{
const toml::result<int, std::string> result(toml::ok(42));
const auto mapped = result.map(
[](const int i) -> int {
return i * 2;
});
BOOST_CHECK(!!mapped);
BOOST_CHECK(mapped.is_ok());
BOOST_CHECK(!mapped.is_err());
BOOST_CHECK_EQUAL(mapped.unwrap(), 42 * 2);
}
{
toml::result<std::unique_ptr<int>, std::string>
result(toml::ok(std::unique_ptr<int>(new int(42))));
const auto mapped = std::move(result).map(
[](std::unique_ptr<int> i) -> int {
return *i;
});
BOOST_CHECK(!!mapped);
BOOST_CHECK(mapped.is_ok());
BOOST_CHECK(!mapped.is_err());
BOOST_CHECK_EQUAL(mapped.unwrap(), 42);
}
{
const toml::result<int, std::string> result(toml::err<std::string>("hoge"));
const auto mapped = result.map(
[](const int i) -> int {
return i * 2;
});
BOOST_CHECK(!mapped);
BOOST_CHECK(!mapped.is_ok());
BOOST_CHECK(mapped.is_err());
BOOST_CHECK_EQUAL(mapped.unwrap_err(), "hoge");
}
{
toml::result<std::unique_ptr<int>, std::string>
result(toml::err<std::string>("hoge"));
const auto mapped = std::move(result).map(
[](std::unique_ptr<int> i) -> int {
return *i;
});
BOOST_CHECK(!mapped);
BOOST_CHECK(!mapped.is_ok());
BOOST_CHECK(mapped.is_err());
BOOST_CHECK_EQUAL(mapped.unwrap_err(), "hoge");
}
}
BOOST_AUTO_TEST_CASE(test_map_err)
{
{
const toml::result<int, std::string> result(toml::ok(42));
const auto mapped = result.map_err(
[](const std::string s) -> std::string {
return s + s;
});
BOOST_CHECK(!!mapped);
BOOST_CHECK(mapped.is_ok());
BOOST_CHECK(!mapped.is_err());
BOOST_CHECK_EQUAL(mapped.unwrap(), 42);
}
{
toml::result<std::unique_ptr<int>, std::string>
result(toml::ok(std::unique_ptr<int>(new int(42))));
const auto mapped = std::move(result).map_err(
[](const std::string s) -> std::string {
return s + s;
});
BOOST_CHECK(!!mapped);
BOOST_CHECK(mapped.is_ok());
BOOST_CHECK(!mapped.is_err());
BOOST_CHECK_EQUAL(*(mapped.unwrap()), 42);
}
{
const toml::result<int, std::string> result(toml::err<std::string>("hoge"));
const auto mapped = result.map_err(
[](const std::string s) -> std::string {
return s + s;
});
BOOST_CHECK(!mapped);
BOOST_CHECK(!mapped.is_ok());
BOOST_CHECK(mapped.is_err());
BOOST_CHECK_EQUAL(mapped.unwrap_err(), "hogehoge");
}
{
toml::result<int, std::unique_ptr<std::string>>
result(toml::err(std::unique_ptr<std::string>(new std::string("hoge"))));
const auto mapped = std::move(result).map_err(
[](std::unique_ptr<std::string> p) -> std::string {
return *p;
});
BOOST_CHECK(!mapped);
BOOST_CHECK(!mapped.is_ok());
BOOST_CHECK(mapped.is_err());
BOOST_CHECK_EQUAL(mapped.unwrap_err(), "hoge");
}
}
BOOST_AUTO_TEST_CASE(test_map_or_else)
{
{
const toml::result<int, std::string> result(toml::ok(42));
const auto mapped = result.map_or_else(
[](const int i) -> int {
return i * 2;
}, 54);
BOOST_CHECK_EQUAL(mapped, 42 * 2);
}
{
toml::result<std::unique_ptr<int>, std::string>
result(toml::ok(std::unique_ptr<int>(new int(42))));
const auto mapped = std::move(result).map_or_else(
[](std::unique_ptr<int> i) -> int {
return *i;
}, 54);
BOOST_CHECK_EQUAL(mapped, 42);
}
{
const toml::result<int, std::string> result(toml::err<std::string>("hoge"));
const auto mapped = result.map_or_else(
[](const int i) -> int {
return i * 2;
}, 54);
BOOST_CHECK_EQUAL(mapped, 54);
}
{
toml::result<std::unique_ptr<int>, std::string>
result(toml::err<std::string>("hoge"));
const auto mapped = std::move(result).map_or_else(
[](std::unique_ptr<int> i) -> int {
return *i;
}, 54);
BOOST_CHECK_EQUAL(mapped, 54);
}
}
BOOST_AUTO_TEST_CASE(test_map_err_or_else)
{
{
const toml::result<int, std::string> result(toml::ok(42));
const auto mapped = result.map_err_or_else(
[](const std::string i) -> std::string {
return i + i;
}, "foobar");
BOOST_CHECK_EQUAL(mapped, "foobar");
}
{
toml::result<std::unique_ptr<int>, std::string>
result(toml::ok(std::unique_ptr<int>(new int(42))));
const auto mapped = std::move(result).map_err_or_else(
[](const std::string i) -> std::string {
return i + i;
}, "foobar");
BOOST_CHECK_EQUAL(mapped, "foobar");
}
{
const toml::result<int, std::string> result(toml::err<std::string>("hoge"));
const auto mapped = result.map_err_or_else(
[](const std::string i) -> std::string {
return i + i;
}, "foobar");
BOOST_CHECK_EQUAL(mapped, "hogehoge");
}
{
toml::result<std::unique_ptr<int>, std::string>
result(toml::err<std::string>("hoge"));
const auto mapped = result.map_err_or_else(
[](const std::string i) -> std::string {
return i + i;
}, "foobar");
BOOST_CHECK_EQUAL(mapped, "hogehoge");
}
}
BOOST_AUTO_TEST_CASE(test_and_then)
{
{
const toml::result<int, std::string> result(toml::ok(42));
const auto mapped = result.and_then(
[](const int i) -> toml::result<int, std::string> {
return toml::ok(i * 2);
});
BOOST_CHECK(!!mapped);
BOOST_CHECK(mapped.is_ok());
BOOST_CHECK(!mapped.is_err());
BOOST_CHECK_EQUAL(mapped.unwrap(), 42 * 2);
}
{
toml::result<std::unique_ptr<int>, std::string>
result(toml::ok(std::unique_ptr<int>(new int(42))));
const auto mapped = std::move(result).and_then(
[](std::unique_ptr<int> i) -> toml::result<int, std::string> {
return toml::ok(*i);
});
BOOST_CHECK(!!mapped);
BOOST_CHECK(mapped.is_ok());
BOOST_CHECK(!mapped.is_err());
BOOST_CHECK_EQUAL(mapped.unwrap(), 42);
}
{
const toml::result<int, std::string> result(toml::err<std::string>("hoge"));
const auto mapped = result.and_then(
[](const int i) -> toml::result<int, std::string> {
return toml::ok(i * 2);
});
BOOST_CHECK(!mapped);
BOOST_CHECK(!mapped.is_ok());
BOOST_CHECK(mapped.is_err());
BOOST_CHECK_EQUAL(mapped.unwrap_err(), "hoge");
}
{
toml::result<std::unique_ptr<int>, std::string>
result(toml::err<std::string>("hoge"));
const auto mapped = std::move(result).and_then(
[](std::unique_ptr<int> i) -> toml::result<int, std::string> {
return toml::ok(*i);
});
BOOST_CHECK(!mapped);
BOOST_CHECK(!mapped.is_ok());
BOOST_CHECK(mapped.is_err());
BOOST_CHECK_EQUAL(mapped.unwrap_err(), "hoge");
}
}
BOOST_AUTO_TEST_CASE(test_or_else)
{
{
const toml::result<int, std::string> result(toml::ok(42));
const auto mapped = result.or_else(
[](const std::string& s) -> toml::result<int, std::string> {
return toml::err(s + s);
});
BOOST_CHECK(!!mapped);
BOOST_CHECK(mapped.is_ok());
BOOST_CHECK(!mapped.is_err());
BOOST_CHECK_EQUAL(mapped.unwrap(), 42);
}
{
toml::result<std::unique_ptr<int>, std::string>
result(toml::ok(std::unique_ptr<int>(new int(42))));
const auto mapped = std::move(result).or_else(
[](const std::string& s) -> toml::result<std::unique_ptr<int>, std::string> {
return toml::err(s + s);
});
BOOST_CHECK(!!mapped);
BOOST_CHECK(mapped.is_ok());
BOOST_CHECK(!mapped.is_err());
BOOST_CHECK_EQUAL(*mapped.unwrap(), 42);
}
{
const toml::result<int, std::string> result(toml::err<std::string>("hoge"));
const auto mapped = result.or_else(
[](const std::string& s) -> toml::result<int, std::string> {
return toml::err(s + s);
});
BOOST_CHECK(!mapped);
BOOST_CHECK(!mapped.is_ok());
BOOST_CHECK(mapped.is_err());
BOOST_CHECK_EQUAL(mapped.unwrap_err(), "hogehoge");
}
{
toml::result<std::unique_ptr<int>, std::string>
result(toml::err<std::string>("hoge"));
const auto mapped = std::move(result).or_else(
[](const std::string& s) -> toml::result<std::unique_ptr<int>, std::string> {
return toml::err(s + s);
});
BOOST_CHECK(!mapped);
BOOST_CHECK(!mapped.is_ok());
BOOST_CHECK(mapped.is_err());
BOOST_CHECK_EQUAL(mapped.unwrap_err(), "hogehoge");
}
}

View File

@@ -9,97 +9,181 @@
#include <map>
#include <list>
BOOST_AUTO_TEST_CASE(test_to_toml_exact)
BOOST_AUTO_TEST_CASE(test_value_boolean)
{
toml::Boolean b(true);
toml::Integer i(42);
toml::Float f(3.14);
toml::String s("hoge");
toml::Datetime d(std::chrono::system_clock::now());
toml::Array a;
a.emplace_back(2);
a.emplace_back(7);
a.emplace_back(1);
a.emplace_back(8);
a.emplace_back(2);
toml::Table t;
t.emplace("val1", true);
t.emplace("val2", 42);
t.emplace("val3", 3.14);
t.emplace("val4", "piyo");
auto v1 = toml::to_toml(b);
auto v2 = toml::to_toml(i);
auto v3 = toml::to_toml(f);
auto v4 = toml::to_toml(s);
auto v5 = toml::to_toml(d);
auto v6 = toml::to_toml(a);
auto v7 = toml::to_toml(t);
toml::value v1 = toml::to_toml(true);
toml::value v2 = toml::to_toml(false);
BOOST_CHECK_EQUAL(v1.type(), toml::value_t::Boolean);
BOOST_CHECK_EQUAL(v2.type(), toml::value_t::Integer);
BOOST_CHECK_EQUAL(v3.type(), toml::value_t::Float);
BOOST_CHECK_EQUAL(v4.type(), toml::value_t::String);
BOOST_CHECK_EQUAL(v5.type(), toml::value_t::Datetime);
BOOST_CHECK_EQUAL(v6.type(), toml::value_t::Array);
BOOST_CHECK_EQUAL(v7.type(), toml::value_t::Table);
BOOST_CHECK_EQUAL(v2.type(), toml::value_t::Boolean);
BOOST_CHECK(v1.is(toml::value_t::Boolean));
BOOST_CHECK(v2.is(toml::value_t::Boolean));
BOOST_CHECK(v1.is<toml::Boolean>());
BOOST_CHECK(v2.is<toml::Boolean>());
BOOST_CHECK_EQUAL(v1.cast<toml::value_t::Boolean >(), b);
BOOST_CHECK_EQUAL(v2.cast<toml::value_t::Integer >(), i);
BOOST_CHECK_EQUAL(v3.cast<toml::value_t::Float >(), f);
BOOST_CHECK_EQUAL(v4.cast<toml::value_t::String >(), s);
const auto& ar = v6.cast<toml::value_t::Array>();
BOOST_CHECK_EQUAL(ar.at(0).cast<toml::value_t::Integer>(), a.at(0).cast<toml::value_t::Integer>());
BOOST_CHECK_EQUAL(ar.at(1).cast<toml::value_t::Integer>(), a.at(1).cast<toml::value_t::Integer>());
BOOST_CHECK_EQUAL(ar.at(2).cast<toml::value_t::Integer>(), a.at(2).cast<toml::value_t::Integer>());
BOOST_CHECK_EQUAL(ar.at(3).cast<toml::value_t::Integer>(), a.at(3).cast<toml::value_t::Integer>());
BOOST_CHECK_EQUAL(ar.at(4).cast<toml::value_t::Integer>(), a.at(4).cast<toml::value_t::Integer>());
BOOST_CHECK_EQUAL(v1.cast<toml::value_t::Boolean>(), true);
BOOST_CHECK_EQUAL(v2.cast<toml::value_t::Boolean>(), false);
}
BOOST_AUTO_TEST_CASE(test_to_toml_castable)
BOOST_AUTO_TEST_CASE(test_value_integer)
{
auto v1 = toml::to_toml(true);
auto v2 = toml::to_toml(42ul);
auto v3 = toml::to_toml(3.14f);
auto v4 = toml::to_toml("hoge");
toml::value v1 = toml::to_toml(-42);
toml::value v2 = toml::to_toml(42u);
BOOST_CHECK_EQUAL(v1.type(), toml::value_t::Boolean);
BOOST_CHECK_EQUAL(v1.type(), toml::value_t::Integer);
BOOST_CHECK_EQUAL(v2.type(), toml::value_t::Integer);
BOOST_CHECK_EQUAL(v3.type(), toml::value_t::Float);
BOOST_CHECK_EQUAL(v4.type(), toml::value_t::String);
BOOST_CHECK(v1.is(toml::value_t::Integer));
BOOST_CHECK(v2.is(toml::value_t::Integer));
BOOST_CHECK(v1.is<toml::Integer>());
BOOST_CHECK(v2.is<toml::Integer>());
BOOST_CHECK_EQUAL(v1.cast<toml::value_t::Boolean >(), true);
BOOST_CHECK_EQUAL(v2.cast<toml::value_t::Integer >(), 42);
BOOST_CHECK_CLOSE_FRACTION(v3.cast<toml::value_t::Float >(), 3.14, 1e-5);
BOOST_CHECK_EQUAL(v4.cast<toml::value_t::String >(), "hoge");
BOOST_CHECK_EQUAL(v1.cast<toml::value_t::Integer>(), -42);
BOOST_CHECK_EQUAL(v2.cast<toml::value_t::Integer>(), 42u);
}
BOOST_AUTO_TEST_CASE(test_to_toml_initializer_list)
BOOST_AUTO_TEST_CASE(test_value_float)
{
toml::value v1 = toml::to_toml({3,1,4,1,5});
toml::value v2 = toml::to_toml({{"hoge", 1}, {"piyo", 3.14}, {"fuga", "string"}});
toml::value v1 = toml::to_toml(3.14);
toml::value v2 = toml::to_toml(3.14f);
BOOST_CHECK_EQUAL(v1.type(), toml::value_t::Float);
BOOST_CHECK_EQUAL(v2.type(), toml::value_t::Float);
BOOST_CHECK(v1.is(toml::value_t::Float));
BOOST_CHECK(v2.is(toml::value_t::Float));
BOOST_CHECK(v1.is<toml::Float>());
BOOST_CHECK(v2.is<toml::Float>());
BOOST_CHECK_EQUAL(v1.cast<toml::value_t::Float>(), 3.14);
BOOST_CHECK_CLOSE_FRACTION(v2.cast<toml::value_t::Float>(), 3.14, 1e-2);
}
BOOST_AUTO_TEST_CASE(test_value_string)
{
toml::value v1 = toml::to_toml(std::string("foo"));
toml::value v2 = toml::to_toml(std::string("foo"), toml::string_t::literal);
toml::value v3 = toml::to_toml("foo");
BOOST_CHECK_EQUAL(v1.type(), toml::value_t::String);
BOOST_CHECK_EQUAL(v2.type(), toml::value_t::String);
BOOST_CHECK_EQUAL(v3.type(), toml::value_t::String);
BOOST_CHECK(v1.is(toml::value_t::String));
BOOST_CHECK(v2.is(toml::value_t::String));
BOOST_CHECK(v3.is(toml::value_t::String));
BOOST_CHECK(v1.is<toml::String>());
BOOST_CHECK(v2.is<toml::String>());
BOOST_CHECK(v3.is<toml::String>());
BOOST_CHECK_EQUAL(v1.cast<toml::value_t::String>(), "foo");
BOOST_CHECK_EQUAL(v2.cast<toml::value_t::String>(), "foo");
BOOST_CHECK_EQUAL(v3.cast<toml::value_t::String>(), "foo");
}
BOOST_AUTO_TEST_CASE(test_value_local_date)
{
toml::value v1 = toml::to_toml(toml::local_date(2018, toml::month_t::Jan, 31));
BOOST_CHECK_EQUAL(v1.type(), toml::value_t::LocalDate);
BOOST_CHECK(v1.is(toml::value_t::LocalDate));
BOOST_CHECK(v1.is<toml::LocalDate>());
BOOST_CHECK_EQUAL(v1.cast<toml::value_t::LocalDate>(),
toml::local_date(2018, toml::month_t::Jan, 31));
}
BOOST_AUTO_TEST_CASE(test_value_local_time)
{
toml::value v1 = toml::to_toml(toml::local_time(12, 30, 45));
toml::value v2 = toml::to_toml(std::chrono::hours(12) + std::chrono::minutes(30) +
std::chrono::seconds(45));
BOOST_CHECK_EQUAL(v1.type(), toml::value_t::LocalTime);
BOOST_CHECK_EQUAL(v2.type(), toml::value_t::LocalTime);
BOOST_CHECK(v1.is(toml::value_t::LocalTime));
BOOST_CHECK(v2.is(toml::value_t::LocalTime));
BOOST_CHECK(v1.is<toml::LocalTime>());
BOOST_CHECK(v2.is<toml::LocalTime>());
BOOST_CHECK_EQUAL(v1.cast<toml::value_t::LocalTime>(),
toml::local_time(12, 30, 45));
BOOST_CHECK_EQUAL(v2.cast<toml::value_t::LocalTime>(),
toml::local_time(12, 30, 45));
BOOST_CHECK_EQUAL(v1.cast<toml::value_t::LocalTime>(),
v2.cast<toml::value_t::LocalTime>());
}
BOOST_AUTO_TEST_CASE(test_value_local_datetime)
{
toml::value v1 = toml::to_toml(toml::local_datetime(
toml::local_date(2018, toml::month_t::Jan, 31),
toml::local_time(12, 30, 45)
));
BOOST_CHECK_EQUAL(v1.type(), toml::value_t::LocalDatetime);
BOOST_CHECK(v1.is(toml::value_t::LocalDatetime));
BOOST_CHECK(v1.is<toml::LocalDatetime>());
BOOST_CHECK_EQUAL(v1.cast<toml::value_t::LocalDatetime>(),
toml::local_datetime(
toml::local_date(2018, toml::month_t::Jan, 31),
toml::local_time(12, 30, 45)));
}
BOOST_AUTO_TEST_CASE(test_value_offset_datetime)
{
toml::value v1 = toml::to_toml(toml::offset_datetime(
toml::local_date(2018, toml::month_t::Jan, 31),
toml::local_time(12, 30, 45),
toml::time_offset(9, 0)
));
BOOST_CHECK_EQUAL(v1.type(), toml::value_t::OffsetDatetime);
BOOST_CHECK(v1.is(toml::value_t::OffsetDatetime));
BOOST_CHECK(v1.is<toml::OffsetDatetime>());
BOOST_CHECK_EQUAL(v1.cast<toml::value_t::OffsetDatetime>(),
toml::offset_datetime(
toml::local_date(2018, toml::month_t::Jan, 31),
toml::local_time(12, 30, 45),
toml::time_offset(9, 0)
));
}
BOOST_AUTO_TEST_CASE(test_value_array)
{
std::vector<int> v{1,2,3,4,5};
toml::value v1 = toml::to_toml(v);
toml::value v2 = toml::to_toml(6,7,8,9,0);
BOOST_CHECK_EQUAL(v1.type(), toml::value_t::Array);
BOOST_CHECK_EQUAL(v2.type(), toml::value_t::Table);
BOOST_CHECK(v1.is(toml::value_t::Array));
BOOST_CHECK(v1.is<toml::Array>());
const auto& ar = v1.cast<toml::value_t::Array>();
BOOST_CHECK_EQUAL(ar.at(0).cast<toml::value_t::Integer>(), 3);
BOOST_CHECK_EQUAL(ar.at(1).cast<toml::value_t::Integer>(), 1);
BOOST_CHECK_EQUAL(ar.at(2).cast<toml::value_t::Integer>(), 4);
BOOST_CHECK_EQUAL(ar.at(3).cast<toml::value_t::Integer>(), 1);
BOOST_CHECK_EQUAL(ar.at(4).cast<toml::value_t::Integer>(), 5);
BOOST_CHECK_EQUAL(v2.type(), toml::value_t::Array);
BOOST_CHECK(v2.is(toml::value_t::Array));
BOOST_CHECK(v2.is<toml::Array>());
const auto& tb = v2.cast<toml::value_t::Table>();
BOOST_CHECK_EQUAL(tb.at("hoge").type(), toml::value_t::Integer);
BOOST_CHECK_EQUAL(tb.at("piyo").type(), toml::value_t::Float);
BOOST_CHECK_EQUAL(tb.at("fuga").type(), toml::value_t::String);
BOOST_CHECK_EQUAL(tb.at("hoge").cast<toml::value_t::Integer>(), 1);
BOOST_CHECK_CLOSE_FRACTION(tb.at("piyo").cast<toml::value_t::Float>(), 3.14, 1e-3);
BOOST_CHECK_EQUAL(tb.at("fuga").cast<toml::value_t::String>(), "string");
BOOST_CHECK_EQUAL(v1.cast<toml::value_t::Array>().at(0).cast<toml::value_t::Integer>(), 1);
BOOST_CHECK_EQUAL(v1.cast<toml::value_t::Array>().at(1).cast<toml::value_t::Integer>(), 2);
BOOST_CHECK_EQUAL(v1.cast<toml::value_t::Array>().at(2).cast<toml::value_t::Integer>(), 3);
BOOST_CHECK_EQUAL(v1.cast<toml::value_t::Array>().at(3).cast<toml::value_t::Integer>(), 4);
BOOST_CHECK_EQUAL(v1.cast<toml::value_t::Array>().at(4).cast<toml::value_t::Integer>(), 5);
BOOST_CHECK_EQUAL(v2.cast<toml::value_t::Array>().at(0).cast<toml::value_t::Integer>(), 6);
BOOST_CHECK_EQUAL(v2.cast<toml::value_t::Array>().at(1).cast<toml::value_t::Integer>(), 7);
BOOST_CHECK_EQUAL(v2.cast<toml::value_t::Array>().at(2).cast<toml::value_t::Integer>(), 8);
BOOST_CHECK_EQUAL(v2.cast<toml::value_t::Array>().at(3).cast<toml::value_t::Integer>(), 9);
BOOST_CHECK_EQUAL(v2.cast<toml::value_t::Array>().at(4).cast<toml::value_t::Integer>(), 0);
}
BOOST_AUTO_TEST_CASE(test_value_table)
{
toml::value v1 = toml::to_toml({{"foo", 42}, {"bar", 3.14}, {"baz", "qux"}});
BOOST_CHECK_EQUAL(v1.type(), toml::value_t::Table);
BOOST_CHECK(v1.is(toml::value_t::Table));
BOOST_CHECK(v1.is<toml::Table>());
BOOST_CHECK_EQUAL(v1.cast<toml::value_t::Table>().at("foo").cast<toml::value_t::Integer>(), 42);
BOOST_CHECK_EQUAL(v1.cast<toml::value_t::Table>().at("bar").cast<toml::value_t::Float>(), 3.14);
BOOST_CHECK_EQUAL(v1.cast<toml::value_t::Table>().at("baz").cast<toml::value_t::String>().str, "qux");
}

View File

@@ -5,7 +5,7 @@
#define BOOST_TEST_NO_LIB
#include <boost/test/included/unit_test.hpp>
#endif
#include <toml.hpp>
#include <toml/types.hpp>
#include <list>
#include <forward_list>
@@ -74,16 +74,11 @@ BOOST_AUTO_TEST_CASE(test_is_xxx)
BOOST_CHECK(toml::detail::is_container<std_array_type>::value);
BOOST_CHECK(toml::detail::is_container<std::set<dummy_type>>::value);
BOOST_CHECK(toml::detail::is_container<std::unordered_set<std::string>>::value);
BOOST_CHECK(toml::detail::is_container<std_map_type>::value);
BOOST_CHECK(toml::detail::is_container<std_unordered_map_type>::value);
BOOST_CHECK(toml::detail::is_container<dummy_container<dummy_type>>::value);
BOOST_CHECK(!toml::detail::is_container<std_map_type>::value);
BOOST_CHECK(!toml::detail::is_container<std_unordered_map_type>::value);
BOOST_CHECK(toml::detail::is_map<std_map_type>::value);
BOOST_CHECK(toml::detail::is_map<std_unordered_map_type>::value);
BOOST_CHECK(toml::detail::is_key_convertible<std_map_type>::value);
BOOST_CHECK(toml::detail::is_key_convertible<std_unordered_map_type>::value);
}

View File

@@ -30,7 +30,7 @@ BOOST_AUTO_TEST_CASE(test_resize)
thrown = true;
}
BOOST_CHECK(!thrown);
BOOST_CHECK_EQUAL(v.size(), 10);
BOOST_CHECK_EQUAL(v.size(), 10u);
}
{
@@ -45,7 +45,7 @@ BOOST_AUTO_TEST_CASE(test_resize)
thrown = true;
}
BOOST_CHECK(!thrown);
BOOST_CHECK_EQUAL(a.size(), 15);
BOOST_CHECK_EQUAL(a.size(), 15u);
}
{
@@ -62,3 +62,21 @@ BOOST_AUTO_TEST_CASE(test_resize)
BOOST_CHECK(thrown);
}
}
BOOST_AUTO_TEST_CASE(test_concat_to_string)
{
const std::string cat = toml::concat_to_string("foo", "bar", 42);
BOOST_CHECK(cat == "foobar42");
}
BOOST_AUTO_TEST_CASE(test_from_string)
{
{
const std::string str("123");
BOOST_CHECK_EQUAL(toml::from_string<int>(str, 0), 123);
}
{
const std::string str("01");
BOOST_CHECK_EQUAL(toml::from_string<int>(str, 0), 1);
}
}

View File

@@ -5,223 +5,604 @@
#define BOOST_TEST_NO_LIB
#include <boost/test/included/unit_test.hpp>
#endif
#include <toml.hpp>
#include <toml/value.hpp>
#include <map>
#include <list>
BOOST_AUTO_TEST_CASE(test_value_exact_constructor)
BOOST_AUTO_TEST_CASE(test_value_boolean)
{
toml::Boolean b(true);
toml::Integer i(42);
toml::Float f(3.14);
toml::String s("hoge");
toml::Datetime d(std::chrono::system_clock::now());
toml::Array a;
a.emplace_back(2);
a.emplace_back(7);
a.emplace_back(1);
a.emplace_back(8);
a.emplace_back(2);
toml::Table t;
t.emplace("val1", true);
t.emplace("val2", 42);
t.emplace("val3", 3.14);
t.emplace("val4", "piyo");
toml::value v1(b);
toml::value v2(i);
toml::value v3(f);
toml::value v4(s);
toml::value v5(d);
toml::value v6(a);
toml::value v7(t);
toml::value v1(true);
toml::value v2(false);
BOOST_CHECK_EQUAL(v1.type(), toml::value_t::Boolean);
BOOST_CHECK_EQUAL(v2.type(), toml::value_t::Boolean);
BOOST_CHECK(v1.is(toml::value_t::Boolean));
BOOST_CHECK(v2.is(toml::value_t::Boolean));
BOOST_CHECK(v1.is<toml::Boolean>());
BOOST_CHECK(v2.is<toml::Boolean>());
BOOST_CHECK_EQUAL(v1.cast<toml::value_t::Boolean>(), true);
BOOST_CHECK_EQUAL(v2.cast<toml::value_t::Boolean>(), false);
v1 = false;
v2 = true;
BOOST_CHECK_EQUAL(v1.type(), toml::value_t::Boolean);
BOOST_CHECK_EQUAL(v2.type(), toml::value_t::Boolean);
BOOST_CHECK(v1.is(toml::value_t::Boolean));
BOOST_CHECK(v2.is(toml::value_t::Boolean));
BOOST_CHECK(v1.is<toml::Boolean>());
BOOST_CHECK(v2.is<toml::Boolean>());
BOOST_CHECK_EQUAL(v1.cast<toml::value_t::Boolean>(), false);
BOOST_CHECK_EQUAL(v2.cast<toml::value_t::Boolean>(), true);
toml::value v3(v1);
toml::value v4(v2);
BOOST_CHECK(v3 == v1);
BOOST_CHECK(v4 == v2);
BOOST_CHECK_EQUAL(v3.type(), toml::value_t::Boolean);
BOOST_CHECK_EQUAL(v4.type(), toml::value_t::Boolean);
BOOST_CHECK(v3.is(toml::value_t::Boolean));
BOOST_CHECK(v4.is(toml::value_t::Boolean));
BOOST_CHECK(v3.is<toml::Boolean>());
BOOST_CHECK(v4.is<toml::Boolean>());
BOOST_CHECK_EQUAL(v3.cast<toml::value_t::Boolean>(), false);
BOOST_CHECK_EQUAL(v4.cast<toml::value_t::Boolean>(), true);
toml::value v5(std::move(v1));
toml::value v6(std::move(v2));
BOOST_CHECK_EQUAL(v5.type(), toml::value_t::Boolean);
BOOST_CHECK_EQUAL(v6.type(), toml::value_t::Boolean);
BOOST_CHECK(v5.is(toml::value_t::Boolean));
BOOST_CHECK(v6.is(toml::value_t::Boolean));
BOOST_CHECK(v5.is<toml::Boolean>());
BOOST_CHECK(v6.is<toml::Boolean>());
BOOST_CHECK_EQUAL(v5.cast<toml::value_t::Boolean>(), false);
BOOST_CHECK_EQUAL(v6.cast<toml::value_t::Boolean>(), true);
v1 = 42;
v2 = 3.14;
BOOST_CHECK_EQUAL(v1.type(), toml::value_t::Integer);
BOOST_CHECK_EQUAL(v2.type(), toml::value_t::Float);
BOOST_CHECK(v1.is(toml::value_t::Integer));
BOOST_CHECK(v2.is(toml::value_t::Float));
BOOST_CHECK(v1.is<toml::Integer>());
BOOST_CHECK(v2.is<toml::Float>());
BOOST_CHECK_EQUAL(v1.cast<toml::value_t::Integer>(), 42);
BOOST_CHECK_EQUAL(v2.cast<toml::value_t::Float>(), 3.14);
}
BOOST_AUTO_TEST_CASE(test_value_integer)
{
toml::value v1(-42);
toml::value v2(42u);
BOOST_CHECK_EQUAL(v1.type(), toml::value_t::Integer);
BOOST_CHECK_EQUAL(v2.type(), toml::value_t::Integer);
BOOST_CHECK_EQUAL(v3.type(), toml::value_t::Float);
BOOST_CHECK_EQUAL(v4.type(), toml::value_t::String);
BOOST_CHECK_EQUAL(v5.type(), toml::value_t::Datetime);
BOOST_CHECK_EQUAL(v6.type(), toml::value_t::Array);
BOOST_CHECK_EQUAL(v7.type(), toml::value_t::Table);
BOOST_CHECK(v1.is(toml::value_t::Integer));
BOOST_CHECK(v2.is(toml::value_t::Integer));
BOOST_CHECK(v1.is<toml::Integer>());
BOOST_CHECK(v2.is<toml::Integer>());
BOOST_CHECK_EQUAL(v1.cast<toml::value_t::Boolean >(), b);
BOOST_CHECK_EQUAL(v2.cast<toml::value_t::Integer >(), i);
BOOST_CHECK_EQUAL(v3.cast<toml::value_t::Float >(), f);
BOOST_CHECK_EQUAL(v4.cast<toml::value_t::String >(), s);
const auto& ar = v6.cast<toml::value_t::Array>();
BOOST_CHECK_EQUAL(ar.at(0).cast<toml::value_t::Integer>(), a.at(0).cast<toml::value_t::Integer>());
BOOST_CHECK_EQUAL(ar.at(1).cast<toml::value_t::Integer>(), a.at(1).cast<toml::value_t::Integer>());
BOOST_CHECK_EQUAL(ar.at(2).cast<toml::value_t::Integer>(), a.at(2).cast<toml::value_t::Integer>());
BOOST_CHECK_EQUAL(ar.at(3).cast<toml::value_t::Integer>(), a.at(3).cast<toml::value_t::Integer>());
BOOST_CHECK_EQUAL(ar.at(4).cast<toml::value_t::Integer>(), a.at(4).cast<toml::value_t::Integer>());
}
BOOST_CHECK_EQUAL(v1.cast<toml::value_t::Integer>(), -42);
BOOST_CHECK_EQUAL(v2.cast<toml::value_t::Integer>(), 42u);
BOOST_AUTO_TEST_CASE(test_value_convertible_constructor)
{
int i(42);
float f(3.14);
const char* s = "hoge";
toml::value v1(i);
toml::value v2(f);
toml::value v3(s);
v1 = 54;
v2 = -54;
BOOST_CHECK_EQUAL(v1.type(), toml::value_t::Integer);
BOOST_CHECK_EQUAL(v2.type(), toml::value_t::Float);
BOOST_CHECK_EQUAL(v3.type(), toml::value_t::String);
BOOST_CHECK_EQUAL(v2.type(), toml::value_t::Integer);
BOOST_CHECK(v1.is(toml::value_t::Integer));
BOOST_CHECK(v2.is(toml::value_t::Integer));
BOOST_CHECK(v1.is<toml::Integer>());
BOOST_CHECK(v2.is<toml::Integer>());
BOOST_CHECK_EQUAL(v1.cast<toml::value_t::Integer >(), i);
BOOST_CHECK_EQUAL(v2.cast<toml::value_t::Float >(), f);
BOOST_CHECK_EQUAL(v3.cast<toml::value_t::String >(), s);
BOOST_CHECK_EQUAL(v1.cast<toml::value_t::Integer>(), 54);
BOOST_CHECK_EQUAL(v2.cast<toml::value_t::Integer>(), -54);
toml::value v3(v1);
toml::value v4(v2);
BOOST_CHECK(v3 == v1);
BOOST_CHECK(v4 == v2);
BOOST_CHECK_EQUAL(v3.type(), toml::value_t::Integer);
BOOST_CHECK_EQUAL(v4.type(), toml::value_t::Integer);
BOOST_CHECK(v3.is(toml::value_t::Integer));
BOOST_CHECK(v4.is(toml::value_t::Integer));
BOOST_CHECK(v3.is<toml::Integer>());
BOOST_CHECK(v4.is<toml::Integer>());
BOOST_CHECK_EQUAL(v3.cast<toml::value_t::Integer>(), 54);
BOOST_CHECK_EQUAL(v4.cast<toml::value_t::Integer>(), -54);
toml::value v5(std::move(v1));
toml::value v6(std::move(v2));
BOOST_CHECK_EQUAL(v5.type(), toml::value_t::Integer);
BOOST_CHECK_EQUAL(v6.type(), toml::value_t::Integer);
BOOST_CHECK(v5.is(toml::value_t::Integer));
BOOST_CHECK(v6.is(toml::value_t::Integer));
BOOST_CHECK(v5.is<toml::Integer>());
BOOST_CHECK(v6.is<toml::Integer>());
BOOST_CHECK_EQUAL(v5.cast<toml::value_t::Integer>(), 54);
BOOST_CHECK_EQUAL(v6.cast<toml::value_t::Integer>(), -54);
v1 = true;
v2 = false;
BOOST_CHECK_EQUAL(v1.type(), toml::value_t::Boolean);
BOOST_CHECK_EQUAL(v2.type(), toml::value_t::Boolean);
BOOST_CHECK(v1.is(toml::value_t::Boolean));
BOOST_CHECK(v2.is(toml::value_t::Boolean));
BOOST_CHECK(v1.is<toml::Boolean>());
BOOST_CHECK(v2.is<toml::Boolean>());
BOOST_CHECK_EQUAL(v1.cast<toml::value_t::Boolean>(), true);
BOOST_CHECK_EQUAL(v2.cast<toml::value_t::Boolean>(), false);
}
BOOST_AUTO_TEST_CASE(test_value_copy_move_constructor)
BOOST_AUTO_TEST_CASE(test_value_float)
{
toml::Array a;
toml::Table t;
toml::value v1(true);
toml::value v2(42);
toml::value v3(3.14);
toml::value v4("hoge");
toml::value v5(std::chrono::system_clock::now());
toml::value v6(a);
toml::value v7(t);
toml::value u1(v1);
toml::value u2(v2);
toml::value u3(v3);
toml::value u4(v4);
toml::value u5(v5);
toml::value u6(v6);
toml::value u7(v7);
BOOST_CHECK_EQUAL(u1.type(), toml::value_t::Boolean);
BOOST_CHECK_EQUAL(u2.type(), toml::value_t::Integer);
BOOST_CHECK_EQUAL(u3.type(), toml::value_t::Float);
BOOST_CHECK_EQUAL(u4.type(), toml::value_t::String);
BOOST_CHECK_EQUAL(u5.type(), toml::value_t::Datetime);
BOOST_CHECK_EQUAL(u6.type(), toml::value_t::Array);
BOOST_CHECK_EQUAL(u7.type(), toml::value_t::Table);
BOOST_CHECK_EQUAL(u1.cast<toml::value_t::Boolean >(), true);
BOOST_CHECK_EQUAL(u2.cast<toml::value_t::Integer >(), 42);
BOOST_CHECK_EQUAL(u3.cast<toml::value_t::Float >(), 3.14);
BOOST_CHECK_EQUAL(u4.cast<toml::value_t::String >(), "hoge");
toml::value w1(std::move(v1));
toml::value w2(std::move(v2));
toml::value w3(std::move(v3));
toml::value w4(std::move(v4));
toml::value w5(std::move(v5));
toml::value w6(std::move(v6));
toml::value w7(std::move(v7));
BOOST_CHECK_EQUAL(w1.type(), toml::value_t::Boolean);
BOOST_CHECK_EQUAL(w2.type(), toml::value_t::Integer);
BOOST_CHECK_EQUAL(w3.type(), toml::value_t::Float);
BOOST_CHECK_EQUAL(w4.type(), toml::value_t::String);
BOOST_CHECK_EQUAL(w5.type(), toml::value_t::Datetime);
BOOST_CHECK_EQUAL(w6.type(), toml::value_t::Array);
BOOST_CHECK_EQUAL(w7.type(), toml::value_t::Table);
BOOST_CHECK_EQUAL(w1.cast<toml::value_t::Boolean >(), true);
BOOST_CHECK_EQUAL(w2.cast<toml::value_t::Integer >(), 42);
BOOST_CHECK_EQUAL(w3.cast<toml::value_t::Float >(), 3.14);
BOOST_CHECK_EQUAL(w4.cast<toml::value_t::String >(), "hoge");
}
BOOST_AUTO_TEST_CASE(test_value_copy_move_substitution)
{
toml::Boolean b(true);
toml::Integer i(42);
toml::Float f(3.14);
toml::String s("hoge");
toml::Datetime d(std::chrono::system_clock::now());
toml::Array a;
a.emplace_back(2);
a.emplace_back(7);
a.emplace_back(1);
a.emplace_back(8);
a.emplace_back(2);
toml::Table t;
t.emplace("val1", true);
t.emplace("val2", 42);
t.emplace("val3", 3.14);
t.emplace("val4", "piyo");
toml::value v1(b);
toml::value v2(i);
toml::value v3(f);
toml::value v4(s);
toml::value v5(d);
toml::value v6(a);
toml::value v7(t);
v1 = i;
v2 = f;
v3 = s;
v4 = d;
v5 = a;
v6 = t;
v7 = b;
BOOST_CHECK_EQUAL(v1.type(), toml::value_t::Integer);
BOOST_CHECK_EQUAL(v2.type(), toml::value_t::Float);
BOOST_CHECK_EQUAL(v3.type(), toml::value_t::String);
BOOST_CHECK_EQUAL(v4.type(), toml::value_t::Datetime);
BOOST_CHECK_EQUAL(v5.type(), toml::value_t::Array);
BOOST_CHECK_EQUAL(v6.type(), toml::value_t::Table);
BOOST_CHECK_EQUAL(v7.type(), toml::value_t::Boolean);
BOOST_CHECK_EQUAL(v7.cast<toml::value_t::Boolean >(), b);
BOOST_CHECK_EQUAL(v1.cast<toml::value_t::Integer >(), i);
BOOST_CHECK_EQUAL(v2.cast<toml::value_t::Float >(), f);
BOOST_CHECK_EQUAL(v3.cast<toml::value_t::String >(), s);
v1 = std::move(f);
v2 = std::move(s);
v3 = std::move(d);
v4 = std::move(a);
v5 = std::move(t);
v6 = std::move(b);
v7 = std::move(i);
toml::value v1(3.14);
toml::value v2(3.14f);
BOOST_CHECK_EQUAL(v1.type(), toml::value_t::Float);
BOOST_CHECK_EQUAL(v2.type(), toml::value_t::String);
BOOST_CHECK_EQUAL(v3.type(), toml::value_t::Datetime);
BOOST_CHECK_EQUAL(v4.type(), toml::value_t::Array);
BOOST_CHECK_EQUAL(v5.type(), toml::value_t::Table);
BOOST_CHECK_EQUAL(v6.type(), toml::value_t::Boolean);
BOOST_CHECK_EQUAL(v7.type(), toml::value_t::Integer);
BOOST_CHECK_EQUAL(v2.type(), toml::value_t::Float);
BOOST_CHECK(v1.is(toml::value_t::Float));
BOOST_CHECK(v2.is(toml::value_t::Float));
BOOST_CHECK(v1.is<toml::Float>());
BOOST_CHECK(v2.is<toml::Float>());
BOOST_CHECK_EQUAL(v6.cast<toml::value_t::Boolean >(), b);
BOOST_CHECK_EQUAL(v7.cast<toml::value_t::Integer >(), i);
BOOST_CHECK_EQUAL(v1.cast<toml::value_t::Float >(), f);
BOOST_CHECK_EQUAL(v2.cast<toml::value_t::String >(), s);
BOOST_CHECK_EQUAL(v1.cast<toml::value_t::Float>(), 3.14);
BOOST_CHECK_CLOSE_FRACTION(v2.cast<toml::value_t::Float>(), 3.14, 1e-2);
v1 = 2.718f;
v2 = 2.718;
BOOST_CHECK_EQUAL(v1.type(), toml::value_t::Float);
BOOST_CHECK_EQUAL(v2.type(), toml::value_t::Float);
BOOST_CHECK(v1.is(toml::value_t::Float));
BOOST_CHECK(v2.is(toml::value_t::Float));
BOOST_CHECK(v1.is<toml::Float>());
BOOST_CHECK(v2.is<toml::Float>());
BOOST_CHECK_CLOSE_FRACTION(v1.cast<toml::value_t::Float>(), 2.718, 1e-3);
BOOST_CHECK_EQUAL(v2.cast<toml::value_t::Float>(), 2.718);
toml::value v3(v1);
toml::value v4(v2);
BOOST_CHECK(v3 == v1);
BOOST_CHECK(v4 == v2);
BOOST_CHECK_EQUAL(v3.type(), toml::value_t::Float);
BOOST_CHECK_EQUAL(v4.type(), toml::value_t::Float);
BOOST_CHECK(v3.is(toml::value_t::Float));
BOOST_CHECK(v4.is(toml::value_t::Float));
BOOST_CHECK(v3.is<toml::Float>());
BOOST_CHECK(v4.is<toml::Float>());
BOOST_CHECK_CLOSE_FRACTION(v3.cast<toml::value_t::Float>(), 2.718, 1e-3);
BOOST_CHECK_EQUAL(v4.cast<toml::value_t::Float>(), 2.718);
toml::value v5(std::move(v1));
toml::value v6(std::move(v2));
BOOST_CHECK_EQUAL(v5.type(), toml::value_t::Float);
BOOST_CHECK_EQUAL(v6.type(), toml::value_t::Float);
BOOST_CHECK(v5.is(toml::value_t::Float));
BOOST_CHECK(v6.is(toml::value_t::Float));
BOOST_CHECK(v5.is<toml::Float>());
BOOST_CHECK(v6.is<toml::Float>());
BOOST_CHECK_CLOSE_FRACTION(v5.cast<toml::value_t::Float>(), 2.718, 1e-3);
BOOST_CHECK_EQUAL(v6.cast<toml::value_t::Float>(), 2.718);
v1 = true;
v2 = false;
BOOST_CHECK_EQUAL(v1.type(), toml::value_t::Boolean);
BOOST_CHECK_EQUAL(v2.type(), toml::value_t::Boolean);
BOOST_CHECK(v1.is(toml::value_t::Boolean));
BOOST_CHECK(v2.is(toml::value_t::Boolean));
BOOST_CHECK(v1.is<toml::Boolean>());
BOOST_CHECK(v2.is<toml::Boolean>());
BOOST_CHECK_EQUAL(v1.cast<toml::value_t::Boolean>(), true);
BOOST_CHECK_EQUAL(v2.cast<toml::value_t::Boolean>(), false);
}
BOOST_AUTO_TEST_CASE(test_value_initializer_list)
BOOST_AUTO_TEST_CASE(test_value_string)
{
toml::value v1{3,1,4,1,5};
toml::value v2{{"hoge", 1}, {"piyo", 3.14}, {"fuga", "string"}};
toml::value v1(std::string("foo"));
toml::value v2(std::string("foo"), toml::string_t::literal);
toml::value v3("foo");
BOOST_CHECK_EQUAL(v1.type(), toml::value_t::String);
BOOST_CHECK_EQUAL(v2.type(), toml::value_t::String);
BOOST_CHECK_EQUAL(v3.type(), toml::value_t::String);
BOOST_CHECK(v1.is(toml::value_t::String));
BOOST_CHECK(v2.is(toml::value_t::String));
BOOST_CHECK(v3.is(toml::value_t::String));
BOOST_CHECK(v1.is<toml::String>());
BOOST_CHECK(v2.is<toml::String>());
BOOST_CHECK(v3.is<toml::String>());
BOOST_CHECK_EQUAL(v1.cast<toml::value_t::String>(), "foo");
BOOST_CHECK_EQUAL(v2.cast<toml::value_t::String>(), "foo");
BOOST_CHECK_EQUAL(v3.cast<toml::value_t::String>(), "foo");
v1 = "bar";
v2 = "bar";
v3 = "bar";
BOOST_CHECK_EQUAL(v1.type(), toml::value_t::String);
BOOST_CHECK_EQUAL(v2.type(), toml::value_t::String);
BOOST_CHECK_EQUAL(v3.type(), toml::value_t::String);
BOOST_CHECK(v1.is(toml::value_t::String));
BOOST_CHECK(v2.is(toml::value_t::String));
BOOST_CHECK(v3.is(toml::value_t::String));
BOOST_CHECK(v1.is<toml::String>());
BOOST_CHECK(v2.is<toml::String>());
BOOST_CHECK(v3.is<toml::String>());
BOOST_CHECK_EQUAL(v1.cast<toml::value_t::String>(), "bar");
BOOST_CHECK_EQUAL(v2.cast<toml::value_t::String>(), "bar");
BOOST_CHECK_EQUAL(v3.cast<toml::value_t::String>(), "bar");
toml::value v4(v1);
toml::value v5(v2);
toml::value v6(v3);
BOOST_CHECK(v4 == v1);
BOOST_CHECK(v5 == v2);
BOOST_CHECK(v6 == v3);
BOOST_CHECK_EQUAL(v4.type(), toml::value_t::String);
BOOST_CHECK_EQUAL(v5.type(), toml::value_t::String);
BOOST_CHECK_EQUAL(v6.type(), toml::value_t::String);
BOOST_CHECK(v4.is(toml::value_t::String));
BOOST_CHECK(v5.is(toml::value_t::String));
BOOST_CHECK(v6.is(toml::value_t::String));
BOOST_CHECK(v4.is<toml::String>());
BOOST_CHECK(v5.is<toml::String>());
BOOST_CHECK(v6.is<toml::String>());
BOOST_CHECK_EQUAL(v4.cast<toml::value_t::String>(), "bar");
BOOST_CHECK_EQUAL(v5.cast<toml::value_t::String>(), "bar");
BOOST_CHECK_EQUAL(v6.cast<toml::value_t::String>(), "bar");
v4.cast<toml::value_t::String>().str.at(2) = 'z';
v5.cast<toml::value_t::String>().str.at(2) = 'z';
v6.cast<toml::value_t::String>().str.at(2) = 'z';
BOOST_CHECK_EQUAL(v4.type(), toml::value_t::String);
BOOST_CHECK_EQUAL(v5.type(), toml::value_t::String);
BOOST_CHECK_EQUAL(v6.type(), toml::value_t::String);
BOOST_CHECK(v4.is(toml::value_t::String));
BOOST_CHECK(v5.is(toml::value_t::String));
BOOST_CHECK(v6.is(toml::value_t::String));
BOOST_CHECK(v4.is<toml::String>());
BOOST_CHECK(v5.is<toml::String>());
BOOST_CHECK(v6.is<toml::String>());
BOOST_CHECK_EQUAL(v4.cast<toml::value_t::String>(), "baz");
BOOST_CHECK_EQUAL(v5.cast<toml::value_t::String>(), "baz");
BOOST_CHECK_EQUAL(v6.cast<toml::value_t::String>(), "baz");
v1 = true;
v2 = true;
v3 = true;
BOOST_CHECK_EQUAL(v1.type(), toml::value_t::Boolean);
BOOST_CHECK_EQUAL(v2.type(), toml::value_t::Boolean);
BOOST_CHECK_EQUAL(v3.type(), toml::value_t::Boolean);
BOOST_CHECK(v1.is(toml::value_t::Boolean));
BOOST_CHECK(v2.is(toml::value_t::Boolean));
BOOST_CHECK(v3.is(toml::value_t::Boolean));
BOOST_CHECK(v1.is<toml::Boolean>());
BOOST_CHECK(v2.is<toml::Boolean>());
BOOST_CHECK(v3.is<toml::Boolean>());
BOOST_CHECK_EQUAL(v1.cast<toml::value_t::Boolean>(), true);
BOOST_CHECK_EQUAL(v2.cast<toml::value_t::Boolean>(), true);
BOOST_CHECK_EQUAL(v3.cast<toml::value_t::Boolean>(), true);
}
BOOST_AUTO_TEST_CASE(test_value_local_date)
{
toml::value v1(toml::local_date(2018, toml::month_t::Jan, 31));
BOOST_CHECK_EQUAL(v1.type(), toml::value_t::LocalDate);
BOOST_CHECK(v1.is(toml::value_t::LocalDate));
BOOST_CHECK(v1.is<toml::LocalDate>());
BOOST_CHECK_EQUAL(v1.cast<toml::value_t::LocalDate>(),
toml::local_date(2018, toml::month_t::Jan, 31));
v1 = toml::local_date(2018, toml::month_t::Apr, 1);
BOOST_CHECK_EQUAL(v1.type(), toml::value_t::LocalDate);
BOOST_CHECK(v1.is(toml::value_t::LocalDate));
BOOST_CHECK(v1.is<toml::LocalDate>());
BOOST_CHECK_EQUAL(v1.cast<toml::value_t::LocalDate>(),
toml::local_date(2018, toml::month_t::Apr, 1));
toml::value v2(v1);
BOOST_CHECK(v2 == v1);
BOOST_CHECK_EQUAL(v2.type(), toml::value_t::LocalDate);
BOOST_CHECK(v2.is(toml::value_t::LocalDate));
BOOST_CHECK(v2.is<toml::LocalDate>());
BOOST_CHECK_EQUAL(v2.cast<toml::value_t::LocalDate>(),
toml::local_date(2018, toml::month_t::Apr, 1));
v1 = true;
BOOST_CHECK_EQUAL(v1.type(), toml::value_t::Boolean);
BOOST_CHECK(v1.is(toml::value_t::Boolean));
BOOST_CHECK(v1.is<toml::Boolean>());
BOOST_CHECK_EQUAL(v1.cast<toml::value_t::Boolean>(), true);
}
BOOST_AUTO_TEST_CASE(test_value_local_time)
{
toml::value v1(toml::local_time(12, 30, 45));
toml::value v2(std::chrono::hours(12) + std::chrono::minutes(30) +
std::chrono::seconds(45));
BOOST_CHECK_EQUAL(v1.type(), toml::value_t::LocalTime);
BOOST_CHECK_EQUAL(v2.type(), toml::value_t::LocalTime);
BOOST_CHECK(v1.is(toml::value_t::LocalTime));
BOOST_CHECK(v2.is(toml::value_t::LocalTime));
BOOST_CHECK(v1.is<toml::LocalTime>());
BOOST_CHECK(v2.is<toml::LocalTime>());
BOOST_CHECK_EQUAL(v1.cast<toml::value_t::LocalTime>(),
toml::local_time(12, 30, 45));
BOOST_CHECK_EQUAL(v2.cast<toml::value_t::LocalTime>(),
toml::local_time(12, 30, 45));
BOOST_CHECK_EQUAL(v1.cast<toml::value_t::LocalTime>(),
v2.cast<toml::value_t::LocalTime>());
v1 = toml::local_time(1, 30, 0, /*ms*/ 100, /*us*/ 0);
BOOST_CHECK_EQUAL(v1.type(), toml::value_t::LocalTime);
BOOST_CHECK(v1.is(toml::value_t::LocalTime));
BOOST_CHECK(v1.is<toml::LocalTime>());
BOOST_CHECK_EQUAL(v1.cast<toml::value_t::LocalTime>(),
toml::local_time(1, 30, 0, 100, 0));
toml::value v3(v1);
BOOST_CHECK(v3 == v1);
BOOST_CHECK_EQUAL(v3.type(), toml::value_t::LocalTime);
BOOST_CHECK(v3.is(toml::value_t::LocalTime));
BOOST_CHECK(v3.is<toml::LocalTime>());
BOOST_CHECK_EQUAL(v3.cast<toml::value_t::LocalTime>(),
toml::local_time(1, 30, 0, 100, 0));
v1 = true;
BOOST_CHECK_EQUAL(v1.type(), toml::value_t::Boolean);
BOOST_CHECK(v1.is(toml::value_t::Boolean));
BOOST_CHECK(v1.is<toml::Boolean>());
BOOST_CHECK_EQUAL(v1.cast<toml::value_t::Boolean>(), true);
}
BOOST_AUTO_TEST_CASE(test_value_local_datetime)
{
toml::value v1(toml::local_datetime(
toml::local_date(2018, toml::month_t::Jan, 31),
toml::local_time(12, 30, 45)
));
BOOST_CHECK_EQUAL(v1.type(), toml::value_t::LocalDatetime);
BOOST_CHECK(v1.is(toml::value_t::LocalDatetime));
BOOST_CHECK(v1.is<toml::LocalDatetime>());
BOOST_CHECK_EQUAL(v1.cast<toml::value_t::LocalDatetime>(),
toml::local_datetime(
toml::local_date(2018, toml::month_t::Jan, 31),
toml::local_time(12, 30, 45)));
v1 = toml::local_datetime(
toml::local_date(2018, toml::month_t::Apr, 1),
toml::local_time(1, 15, 30));
BOOST_CHECK_EQUAL(v1.type(), toml::value_t::LocalDatetime);
BOOST_CHECK(v1.is(toml::value_t::LocalDatetime));
BOOST_CHECK(v1.is<toml::LocalDatetime>());
BOOST_CHECK_EQUAL(v1.cast<toml::value_t::LocalDatetime>(),
toml::local_datetime(
toml::local_date(2018, toml::month_t::Apr, 1),
toml::local_time(1, 15, 30)));
toml::value v2(v1);
BOOST_CHECK(v2 == v1);
BOOST_CHECK_EQUAL(v2.type(), toml::value_t::LocalDatetime);
BOOST_CHECK(v2.is(toml::value_t::LocalDatetime));
BOOST_CHECK(v2.is<toml::LocalDatetime>());
BOOST_CHECK_EQUAL(v2.cast<toml::value_t::LocalDatetime>(),
toml::local_datetime(
toml::local_date(2018, toml::month_t::Apr, 1),
toml::local_time(1, 15, 30)));
v1 = true;
BOOST_CHECK_EQUAL(v1.type(), toml::value_t::Boolean);
BOOST_CHECK(v1.is(toml::value_t::Boolean));
BOOST_CHECK(v1.is<toml::Boolean>());
BOOST_CHECK_EQUAL(v1.cast<toml::value_t::Boolean>(), true);
}
BOOST_AUTO_TEST_CASE(test_value_offset_datetime)
{
toml::value v1(toml::offset_datetime(
toml::local_date(2018, toml::month_t::Jan, 31),
toml::local_time(12, 30, 45),
toml::time_offset(9, 0)
));
BOOST_CHECK_EQUAL(v1.type(), toml::value_t::OffsetDatetime);
BOOST_CHECK(v1.is(toml::value_t::OffsetDatetime));
BOOST_CHECK(v1.is<toml::OffsetDatetime>());
BOOST_CHECK_EQUAL(v1.cast<toml::value_t::OffsetDatetime>(),
toml::offset_datetime(
toml::local_date(2018, toml::month_t::Jan, 31),
toml::local_time(12, 30, 45),
toml::time_offset(9, 0)
));
v1 = toml::offset_datetime(
toml::local_date(2018, toml::month_t::Apr, 1),
toml::local_time(1, 15, 30),
toml::time_offset(9, 0));
BOOST_CHECK_EQUAL(v1.type(), toml::value_t::OffsetDatetime);
BOOST_CHECK(v1.is(toml::value_t::OffsetDatetime));
BOOST_CHECK(v1.is<toml::OffsetDatetime>());
BOOST_CHECK_EQUAL(v1.cast<toml::value_t::OffsetDatetime>(),
toml::offset_datetime(
toml::local_date(2018, toml::month_t::Apr, 1),
toml::local_time(1, 15, 30),
toml::time_offset(9, 0)));
toml::value v2(v1);
BOOST_CHECK(v2 == v1);
BOOST_CHECK_EQUAL(v2.type(), toml::value_t::OffsetDatetime);
BOOST_CHECK(v2.is(toml::value_t::OffsetDatetime));
BOOST_CHECK(v2.is<toml::OffsetDatetime>());
BOOST_CHECK_EQUAL(v2.cast<toml::value_t::OffsetDatetime>(),
toml::offset_datetime(
toml::local_date(2018, toml::month_t::Apr, 1),
toml::local_time(1, 15, 30),
toml::time_offset(9, 0)));
v1 = true;
BOOST_CHECK_EQUAL(v1.type(), toml::value_t::Boolean);
BOOST_CHECK(v1.is(toml::value_t::Boolean));
BOOST_CHECK(v1.is<toml::Boolean>());
BOOST_CHECK_EQUAL(v1.cast<toml::value_t::Boolean>(), true);
}
BOOST_AUTO_TEST_CASE(test_value_array)
{
std::vector<int> v{1,2,3,4,5};
toml::value v1(v);
toml::value v2{6,7,8,9,0};
BOOST_CHECK_EQUAL(v1.type(), toml::value_t::Array);
BOOST_CHECK_EQUAL(v2.type(), toml::value_t::Table);
BOOST_CHECK(v1.is(toml::value_t::Array));
BOOST_CHECK(v1.is<toml::Array>());
const auto& ar = v1.cast<toml::value_t::Array>();
BOOST_CHECK_EQUAL(ar.at(0).cast<toml::value_t::Integer>(), 3);
BOOST_CHECK_EQUAL(ar.at(1).cast<toml::value_t::Integer>(), 1);
BOOST_CHECK_EQUAL(ar.at(2).cast<toml::value_t::Integer>(), 4);
BOOST_CHECK_EQUAL(ar.at(3).cast<toml::value_t::Integer>(), 1);
BOOST_CHECK_EQUAL(ar.at(4).cast<toml::value_t::Integer>(), 5);
BOOST_CHECK_EQUAL(v2.type(), toml::value_t::Array);
BOOST_CHECK(v2.is(toml::value_t::Array));
BOOST_CHECK(v2.is<toml::Array>());
const auto& tb = v2.cast<toml::value_t::Table>();
BOOST_CHECK_EQUAL(v1.cast<toml::value_t::Array>().at(0).cast<toml::value_t::Integer>(), 1);
BOOST_CHECK_EQUAL(v1.cast<toml::value_t::Array>().at(1).cast<toml::value_t::Integer>(), 2);
BOOST_CHECK_EQUAL(v1.cast<toml::value_t::Array>().at(2).cast<toml::value_t::Integer>(), 3);
BOOST_CHECK_EQUAL(v1.cast<toml::value_t::Array>().at(3).cast<toml::value_t::Integer>(), 4);
BOOST_CHECK_EQUAL(v1.cast<toml::value_t::Array>().at(4).cast<toml::value_t::Integer>(), 5);
BOOST_CHECK_EQUAL(tb.at("hoge").type(), toml::value_t::Integer);
BOOST_CHECK_EQUAL(tb.at("piyo").type(), toml::value_t::Float);
BOOST_CHECK_EQUAL(tb.at("fuga").type(), toml::value_t::String);
BOOST_CHECK_EQUAL(v2.cast<toml::value_t::Array>().at(0).cast<toml::value_t::Integer>(), 6);
BOOST_CHECK_EQUAL(v2.cast<toml::value_t::Array>().at(1).cast<toml::value_t::Integer>(), 7);
BOOST_CHECK_EQUAL(v2.cast<toml::value_t::Array>().at(2).cast<toml::value_t::Integer>(), 8);
BOOST_CHECK_EQUAL(v2.cast<toml::value_t::Array>().at(3).cast<toml::value_t::Integer>(), 9);
BOOST_CHECK_EQUAL(v2.cast<toml::value_t::Array>().at(4).cast<toml::value_t::Integer>(), 0);
BOOST_CHECK_EQUAL(tb.at("hoge").cast<toml::value_t::Integer>(), 1);
BOOST_CHECK_CLOSE_FRACTION(tb.at("piyo").cast<toml::value_t::Float>(), 3.14, 1e-3);
BOOST_CHECK_EQUAL(tb.at("fuga").cast<toml::value_t::String>(), "string");
v1 = {6,7,8,9,0};
v2 = v;
BOOST_CHECK_EQUAL(v1.type(), toml::value_t::Array);
BOOST_CHECK(v1.is(toml::value_t::Array));
BOOST_CHECK(v1.is<toml::Array>());
BOOST_CHECK_EQUAL(v2.type(), toml::value_t::Array);
BOOST_CHECK(v2.is(toml::value_t::Array));
BOOST_CHECK(v2.is<toml::Array>());
BOOST_CHECK_EQUAL(v1.cast<toml::value_t::Array>().at(0).cast<toml::value_t::Integer>(), 6);
BOOST_CHECK_EQUAL(v1.cast<toml::value_t::Array>().at(1).cast<toml::value_t::Integer>(), 7);
BOOST_CHECK_EQUAL(v1.cast<toml::value_t::Array>().at(2).cast<toml::value_t::Integer>(), 8);
BOOST_CHECK_EQUAL(v1.cast<toml::value_t::Array>().at(3).cast<toml::value_t::Integer>(), 9);
BOOST_CHECK_EQUAL(v1.cast<toml::value_t::Array>().at(4).cast<toml::value_t::Integer>(), 0);
BOOST_CHECK_EQUAL(v2.cast<toml::value_t::Array>().at(0).cast<toml::value_t::Integer>(), 1);
BOOST_CHECK_EQUAL(v2.cast<toml::value_t::Array>().at(1).cast<toml::value_t::Integer>(), 2);
BOOST_CHECK_EQUAL(v2.cast<toml::value_t::Array>().at(2).cast<toml::value_t::Integer>(), 3);
BOOST_CHECK_EQUAL(v2.cast<toml::value_t::Array>().at(3).cast<toml::value_t::Integer>(), 4);
BOOST_CHECK_EQUAL(v2.cast<toml::value_t::Array>().at(4).cast<toml::value_t::Integer>(), 5);
toml::value v3(v1);
BOOST_CHECK(v3 == v1);
BOOST_CHECK_EQUAL(v3.type(), toml::value_t::Array);
BOOST_CHECK(v3.is(toml::value_t::Array));
BOOST_CHECK(v3.is<toml::Array>());
BOOST_CHECK_EQUAL(v3.cast<toml::value_t::Array>().at(0).cast<toml::value_t::Integer>(), 6);
BOOST_CHECK_EQUAL(v3.cast<toml::value_t::Array>().at(1).cast<toml::value_t::Integer>(), 7);
BOOST_CHECK_EQUAL(v3.cast<toml::value_t::Array>().at(2).cast<toml::value_t::Integer>(), 8);
BOOST_CHECK_EQUAL(v3.cast<toml::value_t::Array>().at(3).cast<toml::value_t::Integer>(), 9);
BOOST_CHECK_EQUAL(v3.cast<toml::value_t::Array>().at(4).cast<toml::value_t::Integer>(), 0);
v1 = true;
BOOST_CHECK_EQUAL(v1.type(), toml::value_t::Boolean);
BOOST_CHECK(v1.is(toml::value_t::Boolean));
BOOST_CHECK(v1.is<toml::Boolean>());
BOOST_CHECK_EQUAL(v1.cast<toml::value_t::Boolean>(), true);
}
BOOST_AUTO_TEST_CASE(test_value_table)
{
toml::value v1{{"foo", 42}, {"bar", 3.14}, {"baz", "qux"}};
BOOST_CHECK_EQUAL(v1.type(), toml::value_t::Table);
BOOST_CHECK(v1.is(toml::value_t::Table));
BOOST_CHECK(v1.is<toml::Table>());
BOOST_CHECK_EQUAL(v1.cast<toml::value_t::Table>().at("foo").cast<toml::value_t::Integer>(), 42);
BOOST_CHECK_EQUAL(v1.cast<toml::value_t::Table>().at("bar").cast<toml::value_t::Float>(), 3.14);
BOOST_CHECK_EQUAL(v1.cast<toml::value_t::Table>().at("baz").cast<toml::value_t::String>().str, "qux");
v1 = toml::table{{"foo", 2.71}, {"bar", 54}, {"baz", "quux"}};
BOOST_CHECK_EQUAL(v1.type(), toml::value_t::Table);
BOOST_CHECK(v1.is(toml::value_t::Table));
BOOST_CHECK(v1.is<toml::Table>());
BOOST_CHECK_EQUAL(v1.cast<toml::value_t::Table>().at("foo").cast<toml::value_t::Float>(), 2.71);
BOOST_CHECK_EQUAL(v1.cast<toml::value_t::Table>().at("bar").cast<toml::value_t::Integer>(), 54);
BOOST_CHECK_EQUAL(v1.cast<toml::value_t::Table>().at("baz").cast<toml::value_t::String>().str, "quux");
toml::value v3(v1);
BOOST_CHECK(v3 == v1);
BOOST_CHECK_EQUAL(v3.type(), toml::value_t::Table);
BOOST_CHECK(v3.is(toml::value_t::Table));
BOOST_CHECK(v3.is<toml::Table>());
BOOST_CHECK_EQUAL(v3.cast<toml::value_t::Table>().at("foo").cast<toml::value_t::Float>(), 2.71);
BOOST_CHECK_EQUAL(v3.cast<toml::value_t::Table>().at("bar").cast<toml::value_t::Integer>(), 54);
BOOST_CHECK_EQUAL(v3.cast<toml::value_t::Table>().at("baz").cast<toml::value_t::String>().str, "quux");
v1 = true;
BOOST_CHECK_EQUAL(v1.type(), toml::value_t::Boolean);
BOOST_CHECK(v1.is(toml::value_t::Boolean));
BOOST_CHECK(v1.is<toml::Boolean>());
BOOST_CHECK_EQUAL(v1.cast<toml::value_t::Boolean>(), true);
}

View File

@@ -1,98 +0,0 @@
#define BOOST_TEST_MODULE "test_value_operator"
#ifdef UNITTEST_FRAMEWORK_LIBRARY_EXIST
#include <boost/test/unit_test.hpp>
#else
#define BOOST_TEST_NO_LIB
#include <boost/test/included/unit_test.hpp>
#endif
#include <toml.hpp>
#include <map>
#include <list>
BOOST_AUTO_TEST_CASE(test_value_equal)
{
toml::Boolean b(true);
toml::Integer i(42);
toml::Float f(3.14);
toml::String s("hoge");
toml::Datetime d(std::chrono::system_clock::now());
toml::Array a;
a.emplace_back(2);
a.emplace_back(7);
a.emplace_back(1);
a.emplace_back(8);
a.emplace_back(2);
toml::Table t;
t.emplace("val1", true);
t.emplace("val2", 42);
t.emplace("val3", 3.14);
t.emplace("val4", "piyo");
toml::value v1(b);
toml::value v2(i);
toml::value v3(f);
toml::value v4(s);
toml::value v5(d);
toml::value v6(a);
toml::value v7(t);
toml::value u1(b);
toml::value u2(i);
toml::value u3(f);
toml::value u4(s);
toml::value u5(d);
toml::value u6(a);
toml::value u7(t);
const bool b1 = v1 == u1;
const bool b2 = v2 == u2;
const bool b3 = v3 == u3;
const bool b4 = v4 == u4;
const bool b5 = v5 == u5;
const bool b6 = v6 == u6;
const bool b7 = v7 == u7;
BOOST_CHECK(b1);
BOOST_CHECK(b2);
BOOST_CHECK(b3);
BOOST_CHECK(b4);
BOOST_CHECK(b5);
BOOST_CHECK(b6);
BOOST_CHECK(b7);
{
const bool n1 = v1 != u1;
const bool n2 = v2 != u2;
const bool n3 = v3 != u3;
const bool n4 = v4 != u4;
const bool n5 = v5 != u5;
const bool n6 = v6 != u6;
const bool n7 = v7 != u7;
BOOST_CHECK(!n1);
BOOST_CHECK(!n2);
BOOST_CHECK(!n3);
BOOST_CHECK(!n4);
BOOST_CHECK(!n5);
BOOST_CHECK(!n6);
BOOST_CHECK(!n7);
}
{
const bool n1 = v1 == u2;
const bool n2 = v2 == u3;
const bool n3 = v3 == u4;
const bool n4 = v4 == u5;
const bool n5 = v5 == u6;
const bool n6 = v6 == u7;
const bool n7 = v7 == u1;
BOOST_CHECK(!n1);
BOOST_CHECK(!n2);
BOOST_CHECK(!n3);
BOOST_CHECK(!n4);
BOOST_CHECK(!n5);
BOOST_CHECK(!n6);
BOOST_CHECK(!n7);
}
}

View File

@@ -1,777 +0,0 @@
#ifndef TOML11_ACCEPTOR
#define TOML11_ACCEPTOR
#include <type_traits>
#include <iterator>
#include <limits>
#include "exception.hpp"
namespace toml
{
template<typename charT, charT c>
struct is_character
{
typedef charT value_type;
constexpr static value_type target = c;
template<typename Iterator, class = typename std::enable_if<
std::is_same<typename std::iterator_traits<Iterator>::value_type,
value_type>::value>::type>
constexpr static Iterator invoke(Iterator iter, Iterator end)
{
return iter == end ? iter : *iter == c ? std::next(iter) : iter;
}
};
template<typename charT, charT lw, charT up>
struct is_in_range
{
typedef charT value_type;
constexpr static value_type upper = up;
constexpr static value_type lower = lw;
static_assert(lower <= upper, "lower <= upper");
template<typename Iterator, class = typename std::enable_if<
std::is_same<typename std::iterator_traits<Iterator>::value_type,
value_type>::value>::type>
constexpr static Iterator invoke(Iterator iter, Iterator end)
{
return iter == end ? iter :
(lower <= *iter && *iter <= upper) ? std::next(iter) : iter;
}
};
template<typename headT, typename ... condT>
struct is_one_of
{
typedef typename headT::value_type value_type;
static_assert(
std::is_same<value_type, typename is_one_of<condT...>::value_type>::value,
"different value_type");
template<typename Iterator, class = typename std::enable_if<
std::is_same<typename std::iterator_traits<Iterator>::value_type,
value_type>::value>::type>
static Iterator invoke(Iterator iter, Iterator end)
{
const Iterator tmp = headT::invoke(iter, end);
return (tmp != iter) ? tmp : is_one_of<condT...>::invoke(iter, end);
}
};
template<typename tailT>
struct is_one_of<tailT>
{
typedef typename tailT::value_type value_type;
template<typename Iterator, class = typename std::enable_if<
std::is_same<typename std::iterator_traits<Iterator>::value_type,
value_type>::value>::type>
static Iterator invoke(Iterator iter, Iterator end)
{
const Iterator tmp = tailT::invoke(iter, end);
return (tmp != iter) ? tmp : iter;
}
};
// just a wrapper for maybe_ignored
template<typename condT>
struct is_ignorable
{
typedef typename condT::value_type value_type;
template<typename Iterator, class = typename std::enable_if<
std::is_same<typename std::iterator_traits<Iterator>::value_type,
value_type>::value>::type>
static Iterator invoke(Iterator iter, Iterator end)
{
const Iterator tmp = condT::invoke(iter, end);
return (tmp != iter) ? tmp : iter;
}
};
template<typename condT>
struct maybe_ignored : std::false_type{};
template<typename condT>
struct maybe_ignored<is_ignorable<condT>> : std::true_type{};
template<typename headT, typename ... condT>
struct is_chain_of_impl
{
typedef typename headT::value_type value_type;
static_assert(std::is_same<value_type,
typename is_chain_of_impl<condT...>::value_type>::value,
"different value_type");
constexpr static bool ignorable = maybe_ignored<headT>::value;
template<typename Iterator, class = typename std::enable_if<
std::is_same<typename std::iterator_traits<Iterator>::value_type,
value_type>::value>::type>
static Iterator invoke(Iterator iter, Iterator end, Iterator rollback)
{
const Iterator tmp = headT::invoke(iter, end);
return (tmp == iter && !ignorable) ? rollback :
is_chain_of_impl<condT...>::invoke(tmp, end, rollback);
}
};
template<typename tailT>
struct is_chain_of_impl<tailT>
{
typedef typename tailT::value_type value_type;
constexpr static bool ignorable = maybe_ignored<tailT>::value;
template<typename Iterator, class = typename std::enable_if<
std::is_same<typename std::iterator_traits<Iterator>::value_type,
value_type>::value>::type>
static Iterator invoke(Iterator iter, Iterator end, Iterator rollback)
{
const Iterator tmp = tailT::invoke(iter, end);
return (tmp == iter) ? (ignorable ? iter : rollback) : tmp;
}
};
template<typename headT, typename ...condT>
struct is_chain_of
{
typedef typename is_chain_of_impl<headT, condT...>::value_type value_type;
template<typename Iterator, class = typename std::enable_if<
std::is_same<typename std::iterator_traits<Iterator>::value_type,
value_type>::value>::type>
static Iterator invoke(Iterator iter, Iterator end)
{
return is_chain_of_impl<headT, condT...>::invoke(iter, end, iter);
}
};
constexpr inline std::size_t repeat_infinite(){return 0ul;}
template<typename condT, std::size_t N>
struct is_repeat_of
{
typedef typename condT::value_type value_type;
template<typename Iterator, class = typename std::enable_if<
std::is_same<typename std::iterator_traits<Iterator>::value_type,
value_type>::value>::type>
static Iterator invoke(Iterator iter, Iterator end)
{
const Iterator rollback = iter;
Iterator tmp;
for(auto i=0ul; i<N; ++i)
{
tmp = condT::invoke(iter, end);
if(tmp == iter) return rollback;
iter = tmp;
}
return iter;
}
};
template<typename condT>
struct is_repeat_of<condT, 0>
{
typedef typename condT::value_type value_type;
template<typename Iterator, class = typename std::enable_if<
std::is_same<typename std::iterator_traits<Iterator>::value_type,
value_type>::value>::type>
static Iterator invoke(Iterator iter, Iterator end)
{
Iterator tmp = condT::invoke(iter, end);
while(tmp != iter)
{
iter = tmp;
tmp = condT::invoke(iter, end);
}
return iter;
}
};
template<typename headT, typename ... tailT>
struct is_none_of
{
typedef typename headT::value_type value_type;
static_assert(
std::is_same<value_type, typename is_one_of<tailT...>::value_type>::value,
"different value_type");
template<typename Iterator, class = typename std::enable_if<
std::is_same<typename std::iterator_traits<Iterator>::value_type,
value_type>::value>::type>
static Iterator invoke(Iterator iter, Iterator end)
{
const Iterator tmp = headT::invoke(iter, end);
return (tmp != iter) ? iter : is_none_of<tailT...>::invoke(iter, end);
}
};
template<typename tailT>
struct is_none_of<tailT>
{
typedef typename tailT::value_type value_type;
template<typename Iterator, class = typename std::enable_if<
std::is_same<typename std::iterator_traits<Iterator>::value_type,
value_type>::value>::type>
static Iterator invoke(Iterator iter, Iterator end)
{
const Iterator tmp = tailT::invoke(iter, end);
return (tmp != iter) ? iter : std::next(iter);
}
};
template<typename notT, typename butT>
struct is_not_but
{
typedef typename notT::value_type value_type;
static_assert(
std::is_same<value_type, typename butT::value_type>::value,
"different value type");
template<typename Iterator, class = typename std::enable_if<
std::is_same<typename std::iterator_traits<Iterator>::value_type,
value_type>::value>::type>
static Iterator invoke(Iterator iter, Iterator end)
{
return (iter != notT::invoke(iter, end)) ? iter : butT::invoke(iter, end);
}
};
template<typename charT>
using is_space = is_character<charT, ' '>;
template<typename charT>
using is_tab = is_character<charT, '\t'>;
template<typename charT>
using is_number = is_in_range<charT, '0', '9'>;
template<typename charT>
using is_lowercase = is_in_range<charT, 'a', 'z'>;
template<typename charT>
using is_uppercase = is_in_range<charT, 'A', 'Z'>;
template<typename charT>
using is_alphabet = is_one_of<is_lowercase<charT>, is_uppercase<charT>>;
template<typename charT>
using is_hex = is_one_of<is_number<charT>, is_in_range<charT, 'a', 'f'>,
is_in_range<charT, 'A', 'F'>>;
template<typename charT>
using is_whitespace = is_one_of<is_space<charT>, is_tab<charT>>;
template<typename charT>
using is_any_num_of_ws =
is_ignorable<is_repeat_of<is_whitespace<charT>, repeat_infinite()>>;
template<typename charT>
using is_newline = is_one_of<is_character<charT, '\n'>,
is_chain_of<is_character<charT, '\r'>, is_character<charT, '\n'>>>;
template<typename charT>
using is_barekey_component = is_one_of<is_alphabet<charT>, is_number<charT>,
is_character<charT, '_'>, is_character<charT, '-'>>;
template<typename charT>
using is_barekey = is_repeat_of<is_barekey_component<charT>, repeat_infinite()>;
template<typename charT>
using is_comment =
is_chain_of<
is_character<charT, '#'>,
is_repeat_of<is_none_of<is_newline<charT>>, repeat_infinite()>,
is_newline<charT>
>;
template<typename charT>
using is_basic_inline_string_component =
is_one_of<
is_none_of< is_in_range<charT, '\0', '\31'>, is_character<charT, '\"'>,
is_character<charT, '\\'>, is_newline<charT>>,
is_chain_of<is_character<charT, '\\'>, is_character<charT, '\"'>>,
is_chain_of<is_character<charT, '\\'>, is_character<charT, '\\'>>,
is_chain_of<is_character<charT, '\\'>, is_character<charT, 'b'>>,
is_chain_of<is_character<charT, '\\'>, is_character<charT, 't'>>,
is_chain_of<is_character<charT, '\\'>, is_character<charT, 'n'>>,
is_chain_of<is_character<charT, '\\'>, is_character<charT, 'f'>>,
is_chain_of<is_character<charT, '\\'>, is_character<charT, 'r'>>,
is_chain_of<is_character<charT, '\\'>, is_character<charT, 'u'>,
is_repeat_of<is_hex<charT>, 4>>,
is_chain_of<is_character<charT, '\\'>, is_character<charT, 'U'>,
is_repeat_of<is_hex<charT>, 8>>
>;
template<typename charT>
using is_basic_inline_string =
is_not_but<
is_repeat_of<is_character<charT, '\"'>, 3>, // not multiline
is_chain_of<
is_character<charT, '\"'>,
is_ignorable<is_repeat_of<is_basic_inline_string_component<charT>,
repeat_infinite()>>,
is_character<charT, '\"'>
>
>;
template<typename charT>
using is_basic_multiline_string_component =
is_one_of<
is_none_of< is_in_range<charT, '\0', '\31'>,
is_repeat_of<is_character<charT, '\"'>, 3>,
is_character<charT, '\\'>>,
is_newline<charT>,
is_chain_of<is_character<charT, '\\'>, is_newline<charT>>,
is_chain_of<is_character<charT, '\\'>, is_character<charT, '\"'>>,
is_chain_of<is_character<charT, '\\'>, is_character<charT, '\\'>>,
is_chain_of<is_character<charT, '\\'>, is_character<charT, 'b'>>,
is_chain_of<is_character<charT, '\\'>, is_character<charT, 't'>>,
is_chain_of<is_character<charT, '\\'>, is_character<charT, 'n'>>,
is_chain_of<is_character<charT, '\\'>, is_character<charT, 'f'>>,
is_chain_of<is_character<charT, '\\'>, is_character<charT, 'r'>>,
is_chain_of<is_character<charT, '\\'>, is_character<charT, 'u'>,
is_repeat_of<is_hex<charT>, 4>>,
is_chain_of<is_character<charT, '\\'>, is_character<charT, 'U'>,
is_repeat_of<is_hex<charT>, 8>>
>;
template<typename charT>
using is_basic_multiline_string =
is_chain_of<
is_repeat_of<is_character<charT, '\"'>, 3>,
is_ignorable<is_repeat_of<is_basic_multiline_string_component<charT>,
repeat_infinite()>>,
is_repeat_of<is_character<charT, '\"'>, 3>
>;
template<typename charT>
using is_literal_inline_string_component =
is_none_of<is_in_range<charT, '\0', '\31'>, is_character<charT, '\''>>;
template<typename charT>
using is_literal_inline_string =
is_not_but<
is_repeat_of<is_character<charT, '\''>, 3>,
is_chain_of<
is_character<charT, '\''>,
is_ignorable<is_repeat_of<is_literal_inline_string_component<charT>,
repeat_infinite()>>,
is_character<charT, '\''>
>
>;
template<typename charT>
using is_literal_multiline_string_component =
is_one_of<
is_none_of<is_in_range<charT, '\0', '\31'>,
is_repeat_of<is_character<charT, '\''>, 3>>,
is_newline<charT>
>;
template<typename charT>
using is_literal_multiline_string =
is_chain_of<
is_repeat_of<is_character<charT, '\''>, 3>,
is_ignorable<is_repeat_of<is_literal_multiline_string_component<charT>,
repeat_infinite()>>,
is_repeat_of<is_character<charT, '\''>, 3>
>;
template<typename charT>
using is_string =
is_one_of<
is_basic_inline_string<charT>,
is_basic_multiline_string<charT>,
is_literal_inline_string<charT>,
is_literal_multiline_string<charT>
>;
template<typename charT>
using is_sign = is_one_of<is_character<charT, '+'>, is_character<charT, '-'>>;
template<typename charT>
using is_nonzero_number = is_in_range<charT, '1', '9'>;
template<typename charT>
using is_integer_component =
is_not_but<
is_repeat_of<is_character<charT, '_'>, 2>,
is_one_of<
is_character<charT, '_'>, is_number<charT>
>
>;
template<typename charT>
using is_integer =
is_chain_of<
is_ignorable<is_sign<charT>>,
is_one_of<
is_character<charT, '0'>,
is_chain_of<
is_nonzero_number<charT>,
is_ignorable<is_repeat_of<is_integer_component<charT>,
repeat_infinite()>
>
>
>
>;
template<typename charT>
using is_fractional_part =
is_chain_of<
is_character<charT, '.'>,
is_repeat_of<is_integer_component<charT>, repeat_infinite()>
>;
template<typename charT>
using is_exponent_part =
is_chain_of<
is_one_of<is_character<charT, 'e'>, is_character<charT, 'E'>>,
is_integer<charT>
>;
template<typename charT>
using is_float =
is_one_of<
is_chain_of<
is_integer<charT>,
is_fractional_part<charT>,
is_exponent_part<charT>
>,
is_chain_of<
is_integer<charT>,
is_fractional_part<charT>
>,
is_chain_of<
is_integer<charT>,
is_exponent_part<charT>
>
>;
template<typename charT>
using is_boolean =
is_one_of<
is_chain_of<
is_character<charT, 't'>,
is_character<charT, 'r'>,
is_character<charT, 'u'>,
is_character<charT, 'e'>
>,
is_chain_of<
is_character<charT, 'f'>,
is_character<charT, 'a'>,
is_character<charT, 'l'>,
is_character<charT, 's'>,
is_character<charT, 'e'>
>
>;
template<typename charT>
using is_local_time =
is_chain_of<
is_repeat_of<is_number<charT>, 2>,
is_character<charT, ':'>,
is_repeat_of<is_number<charT>, 2>,
is_character<charT, ':'>,
is_repeat_of<is_number<charT>, 2>,
is_ignorable<
is_chain_of<
is_character<charT, '.'>,
is_repeat_of<is_number<charT>, repeat_infinite()>
>
>
>;
template<typename charT>
using is_local_date =
is_chain_of<
is_repeat_of<is_number<charT>, 4>,
is_character<charT, '-'>,
is_repeat_of<is_number<charT>, 2>,
is_character<charT, '-'>,
is_repeat_of<is_number<charT>, 2>
>;
template<typename charT>
using is_local_date_time =
is_chain_of<
is_local_date<charT>,
is_character<charT, 'T'>,
is_local_time<charT>
>;
template<typename charT>
using is_offset =
is_one_of<
is_character<charT, 'Z'>,
is_chain_of<
is_sign<charT>,
is_repeat_of<is_number<charT>, 2>,
is_character<charT, ':'>,
is_repeat_of<is_number<charT>, 2>
>
>;
template<typename charT>
using is_offset_date_time =
is_chain_of<
is_local_date_time<charT>,
is_offset<charT>
>;
template<typename charT>
using is_datetime =
is_one_of<
is_offset_date_time<charT>,
is_local_date_time<charT>,
is_local_date<charT>,
is_local_time<charT>
>;
template<typename charT>
using is_fundamental_type =
is_one_of<
is_basic_inline_string<charT>,
is_basic_multiline_string<charT>,
is_literal_inline_string<charT>,
is_literal_multiline_string<charT>,
is_offset_date_time<charT>,
is_local_date_time<charT>,
is_local_date<charT>,
is_local_time<charT>,
is_boolean<charT>,
is_float<charT>,
is_integer<charT>
>;
template<typename charT>
using is_skippable_in_array =
is_repeat_of<
is_one_of<is_whitespace<charT>, is_newline<charT>, is_comment<charT>>,
repeat_infinite()
>;
template<typename charT>
struct is_inline_table;
template<typename charT>
using is_key =
is_one_of<
is_barekey<charT>,
is_string<charT>
>;
template<typename charT, typename is_array_component>
using is_fixed_type_array =
is_chain_of<
is_character<charT, '['>,
is_ignorable<
is_repeat_of<
is_chain_of<
is_ignorable<is_skippable_in_array<charT>>,
is_array_component,
is_ignorable<is_skippable_in_array<charT>>,
is_character<charT, ','>
>,
repeat_infinite()
>
>,
is_ignorable<
is_chain_of<
is_ignorable<is_skippable_in_array<charT>>,
is_array_component,
is_ignorable<is_skippable_in_array<charT>>,
is_ignorable<is_character<charT, ','>>
>
>,
is_ignorable<is_skippable_in_array<charT>>,
is_character<charT, ']'>
>;
template<typename charT>
struct is_array
{
typedef charT value_type;
template<typename Iterator, class = typename std::enable_if<
std::is_same<typename std::iterator_traits<Iterator>::value_type,
value_type>::value>::type>
static Iterator invoke(Iterator iter, Iterator end)
{
return is_one_of<
is_fixed_type_array<charT, is_boolean<charT>>,
is_fixed_type_array<charT, is_integer<charT>>,
is_fixed_type_array<charT, is_float<charT>>,
is_fixed_type_array<charT, is_string<charT>>,
is_fixed_type_array<charT, is_datetime<charT>>,
is_fixed_type_array<charT, is_array<charT>>,
is_fixed_type_array<charT, is_inline_table<charT>>
>::invoke(iter, end);
}
};
template<typename charT>
struct is_inline_table
{
typedef charT value_type;
template<typename Iterator, class = typename std::enable_if<
std::is_same<typename std::iterator_traits<Iterator>::value_type,
value_type>::value>::type>
static Iterator invoke(Iterator iter, Iterator end)
{
typedef is_one_of<is_fundamental_type<charT>,
is_array<charT>, is_inline_table<charT>> is_component;
typedef is_chain_of<
is_any_num_of_ws<charT>,
is_key<charT>,
is_any_num_of_ws<charT>,
is_character<charT, '='>,
is_any_num_of_ws<charT>,
is_component,
is_any_num_of_ws<charT>
> is_inline_key_value_pair;
typedef is_chain_of<
is_character<charT, '{'>,
is_ignorable<
is_repeat_of<
is_chain_of<
is_any_num_of_ws<charT>,
is_inline_key_value_pair,
is_any_num_of_ws<charT>,
is_character<charT, ','>
>,
repeat_infinite()
>
>,
is_ignorable<
is_chain_of<
is_any_num_of_ws<charT>,
is_inline_key_value_pair,
is_any_num_of_ws<charT>,
is_ignorable<is_character<charT, ','>>
>
>,
is_any_num_of_ws<charT>,
is_character<charT, '}'>
> entity;
return entity::invoke(iter, end);
}
};
template<typename charT>
using is_value =
is_one_of<is_fundamental_type<charT>, is_array<charT>, is_inline_table<charT>>;
// []
template<typename charT>
using is_table_definition =
is_chain_of<
is_any_num_of_ws<charT>,
is_character<charT, '['>,
is_any_num_of_ws<charT>,
is_key<charT>,
is_ignorable<
is_repeat_of<
is_chain_of<
is_any_num_of_ws<charT>,
is_character<charT, '.'>,
is_any_num_of_ws<charT>,
is_key<charT>,
is_any_num_of_ws<charT>
>,
repeat_infinite()>
>,
is_character<charT, ']'>
>;
template<typename charT>
using is_array_of_table_definition =
is_chain_of<
is_any_num_of_ws<charT>,
is_repeat_of<is_character<charT, '['>, 2>,
is_any_num_of_ws<charT>,
is_key<charT>,
is_ignorable<
is_repeat_of<
is_chain_of<
is_any_num_of_ws<charT>,
is_character<charT, '.'>,
is_any_num_of_ws<charT>,
is_key<charT>,
is_any_num_of_ws<charT>
>,
repeat_infinite()>
>,
is_repeat_of<is_character<charT, ']'>, 2>
>;
template<typename charT>
using is_key_value_pair =
is_chain_of<
is_any_num_of_ws<charT>,
is_key<charT>,
is_any_num_of_ws<charT>,
is_character<charT, '='>,
is_any_num_of_ws<charT>,
is_value<charT>,
is_any_num_of_ws<charT>
>;
template<typename charT>
using is_empty_line =
is_chain_of<
is_any_num_of_ws<charT>,
is_one_of<is_comment<charT>, is_newline<charT>>
>;
template<typename charT>
using is_empty_lines =
is_repeat_of<
is_chain_of<
is_any_num_of_ws<charT>,
is_one_of<is_comment<charT>, is_newline<charT>>
>,
repeat_infinite()
>;
template<typename charT>
using is_table_contents =
is_repeat_of<
is_one_of<
is_empty_lines<charT>,
is_chain_of<
is_key_value_pair<charT>,
is_one_of<
is_comment<charT>,
is_newline<charT>
>
>
>,
repeat_infinite()
>;
template<typename charT>
using is_standard_table =
is_chain_of<
is_table_definition<charT>,
is_any_num_of_ws<charT>,
is_one_of<is_comment<charT>, is_newline<charT>>,
is_table_contents<charT>
>;
template<typename charT>
using is_array_of_table =
is_chain_of<
is_array_of_table_definition<charT>,
is_any_num_of_ws<charT>,
is_one_of<is_comment<charT>, is_newline<charT>>,
is_table_contents<charT>
>;
template<typename charT>
using is_toml_data =
is_chain_of<
is_ignorable<is_table_contents<charT>>,
is_ignorable<
is_repeat_of<
is_one_of<
is_standard_table<charT>,
is_array_of_table<charT>
>,
repeat_infinite()
>
>
>;
}//toml
#endif// TOML11_ACCEPTOR

364
toml/combinator.hpp Normal file
View File

@@ -0,0 +1,364 @@
// Copyright Toru Niina 2017.
// Distributed under the MIT License.
#ifndef TOML11_COMBINATOR_HPP
#define TOML11_COMBINATOR_HPP
#include "traits.hpp"
#include "result.hpp"
#include "utility.hpp"
#include "region.hpp"
#include <type_traits>
#include <iterator>
#include <limits>
#include <iomanip>
#include <cctype>
// they scans characters and returns region if it matches to the condition.
// when they fail, it does not change the location.
// in lexer.hpp, these are used.
namespace toml
{
namespace detail
{
// to output character as an error message.
inline std::string show_char(const char c)
{
// It supress an error that occurs only in Debug mode of MSVC++ on Windows.
// I'm not completely sure but they check the value of char to be in the
// range [0, 256) and some of the COMPLETELY VALID utf-8 character sometimes
// has negative value (if char has sign). So here it re-interprets c as
// unsigned char through pointer. In general, converting pointer to a
// pointer that has different type cause UB, but `(signed|unsigned)?char`
// are one of the exceptions. Converting pointer only to char and std::byte
// (c++17) are valid.
if(std::isgraph(*reinterpret_cast<unsigned char const*>(std::addressof(c))))
{
return std::string(1, c);
}
else
{
std::ostringstream oss;
oss << std::hex << std::setfill('0') << std::setw(2) << "0x"
<< static_cast<int>(c);
return oss.str();
}
}
template<char C>
struct character
{
static constexpr char target = C;
template<typename Cont>
static result<region<Cont>, std::string> invoke(location<Cont>& loc)
{
static_assert(std::is_same<char, typename Cont::value_type>::value,
"internal error: container::value_type should be `char`.");
if(loc.iter() == loc.end()) {return err("not sufficient characters");}
const auto first = loc.iter();
const char c = *(loc.iter());
if(c != target)
{
return err(concat_to_string("expected '", show_char(target),
"' but got '", show_char(c), "'."));
}
++(loc.iter()); // update location
return ok(region<Cont>(loc, first, loc.iter()));
}
static std::string pattern() {return show_char(target);}
};
template<char C>
constexpr char character<C>::target;
// closed interval [Low, Up]. both Low and Up are included.
template<char Low, char Up>
struct in_range
{
// assuming ascii part of UTF-8...
static_assert(Low <= Up, "lower bound should be less than upper bound.");
static constexpr char upper = Up;
static constexpr char lower = Low;
template<typename Cont>
static result<region<Cont>, std::string> invoke(location<Cont>& loc)
{
static_assert(std::is_same<char, typename Cont::value_type>::value,
"internal error: container::value_type should be `char`.");
if(loc.iter() == loc.end()) {return err("not sufficient characters");}
const auto first = loc.iter();
const char c = *(loc.iter());
if(c < lower || upper < c)
{
return err(concat_to_string("expected character in range "
"[", show_char(lower), ", ", show_char(upper), "] but got ",
"'", show_char(c), "'."));
}
++(loc.iter());
return ok(region<Cont>(loc, first, loc.iter()));
}
static std::string pattern()
{
return concat_to_string("[",show_char(lower),"-",show_char(upper),"]");
}
};
template<char L, char U> constexpr char in_range<L, U>::upper;
template<char L, char U> constexpr char in_range<L, U>::lower;
// keep iterator if `Combinator` matches. otherwise, increment `iter` by 1 char.
// for detecting invalid characters, like control sequences in toml string.
template<typename Combinator>
struct exclude
{
template<typename Cont>
static result<region<Cont>, std::string> invoke(location<Cont>& loc)
{
static_assert(std::is_same<char, typename Cont::value_type>::value,
"internal error: container::value_type should be `char`.");
if(loc.iter() == loc.end()) {return err("not sufficient characters");}
auto first = loc.iter();
auto rslt = Combinator::invoke(loc);
if(rslt.is_ok())
{
loc.iter() = first; // rollback
return err(concat_to_string(
"invalid pattern (", Combinator::pattern(), ") appeared ",
rslt.unwrap().str()));
}
loc.iter() = std::next(first);
return ok(region<Cont>(loc, first, loc.iter()));
}
static std::string pattern()
{
return concat_to_string("^(", Combinator::pattern(), ')');
}
};
// increment `iter`, if matches. otherwise, just return empty string.
template<typename Combinator>
struct maybe
{
template<typename Cont>
static result<region<Cont>, std::string> invoke(location<Cont>& loc)
{
static_assert(std::is_same<char, typename Cont::value_type>::value,
"internal error: container::value_type should be `char`.");
const auto rslt = Combinator::invoke(loc);
if(rslt.is_ok())
{
return rslt;
}
return ok(region<Cont>(loc));
}
static std::string pattern()
{
return concat_to_string('(', Combinator::pattern(), ")?");
}
};
template<typename ... Ts>
struct sequence;
template<typename Head, typename ... Tail>
struct sequence<Head, Tail...>
{
template<typename Cont>
static result<region<Cont>, std::string> invoke(location<Cont>& loc)
{
static_assert(std::is_same<char, typename Cont::value_type>::value,
"internal error: container::value_type should be `char`.");
const auto first = loc.iter();
const auto rslt = Head::invoke(loc);
if(rslt.is_err())
{
loc.iter() = first;
return err(rslt.unwrap_err());
}
return sequence<Tail...>::invoke(loc, std::move(rslt.unwrap()), first);
}
// called from the above function only, recursively.
template<typename Cont, typename Iterator>
static result<region<Cont>, std::string>
invoke(location<Cont>& loc, region<Cont> reg, Iterator first)
{
const auto rslt = Head::invoke(loc);
if(rslt.is_err())
{
loc.iter() = first;
return err(rslt.unwrap_err());
}
reg += rslt.unwrap(); // concat regions
return sequence<Tail...>::invoke(loc, std::move(reg), first);
}
static std::string pattern()
{
return concat_to_string(Head::pattern(), sequence<Tail...>::pattern());
}
};
template<typename Head>
struct sequence<Head>
{
// would be called from sequence<T ...>::invoke only.
template<typename Cont, typename Iterator>
static result<region<Cont>, std::string>
invoke(location<Cont>& loc, region<Cont> reg, Iterator first)
{
const auto rslt = Head::invoke(loc);
if(rslt.is_err())
{
loc.iter() = first;
return err(rslt.unwrap_err());
}
reg += rslt.unwrap(); // concat regions
return ok(reg);
}
static std::string pattern() {return Head::pattern();}
};
template<typename ... Ts>
struct either;
template<typename Head, typename ... Tail>
struct either<Head, Tail...>
{
template<typename Cont>
static result<region<Cont>, std::string> invoke(location<Cont>& loc)
{
static_assert(std::is_same<char, typename Cont::value_type>::value,
"internal error: container::value_type should be `char`.");
const auto rslt = Head::invoke(loc);
if(rslt.is_ok()) {return rslt;}
return either<Tail...>::invoke(loc);
}
static std::string pattern()
{
return concat_to_string('(', Head::pattern(), ")|", either<Tail...>::pattern());
}
};
template<typename Head>
struct either<Head>
{
template<typename Cont>
static result<region<Cont>, std::string> invoke(location<Cont>& loc)
{
static_assert(std::is_same<char, typename Cont::value_type>::value,
"internal error: container::value_type should be `char`.");
return Head::invoke(loc);
}
static std::string pattern()
{
return concat_to_string('(', Head::pattern(), ')');
}
};
template<typename T, typename N>
struct repeat;
template<std::size_t N> struct exactly{};
template<std::size_t N> struct at_least{};
struct unlimited{};
template<typename T, std::size_t N>
struct repeat<T, exactly<N>>
{
template<typename Cont>
static result<region<Cont>, std::string> invoke(location<Cont>& loc)
{
region<Cont> retval(loc);
const auto first = loc.iter();
for(std::size_t i=0; i<N; ++i)
{
auto rslt = T::invoke(loc);
if(rslt.is_err())
{
loc.iter() = first;
return err(rslt.unwrap_err());
}
retval += rslt.unwrap();
}
return ok(std::move(retval));
}
static std::string pattern()
{
return concat_to_string('(', T::pattern(), "){", N, '}');
}
};
template<typename T, std::size_t N>
struct repeat<T, at_least<N>>
{
template<typename Cont>
static result<region<Cont>, std::string> invoke(location<Cont>& loc)
{
region<Cont> retval(loc);
const auto first = loc.iter();
for(std::size_t i=0; i<N; ++i)
{
auto rslt = T::invoke(loc);
if(rslt.is_err())
{
loc.iter() = first;
return err(rslt.unwrap_err());
}
retval += rslt.unwrap();
}
while(true)
{
auto rslt = T::invoke(loc);
if(rslt.is_err())
{
return ok(std::move(retval));
}
retval += rslt.unwrap();
}
}
static std::string pattern()
{
return concat_to_string('(',T::pattern(), "){", N, ",}");
}
};
template<typename T>
struct repeat<T, unlimited>
{
template<typename Cont>
static result<region<Cont>, std::string> invoke(location<Cont>& loc)
{
region<Cont> retval(loc);
while(true)
{
auto rslt = T::invoke(loc);
if(rslt.is_err())
{
return ok(std::move(retval));
}
retval += rslt.unwrap();
}
}
static std::string pattern() {return concat_to_string('(', T::pattern(), ")*");}
};
} // detail
} // toml
#endif// TOML11_COMBINATOR_HPP

View File

@@ -1,231 +1,540 @@
// Copyright Toru Niina 2017.
// Distributed under the MIT License.
#ifndef TOML11_DATETIME
#define TOML11_DATETIME
#include <chrono>
#include <tuple>
#include <array>
#include <ostream>
#include <iomanip>
#include <cstdint>
#include <cstdlib>
#include <ctime>
namespace toml
{
template<typename unsignedT, typename intT>
struct basic_datetime
enum class month_t : std::int8_t
{
typedef unsignedT number_type;
typedef intT offset_type;
constexpr static unsignedT undef = std::numeric_limits<unsignedT>::max();
constexpr static intT nooffset = std::numeric_limits<intT>::max();
Jan = 0,
Feb = 1,
Mar = 2,
Apr = 3,
May = 4,
Jun = 5,
Jul = 6,
Aug = 7,
Sep = 8,
Oct = 9,
Nov = 10,
Dec = 11
};
unsignedT year;
unsignedT month;
unsignedT day;
unsignedT hour;
unsignedT minute;
unsignedT second;
unsignedT millisecond;
unsignedT microsecond;
intT offset_hour;
intT offset_minute;
struct local_date
{
std::int16_t year; // A.D. (like, 2018)
std::uint8_t month; // [0, 11]
std::uint8_t day; // [1, 31]
basic_datetime() = default;
~basic_datetime() = default;
basic_datetime(const basic_datetime&) = default;
basic_datetime(basic_datetime&&) = default;
basic_datetime& operator=(const basic_datetime&) = default;
basic_datetime& operator=(basic_datetime&&) = default;
basic_datetime(unsignedT y, unsignedT m, unsignedT d)
: year(y), month(m), day(d), hour(undef), minute(undef), second(undef),
millisecond(undef), microsecond(undef),
offset_hour(nooffset), offset_minute(nooffset)
{}
basic_datetime(unsignedT h, unsignedT m, unsignedT s,
unsignedT ms, unsignedT us)
: year(undef), month(undef), day(undef), hour(h), minute(m), second(s),
millisecond(ms), microsecond(us),
offset_hour(nooffset), offset_minute(nooffset)
{}
basic_datetime(unsignedT y, unsignedT mth, unsignedT d,
unsignedT h, unsignedT min, unsignedT s,
unsignedT ms, unsignedT us)
: year(y), month(mth), day(d), hour(h), minute(min), second(s),
millisecond(ms), microsecond(us),
offset_hour(nooffset), offset_minute(nooffset)
{}
basic_datetime(unsignedT y, unsignedT mth, unsignedT d,
unsignedT h, unsignedT min, unsignedT s,
unsignedT ss, unsignedT us, intT oh, intT om)
: year(y), month(mth), day(d), hour(h), minute(min), second(s),
millisecond(ss), microsecond(us), offset_hour(oh), offset_minute(om)
local_date(int y, month_t m, int d)
: year (static_cast<std::int16_t>(y)),
month(static_cast<std::uint8_t>(m)),
day (static_cast<std::uint8_t>(d))
{}
basic_datetime(std::chrono::system_clock::time_point tp);
basic_datetime(std::time_t t);
explicit local_date(const std::tm& t)
: year (static_cast<std::int16_t>(t.tm_year + 1900)),
month(static_cast<std::uint8_t>(t.tm_mon)),
day (static_cast<std::uint8_t>(t.tm_mday))
{}
explicit local_date(const std::chrono::system_clock::time_point& tp)
{
const auto t = std::chrono::system_clock::to_time_t(tp);
const auto tmp = std::localtime(&t); //XXX: not threadsafe!
assert(tmp); // if std::localtime fails, tmp is nullptr
const std::tm time = *tmp;
*this = local_date(time);
}
explicit local_date(const std::time_t t)
: local_date(std::chrono::system_clock::from_time_t(t))
{}
operator std::chrono::system_clock::time_point() const
{
std::tm time;
if(this->year == undef || this->month == undef || this->day == undef)
{
const auto now = std::chrono::system_clock::now();
const auto t = std::chrono::system_clock::to_time_t(now);
std::tm* t_ = std::localtime(&t);
time.tm_year = t_->tm_year;
time.tm_mon = t_->tm_mon;
time.tm_mday = t_->tm_mday;
}
else
{
time.tm_year = this->year - 1900;
time.tm_mon = this->month - 1;
time.tm_mday = this->day;
}
time.tm_hour = (this->hour == undef) ? 0 : this->hour;
time.tm_min = (this->minute == undef) ? 0 : this->minute;
time.tm_sec = (this->second == undef) ? 0 : this->second;
auto tp = std::chrono::system_clock::from_time_t(std::mktime(&time));
tp += std::chrono::milliseconds(this->millisecond);
tp += std::chrono::microseconds(this->microsecond);
// mktime regards the tm struct as localtime. so adding offset is not needed.
return tp;
// std::mktime returns date as local time zone. no conversion needed
std::tm t;
t.tm_sec = 0;
t.tm_min = 0;
t.tm_hour = 0;
t.tm_mday = this->day;
t.tm_mon = this->month;
t.tm_year = this->year - 1900;
t.tm_wday = 0; // the value will be ignored
t.tm_yday = 0; // the value will be ignored
t.tm_isdst = -1;
return std::chrono::system_clock::from_time_t(std::mktime(&t));
}
operator std::time_t() const
{
return std::chrono::system_clock::to_time_t(
std::chrono::system_clock::time_point(*this));
std::chrono::system_clock::time_point(*this));
}
local_date() = default;
~local_date() = default;
local_date(local_date const&) = default;
local_date(local_date&&) = default;
local_date& operator=(local_date const&) = default;
local_date& operator=(local_date&&) = default;
};
template<typename uT, typename iT>
basic_datetime<uT, iT>::basic_datetime(std::chrono::system_clock::time_point tp)
inline bool operator==(const local_date& lhs, const local_date& rhs)
{
const auto t = std::chrono::system_clock::to_time_t(tp);
std::tm *time = std::localtime(&t);
this->year = time->tm_year + 1900;
this->month = time->tm_mon + 1;
this->day = time->tm_mday;
this->hour = time->tm_hour;
this->minute = time->tm_min;
this->second = time->tm_sec;
auto t_ = std::chrono::system_clock::from_time_t(std::mktime(time));
auto diff = tp - t_;
this->millisecond = std::chrono::duration_cast<std::chrono::milliseconds
>(diff).count() % 1000;
this->microsecond = std::chrono::duration_cast<std::chrono::microseconds
>(diff).count() % 1000;
std::tm *utc = std::gmtime(&t);
int total_offset = (this->hour - utc->tm_hour) * 60 +
(this->minute - utc->tm_min);
if(total_offset > 720) total_offset -= 1440;
else if(total_offset < -720) total_offset += 1440;
offset_hour = total_offset / 60;
offset_minute = total_offset - (offset_hour * 60);
return std::make_tuple(lhs.year, lhs.month, lhs.day) ==
std::make_tuple(rhs.year, rhs.month, rhs.day);
}
inline bool operator!=(const local_date& lhs, const local_date& rhs)
{
return !(lhs == rhs);
}
inline bool operator< (const local_date& lhs, const local_date& rhs)
{
return std::make_tuple(lhs.year, lhs.month, lhs.day) <
std::make_tuple(rhs.year, rhs.month, rhs.day);
}
inline bool operator<=(const local_date& lhs, const local_date& rhs)
{
return (lhs < rhs) || (lhs == rhs);
}
inline bool operator> (const local_date& lhs, const local_date& rhs)
{
return !(lhs <= rhs);
}
inline bool operator>=(const local_date& lhs, const local_date& rhs)
{
return !(lhs < rhs);
}
template<typename uT, typename iT>
basic_datetime<uT, iT>::basic_datetime(std::time_t t)
{
*this = basic_datetime(std::chrono::system_clock::from_time_t(t));
}
template<typename charT, typename traits, typename uT, typename iT>
template<typename charT, typename traits>
std::basic_ostream<charT, traits>&
operator<<(std::basic_ostream<charT, traits>& os, basic_datetime<uT, iT> const& dt)
operator<<(std::basic_ostream<charT, traits>& os, const local_date& date)
{
bool date = false;
if(dt.year != basic_datetime<uT, iT>::undef &&
dt.month != basic_datetime<uT, iT>::undef &&
dt.day != basic_datetime<uT, iT>::undef)
os << std::setfill('0') << std::setw(4) << static_cast<int>(date.year ) << '-';
os << std::setfill('0') << std::setw(2) << static_cast<int>(date.month + 1) << '-';
os << std::setfill('0') << std::setw(2) << static_cast<int>(date.day );
return os;
}
struct local_time
{
std::uint8_t hour; // [0, 23]
std::uint8_t minute; // [0, 59]
std::uint8_t second; // [0, 60]
std::uint16_t millisecond; // [0, 999]
std::uint16_t microsecond; // [0, 999]
std::uint16_t nanosecond; // [0, 999]
local_time(int h, int m, int s,
int ms = 0, int us = 0, int ns = 0)
: hour (static_cast<std::uint8_t>(h)),
minute(static_cast<std::uint8_t>(m)),
second(static_cast<std::uint8_t>(s)),
millisecond(static_cast<std::uint16_t>(ms)),
microsecond(static_cast<std::uint16_t>(us)),
nanosecond (static_cast<std::uint16_t>(ns))
{}
explicit local_time(const std::tm& t)
: hour (static_cast<std::uint8_t>(t.tm_hour)),
minute(static_cast<std::uint8_t>(t.tm_min)),
second(static_cast<std::uint8_t>(t.tm_sec)),
millisecond(0), microsecond(0), nanosecond(0)
{}
template<typename Rep, typename Period>
explicit local_time(const std::chrono::duration<Rep, Period>& t)
{
os << std::setfill('0') << std::setw(4) << dt.year << '-'
<< std::setfill('0') << std::setw(2) << dt.month << '-'
<< std::setfill('0') << std::setw(2) << dt.day;
date = true;
const auto h = std::chrono::duration_cast<std::chrono::hours>(t);
this->hour = h.count();
const auto t2 = t - h;
const auto m = std::chrono::duration_cast<std::chrono::minutes>(t2);
this->minute = m.count();
const auto t3 = t2 - m;
const auto s = std::chrono::duration_cast<std::chrono::seconds>(t3);
this->second = s.count();
const auto t4 = t3 - s;
const auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(t4);
this->millisecond = ms.count();
const auto t5 = t4 - ms;
const auto us = std::chrono::duration_cast<std::chrono::microseconds>(t5);
this->microsecond = us.count();
const auto t6 = t5 - us;
const auto ns = std::chrono::duration_cast<std::chrono::nanoseconds>(t6);
this->nanosecond = ns.count();
}
if(dt.hour != basic_datetime<uT, iT>::undef &&
dt.minute != basic_datetime<uT, iT>::undef &&
dt.second != basic_datetime<uT, iT>::undef)
operator std::chrono::nanoseconds() const
{
if(date) os << 'T';
os << std::setfill('0') << std::setw(2) << dt.hour << ':'
<< std::setfill('0') << std::setw(2) << dt.minute << ':'
<< std::setfill('0') << std::setw(2) << dt.second << '.'
<< std::setfill('0') << std::setw(3) << dt.millisecond
<< std::setfill('0') << std::setw(3) << dt.microsecond;
return std::chrono::nanoseconds (this->nanosecond) +
std::chrono::microseconds(this->microsecond) +
std::chrono::milliseconds(this->millisecond) +
std::chrono::seconds(this->second) +
std::chrono::minutes(this->minute) +
std::chrono::hours(this->hour);
}
if(dt.offset_hour != basic_datetime<uT, iT>::nooffset &&
dt.offset_minute != basic_datetime<uT, iT>::nooffset)
local_time() = default;
~local_time() = default;
local_time(local_time const&) = default;
local_time(local_time&&) = default;
local_time& operator=(local_time const&) = default;
local_time& operator=(local_time&&) = default;
};
inline bool operator==(const local_time& lhs, const local_time& rhs)
{
return std::make_tuple(lhs.hour, lhs.minute, lhs.second, lhs.millisecond, lhs.microsecond, lhs.nanosecond) ==
std::make_tuple(rhs.hour, rhs.minute, rhs.second, rhs.millisecond, rhs.microsecond, rhs.nanosecond);
}
inline bool operator!=(const local_time& lhs, const local_time& rhs)
{
return !(lhs == rhs);
}
inline bool operator< (const local_time& lhs, const local_time& rhs)
{
return std::make_tuple(lhs.hour, lhs.minute, lhs.second, lhs.millisecond, lhs.microsecond, lhs.nanosecond) <
std::make_tuple(rhs.hour, rhs.minute, rhs.second, rhs.millisecond, rhs.microsecond, rhs.nanosecond);
}
inline bool operator<=(const local_time& lhs, const local_time& rhs)
{
return (lhs < rhs) || (lhs == rhs);
}
inline bool operator> (const local_time& lhs, const local_time& rhs)
{
return !(lhs <= rhs);
}
inline bool operator>=(const local_time& lhs, const local_time& rhs)
{
return !(lhs < rhs);
}
template<typename charT, typename traits>
std::basic_ostream<charT, traits>&
operator<<(std::basic_ostream<charT, traits>& os, const local_time& time)
{
os << std::setfill('0') << std::setw(2) << static_cast<int>(time.hour ) << ':';
os << std::setfill('0') << std::setw(2) << static_cast<int>(time.minute) << ':';
os << std::setfill('0') << std::setw(2) << static_cast<int>(time.second);
if(time.millisecond != 0 || time.microsecond != 0 || time.nanosecond != 0)
{
if(dt.offset_hour == 0 && dt.offset_minute == 0)
os << '.';
os << std::setfill('0') << std::setw(3) << static_cast<int>(time.millisecond);
if(time.microsecond != 0 || time.nanosecond != 0)
{
os << 'Z';
}
else
{
char sign = ' ';
iT oh = dt.offset_hour;
iT om = dt.offset_minute;
om += oh * 60;
if(om > 0) sign = '+'; else sign='-';
oh = om / 60;
om -= oh * 60;
os << sign << std::setfill('0') << std::setw(2) << std::abs(oh) << ':'
<< std::setfill('0') << std::setw(2) << std::abs(om);
os << std::setfill('0') << std::setw(3) << static_cast<int>(time.microsecond);
if(time.nanosecond != 0)
{
os << std::setfill('0') << std::setw(3) << static_cast<int>(time.nanosecond);
}
}
}
return os;
}
template<typename uT, typename iT>
inline bool
operator==(basic_datetime<uT, iT> const& lhs, basic_datetime<uT, iT> const& rhs)
struct time_offset
{
return lhs.year == rhs.year && lhs.month == rhs.month &&
lhs.day == rhs.day && lhs.minute == rhs.minute &&
lhs.second == rhs.second && lhs.millisecond == rhs.millisecond &&
lhs.microsecond == rhs.microsecond &&
lhs.offset_hour == rhs.offset_hour &&
lhs.offset_minute == rhs.offset_minute;
}
std::int8_t hour; // [-12, 12]
std::int8_t minute; // [-59, 59]
template<typename uT, typename iT>
inline bool
operator!=(basic_datetime<uT, iT> const& lhs, basic_datetime<uT, iT> const& rhs)
time_offset(int h, int m)
: hour (static_cast<std::int8_t>(h)),
minute(static_cast<std::int8_t>(m))
{}
operator std::chrono::minutes() const
{
return std::chrono::minutes(this->minute) +
std::chrono::hours(this->hour);
}
time_offset() = default;
~time_offset() = default;
time_offset(time_offset const&) = default;
time_offset(time_offset&&) = default;
time_offset& operator=(time_offset const&) = default;
time_offset& operator=(time_offset&&) = default;
};
inline bool operator==(const time_offset& lhs, const time_offset& rhs)
{
return std::make_tuple(lhs.hour, lhs.minute) ==
std::make_tuple(rhs.hour, rhs.minute);
}
inline bool operator!=(const time_offset& lhs, const time_offset& rhs)
{
return !(lhs == rhs);
}
template<typename uT, typename iT>
inline bool
operator<(basic_datetime<uT, iT> const& lhs, basic_datetime<uT, iT> const& rhs)
inline bool operator< (const time_offset& lhs, const time_offset& rhs)
{
return std::time_t(lhs) < std::time_t(rhs);
return std::make_tuple(lhs.hour, lhs.minute) <
std::make_tuple(rhs.hour, rhs.minute);
}
inline bool operator<=(const time_offset& lhs, const time_offset& rhs)
{
return (lhs < rhs) || (lhs == rhs);
}
inline bool operator> (const time_offset& lhs, const time_offset& rhs)
{
return !(lhs <= rhs);
}
inline bool operator>=(const time_offset& lhs, const time_offset& rhs)
{
return !(lhs < rhs);
}
template<typename uT, typename iT>
inline bool
operator<=(basic_datetime<uT, iT> const& lhs, basic_datetime<uT, iT> const& rhs)
template<typename charT, typename traits>
std::basic_ostream<charT, traits>&
operator<<(std::basic_ostream<charT, traits>& os, const time_offset& offset)
{
return std::time_t(lhs) <= std::time_t(rhs);
if(offset.hour == 0 && offset.minute == 0)
{
os << 'Z';
return os;
}
if(static_cast<int>(offset.hour) * static_cast<int>(offset.minute) < 0)
{
const int min = static_cast<int>(offset.hour) * 60 + offset.minute;
if(min < 0){os << '-';} else {os << '+';}
os << std::setfill('0') << std::setw(2) << min / 60 << ':';
os << std::setfill('0') << std::setw(2) << min % 60;
return os;
}
if(offset.hour < 0){os << '-';} else {os << '+';}
os << std::setfill('0') << std::setw(2) << static_cast<int>(offset.hour) << ':';
os << std::setfill('0') << std::setw(2) << static_cast<int>(offset.minute);
return os;
}
template<typename uT, typename iT>
inline bool
operator>(basic_datetime<uT, iT> const& lhs, basic_datetime<uT, iT> const& rhs)
struct local_datetime
{
return std::time_t(lhs) > std::time_t(rhs);
local_date date;
local_time time;
local_datetime(local_date d, local_time t): date(d), time(t) {}
explicit local_datetime(const std::tm& t): date(t), time(t){}
explicit local_datetime(const std::chrono::system_clock::time_point& tp)
{
const auto t = std::chrono::system_clock::to_time_t(tp);
const auto tmp = std::localtime(&t); //XXX: not threadsafe!
assert(tmp); // if std::localtime fails, tmp is nullptr
std::tm time = *tmp;
this->date = local_date(time);
this->time = local_time(time);
// std::tm lacks subsecond information, so diff between tp and tm
// can be used to get millisecond & microsecond information.
const auto t_diff = tp -
std::chrono::system_clock::from_time_t(std::mktime(&time));
this->time.millisecond = std::chrono::duration_cast<
std::chrono::milliseconds>(t_diff).count();
this->time.microsecond = std::chrono::duration_cast<
std::chrono::microseconds>(t_diff).count();
this->time.nanosecond = std::chrono::duration_cast<
std::chrono::nanoseconds >(t_diff).count();
}
explicit local_datetime(const std::time_t t)
: local_datetime(std::chrono::system_clock::from_time_t(t))
{}
operator std::chrono::system_clock::time_point() const
{
using internal_duration =
typename std::chrono::system_clock::time_point::duration;
// std::mktime returns date as local time zone. no conversion needed
auto dt = std::chrono::system_clock::time_point(this->date);
dt += std::chrono::duration_cast<internal_duration>(
std::chrono::nanoseconds(this->time));
return dt;
}
operator std::time_t() const
{
return std::chrono::system_clock::to_time_t(
std::chrono::system_clock::time_point(*this));
}
local_datetime() = default;
~local_datetime() = default;
local_datetime(local_datetime const&) = default;
local_datetime(local_datetime&&) = default;
local_datetime& operator=(local_datetime const&) = default;
local_datetime& operator=(local_datetime&&) = default;
};
inline bool operator==(const local_datetime& lhs, const local_datetime& rhs)
{
return std::make_tuple(lhs.date, lhs.time) ==
std::make_tuple(rhs.date, rhs.time);
}
inline bool operator!=(const local_datetime& lhs, const local_datetime& rhs)
{
return !(lhs == rhs);
}
inline bool operator< (const local_datetime& lhs, const local_datetime& rhs)
{
return std::make_tuple(lhs.date, lhs.time) <
std::make_tuple(rhs.date, rhs.time);
}
inline bool operator<=(const local_datetime& lhs, const local_datetime& rhs)
{
return (lhs < rhs) || (lhs == rhs);
}
inline bool operator> (const local_datetime& lhs, const local_datetime& rhs)
{
return !(lhs <= rhs);
}
inline bool operator>=(const local_datetime& lhs, const local_datetime& rhs)
{
return !(lhs < rhs);
}
template<typename uT, typename iT>
inline bool
operator>=(basic_datetime<uT, iT> const& lhs, basic_datetime<uT, iT> const& rhs)
template<typename charT, typename traits>
std::basic_ostream<charT, traits>&
operator<<(std::basic_ostream<charT, traits>& os, const local_datetime& dt)
{
return std::time_t(lhs) >= std::time_t(rhs);
os << dt.date << 'T' << dt.time;
return os;
}
struct offset_datetime
{
local_date date;
local_time time;
time_offset offset;
offset_datetime(local_date d, local_time t, time_offset o)
: date(d), time(t), offset(o)
{}
offset_datetime(const local_datetime& dt, time_offset o)
: date(dt.date), time(dt.time), offset(o)
{}
explicit offset_datetime(const local_datetime& ld)
: date(ld.date), time(ld.time), offset(get_local_offset())
{}
explicit offset_datetime(const std::chrono::system_clock::time_point& tp)
: offset_datetime(local_datetime(tp))
{}
explicit offset_datetime(const std::time_t& t)
: offset_datetime(local_datetime(t))
{}
explicit offset_datetime(const std::tm& t)
: offset_datetime(local_datetime(t))
{}
operator std::chrono::system_clock::time_point() const
{
// get date-time
using internal_duration =
typename std::chrono::system_clock::time_point::duration;
std::chrono::system_clock::time_point tp =
std::chrono::system_clock::time_point(this->date) +
std::chrono::duration_cast<internal_duration>(
std::chrono::nanoseconds(this->time));
// get date-time in UTC. let's say we are in +09:00 (JPN).
// writing 12:00:00 in +09:00 means 03:00:00Z. to represent
// 12:00:00Z, first we need to add +09:00.
const auto ofs = get_local_offset();
tp += std::chrono::hours (ofs.hour);
tp += std::chrono::minutes(ofs.minute);
// here, tp represents 12:00:00 in UTC but we have offset information.
// we need to subtract it. For example, let's say the input is
// 12:00:00-08:00. now we have tp = 12:00:00Z as a result of the above
// conversion. But the actual time we need to return is 20:00:00Z
// because of -08:00.
tp -= std::chrono::minutes(this->offset);
return tp;
}
operator std::time_t() const
{
return std::chrono::system_clock::to_time_t(
std::chrono::system_clock::time_point(*this));
}
offset_datetime() = default;
~offset_datetime() = default;
offset_datetime(offset_datetime const&) = default;
offset_datetime(offset_datetime&&) = default;
offset_datetime& operator=(offset_datetime const&) = default;
offset_datetime& operator=(offset_datetime&&) = default;
private:
static time_offset get_local_offset()
{
// get current timezone
const auto tmp1 = std::time(nullptr);
const auto tmp2 = std::localtime(&tmp1); // XXX not threadsafe!
assert(tmp2);
std::tm t = *tmp2;
std::array<char, 6> buf;
const auto result = std::strftime(buf.data(), 6, "%z", &t); // +hhmm\0
if(result != 5)
{
throw std::runtime_error("toml::offset_datetime: cannot obtain "
"timezone information of current env");
}
const int ofs = std::atoi(buf.data());
const int ofs_h = ofs / 100;
const int ofs_m = ofs - (ofs_h * 100);
return time_offset(ofs_h, ofs_m);
}
};
inline bool operator==(const offset_datetime& lhs, const offset_datetime& rhs)
{
return std::make_tuple(lhs.date, lhs.time, lhs.offset) ==
std::make_tuple(rhs.date, rhs.time, rhs.offset);
}
inline bool operator!=(const offset_datetime& lhs, const offset_datetime& rhs)
{
return !(lhs == rhs);
}
inline bool operator< (const offset_datetime& lhs, const offset_datetime& rhs)
{
return std::make_tuple(lhs.date, lhs.time, lhs.offset) <
std::make_tuple(rhs.date, rhs.time, rhs.offset);
}
inline bool operator<=(const offset_datetime& lhs, const offset_datetime& rhs)
{
return (lhs < rhs) || (lhs == rhs);
}
inline bool operator> (const offset_datetime& lhs, const offset_datetime& rhs)
{
return !(lhs <= rhs);
}
inline bool operator>=(const offset_datetime& lhs, const offset_datetime& rhs)
{
return !(lhs < rhs);
}
template<typename charT, typename traits>
std::basic_ostream<charT, traits>&
operator<<(std::basic_ostream<charT, traits>& os, const offset_datetime& dt)
{
os << dt.date << 'T' << dt.time << dt.offset;
return os;
}
}//toml
#endif// TOML11_DATETIME

View File

@@ -1,3 +1,5 @@
// Copyright Toru Niina 2017.
// Distributed under the MIT License.
#ifndef TOML11_EXCEPTION
#define TOML11_EXCEPTION
#include <stdexcept>

View File

@@ -1,386 +0,0 @@
#ifndef TOML11_FORMAT
#define TOML11_FORMAT
#include "value.hpp"
#include <type_traits>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <locale>
namespace toml
{
// synopsis
// toml::format("key", value, toml::make_inline(80))
// toml::format("key", value, toml::forceinline)
// std::cout << toml::make_inline(80) << value;
// std::cout << toml::forceinline << value;
template<typename traits = std::char_traits<toml::character>,
typename alloc = std::allocator<toml::character>>
std::basic_string<toml::character, traits, alloc>
format(const value& v);
template<typename traits = std::char_traits<toml::character>,
typename alloc = std::allocator<toml::character>>
std::basic_string<toml::character, traits, alloc>
format(const value& v, std::size_t mk);
template<typename traits = std::char_traits<toml::character>,
typename alloc = std::allocator<toml::character>>
std::basic_string<toml::character, traits, alloc>
format(const toml::key& k, const value& v);
template<typename traits = std::char_traits<toml::character>,
typename alloc = std::allocator<toml::character>>
std::basic_string<toml::character, traits, alloc>
format(const toml::key& k, const value& v, std::size_t mk);
template<value_t Type>
struct format_impl;
template<> struct format_impl<value_t::Boolean>
{
typedef detail::toml_default_type<value_t::Boolean>::type type;
std::basic_string<toml::character>
operator()(const type& val)
{
return val ? "true" : "false";
}
};
template<> struct format_impl<value_t::Integer>
{
typedef detail::toml_default_type<value_t::Integer>::type type;
std::basic_string<toml::character>
operator()(const type& val)
{
return std::to_string(val);
}
};
template<> struct format_impl<value_t::Float>
{
typedef detail::toml_default_type<value_t::Float>::type type;
std::basic_string<toml::character>
operator()(const type& val)
{
std::basic_ostringstream<toml::character> oss;
oss << std::showpoint << val;
if(oss.str().back() == '.') oss << '0';
return oss.str();
}
};
template<> struct format_impl<value_t::String>
{
typedef detail::toml_default_type<value_t::String>::type type;
std::size_t max_length;
format_impl() : max_length(80){}
format_impl(std::size_t mx) : max_length(mx){}
std::basic_string<toml::character>
operator()(const type& val)
{
auto tmp = make_inline(val);
if(max_length == std::numeric_limits<std::size_t>::max() ||
tmp.size() <= max_length) return tmp;
return convert_multiline(std::move(tmp));
}
private:
std::basic_string<toml::character>
make_inline(const std::basic_string<toml::character>& val)
{
std::basic_string<toml::character> str;
str += '"';
for(const auto& c : val)
{
if('\0' < c && c < '\31')
{
switch(c)
{
case '\b': str += "\\b"; break;
case '\t': str += "\\t"; break;
case '\n': str += "\\n"; break;
case '\f': str += "\\f"; break;
case '\r': str += "\\r"; break;
default:
{
str += 'u';
std::basic_ostringstream<toml::character> oss;
oss << std::setw(4) << std::setfill('0') << std::hex
<< static_cast<std::int8_t>(c);
auto hexdig = oss.str();
std::transform(hexdig.begin(), hexdig.end(), hexdig.begin(), ::toupper);
str += oss.str();
break;
}
}
}
else if(c == '"')
{
str += "\\\"";
}
else if(c == '\\')
{
str += "\\\\";
}
else
{
str += c;
}
}
str += '"';
return str;
}
std::basic_string<toml::character>
convert_multiline(std::basic_string<toml::character>&& val)
{
std::basic_string<toml::character> str; str.reserve(val.size() + 6);
str += "\"\"\"\n";
std::size_t current = 0;
for(auto iter = val.begin()+1; iter != val.end()-1; ++iter)
{
if(*iter != '\\')
{
if(current + 1 == max_length) str += "\\\n";
str += *iter; continue;
}
assert(std::next(iter) < val.end()-1);
if(*std::next(iter) == 'u')
{
if(current + 5 == max_length) str += "\\\n";
assert(iter + 5 < val.end()-1);
str += *iter; ++iter; // u
str += *iter; ++iter; // 0
str += *iter; ++iter; // 1
str += *iter; ++iter; // 2
str += *iter; continue;// 3
}
if(current + 2 == max_length) str += "\\\n";
str += *iter; ++iter; str += *iter;
}
str += "\"\"\"";
return str;
}
};
template<> struct format_impl<value_t::Datetime>
{
typedef detail::toml_default_type<value_t::Datetime>::type type;
std::basic_string<toml::character>
operator()(const type& val)
{
std::basic_ostringstream<toml::character> oss;
oss << val;
return oss.str();
}
};
// TODO max length!
template<> struct format_impl<value_t::Array>
{
typedef detail::toml_default_type<value_t::Array>::type type;
std::size_t max_length;
format_impl() : max_length(80){}
format_impl(std::size_t mx) : max_length(mx){}
std::basic_string<toml::character>
operator()(const type& val)
{
std::basic_string<toml::character> retval;
retval += '[';
for(const auto& item : val)
{
auto tmp = format(val, max_length - 1);
retval += tmp;
retval += ", ";
if(tmp.size() * 2 > max_length) retval += '\n';
}
retval += ']';
return retval;
}
};
// TODO max length && inline!
template<> struct format_impl<value_t::Table>
{
typedef detail::toml_default_type<value_t::Table>::type type;
std::size_t max_length;
format_impl() : max_length(80){}
format_impl(std::size_t mx) : max_length(mx){}
std::basic_string<toml::character>
operator()(const type& val)
{
std::basic_string<toml::character> retval;
for(const auto& item : val)
{
retval += item.first;
retval += " = ";
retval += format(item.second);
retval += '\n';
}
return retval;
}
};
template<typename traits, typename alloc>
std::basic_string<toml::character, traits, alloc>
format(const value& v)
{
switch(v.type())
{
case value_t::Boolean : return format_impl<value_t::Boolean >{}(v.template cast<value_t::Boolean >());
case value_t::Integer : return format_impl<value_t::Integer >{}(v.template cast<value_t::Integer >());
case value_t::Float : return format_impl<value_t::Float >{}(v.template cast<value_t::Float >());
case value_t::String : return format_impl<value_t::String >{}(v.template cast<value_t::String >());
case value_t::Datetime: return format_impl<value_t::Datetime>{}(v.template cast<value_t::Datetime>());
case value_t::Array : return format_impl<value_t::Array >{}(v.template cast<value_t::Array >());
case value_t::Table : return format_impl<value_t::Table >{}(v.template cast<value_t::Table >());
case value_t::Empty : throw std::runtime_error("toml::format: empty value");
case value_t::Unknown : throw std::runtime_error("toml::format: unknown value");
default: throw std::logic_error("toml::format: unknown enum value");
}
}
template<typename traits, typename alloc>
std::basic_string<toml::character, traits, alloc>
format(const value& v, std::size_t inl)
{
switch(v.type())
{
case value_t::Boolean : return format_impl<value_t::Boolean >{}(v.template cast<value_t::Boolean>());
case value_t::Integer : return format_impl<value_t::Integer >{}(v.template cast<value_t::Integer>());
case value_t::Float : return format_impl<value_t::Float >{}(v.template cast<value_t::Float>());
case value_t::String : return format_impl<value_t::String >{inl}(v.template cast<value_t::String>());
case value_t::Datetime: return format_impl<value_t::Datetime>{}(v.template cast<value_t::Datetime>());
case value_t::Array : return format_impl<value_t::Array >{inl}(v.template cast<value_t::Array>());
case value_t::Table : return format_impl<value_t::Table >{inl}(v.template cast<value_t::Table>());
case value_t::Empty : throw std::runtime_error("toml::format: empty value");
case value_t::Unknown : throw std::runtime_error("toml::format: unknown value");
default: throw std::logic_error("toml::format: unknown enum value");
}
}
template<typename traits, typename alloc>
std::basic_string<toml::character, traits, alloc>
format(std::basic_string<toml::character, traits, alloc> key, const value& val)
{
std::basic_string<toml::character, traits, alloc> retval(std::move(key));
retval += " = ";
retval += format(val);
return retval;
}
template<typename traits, typename alloc>
std::basic_string<toml::character, traits, alloc>
format(std::basic_string<toml::character, traits, alloc> key, const value& val, std::size_t mk)
{
std::basic_string<toml::character, traits, alloc> retval(std::move(key));
retval += " = ";
retval += format(val, mk);
return retval;
}
// ----------------------------- stream operators -----------------------------
namespace detail
{
template<typename T = std::size_t>
struct inline_limit
{
static_assert(std::is_same<T, std::size_t>::value, "do not instantiate this");
static const int index;
T limit;
inline_limit() = default;
~inline_limit() = default;
constexpr inline_limit(T i): limit(i){}
constexpr operator T() const {return limit;}
static void callback(std::ios_base::event ev, std::ios_base& ios, int idx)
{
void*& info = ios.pword(idx);
switch (ev)
{
case std::ios_base::erase_event:
{
delete static_cast<std::size_t*>(info);
break;
}
case std::ios_base::copyfmt_event:
{
info = new std::size_t(*static_cast<std::size_t*>(info));
break;
}
case std::ios_base::imbue_event:
{
break;
}
}
}
};
template<typename T>
const int inline_limit<T>::index = std::ios_base::xalloc();
} //detail
template<typename sizeT, typename traits = std::char_traits<toml::character>>
std::basic_ostream<toml::character, traits>&
operator<<(std::basic_ostream<toml::character, traits>& os,
const detail::inline_limit<sizeT>& inl)
{
void*& info = os.pword(detail::inline_limit<sizeT>::index);
if(!os.bad())
{
if(info == nullptr)
{
os.register_callback(detail::inline_limit<sizeT>::callback,
detail::inline_limit<sizeT>::index);
info = new std::size_t(inl.limit);
}
else
{
*static_cast<std::size_t*>(info) = inl.limit;
}
}
return os;
}
constexpr static detail::inline_limit<std::size_t> forceinline(
std::numeric_limits<std::size_t>::max());
inline detail::inline_limit<std::size_t> make_inline(std::size_t sz)
{
return detail::inline_limit<std::size_t>(sz);
}
template<typename T, typename traits = std::char_traits<toml::character>>
std::basic_ostream<toml::character, traits>&
operator<<(std::basic_ostream<toml::character, traits>& os,
const toml::value& v)
{
std::size_t* info =
static_cast<std::size_t*>(os.pword(detail::inline_limit<std::size_t>::index));
return os << (info == nullptr ? toml::format(v) : toml::format(v, *info));
}
}
#endif // TOML11_FORMAT

View File

@@ -1,66 +1,16 @@
// Copyright Toru Niina 2017.
// Distributed under the MIT License.
#ifndef TOML11_FROM_TOML
#define TOML11_FROM_TOML
#include "value.hpp"
#include "get.hpp"
namespace toml
{
template<typename T, toml::value_t vT = toml::detail::check_type<T>(),
typename std::enable_if<(vT != toml::value_t::Unknown &&
vT != value_t::Empty), std::nullptr_t>::type = nullptr>
template<typename T>
void from_toml(T& x, const toml::value& v)
{
if(v.type() != vT)
throw type_error("from_toml: value type: " + stringize(v.type()) +
std::string(" is not arguemnt type: ") + stringize(vT));
x = v.cast<vT>();
return;
}
template<typename T, toml::value_t vT = toml::detail::check_type<T>(),
typename std::enable_if<(vT == toml::value_t::Unknown) &&
(!toml::detail::is_map<T>::value) &&
toml::detail::is_container<T>::value, std::nullptr_t>::type = nullptr>
void from_toml(T& x, const toml::value& v)
{
// TODO the case of x is not dynamic container case
if(v.type() != value_t::Array)
throw type_error("from_toml: value type: " + stringize(v.type()) +
std::string(" is not argument type: Array"));
const auto& ar = v.cast<value_t::Array>();
try
{
toml::resize(x, ar.size());
}
catch(std::invalid_argument& iv)
{
throw toml::type_error("toml::from_toml: static array size is not enough");
}
auto iter = x.begin();
for(const auto& val : ar)
{
typename T::value_type v;
from_toml(v, val);
*iter = std::move(v);
++iter;
}
return;
}
template<typename T, toml::value_t vT = toml::detail::check_type<T>(),
typename std::enable_if<(vT == toml::value_t::Unknown) &&
toml::detail::is_map<T>::value, std::nullptr_t>::type = nullptr>
void from_toml(T& x, const toml::value& v)
{
if(v.type() != value_t::Table)
throw type_error("from_toml: value type: " + stringize(v.type()) +
std::string(" is not argument type: Table"));
x.clear();
const auto& tb = v.cast<value_t::Table>();
for(const auto& kv : tb)
{
x.insert(kv);
}
x = toml::get<typename std::remove_reference<T>::type>(v);
return;
}

View File

@@ -1,69 +1,578 @@
// Copyright Toru Niina 2017.
// Distributed under the MIT License.
#ifndef TOML11_GET
#define TOML11_GET
#include "result.hpp"
#include "value.hpp"
#include <algorithm>
namespace toml
{
template<typename T, toml::value_t vT = toml::detail::check_type<T>(),
typename std::enable_if<(vT != toml::value_t::Unknown &&
vT != value_t::Empty), std::nullptr_t>::type = nullptr>
inline T get(const toml::value& v)
// ============================================================================
// exact toml::* type
template<typename T, typename std::enable_if<
detail::is_exact_toml_type<T>::value, std::nullptr_t>::type = nullptr>
inline T& get(value& v)
{
return static_cast<T>(v.cast<vT>());
return v.cast<detail::toml_value_t<T>::value>();
}
// array-like type
template<typename T, toml::value_t vT = toml::detail::check_type<T>(),
typename std::enable_if<(vT == toml::value_t::Unknown) &&
(!toml::detail::is_map<T>::value) &&
toml::detail::is_container<T>::value, std::nullptr_t>::type = nullptr>
T get(const toml::value& v)
template<typename T, typename std::enable_if<
detail::is_exact_toml_type<T>::value, std::nullptr_t>::type = nullptr>
inline T const& get(const value& v)
{
if(v.type() != value_t::Array)
throw type_error("get: value type: " + stringize(v.type()) +
std::string(" is not argument type: Array"));
return v.cast<detail::toml_value_t<T>::value>();
}
template<typename T, typename std::enable_if<
detail::is_exact_toml_type<T>::value, std::nullptr_t>::type = nullptr>
inline T&& get(value&& v)
{
return std::move(v.cast<detail::toml_value_t<T>::value>());
}
// ============================================================================
// integer convertible from toml::Integer
template<typename T, typename std::enable_if<detail::conjunction<
std::is_integral<T>, // T is integral
detail::negation<std::is_same<T, bool>>, // but not bool
detail::negation<detail::is_exact_toml_type<T>> // but not toml::integer
>::value, std::nullptr_t>::type = nullptr>
inline T get(const value& v)
{
return static_cast<T>(v.cast<value_t::Integer>());
}
// ============================================================================
// floating point convertible from toml::Float
template<typename T, typename std::enable_if<detail::conjunction<
std::is_floating_point<T>, // T is floating_point
detail::negation<detail::is_exact_toml_type<T>> // but not toml::Float
>::value, std::nullptr_t>::type = nullptr>
inline T get(const value& v)
{
return static_cast<T>(v.cast<value_t::Float>());
}
// ============================================================================
// std::string; toml uses its own toml::string, but it should be convertible to
// std::string seamlessly
template<typename T, typename std::enable_if<
std::is_same<T, std::string>::value, std::nullptr_t>::type = nullptr>
inline std::string& get(value& v)
{
return v.cast<value_t::String>().str;
}
template<typename T, typename std::enable_if<
std::is_same<T, std::string>::value, std::nullptr_t>::type = nullptr>
inline std::string const& get(const value& v)
{
return v.cast<value_t::String>().str;
}
template<typename T, typename std::enable_if<
std::is_same<T, std::string>::value, std::nullptr_t>::type = nullptr>
inline std::string get(value&& v)
{
return std::move(v.cast<value_t::String>().str);
}
// ============================================================================
// std::chrono::duration from toml::local_time.
template<typename T, typename std::enable_if<
detail::is_chrono_duration<T>::value, std::nullptr_t>::type = nullptr>
inline T get(value& v)
{
return std::chrono::duration_cast<T>(
std::chrono::nanoseconds(v.cast<value_t::LocalTime>()));
}
// ============================================================================
// std::chrono::system_clock::time_point from toml::datetime variants
template<typename T, typename std::enable_if<
std::is_same<std::chrono::system_clock::time_point, T>::value,
std::nullptr_t>::type = nullptr>
inline T get(value& v)
{
switch(v.type())
{
case value_t::LocalDate:
{
return std::chrono::system_clock::time_point(
v.cast<value_t::LocalDate>());
}
case value_t::LocalDatetime:
{
return std::chrono::system_clock::time_point(
v.cast<value_t::LocalDatetime>());
}
default:
{
return std::chrono::system_clock::time_point(
v.cast<value_t::OffsetDatetime>());
}
}
}
// ============================================================================
// forward declaration to use this recursively. ignore this and go ahead.
template<typename T, typename std::enable_if<detail::conjunction<
detail::is_container<T>, // T is container
detail::has_resize_method<T>, // T::resize(N) works
detail::negation<detail::is_exact_toml_type<T>> // but not toml::array
>::value, std::nullptr_t>::type = nullptr>
T get(const value& v);
template<typename T, typename std::enable_if<detail::conjunction<
detail::is_container<T>, // T is container
detail::negation<detail::has_resize_method<T>>, // no T::resize() exists
detail::negation<detail::is_exact_toml_type<T>> // not toml::array
>::value, std::nullptr_t>::type = nullptr>
T get(const value& v);
template<typename T, typename std::enable_if<
detail::is_std_pair<T>::value, std::nullptr_t>::type = nullptr>
T get(const value& v);
template<typename T, typename std::enable_if<
detail::is_std_tuple<T>::value, std::nullptr_t>::type = nullptr>
T get(const value& v);
template<typename T, typename std::enable_if<detail::conjunction<
detail::is_map<T>, // T is map
detail::negation<detail::is_exact_toml_type<T>> // but not toml::table
>::value, std::nullptr_t>::type = nullptr>
T get(const toml::value& v);
// ============================================================================
// array-like types; most likely STL container, like std::vector, etc.
template<typename T, typename std::enable_if<detail::conjunction<
detail::is_container<T>, // T is container
detail::has_resize_method<T>, // T::resize(N) works
detail::negation<detail::is_exact_toml_type<T>> // but not toml::array
>::value, std::nullptr_t>::type>
T get(const value& v)
{
using value_type = typename T::value_type;
const auto& ar = v.cast<value_t::Array>();
T container; container.resize(ar.size());
std::transform(ar.cbegin(), ar.cend(), container.begin(),
[](const value& x){return ::toml::get<value_type>(x);});
return container;
}
// ============================================================================
// array-like types; but does not have resize(); most likely std::array.
template<typename T, typename std::enable_if<detail::conjunction<
detail::is_container<T>, // T is container
detail::negation<detail::has_resize_method<T>>, // no T::resize() exists
detail::negation<detail::is_exact_toml_type<T>> // not toml::array
>::value, std::nullptr_t>::type>
T get(const value& v)
{
using value_type = typename T::value_type;
const auto& ar = v.cast<value_t::Array>();
T container;
if(ar.size() != container.size())
{
throw std::out_of_range(detail::format_underline(concat_to_string(
"[erorr] toml::get specified container size is ", container.size(),
" but there are ", ar.size(), " elements in toml array."),
detail::get_region(v), "here"));
}
std::transform(ar.cbegin(), ar.cend(), container.begin(),
[](const value& x){return ::toml::get<value_type>(x);});
return container;
}
// ============================================================================
// std::pair.
template<typename T, typename std::enable_if<
detail::is_std_pair<T>::value, std::nullptr_t>::type>
T get(const value& v)
{
using first_type = typename T::first_type;
using second_type = typename T::second_type;
const auto& ar = v.cast<value_t::Array>();
T tmp;
try
if(ar.size() != 2)
{
toml::resize(tmp, ar.size());
throw std::out_of_range(detail::format_underline(concat_to_string(
"[erorr] toml::get specified std::pair but there are ", ar.size(),
" elements in toml array."), detail::get_region(v), "here"));
}
catch(std::invalid_argument& iv)
{
throw toml::type_error("toml::get: static array size is not enough");
}
std::transform(ar.cbegin(), ar.cend(), tmp.begin(),
[](toml::value const& elem){return get<typename T::value_type>(elem);});
return tmp;
return std::make_pair(::toml::get<first_type >(ar.at(0)),
::toml::get<second_type>(ar.at(1)));
}
// table-like case
template<typename T, toml::value_t vT = toml::detail::check_type<T>(),
typename std::enable_if<(vT == toml::value_t::Unknown) &&
toml::detail::is_map<T>::value, std::nullptr_t>::type = nullptr>
// ============================================================================
// std::tuple.
namespace detail
{
template<typename T, std::size_t ...I>
T get_tuple_impl(const toml::Array& a, index_sequence<I...>)
{
return std::make_tuple(
::toml::get<typename std::tuple_element<I, T>::type>(a.at(I))...);
}
} // detail
template<typename T, typename std::enable_if<
detail::is_std_tuple<T>::value, std::nullptr_t>::type>
T get(const value& v)
{
const auto& ar = v.cast<value_t::Array>();
if(ar.size() != std::tuple_size<T>::value)
{
throw std::out_of_range(detail::format_underline(concat_to_string(
"[erorr] toml::get specified std::tuple with ",
std::tuple_size<T>::value, "elements, but there are ", ar.size(),
" elements in toml array."), detail::get_region(v), "here"));
}
return detail::get_tuple_impl<T>(ar,
detail::make_index_sequence<std::tuple_size<T>::value>{});
}
// ============================================================================
// map-like types; most likely STL map, like std::map or std::unordered_map.
template<typename T, typename std::enable_if<detail::conjunction<
detail::is_map<T>, // T is map
detail::negation<detail::is_exact_toml_type<T>> // but not toml::table
>::value, std::nullptr_t>::type>
T get(const toml::value& v)
{
if(v.type() != value_t::Table)
throw type_error("get: value type: " + stringize(v.type()) +
std::string(" is not argument type: Table"));
T tmp;
const auto& tb = v.cast<value_t::Table>();
for(const auto& kv : tb){tmp.insert(kv);}
return tmp;
using key_type = typename T::key_type;
using mapped_type = typename T::mapped_type;
static_assert(std::is_convertible<std::string, key_type>::value,
"toml::get only supports map type of which key_type is "
"convertible from std::string.");
T map;
for(const auto& kv : v.cast<value_t::Table>())
{
map[key_type(kv.first)] = ::toml::get<mapped_type>(kv.second);
}
return map;
}
// get_or -----------------------------------------------------------------
// ============================================================================
// find and get
template<typename T>
inline typename std::remove_cv<typename std::remove_reference<T>::type>::type
get_or(const toml::Table& tab, const toml::key& ky, T&& opt)
decltype(::toml::get<T>(std::declval<const ::toml::value&>()))
find(const toml::table& tab, const toml::key& ky,
std::string tablename = "unknown table")
{
if(tab.count(ky) == 0)
{
throw std::out_of_range(concat_to_string("[error] key \"", ky,
"\" not found in ", tablename));
}
return ::toml::get<T>(tab.at(ky));
}
template<typename T>
decltype(::toml::get<T>(std::declval<::toml::value&>()))
find(toml::table& tab, const toml::key& ky,
std::string tablename = "unknown table")
{
if(tab.count(ky) == 0)
{
throw std::out_of_range(concat_to_string("[error] key \"", ky,
"\" not found in ", tablename));
}
return ::toml::get<T>(tab[ky]);
}
template<typename T>
decltype(::toml::get<T>(std::declval<::toml::value&&>()))
find(toml::table&& tab, const toml::key& ky,
std::string tablename = "unknown table")
{
if(tab.count(ky) == 0)
{
throw std::out_of_range(concat_to_string("[error] key \"", ky,
"\" not found in ", tablename));
}
return ::toml::get<T>(std::move(tab[ky]));
}
template<typename T>
decltype(::toml::get<T>(std::declval<const ::toml::value&>()))
find(const toml::value& v, const toml::key& ky)
{
const auto& tab = ::toml::get<toml::table>(v);
if(tab.count(ky) == 0)
{
throw std::out_of_range(detail::format_underline(concat_to_string(
"[error] key \"", ky, "\" not found"), detail::get_region(v),
"in this table"));
}
return ::toml::get<T>(tab.at(ky));
}
template<typename T>
decltype(::toml::get<T>(std::declval<::toml::value&>()))
find(toml::value& v, const toml::key& ky)
{
auto& tab = ::toml::get<toml::table>(v);
if(tab.count(ky) == 0)
{
throw std::out_of_range(detail::format_underline(concat_to_string(
"[error] key \"", ky, "\" not found"), detail::get_region(v),
"in this table"));
}
return ::toml::get<T>(tab.at(ky));
}
template<typename T>
decltype(::toml::get<T>(std::declval<::toml::value&&>()))
find(toml::value&& v, const toml::key& ky)
{
auto tab = ::toml::get<toml::table>(std::move(v));
if(tab.count(ky) == 0)
{
throw std::out_of_range(detail::format_underline(concat_to_string(
"[error] key \"", ky, "\" not found"), detail::get_region(v),
"in this table"));
}
return ::toml::get<T>(std::move(tab[ky]));
}
// ============================================================================
// get_or
template<typename T>
decltype(::toml::get<typename std::remove_cv<
typename std::remove_reference<T>::type>::type>(
std::declval<const toml::value&>()))
get_or(const toml::value& v, T&& opt)
{
try
{
return get<typename std::remove_cv<
typename std::remove_reference<T>::type>::type>(v);
}
catch(...)
{
return std::forward<T>(opt);
}
}
template<typename T>
decltype(::toml::get<typename std::remove_cv<
typename std::remove_reference<T>::type>::type>(
std::declval<toml::value&>()))
get_or(toml::value& v, T&& opt)
{
try
{
return get<typename std::remove_cv<
typename std::remove_reference<T>::type>::type>(v);
}
catch(...)
{
return std::forward<T>(opt);
}
}
template<typename T>
decltype(::toml::get<typename std::remove_cv<
typename std::remove_reference<T>::type>::type>(
std::declval<toml::value&&>()))
get_or(toml::value&& v, T&& opt)
{
try
{
return get<typename std::remove_cv<
typename std::remove_reference<T>::type>::type>(std::move(v));
}
catch(...)
{
return std::forward<T>(opt);
}
}
template<typename T>
auto get_or(const toml::table& tab, const toml::key& ky, T&& opt)
-> decltype(get_or(std::declval<value const&>(), std::forward<T>(opt)))
{
if(tab.count(ky) == 0) {return std::forward<T>(opt);}
return get<typename std::remove_cv<
typename std::remove_reference<T>::type>::type>(tab.find(ky)->second);
return ::toml::get_or(tab.at(ky), std::forward<T>(opt));
}
template<typename T>
auto get_or(toml::table& tab, const toml::key& ky, T&& opt)
-> decltype(get_or(std::declval<value&>(), std::forward<T>(opt)))
{
if(tab.count(ky) == 0) {return std::forward<T>(opt);}
return ::toml::get_or(tab[ky], std::forward<T>(opt));
}
template<typename T>
auto get_or(toml::table&& tab, const toml::key& ky, T&& opt)
-> decltype(get_or(std::declval<value&&>(), std::forward<T>(opt)))
{
if(tab.count(ky) == 0) {return std::forward<T>(opt);}
return ::toml::get_or(std::move(tab[ky]), std::forward<T>(opt));
}
template<typename T>
auto get_or(const toml::value& v, const toml::key& ky, T&& opt)
-> decltype(get_or(std::declval<value const&>(), std::forward<T>(opt)))
{
if(v.type() != toml::value_t::Table){return std::forward<T>(opt);}
const auto& tab = toml::get<toml::table>(v);
if(tab.count(ky) == 0) {return std::forward<T>(opt);}
return ::toml::get_or(tab.at(ky), std::forward<T>(opt));
}
template<typename T>
auto get_or(toml::value& v, const toml::key& ky, T&& opt)
-> decltype(get_or(std::declval<value&>(), std::forward<T>(opt)))
{
if(v.type() != toml::value_t::Table){return std::forward<T>(opt);}
auto& tab = toml::get<toml::table>(v);
if(tab.count(ky) == 0) {return std::forward<T>(opt);}
return ::toml::get_or(tab[ky], std::forward<T>(opt));
}
template<typename T>
auto get_or(toml::value&& v, const toml::key& ky, T&& opt)
-> decltype(get_or(std::declval<value&&>(), std::forward<T>(opt)))
{
if(v.type() != toml::value_t::Table){return std::forward<T>(opt);}
auto tab = toml::get<toml::table>(std::move(v));
if(tab.count(ky) == 0) {return std::forward<T>(opt);}
return ::toml::get_or(std::move(tab[ky]), std::forward<T>(opt));
}
// ============================================================================
// expect
template<typename T>
auto expect(const toml::value& v)
-> result<decltype(::toml::get<T>(v)), std::string>
{
try
{
return ok(get<T>(v));
}
catch(const type_error& te)
{
return err(te.what());
}
}
template<typename T>
auto expect(toml::value& v)
-> result<decltype(::toml::get<T>(v)), std::string>
{
try
{
return ok(get<T>(v));
}
catch(const type_error& te)
{
return err(te.what());
}
}
template<typename T>
auto expect(toml::value&& v)
-> result<decltype(::toml::get<T>(std::move(v))), std::string>
{
try
{
return ok(get<T>(std::move(v)));
}
catch(const type_error& te)
{
return err(te.what());
}
}
template<typename T>
auto expect(const toml::value& v, const toml::key& k)
-> result<decltype(::toml::get<T>(v, k)), std::string>
{
try
{
return ok(get<T>(v, k));
}
catch(const std::exception& e)
{
return err(e.what());
}
}
template<typename T>
auto expect(toml::value& v, const toml::key& k)
-> result<decltype(::toml::get<T>(v, k)), std::string>
{
try
{
return ok(get<T>(v, k));
}
catch(const std::exception& e)
{
return err(e.what());
}
}
template<typename T>
auto expect(toml::value&& v, const toml::key& k)
-> result<decltype(::toml::get<T>(std::move(v), k)), std::string>
{
try
{
return ok(get<T>(std::move(v), k));
}
catch(const std::exception& e)
{
return err(e.what());
}
}
template<typename T>
auto expect(const toml::table& t, const toml::key& k, std::string tn)
-> result<decltype(::toml::get<T>(t, k, std::move(tn))), std::string>
{
try
{
return ok(get<T>(t, k, std::move(tn)));
}
catch(const std::exception& e)
{
return err(e.what());
}
}
template<typename T>
auto expect(toml::table& t, const toml::key& k, std::string tn)
-> result<decltype(::toml::get<T>(t, k, std::move(tn))), std::string>
{
try
{
return ok(get<T>(t, k, std::move(tn)));
}
catch(const std::exception& e)
{
return err(e.what());
}
}
template<typename T>
auto expect(toml::table&& t, const toml::key& k, std::string tn)
-> result<decltype(::toml::get<T>(std::move(t), k, std::move(tn))), std::string>
{
try
{
return ok(get<T>(std::move(t), k, std::move(tn)));
}
catch(const std::exception& e)
{
return err(e.what());
}
}
} // toml

222
toml/lexer.hpp Normal file
View File

@@ -0,0 +1,222 @@
// Copyright Toru Niina 2017.
// Distributed under the MIT License.
#ifndef TOML11_LEXER_HPP
#define TOML11_LEXER_HPP
#include "combinator.hpp"
#include <stdexcept>
#include <istream>
#include <sstream>
#include <fstream>
namespace toml
{
namespace detail
{
// these scans contents from current location in a container of char
// and extract a region that matches their own pattern.
// to see the implementation of each component, see combinator.hpp.
using lex_wschar = either<character<' '>, character<'\t'>>;
using lex_ws = repeat<lex_wschar, at_least<1>>;
using lex_newline = either<character<'\n'>,
sequence<character<'\r'>, character<'\n'>>>;
using lex_lower = in_range<'a', 'z'>;
using lex_upper = in_range<'A', 'Z'>;
using lex_alpha = either<lex_lower, lex_upper>;
using lex_digit = in_range<'0', '9'>;
using lex_nonzero = in_range<'1', '9'>;
using lex_oct_dig = in_range<'0', '7'>;
using lex_bin_dig = in_range<'0', '1'>;
using lex_hex_dig = either<lex_digit, in_range<'A', 'F'>, in_range<'a', 'f'>>;
using lex_hex_prefix = sequence<character<'0'>, character<'x'>>;
using lex_oct_prefix = sequence<character<'0'>, character<'o'>>;
using lex_bin_prefix = sequence<character<'0'>, character<'b'>>;
using lex_underscore = character<'_'>;
using lex_plus = character<'+'>;
using lex_minus = character<'-'>;
using lex_sign = either<lex_plus, lex_minus>;
// digit | nonzero 1*(digit | _ digit)
using lex_unsigned_dec_int = either<sequence<lex_nonzero, repeat<
either<lex_digit, sequence<lex_underscore, lex_digit>>, at_least<1>>>,
lex_digit>;
// (+|-)? unsigned_dec_int
using lex_dec_int = sequence<maybe<lex_sign>, lex_unsigned_dec_int>;
// hex_prefix hex_dig *(hex_dig | _ hex_dig)
using lex_hex_int = sequence<lex_hex_prefix, sequence<lex_hex_dig, repeat<
either<lex_hex_dig, sequence<lex_underscore, lex_hex_dig>>, unlimited>>>;
// oct_prefix oct_dig *(oct_dig | _ oct_dig)
using lex_oct_int = sequence<lex_oct_prefix, sequence<lex_oct_dig, repeat<
either<lex_oct_dig, sequence<lex_underscore, lex_oct_dig>>, unlimited>>>;
// bin_prefix bin_dig *(bin_dig | _ bin_dig)
using lex_bin_int = sequence<lex_bin_prefix, sequence<lex_bin_dig, repeat<
either<lex_bin_dig, sequence<lex_underscore, lex_bin_dig>>, unlimited>>>;
// (dec_int | hex_int | oct_int | bin_int)
using lex_integer = either<lex_bin_int, lex_oct_int, lex_hex_int, lex_dec_int>;
// ===========================================================================
using lex_inf = sequence<character<'i'>, character<'n'>, character<'f'>>;
using lex_nan = sequence<character<'n'>, character<'a'>, character<'n'>>;
using lex_special_float = sequence<maybe<lex_sign>, either<lex_inf, lex_nan>>;
using lex_exponent_part = sequence<either<character<'e'>, character<'E'>>, lex_dec_int>;
using lex_zero_prefixable_int = sequence<lex_digit, repeat<either<lex_digit,
sequence<lex_underscore, lex_digit>>, unlimited>>;
using lex_fractional_part = sequence<character<'.'>, lex_zero_prefixable_int>;
using lex_float = either<lex_special_float,
sequence<lex_dec_int, either<lex_exponent_part,
sequence<lex_fractional_part, maybe<lex_exponent_part>>>>>;
// ===========================================================================
using lex_true = sequence<character<'t'>, character<'r'>,
character<'u'>, character<'e'>>;
using lex_false = sequence<character<'f'>, character<'a'>, character<'l'>,
character<'s'>, character<'e'>>;
using lex_boolean = either<lex_true, lex_false>;
// ===========================================================================
using lex_date_fullyear = repeat<lex_digit, exactly<4>>;
using lex_date_month = repeat<lex_digit, exactly<2>>;
using lex_date_mday = repeat<lex_digit, exactly<2>>;
using lex_time_delim = either<character<'T'>, character<'t'>, character<' '>>;
using lex_time_hour = repeat<lex_digit, exactly<2>>;
using lex_time_minute = repeat<lex_digit, exactly<2>>;
using lex_time_second = repeat<lex_digit, exactly<2>>;
using lex_time_secfrac = sequence<character<'.'>,
repeat<lex_digit, at_least<1>>>;
using lex_time_numoffset = sequence<either<character<'+'>, character<'-'>>,
sequence<lex_time_hour, character<':'>,
lex_time_minute>>;
using lex_time_offset = either<character<'Z'>, character<'z'>,
lex_time_numoffset>;
using lex_partial_time = sequence<lex_time_hour, character<':'>,
lex_time_minute, character<':'>,
lex_time_second, maybe<lex_time_secfrac>>;
using lex_full_date = sequence<lex_date_fullyear, character<'-'>,
lex_date_month, character<'-'>,
lex_date_mday>;
using lex_full_time = sequence<lex_partial_time, lex_time_offset>;
using lex_offset_date_time = sequence<lex_full_date, lex_time_delim, lex_full_time>;
using lex_local_date_time = sequence<lex_full_date, lex_time_delim, lex_partial_time>;
using lex_local_date = lex_full_date;
using lex_local_time = lex_partial_time;
// ===========================================================================
using lex_quotation_mark = character<'"'>;
using lex_basic_unescaped = exclude<either<in_range<0x00, 0x1F>,
character<0x22>, character<0x5C>,
character<0x7F>>>;
using lex_escape = character<'\\'>;
using lex_escape_unicode_short = sequence<character<'u'>,
repeat<lex_hex_dig, exactly<4>>>;
using lex_escape_unicode_long = sequence<character<'U'>,
repeat<lex_hex_dig, exactly<8>>>;
using lex_escape_seq_char = either<character<'"'>, character<'\\'>,
character<'/'>, character<'b'>,
character<'f'>, character<'n'>,
character<'r'>, character<'t'>,
lex_escape_unicode_short,
lex_escape_unicode_long
>;
using lex_escaped = sequence<lex_escape, lex_escape_seq_char>;
using lex_basic_char = either<lex_basic_unescaped, lex_escaped>;
using lex_basic_string = sequence<lex_quotation_mark,
repeat<lex_basic_char, unlimited>,
lex_quotation_mark>;
using lex_ml_basic_string_delim = repeat<lex_quotation_mark, exactly<3>>;
using lex_ml_basic_unescaped = exclude<either<in_range<0x00, 0x1F>,
character<0x5C>,
character<0x7F>,
lex_ml_basic_string_delim>>;
using lex_ml_basic_escaped_newline = sequence<
lex_escape, maybe<lex_ws>, lex_newline,
repeat<either<lex_ws, lex_newline>, unlimited>>;
using lex_ml_basic_char = either<lex_ml_basic_unescaped, lex_escaped>;
using lex_ml_basic_body = repeat<either<lex_ml_basic_char, lex_newline,
lex_ml_basic_escaped_newline>,
unlimited>;
using lex_ml_basic_string = sequence<lex_ml_basic_string_delim,
lex_ml_basic_body,
lex_ml_basic_string_delim>;
using lex_literal_char = exclude<either<in_range<0x00, 0x08>,
in_range<0x10, 0x19>, character<0x27>>>;
using lex_apostrophe = character<'\''>;
using lex_literal_string = sequence<lex_apostrophe,
repeat<lex_literal_char, unlimited>,
lex_apostrophe>;
using lex_ml_literal_string_delim = repeat<lex_apostrophe, exactly<3>>;
using lex_ml_literal_char = exclude<either<in_range<0x00, 0x08>,
in_range<0x10, 0x1F>,
character<0x7F>,
lex_ml_literal_string_delim>>;
using lex_ml_literal_body = repeat<either<lex_ml_literal_char, lex_newline>,
unlimited>;
using lex_ml_literal_string = sequence<lex_ml_literal_string_delim,
lex_ml_literal_body,
lex_ml_literal_string_delim>;
using lex_string = either<lex_ml_basic_string, lex_basic_string,
lex_ml_literal_string, lex_literal_string>;
// ===========================================================================
using lex_comment_start_symbol = character<'#'>;
using lex_non_eol = either<character<'\t'>, exclude<in_range<0x00, 0x19>>>;
using lex_comment = sequence<lex_comment_start_symbol,
repeat<lex_non_eol, unlimited>>;
using lex_dot_sep = sequence<maybe<lex_ws>, character<'.'>, maybe<lex_ws>>;
using lex_unquoted_key = repeat<either<lex_alpha, lex_digit,
character<'-'>, character<'_'>>,
at_least<1>>;
using lex_quoted_key = either<lex_basic_string, lex_literal_string>;
using lex_simple_key = either<lex_unquoted_key, lex_quoted_key>;
using lex_dotted_key = sequence<lex_simple_key,
repeat<sequence<lex_dot_sep, lex_simple_key>,
at_least<1>
>
>;
using lex_key = either<lex_dotted_key, lex_simple_key>;
using lex_keyval_sep = sequence<maybe<lex_ws>,
character<'='>,
maybe<lex_ws>>;
using lex_std_table_open = character<'['>;
using lex_std_table_close = character<']'>;
using lex_std_table = sequence<lex_std_table_open,
maybe<lex_ws>,
lex_key,
maybe<lex_ws>,
lex_std_table_close>;
using lex_array_table_open = sequence<lex_std_table_open, lex_std_table_open>;
using lex_array_table_close = sequence<lex_std_table_close, lex_std_table_close>;
using lex_array_table = sequence<lex_array_table_open,
maybe<lex_ws>,
lex_key,
maybe<lex_ws>,
lex_array_table_close>;
} // detail
} // toml
#endif // TOML_LEXER_HPP

File diff suppressed because it is too large Load Diff

361
toml/region.hpp Normal file
View File

@@ -0,0 +1,361 @@
// Copyright Toru Niina 2017.
// Distributed under the MIT License.
#ifndef TOML11_REGION_H
#define TOML11_REGION_H
#include "exception.hpp"
#include <memory>
#include <vector>
#include <algorithm>
#include <initializer_list>
#include <iterator>
#include <iomanip>
namespace toml
{
namespace detail
{
// helper function to avoid std::string(0, 'c')
template<typename Iterator>
std::string make_string(Iterator first, Iterator last)
{
if(first == last) {return "";}
return std::string(first, last);
}
inline std::string make_string(std::size_t len, char c)
{
if(len == 0) {return "";}
return std::string(len, c);
}
// location in a container, normally in a file content.
// shared_ptr points the resource that the iter points.
// it can be used not only for resource handling, but also error message.
template<typename Container>
struct location
{
static_assert(std::is_same<char, typename Container::value_type>::value,"");
using const_iterator = typename Container::const_iterator;
using source_ptr = std::shared_ptr<const Container>;
location(std::string name, Container cont)
: source_(std::make_shared<Container>(std::move(cont))),
source_name_(std::move(name)), iter_(source_->cbegin())
{}
location(const location&) = default;
location(location&&) = default;
location& operator=(const location&) = default;
location& operator=(location&&) = default;
~location() = default;
const_iterator& iter() noexcept {return iter_;}
const_iterator iter() const noexcept {return iter_;}
const_iterator begin() const noexcept {return source_->cbegin();}
const_iterator end() const noexcept {return source_->cend();}
source_ptr const& source() const& noexcept {return source_;}
source_ptr&& source() && noexcept {return std::move(source_);}
std::string const& name() const noexcept {return source_name_;}
private:
source_ptr source_;
std::string source_name_;
const_iterator iter_;
};
// region in a container, normally in a file content.
// shared_ptr points the resource that the iter points.
// combinators returns this.
// it will be used to generate better error messages.
struct region_base
{
region_base() = default;
virtual ~region_base() = default;
region_base(const region_base&) = default;
region_base(region_base&& ) = default;
region_base& operator=(const region_base&) = default;
region_base& operator=(region_base&& ) = default;
virtual bool is_ok() const noexcept {return false;}
virtual std::string str() const {return std::string("unknown region");}
virtual std::string name() const {return std::string("unknown file");}
virtual std::string line() const {return std::string("unknown line");}
virtual std::string line_num() const {return std::string("?");}
virtual std::size_t before() const noexcept {return 0;}
virtual std::size_t size() const noexcept {return 0;}
virtual std::size_t after() const noexcept {return 0;}
};
template<typename Container>
struct region final : public region_base
{
static_assert(std::is_same<char, typename Container::value_type>::value,"");
using const_iterator = typename Container::const_iterator;
using source_ptr = std::shared_ptr<const Container>;
// delete default constructor. source_ never be null.
region() = delete;
region(const location<Container>& loc)
: source_(loc.source()), source_name_(loc.name()),
first_(loc.iter()), last_(loc.iter())
{}
region(location<Container>&& loc)
: source_(loc.source()), source_name_(loc.name()),
first_(loc.iter()), last_(loc.iter())
{}
region(const location<Container>& loc, const_iterator f, const_iterator l)
: source_(loc.source()), source_name_(loc.name()), first_(f), last_(l)
{}
region(location<Container>&& loc, const_iterator f, const_iterator l)
: source_(loc.source()), source_name_(loc.name()), first_(f), last_(l)
{}
region(const region&) = default;
region(region&&) = default;
region& operator=(const region&) = default;
region& operator=(region&&) = default;
~region() = default;
region& operator+=(const region& other)
{
if(this->begin() != other.begin() || this->end() != other.end() ||
this->last_ != other.first_)
{
throw internal_error("invalid region concatenation");
}
this->last_ = other.last_;
return *this;
}
bool is_ok() const noexcept override {return static_cast<bool>(source_);}
std::string str() const override {return make_string(first_, last_);}
std::string line() const override
{
if(this->contain_newline())
{
return make_string(this->line_begin(),
std::find(this->line_begin(), this->last(), '\n'));
}
return make_string(this->line_begin(), this->line_end());
}
std::string line_num() const override
{
return std::to_string(1 + std::count(this->begin(), this->first(), '\n'));
}
std::size_t size() const noexcept override
{
return std::distance(first_, last_);
}
std::size_t before() const noexcept override
{
return std::distance(this->line_begin(), this->first());
}
std::size_t after() const noexcept override
{
return std::distance(this->last(), this->line_end());
}
bool contain_newline() const noexcept
{
return std::find(this->first(), this->last(), '\n') != this->last();
}
const_iterator line_begin() const noexcept
{
using reverse_iterator = std::reverse_iterator<const_iterator>;
return std::find(reverse_iterator(this->first()),
reverse_iterator(this->begin()), '\n').base();
}
const_iterator line_end() const noexcept
{
return std::find(this->last(), this->end(), '\n');
}
const_iterator begin() const noexcept {return source_->cbegin();}
const_iterator end() const noexcept {return source_->cend();}
const_iterator first() const noexcept {return first_;}
const_iterator last() const noexcept {return last_;}
source_ptr const& source() const& noexcept {return source_;}
source_ptr&& source() && noexcept {return std::move(source_);}
std::string name() const override {return source_name_;}
private:
source_ptr source_;
std::string source_name_;
const_iterator first_, last_;
};
// to show a better error message.
inline std::string format_underline(const std::string& message,
const region_base& reg, const std::string& comment_for_underline,
std::vector<std::string> helps = {})
{
#ifdef _WIN32
const auto newline = "\r\n";
#else
const char newline = '\n';
#endif
const auto line = reg.line();
const auto line_number = reg.line_num();
std::string retval;
retval += message;
retval += newline;
retval += " --> ";
retval += reg.name();
retval += newline;
retval += ' ';
retval += line_number;
retval += " | ";
retval += line;
retval += newline;
retval += make_string(line_number.size() + 1, ' ');
retval += " | ";
retval += make_string(reg.before(), ' ');
retval += make_string(reg.size(), '~');
retval += ' ';
retval += comment_for_underline;
if(helps.size() != 0)
{
retval += newline;
retval += make_string(line_number.size() + 1, ' ');
retval += " | ";
for(const auto help : helps)
{
retval += newline;
retval += "Hint: ";
retval += help;
}
}
return retval;
}
// to show a better error message.
inline std::string format_underline(const std::string& message,
const region_base& reg1, const std::string& comment_for_underline1,
const region_base& reg2, const std::string& comment_for_underline2,
std::vector<std::string> helps = {})
{
#ifdef _WIN32
const auto newline = "\r\n";
#else
const char newline = '\n';
#endif
const auto line1 = reg1.line();
const auto line_number1 = reg1.line_num();
const auto line2 = reg2.line();
const auto line_number2 = reg2.line_num();
const auto line_num_width =
std::max(line_number1.size(), line_number2.size());
std::ostringstream retval;
retval << message;
retval << newline;
retval << " --> ";
retval << reg1.name() << newline;;
// ---------------------------------------
retval << ' ' << std::setw(line_num_width) << line_number1;
retval << " | " << line1 << newline;
retval << make_string(line_num_width + 1, ' ');
retval << " | ";
retval << make_string(reg1.before(), ' ');
retval << make_string(reg1.size(), '~');
retval << ' ';
retval << comment_for_underline1 << newline;
// ---------------------------------------
retval << " ..." << newline;
retval << ' ' << std::setw(line_num_width) << line_number2;
retval << " | " << line2 << newline;
retval << make_string(line_num_width + 1, ' ');
retval << " | ";
retval << make_string(reg2.before(), ' ');
retval << make_string(reg2.size(), '~');
retval << ' ';
retval << comment_for_underline2;
if(helps.size() != 0)
{
retval << newline;
retval << make_string(line_num_width + 1, ' ');
retval << " | ";
for(const auto help : helps)
{
retval << newline;
retval << "Hint: ";
retval << help;
}
}
return retval.str();
}
// to show a better error message.
template<typename Container>
std::string
format_underline(const std::string& message, const location<Container>& loc,
const std::string& comment_for_underline,
std::vector<std::string> helps = {})
{
#ifdef _WIN32
const auto newline = "\r\n";
#else
const char newline = '\n';
#endif
using const_iterator = typename location<Container>::const_iterator;
using reverse_iterator = std::reverse_iterator<const_iterator>;
const auto line_begin = std::find(reverse_iterator(loc.iter()),
reverse_iterator(loc.begin()),
'\n').base();
const auto line_end = std::find(loc.iter(), loc.end(), '\n');
const auto line_number = std::to_string(
1 + std::count(loc.begin(), loc.iter(), '\n'));
std::string retval;
retval += message;
retval += newline;
retval += " --> ";
retval += loc.name();
retval += newline;
retval += ' ';
retval += line_number;
retval += " | ";
retval += make_string(line_begin, line_end);
retval += newline;
retval += make_string(line_number.size() + 1, ' ');
retval += " | ";
retval += make_string(std::distance(line_begin, loc.iter()),' ');
retval += '^';
retval += make_string(std::distance(loc.iter(), line_end), '-');
retval += ' ';
retval += comment_for_underline;
if(helps.size() != 0)
{
retval += newline;
retval += make_string(line_number.size() + 1, ' ');
retval += " | ";
for(const auto help : helps)
{
retval += newline;
retval += "Hint: ";
retval += help;
}
}
return retval;
}
} // detail
} // toml
#endif// TOML11_REGION_H

642
toml/result.hpp Normal file
View File

@@ -0,0 +1,642 @@
// Copyright Toru Niina 2017.
// Distributed under the MIT License.
#ifndef TOML11_RESULT_H
#define TOML11_RESULT_H
#include <type_traits>
#include <stdexcept>
#include <utility>
#include <new>
#include <string>
#include <sstream>
#include <cassert>
namespace toml
{
#if __cplusplus >= 201703L
template<typename F, typename ... Args>
using return_type_of_t = std::invoke_result_t<F, Args...>;
#else
// result_of is deprecated after C++17
template<typename F, typename ... Args>
using return_type_of_t = typename std::result_of<F(Args...)>::type;
#endif
template<typename T>
struct success
{
using value_type = T;
value_type value;
explicit success(const value_type& v)
noexcept(std::is_nothrow_copy_constructible<value_type>::value)
: value(v)
{}
explicit success(value_type&& v)
noexcept(std::is_nothrow_move_constructible<value_type>::value)
: value(std::move(v))
{}
template<typename U>
explicit success(U&& v): value(std::forward<U>(v)) {}
template<typename U>
explicit success(const success<U>& v): value(v.value) {}
template<typename U>
explicit success(success<U>&& v): value(std::move(v.value)) {}
~success() = default;
success(const success&) = default;
success(success&&) = default;
success& operator=(const success&) = default;
success& operator=(success&&) = default;
};
template<typename T>
struct failure
{
using value_type = T;
value_type value;
explicit failure(const value_type& v)
noexcept(std::is_nothrow_copy_constructible<value_type>::value)
: value(v)
{}
explicit failure(value_type&& v)
noexcept(std::is_nothrow_move_constructible<value_type>::value)
: value(std::move(v))
{}
template<typename U>
explicit failure(U&& v): value(std::forward<U>(v)) {}
template<typename U>
explicit failure(const failure<U>& v): value(v.value) {}
template<typename U>
explicit failure(failure<U>&& v): value(std::move(v.value)) {}
~failure() = default;
failure(const failure&) = default;
failure(failure&&) = default;
failure& operator=(const failure&) = default;
failure& operator=(failure&&) = default;
};
template<typename T>
success<typename std::remove_cv<typename std::remove_reference<T>::type>::type>
ok(T&& v)
{
return success<
typename std::remove_cv<typename std::remove_reference<T>::type>::type
>(std::forward<T>(v));
}
template<typename T>
failure<typename std::remove_cv<typename std::remove_reference<T>::type>::type>
err(T&& v)
{
return failure<
typename std::remove_cv<typename std::remove_reference<T>::type>::type
>(std::forward<T>(v));
}
inline success<std::string> ok(const char* literal)
{
return success<std::string>(std::string(literal));
}
inline failure<std::string> err(const char* literal)
{
return failure<std::string>(std::string(literal));
}
template<typename T, typename E>
struct result
{
using value_type = T;
using error_type = E;
using success_type = success<value_type>;
using failure_type = failure<error_type>;
result(const success_type& s): is_ok_(true)
{
auto tmp = ::new(std::addressof(this->succ)) success_type(s);
assert(tmp == std::addressof(this->succ));
}
result(const failure_type& f): is_ok_(false)
{
auto tmp = ::new(std::addressof(this->fail)) failure_type(f);
assert(tmp == std::addressof(this->fail));
}
result(success_type&& s): is_ok_(true)
{
auto tmp = ::new(std::addressof(this->succ)) success_type(std::move(s));
assert(tmp == std::addressof(this->succ));
}
result(failure_type&& f): is_ok_(false)
{
auto tmp = ::new(std::addressof(this->fail)) failure_type(std::move(f));
assert(tmp == std::addressof(this->fail));
}
template<typename U>
result(const success<U>& s): is_ok_(true)
{
auto tmp = ::new(std::addressof(this->succ)) success_type(s.value);
assert(tmp == std::addressof(this->succ));
}
template<typename U>
result(const failure<U>& f): is_ok_(false)
{
auto tmp = ::new(std::addressof(this->fail)) failure_type(f.value);
assert(tmp == std::addressof(this->fail));
}
template<typename U>
result(success<U>&& s): is_ok_(true)
{
auto tmp = ::new(std::addressof(this->succ)) success_type(std::move(s.value));
assert(tmp == std::addressof(this->succ));
}
template<typename U>
result(failure<U>&& f): is_ok_(false)
{
auto tmp = ::new(std::addressof(this->fail)) failure_type(std::move(f.value));
assert(tmp == std::addressof(this->fail));
}
result& operator=(const success_type& s)
{
this->cleanup();
this->is_ok_ = true;
auto tmp = ::new(std::addressof(this->succ)) success_type(s);
assert(tmp == std::addressof(this->succ));
return *this;
}
result& operator=(const failure_type& f)
{
this->cleanup();
this->is_ok_ = false;
auto tmp = ::new(std::addressof(this->fail)) failure_type(f);
assert(tmp == std::addressof(this->fail));
return *this;
}
result& operator=(success_type&& s)
{
this->cleanup();
this->is_ok_ = true;
auto tmp = ::new(std::addressof(this->succ)) success_type(std::move(s));
assert(tmp == std::addressof(this->succ));
return *this;
}
result& operator=(failure_type&& f)
{
this->cleanup();
this->is_ok_ = false;
auto tmp = ::new(std::addressof(this->fail)) failure_type(std::move(f));
assert(tmp == std::addressof(this->fail));
return *this;
}
template<typename U>
result& operator=(const success<U>& s)
{
this->cleanup();
this->is_ok_ = true;
auto tmp = ::new(std::addressof(this->succ)) success_type(s.value);
assert(tmp == std::addressof(this->succ));
return *this;
}
template<typename U>
result& operator=(const failure<U>& f)
{
this->cleanup();
this->is_ok_ = false;
auto tmp = ::new(std::addressof(this->fail)) failure_type(f.value);
assert(tmp == std::addressof(this->fail));
return *this;
}
template<typename U>
result& operator=(success<U>&& s)
{
this->cleanup();
this->is_ok_ = true;
auto tmp = ::new(std::addressof(this->succ)) success_type(std::move(s.value));
assert(tmp == std::addressof(this->succ));
return *this;
}
template<typename U>
result& operator=(failure<U>&& f)
{
this->cleanup();
this->is_ok_ = false;
auto tmp = ::new(std::addressof(this->fail)) failure_type(std::move(f.value));
assert(tmp == std::addressof(this->fail));
return *this;
}
~result() noexcept {this->cleanup();}
result(const result& other): is_ok_(other.is_ok())
{
if(other.is_ok())
{
auto tmp = ::new(std::addressof(this->succ)) success_type(other.as_ok());
assert(tmp == std::addressof(this->succ));
}
else
{
auto tmp = ::new(std::addressof(this->fail)) failure_type(other.as_err());
assert(tmp == std::addressof(this->fail));
}
}
result(result&& other): is_ok_(other.is_ok())
{
if(other.is_ok())
{
auto tmp = ::new(std::addressof(this->succ)) success_type(std::move(other.as_ok()));
assert(tmp == std::addressof(this->succ));
}
else
{
auto tmp = ::new(std::addressof(this->fail)) failure_type(std::move(other.as_err()));
assert(tmp == std::addressof(this->fail));
}
}
template<typename U, typename F>
result(const result<U, F>& other): is_ok_(other.is_ok())
{
if(other.is_ok())
{
auto tmp = ::new(std::addressof(this->succ)) success_type(other.as_ok());
assert(tmp == std::addressof(this->succ));
}
else
{
auto tmp = ::new(std::addressof(this->fail)) failure_type(other.as_err());
assert(tmp == std::addressof(this->fail));
}
}
template<typename U, typename F>
result(result<U, F>&& other): is_ok_(other.is_ok())
{
if(other.is_ok())
{
auto tmp = ::new(std::addressof(this->succ)) success_type(std::move(other.as_ok()));
assert(tmp == std::addressof(this->succ));
}
else
{
auto tmp = ::new(std::addressof(this->fail)) failure_type(std::move(other.as_err()));
assert(tmp == std::addressof(this->fail));
}
}
result& operator=(const result& other)
{
this->cleanup();
if(other.is_ok())
{
auto tmp = ::new(std::addressof(this->succ)) success_type(other.as_ok());
assert(tmp == std::addressof(this->succ));
}
else
{
auto tmp = ::new(std::addressof(this->fail)) failure_type(other.as_err());
assert(tmp == std::addressof(this->fail));
}
is_ok_ = other.is_ok();
return *this;
}
result& operator=(result&& other)
{
this->cleanup();
if(other.is_ok())
{
auto tmp = ::new(std::addressof(this->succ)) success_type(std::move(other.as_ok()));
assert(tmp == std::addressof(this->succ));
}
else
{
auto tmp = ::new(std::addressof(this->fail)) failure_type(std::move(other.as_err()));
assert(tmp == std::addressof(this->fail));
}
is_ok_ = other.is_ok();
return *this;
}
template<typename U, typename F>
result& operator=(const result<U, F>& other)
{
this->cleanup();
if(other.is_ok())
{
auto tmp = ::new(std::addressof(this->succ)) success_type(other.as_ok());
assert(tmp == std::addressof(this->succ));
}
else
{
auto tmp = ::new(std::addressof(this->fail)) failure_type(other.as_err());
assert(tmp == std::addressof(this->fail));
}
is_ok_ = other.is_ok();
return *this;
}
template<typename U, typename F>
result& operator=(result<U, F>&& other)
{
this->cleanup();
if(other.is_ok())
{
auto tmp = ::new(std::addressof(this->succ)) success_type(std::move(other.as_ok()));
assert(tmp == std::addressof(this->succ));
}
else
{
auto tmp = ::new(std::addressof(this->fail)) failure_type(std::move(other.as_err()));
assert(tmp == std::addressof(this->fail));
}
is_ok_ = other.is_ok();
return *this;
}
bool is_ok() const noexcept {return is_ok_;}
bool is_err() const noexcept {return !is_ok_;}
operator bool() const noexcept {return is_ok_;}
value_type& unwrap() &
{
if(is_err())
{
throw std::runtime_error("toml::result: bad unwrap: " +
format_error(this->as_err()));
}
return this->succ.value;
}
value_type const& unwrap() const&
{
if(is_err())
{
throw std::runtime_error("toml::result: bad unwrap: " +
format_error(this->as_err()));
}
return this->succ.value;
}
value_type&& unwrap() &&
{
if(is_err())
{
throw std::runtime_error("toml::result: bad unwrap: " +
format_error(this->as_err()));
}
return std::move(this->succ.value);
}
template<typename U>
value_type& unwrap_or(U& opt) &
{
if(is_err()) {return opt;}
return this->succ.value;
}
template<typename U>
value_type const& unwrap_or(U const& opt) const&
{
if(is_err()) {return opt;}
return this->succ.value;
}
template<typename U>
value_type&& unwrap_or(U&& opt) &&
{
if(is_err()) {return std::move(opt);}
return std::move(this->succ.value);
}
error_type& unwrap_err() &
{
if(is_ok()) {throw std::runtime_error("toml::result: bad unwrap_err");}
return this->fail.value;
}
error_type const& unwrap_err() const&
{
if(is_ok()) {throw std::runtime_error("toml::result: bad unwrap_err");}
return this->fail.value;
}
error_type&& unwrap_err() &&
{
if(is_ok()) {throw std::runtime_error("toml::result: bad unwrap_err");}
return std::move(this->fail.value);
}
value_type& as_ok() & noexcept {return this->succ.value;}
value_type const& as_ok() const& noexcept {return this->succ.value;}
value_type&& as_ok() && noexcept {return std::move(this->succ.value);}
error_type& as_err() & noexcept {return this->fail.value;}
error_type const& as_err() const& noexcept {return this->fail.value;}
error_type&& as_err() && noexcept {return std::move(this->fail.value);}
// prerequisities
// F: T -> U
// retval: result<U, E>
template<typename F>
result<return_type_of_t<F, value_type&>, error_type>
map(F&& f) &
{
if(this->is_ok()){return ok(f(this->as_ok()));}
return err(this->as_err());
}
template<typename F>
result<return_type_of_t<F, value_type const&>, error_type>
map(F&& f) const&
{
if(this->is_ok()){return ok(f(this->as_ok()));}
return err(this->as_err());
}
template<typename F>
result<return_type_of_t<F, value_type &&>, error_type>
map(F&& f) &&
{
if(this->is_ok()){return ok(f(std::move(this->as_ok())));}
return err(std::move(this->as_err()));
}
// prerequisities
// F: E -> F
// retval: result<T, F>
template<typename F>
result<value_type, return_type_of_t<F, error_type&>>
map_err(F&& f) &
{
if(this->is_err()){return err(f(this->as_err()));}
return ok(this->as_ok());
}
template<typename F>
result<value_type, return_type_of_t<F, error_type const&>>
map_err(F&& f) const&
{
if(this->is_err()){return err(f(this->as_err()));}
return ok(this->as_ok());
}
template<typename F>
result<value_type, return_type_of_t<F, error_type&&>>
map_err(F&& f) &&
{
if(this->is_err()){return err(f(std::move(this->as_err())));}
return ok(std::move(this->as_ok()));
}
// prerequisities
// F: T -> U
// retval: U
template<typename F, typename U>
return_type_of_t<F, value_type&>
map_or_else(F&& f, U&& opt) &
{
if(this->is_err()){return std::forward<U>(opt);}
return f(this->as_ok());
}
template<typename F, typename U>
return_type_of_t<F, value_type const&>
map_or_else(F&& f, U&& opt) const&
{
if(this->is_err()){return std::forward<U>(opt);}
return f(this->as_ok());
}
template<typename F, typename U>
return_type_of_t<F, value_type&&>
map_or_else(F&& f, U&& opt) &&
{
if(this->is_err()){return std::forward<U>(opt);}
return f(std::move(this->as_ok()));
}
// prerequisities
// F: E -> U
// retval: U
template<typename F, typename U>
return_type_of_t<F, error_type&>
map_err_or_else(F&& f, U&& opt) &
{
if(this->is_ok()){return std::forward<U>(opt);}
return f(this->as_err());
}
template<typename F, typename U>
return_type_of_t<F, error_type const&>
map_err_or_else(F&& f, U&& opt) const&
{
if(this->is_ok()){return std::forward<U>(opt);}
return f(this->as_err());
}
template<typename F, typename U>
return_type_of_t<F, error_type&&>
map_err_or_else(F&& f, U&& opt) &&
{
if(this->is_ok()){return std::forward<U>(opt);}
return f(std::move(this->as_err()));
}
// prerequisities:
// F: func T -> U
// toml::err(error_type) should be convertible to U.
// normally, type U is another result<S, F> and E is convertible to F
template<typename F>
return_type_of_t<F, value_type&>
and_then(F&& f) &
{
if(this->is_ok()){return f(this->as_ok());}
return err(this->as_err());
}
template<typename F>
return_type_of_t<F, value_type const&>
and_then(F&& f) const&
{
if(this->is_ok()){return f(this->as_ok());}
return err(this->as_err());
}
template<typename F>
return_type_of_t<F, value_type&&>
and_then(F&& f) &&
{
if(this->is_ok()){return f(std::move(this->as_ok()));}
return err(std::move(this->as_err()));
}
// prerequisities:
// F: func E -> U
// toml::ok(value_type) should be convertible to U.
// normally, type U is another result<S, F> and T is convertible to S
template<typename F>
return_type_of_t<F, error_type&>
or_else(F&& f) &
{
if(this->is_err()){return f(this->as_err());}
return ok(this->as_ok());
}
template<typename F>
return_type_of_t<F, error_type const&>
or_else(F&& f) const&
{
if(this->is_err()){return f(this->as_err());}
return ok(this->as_ok());
}
template<typename F>
return_type_of_t<F, error_type&&>
or_else(F&& f) &&
{
if(this->is_err()){return f(std::move(this->as_err()));}
return ok(std::move(this->as_ok()));
}
void swap(result<T, E>& other)
{
result<T, E> tmp(std::move(*this));
*this = std::move(other);
other = std::move(tmp);
return ;
}
private:
static std::string format_error(std::exception const& excpt)
{
return std::string(excpt.what());
}
template<typename U, typename std::enable_if<!std::is_base_of<
std::exception, U>::value, std::nullptr_t>::type = nullptr>
static std::string format_error(U const& others)
{
std::ostringstream oss; oss << others;
return oss.str();
}
void cleanup() noexcept
{
if(this->is_ok_) {this->succ.~success_type();}
else {this->fail.~failure_type();}
return;
}
private:
bool is_ok_;
union
{
success_type succ;
failure_type fail;
};
};
template<typename T, typename E>
void swap(result<T, E>& lhs, result<T, E>& rhs)
{
lhs.swap(rhs);
return;
}
} // toml11
#endif// TOML11_RESULT_H

44
toml/storage.hpp Normal file
View File

@@ -0,0 +1,44 @@
// Copyright Toru Niina 2017.
// Distributed under the MIT License.
#ifndef TOML11_STORAGE_HPP
#define TOML11_STORAGE_HPP
#include "utility.hpp"
namespace toml
{
namespace detail
{
// this contains pointer and deep-copy the content if copied.
// to avoid recursive pointer.
template<typename T>
struct storage
{
using value_type = T;
storage(value_type const& v): ptr(toml::make_unique<T>(v)) {}
storage(value_type&& v): ptr(toml::make_unique<T>(std::move(v))) {}
~storage() = default;
storage(const storage& rhs): ptr(toml::make_unique<T>(*rhs.ptr)) {}
storage& operator=(const storage& rhs)
{
this->ptr = toml::make_unique<T>(*rhs.ptr);
return *this;
}
storage(storage&&) = default;
storage& operator=(storage&&) = default;
bool is_ok() const noexcept {return static_cast<bool>(ptr);}
value_type& value() & noexcept {return *ptr;}
value_type const& value() const& noexcept {return *ptr;}
value_type&& value() && noexcept {return std::move(*ptr);}
private:
std::unique_ptr<value_type> ptr;
};
} // detail
} // toml
#endif// TOML11_STORAGE_HPP

133
toml/string.hpp Normal file
View File

@@ -0,0 +1,133 @@
// Copyright Toru Niina 2017.
// Distributed under the MIT License.
#ifndef TOML11_STRING_H
#define TOML11_STRING_H
#include <string>
#include <cstdint>
namespace toml
{
enum class string_t : std::uint8_t
{
basic = 0,
literal = 1,
};
struct string
{
string() = default;
~string() = default;
string(const string& s) = default;
string(string&& s) = default;
string& operator=(const string& s) = default;
string& operator=(string&& s) = default;
string(const std::string& s): kind(string_t::basic), str(s){}
string(const std::string& s, string_t k): kind(k), str(s){}
string(const char* s): kind(string_t::basic), str(s){}
string(const char* s, string_t k): kind(k), str(s){}
string(std::string&& s): kind(string_t::basic), str(std::move(s)){}
string(std::string&& s, string_t k): kind(k), str(std::move(s)){}
string& operator=(const std::string& s)
{kind = string_t::basic; str = s; return *this;}
string& operator=(std::string&& s)
{kind = string_t::basic; str = std::move(s); return *this;}
operator std::string& () & noexcept {return str;}
operator std::string const& () const& noexcept {return str;}
operator std::string&& () && noexcept {return std::move(str);}
string_t kind;
std::string str;
};
inline bool operator==(const string& lhs, const string& rhs)
{
return lhs.kind == rhs.kind && lhs.str == rhs.str;
}
inline bool operator!=(const string& lhs, const string& rhs)
{
return !(lhs == rhs);
}
inline bool operator<(const string& lhs, const string& rhs)
{
return (lhs.kind == rhs.kind) ? (lhs.str < rhs.str) : (lhs.kind < rhs.kind);
}
inline bool operator>(const string& lhs, const string& rhs)
{
return rhs < lhs;
}
inline bool operator<=(const string& lhs, const string& rhs)
{
return !(rhs < lhs);
}
inline bool operator>=(const string& lhs, const string& rhs)
{
return !(lhs < rhs);
}
inline bool
operator==(const string& lhs, const std::string& rhs) {return lhs.str == rhs;}
inline bool
operator!=(const string& lhs, const std::string& rhs) {return lhs.str != rhs;}
inline bool
operator< (const string& lhs, const std::string& rhs) {return lhs.str < rhs;}
inline bool
operator> (const string& lhs, const std::string& rhs) {return lhs.str > rhs;}
inline bool
operator<=(const string& lhs, const std::string& rhs) {return lhs.str <= rhs;}
inline bool
operator>=(const string& lhs, const std::string& rhs) {return lhs.str >= rhs;}
inline bool
operator==(const std::string& lhs, const string& rhs) {return lhs == rhs.str;}
inline bool
operator!=(const std::string& lhs, const string& rhs) {return lhs != rhs.str;}
inline bool
operator< (const std::string& lhs, const string& rhs) {return lhs < rhs.str;}
inline bool
operator> (const std::string& lhs, const string& rhs) {return lhs > rhs.str;}
inline bool
operator<=(const std::string& lhs, const string& rhs) {return lhs <= rhs.str;}
inline bool
operator>=(const std::string& lhs, const string& rhs) {return lhs >= rhs.str;}
inline bool
operator==(const string& lhs, const char* rhs) {return lhs.str == std::string(rhs);}
inline bool
operator!=(const string& lhs, const char* rhs) {return lhs.str != std::string(rhs);}
inline bool
operator< (const string& lhs, const char* rhs) {return lhs.str < std::string(rhs);}
inline bool
operator> (const string& lhs, const char* rhs) {return lhs.str > std::string(rhs);}
inline bool
operator<=(const string& lhs, const char* rhs) {return lhs.str <= std::string(rhs);}
inline bool
operator>=(const string& lhs, const char* rhs) {return lhs.str >= std::string(rhs);}
inline bool
operator==(const char* lhs, const string& rhs) {return std::string(lhs) == rhs.str;}
inline bool
operator!=(const char* lhs, const string& rhs) {return std::string(lhs) != rhs.str;}
inline bool
operator< (const char* lhs, const string& rhs) {return std::string(lhs) < rhs.str;}
inline bool
operator> (const char* lhs, const string& rhs) {return std::string(lhs) > rhs.str;}
inline bool
operator<=(const char* lhs, const string& rhs) {return std::string(lhs) <= rhs.str;}
inline bool
operator>=(const char* lhs, const string& rhs) {return std::string(lhs) >= rhs.str;}
template<typename charT, typename traits>
std::basic_ostream<charT, traits>&
operator<<(std::basic_ostream<charT, traits>& os, const string& str)
{
os << str.str;
return os;
}
} // toml
#endif// TOML11_STRING_H

View File

@@ -1,3 +1,5 @@
// Copyright Toru Niina 2017.
// Distributed under the MIT License.
#ifndef TOML11_TO_TOML
#define TOML11_TO_TOML
#include "value.hpp"
@@ -5,47 +7,36 @@
namespace toml
{
template<typename T, toml::value_t vT = toml::detail::check_type<T>(),
typename std::enable_if<(vT != toml::value_t::Unknown &&
vT != value_t::Empty), std::nullptr_t>::type = nullptr>
inline toml::value to_toml(T&& x)
template<typename T>
inline value to_toml(T&& x)
{
return toml::value(std::forward<T>(x));
}
template<typename T, toml::value_t vT = toml::detail::check_type<T>(),
typename std::enable_if<(vT == toml::value_t::Unknown) &&
(!toml::detail::is_map<T>::value) &&
toml::detail::is_container<T>::value, std::nullptr_t>::type = nullptr>
toml::value to_toml(T&& x)
{
toml::Array tmp; tmp.reserve(std::distance(std::begin(x), std::end(x)));
for(auto iter = std::begin(x); iter != std::end(x); ++iter)
tmp.emplace_back(*iter);
return toml::value(std::move(tmp));
}
template<typename T, toml::value_t vT = toml::detail::check_type<T>(),
typename std::enable_if<(vT == toml::value_t::Unknown) &&
toml::detail::is_map<T>::value, std::nullptr_t>::type = nullptr>
toml::value to_toml(T&& x)
{
toml::Table tmp;
for(auto iter = std::begin(x); iter != std::end(x); ++iter)
tmp.emplace(iter->first, to_toml(iter->second));
return toml::value(std::move(tmp));
return value(std::forward<T>(x));
}
template<typename T>
inline toml::value to_toml(std::initializer_list<T> init)
inline value to_toml(T&& x, string_t kind)
{
return toml::value(std::move(init));
return value(std::forward<T>(x), kind);
}
inline toml::value
to_toml(std::initializer_list<std::pair<std::string, toml::value>> init)
inline value to_toml(local_date d, local_time t)
{
return toml::value(std::move(init));
return value(local_datetime(d, t));
}
inline value to_toml(local_date d, local_time t, time_offset ofs)
{
return value(offset_datetime(d, t, ofs));
}
template<typename ... Ts>
inline value to_toml(Ts&& ... xs)
{
return value(toml::array{toml::value(std::forward<Ts>(xs)) ... });
}
inline value to_toml(std::initializer_list<std::pair<std::string, toml::value>> xs)
{
return value(toml::table(xs.begin(), xs.end()));
}
} // toml

View File

@@ -1,6 +1,11 @@
// Copyright Toru Niina 2017.
// Distributed under the MIT License.
#ifndef TOML11_TRAITS
#define TOML11_TRAITS
#include <type_traits>
#include <utility>
#include <chrono>
#include <tuple>
namespace toml
{
@@ -58,14 +63,58 @@ struct has_resize_method : decltype(has_resize_method_impl::check<T>(nullptr)){}
#undef decltype(...)
#endif
template<typename T>
struct is_container : std::integral_constant<bool,
has_iterator<T>::value && has_value_type<T>::value>{};
template<typename ...> struct conjunction : std::true_type{};
template<typename T> struct conjunction<T> : T{};
template<typename T, typename ... Ts>
struct conjunction<T, Ts...> :
std::conditional<static_cast<bool>(T::value), conjunction<Ts...>, T>::type
{};
template<typename ...> struct disjunction : std::false_type{};
template<typename T> struct disjunction<T> : T {};
template<typename T, typename ... Ts>
struct disjunction<T, Ts...> :
std::conditional<static_cast<bool>(T::value), T, disjunction<Ts...>>::type
{};
template<typename T>
struct is_map : std::integral_constant<bool,
has_iterator<T>::value && has_key_type<T>::value &&
has_mapped_type<T>::value>{};
struct negation : std::integral_constant<bool, !static_cast<bool>(T::value)>{};
template<typename T> struct is_std_pair : std::false_type{};
template<typename T1, typename T2>
struct is_std_pair<std::pair<T1, T2>> : std::true_type{};
template<typename T> struct is_std_tuple : std::false_type{};
template<typename ... Ts>
struct is_std_tuple<std::tuple<Ts...>> : std::true_type{};
template<typename T> struct is_chrono_duration: std::false_type{};
template<typename Rep, typename Period>
struct is_chrono_duration<std::chrono::duration<Rep, Period>>: std::true_type{};
// to use toml::get<std::tuple<T1, T2, ...>> in C++11
template<std::size_t ... Ns> struct index_sequence{};
template<typename IS, std::size_t N> struct push_back_index_sequence{};
template<std::size_t N, std::size_t ... Ns>
struct push_back_index_sequence<index_sequence<Ns...>, N>
{
typedef index_sequence<Ns..., N> type;
};
template<std::size_t N>
struct index_sequence_maker
{
typedef typename push_back_index_sequence<
typename index_sequence_maker<N-1>::type, N>::type type;
};
template<>
struct index_sequence_maker<0>
{
typedef index_sequence<0> type;
};
template<std::size_t N>
using make_index_sequence = typename index_sequence_maker<N-1>::type;
}// detail
}//toml

197
toml/types.hpp Normal file
View File

@@ -0,0 +1,197 @@
// Copyright Toru Niina 2017.
// Distributed under the MIT License.
#ifndef TOML11_TYPES_H
#define TOML11_TYPES_H
#include "datetime.hpp"
#include "string.hpp"
#include "traits.hpp"
#include <vector>
#include <unordered_map>
namespace toml
{
using character = char;
class value;
using key = std::string;
using Boolean = bool;
using Integer = std::int64_t;
using Float = double;
using String = ::toml::string;
using Datetime = offset_datetime;
using OffsetDatetime = offset_datetime;
using LocalDatetime = local_datetime;
using LocalDate = local_date;
using LocalTime = local_time;
using Array = std::vector<value>;
using Table = std::unordered_map<key, value>;
// alias for snake_case, consistency with STL/Boost, toml::key, toml::value
using boolean = Boolean;
using integer = Integer;
using floating = Float; // XXX `float` is keyword. we can't use it here
using array = Array;
using table = Table;
enum class value_t : std::uint8_t
{
Empty = 0,
Boolean = 1,
Integer = 2,
Float = 3,
String = 4,
OffsetDatetime = 5,
LocalDatetime = 6,
LocalDate = 7,
LocalTime = 8,
Array = 9,
Table = 10,
Unknown = 255,
};
template<typename charT, typename traits>
inline std::basic_ostream<charT, traits>&
operator<<(std::basic_ostream<charT, traits>& os, value_t t)
{
switch(t)
{
case toml::value_t::Boolean : os << "boolean"; return os;
case toml::value_t::Integer : os << "integer"; return os;
case toml::value_t::Float : os << "float"; return os;
case toml::value_t::String : os << "string"; return os;
case toml::value_t::OffsetDatetime: os << "offset_datetime"; return os;
case toml::value_t::LocalDatetime : os << "local_datetime"; return os;
case toml::value_t::LocalDate : os << "local_date"; return os;
case toml::value_t::LocalTime : os << "local_time"; return os;
case toml::value_t::Array : os << "array"; return os;
case toml::value_t::Table : os << "table"; return os;
case toml::value_t::Empty : os << "empty"; return os;
case toml::value_t::Unknown : os << "unknown"; return os;
default : os << "nothing"; return os;
}
}
template<typename charT = character, typename traits = std::char_traits<charT>,
typename alloc = std::allocator<charT>>
inline std::basic_string<charT, traits, alloc> stringize(value_t t)
{
std::ostringstream oss;
oss << t;
return oss.str();
}
namespace detail
{
template<typename T>
constexpr inline value_t check_type()
{
using type = typename std::remove_cv<
typename std::remove_reference<T>::type
>::type;
return std::is_same<type, toml::boolean>::value ? value_t::Boolean :
std::is_integral<type>::value ? value_t::Integer :
std::is_floating_point<type>::value ? value_t::Float :
std::is_same<type, std::string>::value ? value_t::String :
std::is_same<type, toml::string>::value ? value_t::String :
std::is_same<type, toml::local_date>::value ? value_t::LocalDate :
std::is_same<type, toml::local_time>::value ? value_t::LocalTime :
is_chrono_duration<type>::value ? value_t::LocalTime :
std::is_same<type, toml::local_datetime>::value ? value_t::LocalDatetime :
std::is_same<type, toml::offset_datetime>::value ? value_t::OffsetDatetime :
std::is_same<type, std::chrono::system_clock::time_point>::value ? value_t::OffsetDatetime :
std::is_convertible<type, toml::array>::value ? value_t::Array :
std::is_convertible<type, toml::table>::value ? value_t::Table :
value_t::Unknown;
}
constexpr inline bool is_valid(value_t vt)
{
return vt != value_t::Unknown;
}
template<value_t t> struct toml_default_type;
template<> struct toml_default_type<value_t::Boolean > {typedef boolean type;};
template<> struct toml_default_type<value_t::Integer > {typedef integer type;};
template<> struct toml_default_type<value_t::Float > {typedef floating type;};
template<> struct toml_default_type<value_t::String > {typedef string type;};
template<> struct toml_default_type<value_t::OffsetDatetime>{typedef offset_datetime type;};
template<> struct toml_default_type<value_t::LocalDatetime> {typedef local_datetime type;};
template<> struct toml_default_type<value_t::LocalDate> {typedef local_date type;};
template<> struct toml_default_type<value_t::LocalTime> {typedef local_time type;};
template<> struct toml_default_type<value_t::Array > {typedef array type;};
template<> struct toml_default_type<value_t::Table > {typedef table type;};
template<> struct toml_default_type<value_t::Empty > {typedef void type;};
template<> struct toml_default_type<value_t::Unknown > {typedef void type;};
template<typename T> struct toml_value_t {static constexpr value_t value = value_t::Unknown ;};
template<> struct toml_value_t<Boolean >{static constexpr value_t value = value_t::Boolean ;};
template<> struct toml_value_t<Integer >{static constexpr value_t value = value_t::Integer ;};
template<> struct toml_value_t<Float >{static constexpr value_t value = value_t::Float ;};
template<> struct toml_value_t<String >{static constexpr value_t value = value_t::String ;};
template<> struct toml_value_t<OffsetDatetime>{static constexpr value_t value = value_t::OffsetDatetime;};
template<> struct toml_value_t<LocalDatetime >{static constexpr value_t value = value_t::LocalDatetime ;};
template<> struct toml_value_t<LocalDate >{static constexpr value_t value = value_t::LocalDate ;};
template<> struct toml_value_t<LocalTime >{static constexpr value_t value = value_t::LocalTime ;};
template<> struct toml_value_t<Array >{static constexpr value_t value = value_t::Array ;};
template<> struct toml_value_t<Table >{static constexpr value_t value = value_t::Table ;};
template<typename T> constexpr value_t toml_value_t<T>::value;
constexpr value_t toml_value_t<Boolean >::value;
constexpr value_t toml_value_t<Integer >::value;
constexpr value_t toml_value_t<Float >::value;
constexpr value_t toml_value_t<String >::value;
constexpr value_t toml_value_t<OffsetDatetime>::value;
constexpr value_t toml_value_t<LocalDatetime >::value;
constexpr value_t toml_value_t<LocalDate >::value;
constexpr value_t toml_value_t<LocalTime >::value;
constexpr value_t toml_value_t<Array >::value;
constexpr value_t toml_value_t<Table >::value;
template<typename T>
struct is_exact_toml_type : disjunction<
std::is_same<T, Boolean >,
std::is_same<T, Integer >,
std::is_same<T, Float >,
std::is_same<T, String >,
std::is_same<T, offset_datetime>,
std::is_same<T, local_datetime>,
std::is_same<T, local_date>,
std::is_same<T, local_time>,
std::is_same<T, Array >,
std::is_same<T, Table >
>{};
template<typename T> struct is_exact_toml_type<T&> : is_exact_toml_type<T>{};
template<typename T> struct is_exact_toml_type<T const&> : is_exact_toml_type<T>{};
template<typename T> struct is_exact_toml_type<T volatile&> : is_exact_toml_type<T>{};
template<typename T> struct is_exact_toml_type<T const volatile&>: is_exact_toml_type<T>{};
template<typename T>
struct is_map : conjunction<
has_iterator<T>,
has_value_type<T>,
has_key_type<T>,
has_mapped_type<T>
>{};
template<typename T> struct is_map<T&> : is_map<T>{};
template<typename T> struct is_map<T const&> : is_map<T>{};
template<typename T> struct is_map<T volatile&> : is_map<T>{};
template<typename T> struct is_map<T const volatile&> : is_map<T>{};
template<typename T>
struct is_container : conjunction<
negation<is_map<T>>,
negation<std::is_same<T, std::string>>,
has_iterator<T>,
has_value_type<T>
>{};
template<typename T> struct is_container<T&> : is_container<T>{};
template<typename T> struct is_container<T const&> : is_container<T>{};
template<typename T> struct is_container<T volatile&> : is_container<T>{};
template<typename T> struct is_container<T const volatile&> : is_container<T>{};
} // detail
} // toml
#endif// TOML11_TYPES_H

View File

@@ -1,8 +1,11 @@
// Copyright Toru Niina 2017.
// Distributed under the MIT License.
#ifndef TOML11_UTILITY
#define TOML11_UTILITY
#include "traits.hpp"
#include <utility>
#include <memory>
#include <sstream>
namespace toml
{
@@ -30,7 +33,7 @@ inline void resize_impl(T& container, std::size_t N, std::false_type)
else throw std::invalid_argument("not resizable type");
}
}
} // detail
template<typename T>
inline void resize(T& container, std::size_t N)
@@ -39,5 +42,38 @@ inline void resize(T& container, std::size_t N)
else return detail::resize_impl(container, N, detail::has_resize_method<T>());
}
namespace detail
{
inline std::string concat_to_string_impl(std::ostringstream& oss)
{
return oss.str();
}
template<typename T, typename ... Ts>
std::string concat_to_string_impl(std::ostringstream& oss, T&& head, Ts&& ... tail)
{
oss << std::forward<T>(head);
return concat_to_string_impl(oss, std::forward<Ts>(tail) ... );
}
} // detail
template<typename ... Ts>
std::string concat_to_string(Ts&& ... args)
{
std::ostringstream oss;
oss << std::boolalpha << std::fixed;
return detail::concat_to_string_impl(oss, std::forward<Ts>(args) ...);
}
template<typename T, typename U>
T from_string(const std::string& str, U&& opt)
{
T v(std::forward<U>(opt));
std::istringstream iss(str);
iss >> v;
return v;
}
}// toml
#endif // TOML11_UTILITY

File diff suppressed because it is too large Load Diff