github.com/andrewhsu/cli/v2@v2.0.1-0.20210910131313-d4b4061f5b89/pkg/cmd/gist/shared/shared_test.go (about)

     1  package shared
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  )
     8  
     9  func Test_GetGistIDFromURL(t *testing.T) {
    10  	tests := []struct {
    11  		name    string
    12  		url     string
    13  		want    string
    14  		wantErr bool
    15  	}{
    16  		{
    17  			name: "url",
    18  			url:  "https://gist.github.com/1234",
    19  			want: "1234",
    20  		},
    21  		{
    22  			name: "url with username",
    23  			url:  "https://gist.github.com/octocat/1234",
    24  			want: "1234",
    25  		},
    26  		{
    27  			name: "url, specific file",
    28  			url:  "https://gist.github.com/1234#file-test-md",
    29  			want: "1234",
    30  		},
    31  		{
    32  			name:    "invalid url",
    33  			url:     "https://gist.github.com",
    34  			wantErr: true,
    35  			want:    "Invalid gist URL https://gist.github.com",
    36  		},
    37  	}
    38  
    39  	for _, tt := range tests {
    40  		t.Run(tt.name, func(t *testing.T) {
    41  			id, err := GistIDFromURL(tt.url)
    42  			if tt.wantErr {
    43  				assert.Error(t, err)
    44  				assert.EqualError(t, err, tt.want)
    45  				return
    46  			}
    47  			assert.NoError(t, err)
    48  
    49  			assert.Equal(t, tt.want, id)
    50  		})
    51  	}
    52  }
    53  
    54  func TestIsBinaryContents(t *testing.T) {
    55  	tests := []struct {
    56  		fileContent []byte
    57  		want        bool
    58  	}{
    59  		{
    60  			want:        false,
    61  			fileContent: []byte("package main"),
    62  		},
    63  		{
    64  			want:        false,
    65  			fileContent: []byte(""),
    66  		},
    67  		{
    68  			want:        false,
    69  			fileContent: []byte(nil),
    70  		},
    71  		{
    72  			want: true,
    73  			fileContent: []byte{239, 191, 189, 239, 191, 189, 239, 191, 189, 239,
    74  				191, 189, 239, 191, 189, 16, 74, 70, 73, 70, 239, 191, 189, 1, 1, 1,
    75  				1, 44, 1, 44, 239, 191, 189, 239, 191, 189, 239, 191, 189, 239, 191,
    76  				189, 239, 191, 189, 67, 239, 191, 189, 8, 6, 6, 7, 6, 5, 8, 7, 7, 7,
    77  				9, 9, 8, 10, 12, 20, 10, 12, 11, 11, 12, 25, 18, 19, 15, 20, 29, 26,
    78  				31, 30, 29, 26, 28, 28, 32, 36, 46, 39, 32, 34, 44, 35, 28, 28, 40,
    79  				55, 41, 44, 48, 49, 52, 52, 52, 31, 39, 57, 61, 56, 50, 60, 46, 51,
    80  				52, 50, 239, 191, 189, 239, 191, 189, 239, 191, 189, 67, 1, 9, 9, 9, 12},
    81  		},
    82  	}
    83  
    84  	for _, tt := range tests {
    85  		assert.Equal(t, tt.want, IsBinaryContents(tt.fileContent))
    86  	}
    87  }