summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorArun Isaac2021-02-27 19:47:47 +0530
committerArun Isaac2021-02-27 19:47:47 +0530
commitd475760dfbbc0a3bb2732a924b006e68f6dcc065 (patch)
treef7381f6ff25ef3b91806e66dacef3bc7425e6292
parent52980a641e7434cb7610d7c857df6f1482bd07ec (diff)
downloadccwl-d475760dfbbc0a3bb2732a924b006e68f6dcc065.tar.gz
ccwl-d475760dfbbc0a3bb2732a924b006e68f6dcc065.tar.lz
ccwl-d475760dfbbc0a3bb2732a924b006e68f6dcc065.zip
Display dictionary entries with empty values on the same line.
* ccwl/yaml.scm (display-dictionary-entry): If value is an empty array or dictionary, display it on the same line. (scm->yaml): Go to next line after printing empty array. * tests/yaml.scm: New file. * tests/yaml.scm ("dictionary entries with empty arrays and dictionaries for values must render on the same line"): New test case.
-rw-r--r--ccwl/yaml.scm16
-rw-r--r--tests/yaml.scm14
2 files changed, 24 insertions, 6 deletions
diff --git a/ccwl/yaml.scm b/ccwl/yaml.scm
index f24d27e..0c0bf1d 100644
--- a/ccwl/yaml.scm
+++ b/ccwl/yaml.scm
@@ -53,13 +53,16 @@ ATOM is a symbol."
(display-atom key port)
(display ":" port)
(match value
- ((or #(_ ...)
- ((_ . _) (_ . _) ...))
+ ;; If value is an empty array or dictionary, display it on the
+ ;; same line.
+ ((or #() ())
+ (display " " port)
+ (scm->yaml value port level))
+ ;; Else, display it on the next line.
+ (_
(newline port)
(indent-level port (1+ level))
- (scm->yaml value port (1+ level)))
- (_ (display " " port)
- (scm->yaml value port level))))))
+ (scm->yaml value port (1+ level)))))))
(define* (scm->yaml scm #:optional (port (current-output-port)) (level 0))
"Convert SCM, an S-expression tree, to YAML and display to
@@ -72,7 +75,8 @@ PORT. LEVEL is an internal recursion variable."
(display-array-element element port level))
tail))
(#()
- (display "[]" port))
+ (display "[]" port)
+ (newline port))
((head tail ...)
(display-dictionary-entry head port level)
(for-each (lambda (entry)
diff --git a/tests/yaml.scm b/tests/yaml.scm
new file mode 100644
index 0000000..767cdff
--- /dev/null
+++ b/tests/yaml.scm
@@ -0,0 +1,14 @@
+(use-modules (ccwl yaml)
+ (srfi srfi-64))
+
+(test-begin "yaml")
+
+(test-equal "dictionary entries with empty arrays and dictionaries for values must render on the same line"
+ (scm->yaml-string
+ '((foo . #())
+ (bar)))
+ "foo: []
+bar: {}
+")
+
+(test-end "yaml")