fix: date-only timestamp parsing

This commit is contained in:
Daniel Ziltener 2022-10-14 15:20:11 +02:00
parent e3400352bb
commit c966b0e022
7 changed files with 435 additions and 450 deletions

View file

@ -1,67 +1,52 @@
(import (r7rs)
(scheme base)
(scheme write)
(srfi 64)
(srfi 152)
(test)
(rfc3339)
(toml))
(define (tap-test-runner)
(let ((runner (test-runner-null))
(testcounter 0))
(display "TAP version 13\n")
(test-runner-on-test-end! runner
(lambda (runner)
(set! testcounter (+ testcounter 1))
(display
(string-append
(if (test-passed? runner) "ok " "not ok ")
(number->string testcounter) " - "
(string-join (test-runner-group-path runner) " - ")
" - " (test-runner-test-name runner)
(if (eq? 'skip (test-result-kind runner)) "# SKIP" "")
"\n"))))
(test-runner-on-final! runner
(lambda (runner)
(display (string-append "1.." (number->string testcounter) "\n"))))
runner))
(test-runner-factory
(lambda () (tap-test-runner)))
(test-begin "Basic")
(test-group "Basic"
(let ((tdat (table-from-file "basic.toml")))
(test-equal 2 (toml-count-key-vals tdat))
(test-equal "TOML" (toml-string tdat "name"))
(test-equal "Chicken Scheme" (toml-string tdat "language"))
(test-equal #t (toml-bool tdat "has-bool"))
(test-equal 5 (toml-int tdat "int"))
(test-equal 10.8 (toml-double tdat "double"))
(test-equal (rfc3339->string (vector->rfc3339 #(1979 05 27 07 32 00 0 0)))
(rfc3339->string (toml-timestamp tdat "timestamp"))))
(test-end "Basic")
(test-begin "Table")
(test "7 Key-Value-Pairs"
7 (toml-count-key-vals tdat))
(test "Field name is TOML"
"TOML" (toml-string tdat "name"))
(test "Field language is Chicken Scheme"
"Chicken Scheme" (toml-string tdat "language"))
(test "has-bool is #t"
#t (toml-bool tdat "has-bool"))
(test "int is 5"
5 (toml-int tdat "int"))
(test "double is 10.8"
10.8 (toml-double tdat "double"))
(test "timestamp parsing"
#(1979 05 27 07 32 00 0.0 0)
(rfc3339->vector (toml-timestamp tdat "timestamp")))))
(test-group "Table"
(let ((tdat (table-from-file "table.toml")))
(test-equal 0 (toml-count-key-vals tdat))
(test-equal 1 (toml-count-tables tdat))
(test "No top-level Key-Value-Pairs"
0 (toml-count-key-vals tdat))
(test "One top-level table"
1 (toml-count-tables tdat))
(let ((servertbl (toml-table tdat "server")))
(test-equal 1 (toml-count-key-vals servertbl))
(test-equal "www.example.com" (toml-string servertbl "host"))))
(test-end "Table")
(test-begin "Array")
(test "\"server\" table has 2 Key-Value-Pairs"
2 (toml-count-key-vals servertbl))
(test "host is www.example.com"
"www.example.com" (toml-string servertbl "host"))
(test "timestamp parsing"
#(2022 09 09 0 0 0 0.0 0)
(rfc3339->vector (toml-timestamp servertbl "timestamp"))))))
(test-group "Array"
(let* ((tdat (table-from-file "table.toml"))
(tserv (toml-table tdat "server"))
(tarr (toml-array tserv "port")))
(test-equal 1 (toml-count-arrays tserv))
(test-equal 3 (toml-count-entries tarr))
(test-equal 8080 (toml-int tarr 0))
(test-equal 8282 (toml-int tarr 2)))
(test "There is one array"
1 (toml-count-arrays tserv))
(test "The array has three entries"
3 (toml-count-entries tarr))
(test "Element 0 is 8080"
8080 (toml-int tarr 0))
(test "Element 2 is 8282"
8282 (toml-int tarr 2))))
(test-end "Array")
(test-exit)

View file

@ -1,3 +1,4 @@
[server]
host = "www.example.com"
port = [ 8080, 8181, 8282 ]
timestamp = 2022-09-09

View file

@ -75,7 +75,7 @@
(int index))
"toml_datum_t datum = toml_timestamp_at(tarr, index);"
"toml_timestamp_t* stamp = datum.u.ts;"
"C_word* s = C_alloc(C_SIZEOF_STRING(strlen(stamp->z)));"
"C_word* s = C_alloc(C_SIZEOF_STRING(strlen(stamp->z ?: \"Z\")));"
"C_word data[10] = { C_SCHEME_UNDEFINED, C_k, "
"C_fix(stamp->year ? *stamp->year : 0), C_fix(stamp->month ? *stamp->month : 0), C_fix(stamp->day ? *stamp->day : 0), "
"C_fix(stamp->hour ? *stamp->hour : 0), C_fix(stamp->minute ? *stamp->minute : 0),"
@ -106,11 +106,6 @@
(define (table-from-file filename)
(let ((ttp ((foreign-lambda* c-pointer ((c-string fname))
"FILE* fp = fopen(fname, \"r\");"
@ -209,7 +204,7 @@
(c-string key))
"toml_datum_t datum = toml_timestamp_in(ttbl, key);"
"toml_timestamp_t* stamp = datum.u.ts;"
"C_word* s = C_alloc(C_SIZEOF_STRING(strlen(stamp->z)));"
"C_word* s = C_alloc(C_SIZEOF_STRING(strlen(stamp->z ?: \"Z\")));"
"C_word data[10] = { C_SCHEME_UNDEFINED, C_k, "
"C_fix(stamp->year ? *stamp->year : 0), C_fix(stamp->month ? *stamp->month : 0), C_fix(stamp->day ? *stamp->day : 0), "
"C_fix(stamp->hour ? *stamp->hour : 0), C_fix(stamp->minute ? *stamp->minute : 0),"

View file

@ -5,7 +5,7 @@
(license "MIT")
(version "0.6")
(dependencies r7rs rfc3339 coops)
(test-dependencies srfi-64 srfi-152)
(test-dependencies test)
(components
(c-object tomlc99/toml

View file

@ -1,3 +1,4 @@
;; -*- Scheme -*-
(repo git "https://gitea.lyrion.ch/zilti/toml.git")
(uri targz "https://gitea.lyrion.ch/zilti/toml/archive/{egg-release}.tar.gz")
(release "0.6")

View file

@ -88,6 +88,9 @@ The repository of the Chicken wrapper can be found at [[https://gitea.lyrion.ch/
The repository of the C implementation being wrapped can be found at [[https://github.com/cktan/tomlc99|https://github.com/cktan/tomlc99]].
=== Version History
; 0.7 : fixed an issue with timestamp parsing
; 0.6 : first version of the wrapper
=== License