diff --git a/README.md b/README.md index 2c90c26..956ef9b 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ If you are looking for a C++ library, you might try this wrapper: [https://githu * Compatible with [TOML v1.0.0](https://toml.io/en/v1.0.0). * Tested with multiple test suites, including -[BurntSushi/toml-test](https://github.com/BurntSushi/toml-test) and +[toml-lang/toml-test](https://github.com/toml-lang/toml-test) and [iarna/toml-spec-tests](https://github.com/iarna/toml-spec-tests). * Provides very simple and intuitive interface. @@ -174,7 +174,7 @@ Alternatively, specify `make install prefix=/a/file/path` to install into ## Testing -To test against the standard test set provided by BurntSushi/toml-test: +To test against the standard test set provided by toml-lang/toml-test: ```sh % make @@ -191,4 +191,4 @@ To test against the standard test set provided by iarna/toml: % cd test2 % bash build.sh # do this once % bash run.sh # this will run the test suite -``` +``` \ No newline at end of file diff --git a/test1/build.sh b/test1/build.sh old mode 100644 new mode 100755 index 645a271..496806a --- a/test1/build.sh +++ b/test1/build.sh @@ -1,9 +1,6 @@ +#!/usr/bin/env bash DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" -mkdir -p $DIR/goworkspace -export GOPATH=$DIR/goworkspace -go get github.com/BurntSushi/toml-test@latest # install test suite -go install github.com/BurntSushi/toml/cmd/toml-test-decoder@latest # e.g., install my parser -cp $GOPATH/bin/* . - +export GOBIN=$DIR +go install github.com/toml-lang/toml-test/cmd/toml-test@latest # install test suite \ No newline at end of file diff --git a/test1/run.sh b/test1/run.sh old mode 100644 new mode 100755 index 356a8cb..2e50ba4 --- a/test1/run.sh +++ b/test1/run.sh @@ -1,5 +1,5 @@ +#!/usr/bin/env bash + DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" -rm -f tests -ln -s ./goworkspace/pkg/mod/github.com/\!burnt\!sushi/toml-test@v0.1.0/tests -./toml-test ../toml_json +$DIR/toml-test $DIR/../toml_json diff --git a/test2/build.sh b/test2/build.sh old mode 100644 new mode 100755 index 74da4d0..1f25f8c --- a/test2/build.sh +++ b/test2/build.sh @@ -1,6 +1,7 @@ +#!/usr/bin/env bash + set -e DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" [ -d toml-spec-tests ] || git clone https://github.com/cktan/toml-spec-tests.git - diff --git a/test2/run.sh b/test2/run.sh old mode 100644 new mode 100755 index e1d93df..f67995a --- a/test2/run.sh +++ b/test2/run.sh @@ -1,4 +1,6 @@ -if ! (which jq >& /dev/null); then +#!/usr/bin/env bash + +if ! (which jq >& /dev/null); then echo "ERROR: please install the 'jq' utility" exit 1 fi @@ -12,11 +14,11 @@ for i in toml-spec-tests/values/*.toml; do fname="${fname%.*}" echo -n $fname ' ' res='[OK]' - if (../toml_json $fname.toml >& $fname.json.out); then + if (../toml_json $fname.toml >& $fname.json.out); then jq -S . $fname.json.out > t.json mv t.json $fname.json.out if [ -f $fname.json ]; then - if ! (diff $fname.json $fname.json.out >& /dev/null); then + if ! (diff $fname.json $fname.json.out >& /dev/null); then res='[FAILED]' else rm -f $fname.json.out @@ -32,10 +34,10 @@ done # # NEGATIVE tests # -for i in toml-spec-tests/errors/*.toml; do +for i in toml-spec-tests/errors/*.toml; do echo -n $i ' ' res='[OK]' - if (../toml_json $i >& $i.json.out); then + if (../toml_json $i >& $i.json.out); then res='[FAILED]' fi echo ... $res diff --git a/toml_json.c b/toml_json.c index a939a23..936252d 100644 --- a/toml_json.c +++ b/toml_json.c @@ -91,15 +91,16 @@ static void print_raw(const char *s) { else millisec[0] = 0; if (ts.year && ts.hour) { - printf("{\"type\":\"datetime\",\"value\":\"%04d-%02d-%02dT%02d:%02d:%02d%" + printf("{\"type\":\"%s\",\"value\":\"%04d-%02d-%02dT%02d:%02d:%02d%" "s%s\"}", + (ts.z ? "datetime" : "datetime-local"), *ts.year, *ts.month, *ts.day, *ts.hour, *ts.minute, *ts.second, millisec, (ts.z ? ts.z : "")); } else if (ts.year) { - printf("{\"type\":\"date\",\"value\":\"%04d-%02d-%02d\"}", *ts.year, + printf("{\"type\":\"date-local\",\"value\":\"%04d-%02d-%02d\"}", *ts.year, *ts.month, *ts.day); } else if (ts.hour) { - printf("{\"type\":\"time\",\"value\":\"%02d:%02d:%02d%s\"}", *ts.hour, + printf("{\"type\":\"time-local\",\"value\":\"%02d:%02d:%02d%s\"}", *ts.hour, *ts.minute, *ts.second, millisec); } } else { @@ -149,36 +150,43 @@ static void print_table_array(toml_array_t *curarr) { } static void print_array(toml_array_t *curarr) { - toml_array_t *arr; - const char *raw; - int i; - if (toml_array_kind(curarr) == 't') { print_table_array(curarr); return; } - printf("{\"type\":\"array\",\"value\":["); - switch (toml_array_kind(curarr)) { + printf("["); - case 'v': - for (i = 0; 0 != (raw = toml_raw_at(curarr, i)); i++) { - printf("%s", i > 0 ? "," : ""); - print_raw(raw); - } - break; + const char *raw; + toml_array_t *arr; + toml_table_t *tab; - case 'a': - for (i = 0; 0 != (arr = toml_array_at(curarr, i)); i++) { - printf("%s", i > 0 ? "," : ""); + const int n = toml_array_nelem(curarr); + for (int i = 0; i < n; i++) { + printf("%s", i > 0 ? "," : ""); + + if (0 != (arr = toml_array_at(curarr, i))) { print_array(arr); + continue; } - break; - default: - break; + if (0 != (tab = toml_table_at(curarr, i))) { + print_table(tab); + continue; + } + + raw = toml_raw_at(curarr, i); + if (raw) { + print_raw(raw); + continue; + } + + fflush(stdout); + fprintf(stderr, "ERROR: unable to decode value in array\n"); + exit(1); } - printf("]}"); + + printf("]"); } static void cat(FILE *fp) {