AtlatestRepositorysigil-sxml
sigil-sxml / tree / testtest-sxml.sgl
1
(import (sigil test)2
(sigil sxml))4
;; ============================================================5
;; xml-escape6
;; ============================================================8
(test-group "xml-escape"9
(test "escapes ampersand"10
(assert-equal "Tom & Jerry" (xml-escape "Tom & Jerry")))12
(test "escapes less-than"13
(assert-equal "<script>" (xml-escape "<script>")))15
(test "escapes double quote"16
(assert-equal "say "hi"" (xml-escape "say \"hi\"")))18
(test "escapes single quote"19
(assert-equal "it's" (xml-escape "it's")))21
(test "plain text unchanged"22
(assert-equal "hello world" (xml-escape "hello world")))24
(test "empty string"25
(assert-equal "" (xml-escape ""))))27
;; ============================================================28
;; Element inspection29
;; ============================================================31
(test-group "element inspection"32
(test "sxml-element? on element"33
(assert-true (sxml-element? '(div "hello"))))35
(test "sxml-element? on string"36
(assert-false (sxml-element? "text")))38
(test "sxml-element? on number"39
(assert-false (sxml-element? 42)))41
(test "sxml-tag"42
(assert-equal 'div (sxml-tag '(div "hello"))))44
(test "sxml-attributes with @"45
(assert-equal '((id "main") (class "box"))46
(sxml-attributes '(div (@ (id "main") (class "box")) "text"))))48
(test "sxml-attributes without @"49
(assert-equal '() (sxml-attributes '(p "text"))))51
(test "sxml-content with @"52
(assert-equal '("hello" "world")53
(sxml-content '(div (@ (id "main")) "hello" "world"))))55
(test "sxml-content without @"56
(assert-equal '("text") (sxml-content '(p "text")))))58
;; ============================================================59
;; Attribute utilities60
;; ============================================================62
(test-group "sxml-attr-ref"63
(test "finds attribute"64
(assert-equal "main" (sxml-attr-ref '(div (@ (id "main"))) 'id)))66
(test "returns #f for missing attribute"67
(assert-false (sxml-attr-ref '(div (@ (id "main"))) 'class))))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))))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))))79
(test "preserves content"80
(let ((result (sxml-attr-set '(div (@ (class "a")) "hello") 'id "b")))81
(assert-equal '("hello") (sxml-content result)))))83
;; ============================================================84
;; sxml->html85
;; ============================================================87
(test-group "sxml->html"88
(test "simple element"89
(assert-equal "<p>Hello</p>" (sxml->html '(p "Hello"))))91
(test "element with attributes"92
(assert-equal "<div class=\"main\">text</div>"93
(sxml->html '(div (@ (class "main")) "text"))))95
(test "void element (br) no closing tag"96
(assert-equal "<br>" (sxml->html '(br))))98
(test "void element (img) no closing slash"99
(assert-equal "<img src=\"photo.jpg\">"100
(sxml->html '(img (@ (src "photo.jpg"))))))102
(test "nested elements"103
(assert-equal "<div><p>inner</p></div>"104
(sxml->html '(div (p "inner")))))106
(test "text escaping"107
(assert-equal "<p>A & B</p>"108
(sxml->html '(p "A & B"))))110
(test "raw text element (script) not escaped"111
(assert-equal "<script>x < 1</script>"112
(sxml->html '(script "x < 1"))))114
(test "document wrapper stripped"115
(assert-equal "<p>From markdown</p>"116
(sxml->html '(document (p "From markdown")))))118
(test "*raw* content passes through"119
(assert-equal "<div><b>bold</b></div>"120
(sxml->html '(div (*raw* "<b>bold</b>"))))))122
;; ============================================================123
;; sxml->xml124
;; ============================================================126
(test-group "sxml->xml"127
(test "simple element"128
(assert-equal "<p>Hello</p>" (sxml->xml '(p "Hello"))))130
(test "void element self-closes with />"131
(assert-equal "<br />" (sxml->xml '(br))))133
(test "void element with attributes"134
(assert-equal "<img src=\"photo.jpg\" />"135
(sxml->xml '(img (@ (src "photo.jpg")))))))137
(run-tests)