Skip to content

Commit

Permalink
Fix toml_json.c output (#85)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
arp242 authored Sep 30, 2023
1 parent 6fe7fee commit 5221b3d
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 40 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down Expand Up @@ -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
Expand All @@ -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 changes: 3 additions & 6 deletions test1/build.sh
100644 → 100755
Original file line number Diff line number Diff line change
@@ -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 changes: 3 additions & 3 deletions test1/run.sh
100644 → 100755
Original file line number Diff line number Diff line change
@@ -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/[email protected]/tests
./toml-test ../toml_json
$DIR/toml-test $DIR/../toml_json
3 changes: 2 additions & 1 deletion test2/build.sh
100644 → 100755
Original file line number Diff line number Diff line change
@@ -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 changes: 7 additions & 5 deletions test2/run.sh
100644 → 100755
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
52 changes: 30 additions & 22 deletions toml_json.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
}

if (0 != (tab = toml_table_at(curarr, i))) {
print_table(tab);
continue;
}

raw = toml_raw_at(curarr, i);
if (raw) {
print_raw(raw);
continue;
}
break;

default:
break;
fflush(stdout);
fprintf(stderr, "ERROR: unable to decode value in array\n");
exit(1);
}
printf("]}");

printf("]");
}

static void cat(FILE *fp) {
Expand Down

0 comments on commit 5221b3d

Please sign in to comment.