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:
Martin Tournoij 2023-09-30 21:36:59 +02:00 committed by GitHub
parent 6fe7fee127
commit 5221b3d3d6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 48 additions and 40 deletions

View File

@ -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
```
```

9
test1/build.sh Normal file → Executable file
View File

@ -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

6
test1/run.sh Normal file → Executable file
View File

@ -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

3
test2/build.sh Normal file → Executable file
View File

@ -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

12
test2/run.sh Normal file → Executable file
View 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"
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

View File

@ -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) {