go.fuchsia.dev/infra@v0.0.0-20240507153436-9b593402251b/cmd/cl-util/create_cl_test.go (about)

     1  // Copyright 2020 The Fuchsia Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style license that can be
     3  // found in the LICENSE file.
     4  
     5  package main
     6  
     7  import (
     8  	"testing"
     9  
    10  	"github.com/google/go-cmp/cmp"
    11  )
    12  
    13  func TestNewFileEdit(t *testing.T) {
    14  	t.Parallel()
    15  	tests := []struct {
    16  		input       string
    17  		expected    *fileEdit
    18  		expectedErr bool
    19  	}{
    20  		{
    21  			input: "path/to/file:test-contents",
    22  			expected: &fileEdit{
    23  				filepath: "path/to/file",
    24  				contents: "test-contents",
    25  			},
    26  			expectedErr: false,
    27  		},
    28  		{
    29  			input: "path/to/file:test-contents:with-another-colon",
    30  			expected: &fileEdit{
    31  				filepath: "path/to/file",
    32  				contents: "test-contents:with-another-colon",
    33  			},
    34  			expectedErr: false,
    35  		},
    36  		{
    37  			input:       "invalid-input",
    38  			expected:    nil,
    39  			expectedErr: true,
    40  		},
    41  	}
    42  	for _, test := range tests {
    43  		edit, err := newFileEdit(test.input)
    44  		if err == nil {
    45  			if test.expectedErr {
    46  				t.Errorf("expected error, got nil")
    47  			}
    48  		} else if !test.expectedErr {
    49  			t.Errorf("got unexpected error: %s", err)
    50  		}
    51  		if diff := cmp.Diff(test.expected, edit, cmp.AllowUnexported(fileEdit{})); diff != "" {
    52  			t.Fatalf("different (-want +got):\n%s", diff)
    53  		}
    54  	}
    55  }