Fix toml_json.c output (#85)
* Fix toml_json.c output There were two issues; partly related to changes in upstream toml-test: - Use appropriate type for local date and times. - The arrays would get printed as: {"type": "array", "value": [...the values...]} But this should just be: [...the values...] It also wouldn't print mixed arrays because 'm' was missing in the switch; I adapted this from toml_cat.c. Before: toml-test [./toml_json]: using embedded tests: 328 passed, 87 failed After: toml-test [./toml_json]: using embedded tests: 351 passed, 64 failed The remaining test failures look like a few minor issues in toml.c, rather than toml_json.c * Also fix the "test1" toml-test runner
This commit is contained in:
parent
6fe7fee127
commit
5221b3d3d6
6 changed files with 48 additions and 40 deletions
|
@ -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).
|
* Compatible with [TOML v1.0.0](https://toml.io/en/v1.0.0).
|
||||||
* Tested with multiple test suites, including
|
* 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).
|
[iarna/toml-spec-tests](https://github.com/iarna/toml-spec-tests).
|
||||||
* Provides very simple and intuitive interface.
|
* Provides very simple and intuitive interface.
|
||||||
|
|
||||||
|
@ -174,7 +174,7 @@ Alternatively, specify `make install prefix=/a/file/path` to install into
|
||||||
|
|
||||||
## Testing
|
## 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
|
```sh
|
||||||
% make
|
% make
|
||||||
|
@ -191,4 +191,4 @@ To test against the standard test set provided by iarna/toml:
|
||||||
% cd test2
|
% cd test2
|
||||||
% bash build.sh # do this once
|
% bash build.sh # do this once
|
||||||
% bash run.sh # this will run the test suite
|
% bash run.sh # this will run the test suite
|
||||||
```
|
```
|
9
test1/build.sh
Normal file → Executable file
9
test1/build.sh
Normal file → Executable file
|
@ -1,9 +1,6 @@
|
||||||
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
|
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
|
||||||
|
|
||||||
mkdir -p $DIR/goworkspace
|
export GOBIN=$DIR
|
||||||
export GOPATH=$DIR/goworkspace
|
go install github.com/toml-lang/toml-test/cmd/toml-test@latest # install test suite
|
||||||
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/* .
|
|
||||||
|
|
6
test1/run.sh
Normal file → Executable file
6
test1/run.sh
Normal file → Executable file
|
@ -1,5 +1,5 @@
|
||||||
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
|
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
|
||||||
|
|
||||||
rm -f tests
|
$DIR/toml-test $DIR/../toml_json
|
||||||
ln -s ./goworkspace/pkg/mod/github.com/\!burnt\!sushi/toml-test@v0.1.0/tests
|
|
||||||
./toml-test ../toml_json
|
|
||||||
|
|
3
test2/build.sh
Normal file → Executable file
3
test2/build.sh
Normal file → Executable file
|
@ -1,6 +1,7 @@
|
||||||
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
set -e
|
set -e
|
||||||
|
|
||||||
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
|
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
|
||||||
|
|
||||||
[ -d toml-spec-tests ] || git clone https://github.com/cktan/toml-spec-tests.git
|
[ -d toml-spec-tests ] || git clone https://github.com/cktan/toml-spec-tests.git
|
||||||
|
|
||||||
|
|
12
test2/run.sh
Normal file → Executable file
12
test2/run.sh
Normal file → Executable file
|
@ -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"
|
echo "ERROR: please install the 'jq' utility"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
@ -12,11 +14,11 @@ for i in toml-spec-tests/values/*.toml; do
|
||||||
fname="${fname%.*}"
|
fname="${fname%.*}"
|
||||||
echo -n $fname ' '
|
echo -n $fname ' '
|
||||||
res='[OK]'
|
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
|
jq -S . $fname.json.out > t.json
|
||||||
mv t.json $fname.json.out
|
mv t.json $fname.json.out
|
||||||
if [ -f $fname.json ]; then
|
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]'
|
res='[FAILED]'
|
||||||
else
|
else
|
||||||
rm -f $fname.json.out
|
rm -f $fname.json.out
|
||||||
|
@ -32,10 +34,10 @@ done
|
||||||
#
|
#
|
||||||
# NEGATIVE tests
|
# NEGATIVE tests
|
||||||
#
|
#
|
||||||
for i in toml-spec-tests/errors/*.toml; do
|
for i in toml-spec-tests/errors/*.toml; do
|
||||||
echo -n $i ' '
|
echo -n $i ' '
|
||||||
res='[OK]'
|
res='[OK]'
|
||||||
if (../toml_json $i >& $i.json.out); then
|
if (../toml_json $i >& $i.json.out); then
|
||||||
res='[FAILED]'
|
res='[FAILED]'
|
||||||
fi
|
fi
|
||||||
echo ... $res
|
echo ... $res
|
||||||
|
|
52
toml_json.c
52
toml_json.c
|
@ -91,15 +91,16 @@ static void print_raw(const char *s) {
|
||||||
else
|
else
|
||||||
millisec[0] = 0;
|
millisec[0] = 0;
|
||||||
if (ts.year && ts.hour) {
|
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\"}",
|
"s%s\"}",
|
||||||
|
(ts.z ? "datetime" : "datetime-local"),
|
||||||
*ts.year, *ts.month, *ts.day, *ts.hour, *ts.minute, *ts.second,
|
*ts.year, *ts.month, *ts.day, *ts.hour, *ts.minute, *ts.second,
|
||||||
millisec, (ts.z ? ts.z : ""));
|
millisec, (ts.z ? ts.z : ""));
|
||||||
} else if (ts.year) {
|
} 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);
|
*ts.month, *ts.day);
|
||||||
} else if (ts.hour) {
|
} 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);
|
*ts.minute, *ts.second, millisec);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
@ -149,36 +150,43 @@ static void print_table_array(toml_array_t *curarr) {
|
||||||
}
|
}
|
||||||
|
|
||||||
static void print_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') {
|
if (toml_array_kind(curarr) == 't') {
|
||||||
print_table_array(curarr);
|
print_table_array(curarr);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("{\"type\":\"array\",\"value\":[");
|
printf("[");
|
||||||
switch (toml_array_kind(curarr)) {
|
|
||||||
|
|
||||||
case 'v':
|
const char *raw;
|
||||||
for (i = 0; 0 != (raw = toml_raw_at(curarr, i)); i++) {
|
toml_array_t *arr;
|
||||||
printf("%s", i > 0 ? "," : "");
|
toml_table_t *tab;
|
||||||
print_raw(raw);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 'a':
|
const int n = toml_array_nelem(curarr);
|
||||||
for (i = 0; 0 != (arr = toml_array_at(curarr, i)); i++) {
|
for (int i = 0; i < n; i++) {
|
||||||
printf("%s", i > 0 ? "," : "");
|
printf("%s", i > 0 ? "," : "");
|
||||||
|
|
||||||
|
if (0 != (arr = toml_array_at(curarr, i))) {
|
||||||
print_array(arr);
|
print_array(arr);
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
break;
|
|
||||||
|
|
||||||
default:
|
if (0 != (tab = toml_table_at(curarr, i))) {
|
||||||
break;
|
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) {
|
static void cat(FILE *fp) {
|
||||||
|
|
Loading…
Reference in a new issue