...

Source file src/github.com/donovanhide/eventsource/normalise_test.go

Documentation: github.com/donovanhide/eventsource

     1  package eventsource
     2  
     3  import (
     4  	"bufio"
     5  	"fmt"
     6  	"strings"
     7  	"testing"
     8  )
     9  
    10  var (
    11  	inputFormat  = "line1%sline2%sline3%s"
    12  	endings      = []string{"\n", "\r\n", "\r"}
    13  	suffixes     = []string{"\n", "\r\n", "\r", ""}
    14  	descriptions = []string{"LF", "CRLF", "CR", "EOF"}
    15  	expected     = []string{"line1", "line2", "line3"}
    16  )
    17  
    18  func Testnormaliser(t *testing.T) {
    19  	for i, first := range endings {
    20  		for j, second := range endings {
    21  			for k, suffix := range suffixes {
    22  				input := fmt.Sprintf(inputFormat, first, second, suffix)
    23  				r := bufio.NewReader(newNormaliser(strings.NewReader(input)))
    24  				for _, want := range expected {
    25  					line, err := r.ReadString('\n')
    26  					if err != nil && suffix != "" {
    27  						t.Error("Unexpected error:", err)
    28  					}
    29  					line = strings.TrimSuffix(line, "\n")
    30  					if line != want {
    31  						expanded := fmt.Sprintf(inputFormat, descriptions[i], descriptions[j], descriptions[k])
    32  						t.Errorf(`Using %s Expected: "%s" Got: "%s"`, expanded, want, line)
    33  						t.Log([]byte(line))
    34  					}
    35  				}
    36  				if _, err := r.ReadString('\n'); err == nil {
    37  					t.Error("Expected EOF")
    38  				}
    39  			}
    40  		}
    41  	}
    42  }
    43  

View as plain text