Commit60536fc7Recorded15 Feb 2026Repositorysigil-sxml

test(sxml): Add test coverage for sigil-sxml package (Phase 3.3)

Message

Tests xml-escape, element inspection (sxml-element?, sxml-tag, sxml-attributes, sxml-content), attribute utilities, sxml->html with void/raw/nested/document elements, and sxml->xml self-closing.

Changed
 test/test-sxml.sgl | 137 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 137 insertions(+)
Diff
test/test-sxml.sgladded
@@ -0,0 +1,137 @@
+1
(import (sigil test)
+2
(sigil sxml))
+3
+4
;; ============================================================
+5
;; xml-escape
+6
;; ============================================================
+7
+8
(test-group "xml-escape"
+9
(test "escapes ampersand"
+10
(assert-equal "Tom & Jerry" (xml-escape "Tom & Jerry")))
+11
+12
(test "escapes less-than"
+13
(assert-equal "&lt;script&gt;" (xml-escape "<script>")))
+14
+15
(test "escapes double quote"
+16
(assert-equal "say &quot;hi&quot;" (xml-escape "say \"hi\"")))
+17
+18
(test "escapes single quote"
+19
(assert-equal "it&apos;s" (xml-escape "it's")))
+20
+21
(test "plain text unchanged"
+22
(assert-equal "hello world" (xml-escape "hello world")))
+23
+24
(test "empty string"
+25
(assert-equal "" (xml-escape ""))))
+26
+27
;; ============================================================
+28
;; Element inspection
+29
;; ============================================================
+30
+31
(test-group "element inspection"
+32
(test "sxml-element? on element"
+33
(assert-true (sxml-element? '(div "hello"))))
+34
+35
(test "sxml-element? on string"
+36
(assert-false (sxml-element? "text")))
+37
+38
(test "sxml-element? on number"
+39
(assert-false (sxml-element? 42)))
+40
+41
(test "sxml-tag"
+42
(assert-equal 'div (sxml-tag '(div "hello"))))
+43
+44
(test "sxml-attributes with @"
+45
(assert-equal '((id "main") (class "box"))
+46
(sxml-attributes '(div (@ (id "main") (class "box")) "text"))))
+47
+48
(test "sxml-attributes without @"
+49
(assert-equal '() (sxml-attributes '(p "text"))))
+50
+51
(test "sxml-content with @"
+52
(assert-equal '("hello" "world")
+53
(sxml-content '(div (@ (id "main")) "hello" "world"))))
+54
+55
(test "sxml-content without @"
+56
(assert-equal '("text") (sxml-content '(p "text")))))
+57
+58
;; ============================================================
+59
;; Attribute utilities
+60
;; ============================================================
+61
+62
(test-group "sxml-attr-ref"
+63
(test "finds attribute"
+64
(assert-equal "main" (sxml-attr-ref '(div (@ (id "main"))) 'id)))
+65
+66
(test "returns #f for missing attribute"
+67
(assert-false (sxml-attr-ref '(div (@ (id "main"))) 'class))))
+68
+69
(test-group "sxml-attr-set"
+70
(test "adds new attribute"
+71
(let ((result (sxml-attr-set '(div "text") 'id "main")))
+72
(assert-equal "main" (sxml-attr-ref result 'id))
+73
(assert-equal '("text") (sxml-content result))))
+74
+75
(test "updates existing attribute"
+76
(let ((result (sxml-attr-set '(div (@ (id "old"))) 'id "new")))
+77
(assert-equal "new" (sxml-attr-ref result 'id))))
+78
+79
(test "preserves content"
+80
(let ((result (sxml-attr-set '(div (@ (class "a")) "hello") 'id "b")))
+81
(assert-equal '("hello") (sxml-content result)))))
+82
+83
;; ============================================================
+84
;; sxml->html
+85
;; ============================================================
+86
+87
(test-group "sxml->html"
+88
(test "simple element"
+89
(assert-equal "<p>Hello</p>" (sxml->html '(p "Hello"))))
+90
+91
(test "element with attributes"
+92
(assert-equal "<div class=\"main\">text</div>"
+93
(sxml->html '(div (@ (class "main")) "text"))))
+94
+95
(test "void element (br) no closing tag"
+96
(assert-equal "<br>" (sxml->html '(br))))
+97
+98
(test "void element (img) no closing slash"
+99
(assert-equal "<img src=\"photo.jpg\">"
+100
(sxml->html '(img (@ (src "photo.jpg"))))))
+101
+102
(test "nested elements"
+103
(assert-equal "<div><p>inner</p></div>"
+104
(sxml->html '(div (p "inner")))))
+105
+106
(test "text escaping"
+107
(assert-equal "<p>A &amp; B</p>"
+108
(sxml->html '(p "A & B"))))
+109
+110
(test "raw text element (script) not escaped"
+111
(assert-equal "<script>x < 1</script>"
+112
(sxml->html '(script "x < 1"))))
+113
+114
(test "document wrapper stripped"
+115
(assert-equal "<p>From markdown</p>"
+116
(sxml->html '(document (p "From markdown")))))
+117
+118
(test "*raw* content passes through"
+119
(assert-equal "<div><b>bold</b></div>"
+120
(sxml->html '(div (*raw* "<b>bold</b>"))))))
+121
+122
;; ============================================================
+123
;; sxml->xml
+124
;; ============================================================
+125
+126
(test-group "sxml->xml"
+127
(test "simple element"
+128
(assert-equal "<p>Hello</p>" (sxml->xml '(p "Hello"))))
+129
+130
(test "void element self-closes with />"
+131
(assert-equal "<br />" (sxml->xml '(br))))
+132
+133
(test "void element with attributes"
+134
(assert-equal "<img src=\"photo.jpg\" />"
+135
(sxml->xml '(img (@ (src "photo.jpg")))))))
+136
+137
(run-tests)