~samwhited/xmpp

ref: 6026cbc20a8f0e1269108b97cb7d6853fa2975ce xmpp/internal/xmpptest/tokens.go -rw-r--r-- 801 bytes
6026cbc2Sam Whited xmpp: add In and Out methods to session 9 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
// Copyright 2020 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 xmpptest

import (
	"encoding/xml"
	"io"
)

// Tokens is a slice of XML tokens that can also act as an xml.TokenReader by
// popping tokens from itself.
// This is useful for testing contrived scenarios where the tokens cannot be
// constructed using an xml.Decoder because the stream to be tested violates the
// well-formedness rules of XML or otherwise would result in an error from the
// decoder.
type Tokens []xml.Token

// Token satisfies the xml.TokenReader interface for Tokens.
func (r *Tokens) Token() (xml.Token, error) {
	if len(*r) == 0 {
		return nil, io.EOF
	}

	var t xml.Token
	t, *r = (*r)[0], (*r)[1:]
	return t, nil
}