File: normalise_test.go

package info (click to toggle)
golang-github-donovanhide-eventsource 0.0~git20210830.c590279-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, experimental, forky, sid, trixie
  • size: 132 kB
  • sloc: makefile: 2
file content (42 lines) | stat: -rw-r--r-- 1,115 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
package eventsource

import (
	"bufio"
	"fmt"
	"strings"
	"testing"
)

var (
	inputFormat  = "line1%sline2%sline3%s"
	endings      = []string{"\n", "\r\n", "\r"}
	suffixes     = []string{"\n", "\r\n", "\r", ""}
	descriptions = []string{"LF", "CRLF", "CR", "EOF"}
	expected     = []string{"line1", "line2", "line3"}
)

func Testnormaliser(t *testing.T) {
	for i, first := range endings {
		for j, second := range endings {
			for k, suffix := range suffixes {
				input := fmt.Sprintf(inputFormat, first, second, suffix)
				r := bufio.NewReader(newNormaliser(strings.NewReader(input)))
				for _, want := range expected {
					line, err := r.ReadString('\n')
					if err != nil && suffix != "" {
						t.Error("Unexpected error:", err)
					}
					line = strings.TrimSuffix(line, "\n")
					if line != want {
						expanded := fmt.Sprintf(inputFormat, descriptions[i], descriptions[j], descriptions[k])
						t.Errorf(`Using %s Expected: "%s" Got: "%s"`, expanded, want, line)
						t.Log([]byte(line))
					}
				}
				if _, err := r.ReadString('\n'); err == nil {
					t.Error("Expected EOF")
				}
			}
		}
	}
}