|
8 | 8 | "strings" |
9 | 9 | "testing" |
10 | 10 |
|
| 11 | + "github.com/antchfx/xmlquery" |
11 | 12 | "github.com/sibprogrammer/xq/internal/utils" |
12 | 13 | "github.com/spf13/cobra" |
13 | 14 | "github.com/spf13/pflag" |
@@ -199,3 +200,38 @@ Bye.`, |
199 | 200 | }) |
200 | 201 | } |
201 | 202 | } |
| 203 | + |
| 204 | +func TestFormatDetection(t *testing.T) { |
| 205 | + t.Run("CDATA should be detected as XML not JSON", func(t *testing.T) { |
| 206 | + input := "<root><![CDATA[1 & 2]]></root>" |
| 207 | + reader := strings.NewReader(input) |
| 208 | + |
| 209 | + flags := pflag.NewFlagSet("test", pflag.ContinueOnError) |
| 210 | + flags.Bool("html", false, "") |
| 211 | + |
| 212 | + contentType, _ := detectFormat(flags, reader) |
| 213 | + |
| 214 | + // Should be XML (0), not JSON (2) |
| 215 | + assert.Equal(t, utils.ContentXml, contentType, "CDATA should be detected as XML") |
| 216 | + }) |
| 217 | + |
| 218 | + t.Run("Debug CDATA node structure", func(t *testing.T) { |
| 219 | + input := "<root><![CDATA[1 & 2]]></root>" |
| 220 | + doc, err := xmlquery.Parse(strings.NewReader(input)) |
| 221 | + assert.Nil(t, err) |
| 222 | + |
| 223 | + // Walk the tree and log all node types |
| 224 | + var walk func(*xmlquery.Node, int) |
| 225 | + walk = func(n *xmlquery.Node, depth int) { |
| 226 | + if n == nil { |
| 227 | + return |
| 228 | + } |
| 229 | + indent := strings.Repeat(" ", depth) |
| 230 | + t.Logf("%sType: %d, Data: %q", indent, n.Type, n.Data) |
| 231 | + for child := n.FirstChild; child != nil; child = child.NextSibling { |
| 232 | + walk(child, depth+1) |
| 233 | + } |
| 234 | + } |
| 235 | + walk(doc, 0) |
| 236 | + }) |
| 237 | +} |
0 commit comments