github.com/ungtb10d/cli/v2@v2.0.0-20221110210412-98537dd9d6a1/pkg/cmd/pr/create/regexp_writer_test.go (about)

     1  package create
     2  
     3  import (
     4  	"bytes"
     5  	"regexp"
     6  	"testing"
     7  
     8  	"github.com/MakeNowJust/heredoc"
     9  	"github.com/stretchr/testify/assert"
    10  )
    11  
    12  func Test_Write(t *testing.T) {
    13  	type input struct {
    14  		in   []string
    15  		re   *regexp.Regexp
    16  		repl string
    17  	}
    18  	type output struct {
    19  		wantsErr bool
    20  		out      string
    21  		length   int
    22  	}
    23  	tests := []struct {
    24  		name   string
    25  		input  input
    26  		output output
    27  	}{
    28  		{
    29  			name: "single line input",
    30  			input: input{
    31  				in:   []string{"some input line that has wrong information"},
    32  				re:   regexp.MustCompile("wrong"),
    33  				repl: "right",
    34  			},
    35  			output: output{
    36  				wantsErr: false,
    37  				out:      "some input line that has right information",
    38  				length:   42,
    39  			},
    40  		},
    41  		{
    42  			name: "multiple line input",
    43  			input: input{
    44  				in:   []string{"multiple lines\nin this\ninput lines"},
    45  				re:   regexp.MustCompile("lines"),
    46  				repl: "tests",
    47  			},
    48  			output: output{
    49  				wantsErr: false,
    50  				out:      "multiple tests\nin this\ninput tests",
    51  				length:   34,
    52  			},
    53  		},
    54  		{
    55  			name: "no matches",
    56  			input: input{
    57  				in:   []string{"this line has no matches"},
    58  				re:   regexp.MustCompile("wrong"),
    59  				repl: "right",
    60  			},
    61  			output: output{
    62  				wantsErr: false,
    63  				out:      "this line has no matches",
    64  				length:   24,
    65  			},
    66  		},
    67  		{
    68  			name: "no output",
    69  			input: input{
    70  				in:   []string{"remove this whole line"},
    71  				re:   regexp.MustCompile("^remove.*$"),
    72  				repl: "",
    73  			},
    74  			output: output{
    75  				wantsErr: false,
    76  				out:      "",
    77  				length:   22,
    78  			},
    79  		},
    80  		{
    81  			name: "no input",
    82  			input: input{
    83  				in:   []string{""},
    84  				re:   regexp.MustCompile("remove"),
    85  				repl: "",
    86  			},
    87  			output: output{
    88  				wantsErr: false,
    89  				out:      "",
    90  				length:   0,
    91  			},
    92  		},
    93  		{
    94  			name: "multiple lines removed",
    95  			input: input{
    96  				in:   []string{"beginning line\nremove this whole line\nremove this one also\nnot this one"},
    97  				re:   regexp.MustCompile("(?s)^remove.*$"),
    98  				repl: "",
    99  			},
   100  			output: output{
   101  				wantsErr: false,
   102  				out:      "beginning line\nnot this one",
   103  				length:   71,
   104  			},
   105  		},
   106  		{
   107  			name: "removes remote from git push output",
   108  			input: input{
   109  				in: []string{heredoc.Doc(`
   110  					output: some information
   111  					remote:
   112  					remote: Create a pull request for 'regex' on GitHub by visiting:
   113  					remote:      https://github.com/owner/repo/pull/new/regex
   114  					remote:
   115  					output: more information
   116  			 `)},
   117  				re:   regexp.MustCompile("^remote: (Create a pull request.*by visiting|[[:space:]]*https://.*/pull/new/).*\n?$"),
   118  				repl: "",
   119  			},
   120  			output: output{
   121  				wantsErr: false,
   122  				out:      "output: some information\nremote:\nremote:\noutput: more information\n",
   123  				length:   189,
   124  			},
   125  		},
   126  		{
   127  			name: "multiple writes",
   128  			input: input{
   129  				in:   []string{"first write\n", "second write ", "third write"},
   130  				re:   regexp.MustCompile("write"),
   131  				repl: "read",
   132  			},
   133  			output: output{
   134  				wantsErr: false,
   135  				out:      "first read\nsecond read third read",
   136  				length:   36,
   137  			},
   138  		},
   139  	}
   140  
   141  	for _, tt := range tests {
   142  		out := &bytes.Buffer{}
   143  		writer := NewRegexpWriter(out, tt.input.re, tt.input.repl)
   144  		t.Run(tt.name, func(t *testing.T) {
   145  			length := 0
   146  			for _, in := range tt.input.in {
   147  				l, err := writer.Write([]byte(in))
   148  				length = length + l
   149  				if tt.output.wantsErr {
   150  					assert.Error(t, err)
   151  					return
   152  				}
   153  				assert.NoError(t, err)
   154  			}
   155  			writer.Flush()
   156  			assert.Equal(t, tt.output.out, out.String())
   157  			assert.Equal(t, tt.output.length, length)
   158  		})
   159  	}
   160  }