~samwhited/xmpp

ref: d4b835bf2b08a22e6a13cd9b33e62c88f9854677 xmpp/internal/decl/decl_test.go -rw-r--r-- 1.4 KiB
d4b835bfSam Whited internal/marshal: prevent duplicate xmlns attr 6 months ago
                                                                                
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
// Copyright 2019 The Mellium Contributors.
// Use of this source code is governed by the BSD 2-clause
// license that can be found in the LICENSE file.

package decl_test

import (
	"bytes"
	"encoding/xml"
	"io"
	"strconv"
	"strings"
	"testing"

	"mellium.im/xmlstream"
	"mellium.im/xmpp/internal/decl"
)

var skipTests = [...]struct {
	in  string
	out string
}{
	0: {},
	1: {in: "<a/>", out: "<a></a>"},
	2: {in: xml.Header + "<a/>", out: "\n<a></a>"},
	3: {in: `<?xml?><a/>`, out: "<a></a>"},
	4: {in: `<?sgml?><a/>`, out: "<?sgml?><a></a>"},
	5: {in: `<?xml?>`},
}

func TestDecl(t *testing.T) {
	for i, tc := range skipTests {
		t.Run(strconv.Itoa(i), func(t *testing.T) {
			d := decl.Skip(xml.NewDecoder(strings.NewReader(tc.in)))
			buf := &bytes.Buffer{}
			e := xml.NewEncoder(buf)
			if _, err := xmlstream.Copy(e, d); err != nil {
				t.Fatalf("Error copying tokens: %q", err)
			}
			if err := e.Flush(); err != nil {
				t.Fatalf("Error flushing tokens: %q", err)
			}
			if s := buf.String(); s != tc.out {
				t.Errorf("Output does not match: want=%q, got=%q", tc.out, s)
			}
		})
	}
}

func TestImmediateEOF(t *testing.T) {
	d := decl.Skip(xmlstream.Token(xml.ProcInst{Target: "xml"}))

	for i := 0; i < 2; i++ {
		tok, err := d.Token()
		if err != io.EOF {
			t.Errorf("Expected EOF on %d but got %q", i, err)
		}
		if tok != nil {
			t.Errorf("Did not expect token on %d but got %T %[2]v", i, tok)
		}
	}
}