code.gitea.io/gitea@v1.19.3/modules/eventsource/event_test.go (about)

     1  // Copyright 2020 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package eventsource
     5  
     6  import (
     7  	"bytes"
     8  	"testing"
     9  )
    10  
    11  func Test_wrapNewlines(t *testing.T) {
    12  	tests := []struct {
    13  		name   string
    14  		prefix string
    15  		value  string
    16  		output string
    17  	}{
    18  		{
    19  			"check no new lines",
    20  			"prefix: ",
    21  			"value",
    22  			"prefix: value\n",
    23  		},
    24  		{
    25  			"check simple newline",
    26  			"prefix: ",
    27  			"value1\nvalue2",
    28  			"prefix: value1\nprefix: value2\n",
    29  		},
    30  		{
    31  			"check pathological newlines",
    32  			"p: ",
    33  			"\n1\n\n2\n3\n",
    34  			"p: \np: 1\np: \np: 2\np: 3\np: \n",
    35  		},
    36  	}
    37  	for _, tt := range tests {
    38  		t.Run(tt.name, func(t *testing.T) {
    39  			w := &bytes.Buffer{}
    40  			gotSum, err := wrapNewlines(w, []byte(tt.prefix), []byte(tt.value))
    41  			if err != nil {
    42  				t.Errorf("wrapNewlines() error = %v", err)
    43  				return
    44  			}
    45  			if gotSum != int64(len(tt.output)) {
    46  				t.Errorf("wrapNewlines() = %v, want %v", gotSum, int64(len(tt.output)))
    47  			}
    48  			if gotW := w.String(); gotW != tt.output {
    49  				t.Errorf("wrapNewlines() = %v, want %v", gotW, tt.output)
    50  			}
    51  		})
    52  	}
    53  }