File: root_test.go

package info (click to toggle)
xq 1.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 596 kB
  • sloc: xml: 196; sh: 35; makefile: 6
file content (192 lines) | stat: -rw-r--r-- 4,635 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package cmd

import (
	"bytes"
	"encoding/json"
	"fmt"
	"path"
	"strings"
	"testing"

	"github.com/sibprogrammer/xq/internal/utils"
	"github.com/spf13/cobra"
	"github.com/spf13/pflag"
	"github.com/stretchr/testify/assert"
)

func execute(cmd *cobra.Command, args ...string) (string, error) {
	buf := new(bytes.Buffer)
	cmd.SetOut(buf)
	cmd.SetErr(buf)
	if len(args) > 0 {
		cmd.SetArgs(args)
	} else {
		cmd.SetArgs([]string{})
	}

	err := cmd.Execute()

	return strings.TrimSpace(buf.String()), err
}

func TestRootCmd(t *testing.T) {
	command := NewRootCmd()
	InitFlags(command)

	var output string
	var err error
	xmlFilePath := path.Join("..", "test", "data", "xml", "unformatted.xml")
	htmlFilePath := path.Join("..", "test", "data", "html", "unformatted.html")
	jsonFilePath := path.Join("..", "test", "data", "json", "unformatted.json")

	output, err = execute(command)
	assert.Nil(t, err)
	assert.Contains(t, output, "Usage:")

	output, err = execute(command, xmlFilePath)
	assert.Nil(t, err)
	assert.Contains(t, output, "This is not a real user")

	output, err = execute(command, "--no-color", xmlFilePath)
	assert.Nil(t, err)
	assert.Contains(t, output, "first_name")

	output, err = execute(command, "--indent", "0", xmlFilePath)
	assert.Nil(t, err)
	assert.NotContains(t, output, "\n")

	output, err = execute(command, jsonFilePath)
	assert.Nil(t, err)
	assert.Contains(t, output, "{")

	output, err = execute(command, "--tab", xmlFilePath)
	assert.Nil(t, err)
	assert.Contains(t, output, "\t")

	output, err = execute(command, "-m", htmlFilePath)
	assert.Nil(t, err)
	assert.Contains(t, output, "<html>")

	output, err = execute(command, "-q", "body > p", htmlFilePath)
	assert.Nil(t, err)
	assert.Contains(t, output, "text")

	output, err = execute(command, "-x", "/user/@status", xmlFilePath)
	assert.Nil(t, err)
	assert.Contains(t, output, "active")

	output, err = execute(command, "--no-color", "-x", "/user/@status", xmlFilePath)
	assert.Nil(t, err)
	assert.Contains(t, output, "active")

	output, err = execute(command, "--color", "-x", "/user/@status", xmlFilePath)
	assert.Nil(t, err)
	assert.Contains(t, output, "active")

	_, err = execute(command, "nonexistent.xml")
	assert.ErrorContains(t, err, "no such file or directory")

	_, err = execute(command, "--indent", "-1", xmlFilePath)
	assert.ErrorContains(t, err, "indent should be")

	_, err = execute(command, "--indent", "incorrect", xmlFilePath)
	assert.ErrorContains(t, err, "invalid argument")
}

func TestProcessAsJSON(t *testing.T) {
	tests := []struct {
		name        string
		input       string
		contentType utils.ContentType
		flags       map[string]interface{}
		expected    map[string]interface{}
		wantErr     bool
	}{
		{
			name:        "Simple XML",
			input:       "<root><child>value</child></root>",
			contentType: utils.ContentXml,
			expected: map[string]interface{}{
				"root": map[string]interface{}{
					"child": "value",
				},
			},
		},
		{name: "Simple JSON",
			input:       `{"root": {"child": "value"}}`,
			contentType: utils.ContentJson,
			expected: map[string]interface{}{
				"root": map[string]interface{}{
					"child": "value",
				},
			},
		},
		{
			name:        "Simple HTML",
			input:       "<html><body><p>text</p></body></html>",
			contentType: utils.ContentHtml,
			expected: map[string]interface{}{
				"html": map[string]interface{}{
					"body": map[string]interface{}{
						"p": "text",
					},
				},
			},
		},
		{
			name:        "Plain text",
			input:       "text",
			contentType: utils.ContentText,
			expected: map[string]interface{}{
				"text": "text",
			},
		},
		{
			name:    "invalid input",
			input:   "thinking>\nI'll analyze each command and its output:\n</thinking>",
			wantErr: true,
		},
		{
			name: "combined",
			expected: map[string]interface{}{
				"#text":    "Thank you\nBye.",
				"thinking": "1. woop",
			},
			input: `Thank you
<thinking>
1. woop
</thinking>

Bye.`,
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			// Set up flags
			flags := pflag.NewFlagSet("test", pflag.ContinueOnError)
			flags.Bool("compact", false, "")
			flags.Int("depth", -1, "")
			for name, v := range tt.flags {
				_ = flags.Set(name, fmt.Sprint(v))
			}

			reader := strings.NewReader(tt.input)
			var output bytes.Buffer

			err := processAsJSON(flags, reader, &output, tt.contentType)

			if tt.wantErr {
				assert.Error(t, err)
			} else {
				assert.NoError(t, err)

				var resultMap map[string]interface{}
				err = json.Unmarshal(output.Bytes(), &resultMap)
				assert.NoError(t, err)

				assert.Equal(t, tt.expected, resultMap)
			}
		})
	}
}