github.com/soulteary/pocket-bookcase@v0.0.0-20240428065142-0b5a9a0fc98a/internal/cmd/utils_test.go (about)

     1  package cmd
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  )
     7  
     8  func Test_normalizeSpace(t *testing.T) {
     9  	tests := []struct {
    10  		name string
    11  		args string
    12  		want string
    13  	}{{
    14  		name: "normal sentence",
    15  		args: "What a perfect, beautiful sentence",
    16  		want: "What a perfect, beautiful sentence",
    17  	}, {
    18  		name: "has unnecessary space before and after sentence",
    19  		args: "    I'm surrounded with spaces    ",
    20  		want: "I'm surrounded with spaces",
    21  	}, {
    22  		name: "has unnecessary spaces in middle of sentence",
    23  		args: "I'm hollow         inside",
    24  		want: "I'm hollow inside",
    25  	}, {
    26  		name: "has unnecessary new line in middle of sentence",
    27  		args: "I'm broken \n\n\ninside",
    28  		want: "I'm broken inside",
    29  	}, {
    30  		name: "has unnecessary new line and spaces everywhere",
    31  		args: "    I'm hollow     broken\n\n\n\nand surrounded by spaces    ",
    32  		want: "I'm hollow broken and surrounded by spaces",
    33  	}}
    34  
    35  	for _, tt := range tests {
    36  		t.Run(tt.name, func(t *testing.T) {
    37  			if got := normalizeSpace(tt.args); got != tt.want {
    38  				t.Errorf("normalizeSpace() = %v, want %v", got, tt.want)
    39  			}
    40  		})
    41  	}
    42  }
    43  
    44  func Test_isURLValid(t *testing.T) {
    45  	tests := []struct {
    46  		name string
    47  		args string
    48  		want bool
    49  	}{{
    50  		name: "valid URL",
    51  		args: "https://www.google.com",
    52  		want: true,
    53  	}, {
    54  		name: "valid localhost URL",
    55  		args: "http://localhost:8080",
    56  		want: true,
    57  	}, {
    58  		name: "valid non-HTTP URL",
    59  		args: "ftp://www.example.com/storage",
    60  		want: true,
    61  	}, {
    62  		name: "invalid URL",
    63  		args: "https:/www.google.com",
    64  		want: false,
    65  	}, {
    66  		name: "hash URL",
    67  		args: "#some-awesome-heading",
    68  		want: false,
    69  	}, {
    70  		name: "relative URL",
    71  		args: "/page/contact",
    72  		want: false,
    73  	}}
    74  
    75  	for _, tt := range tests {
    76  		t.Run(tt.name, func(t *testing.T) {
    77  			if got := isURLValid(tt.args); got != tt.want {
    78  				t.Errorf("isURLValid() = %v, want %v", got, tt.want)
    79  			}
    80  		})
    81  	}
    82  }
    83  
    84  func Test_parseStrIndices(t *testing.T) {
    85  	tests := []struct {
    86  		name    string
    87  		args    []string
    88  		want    []int
    89  		wantErr bool
    90  	}{{
    91  		name:    "single number",
    92  		args:    []string{"1"},
    93  		want:    []int{1},
    94  		wantErr: false,
    95  	}, {
    96  		name:    "multiple number",
    97  		args:    []string{"1", "2", "3"},
    98  		want:    []int{1, 2, 3},
    99  		wantErr: false,
   100  	}, {
   101  		name:    "single ranged number",
   102  		args:    []string{"1-5"},
   103  		want:    []int{1, 2, 3, 4, 5},
   104  		wantErr: false,
   105  	}, {
   106  		name:    "multiple ranged number",
   107  		args:    []string{"1-5", "8-9"},
   108  		want:    []int{1, 2, 3, 4, 5, 8, 9},
   109  		wantErr: false,
   110  	}, {
   111  		name:    "mixed single and ranged number",
   112  		args:    []string{"1-5", "8-9", "11", "12"},
   113  		want:    []int{1, 2, 3, 4, 5, 8, 9, 11, 12},
   114  		wantErr: false,
   115  	}, {
   116  		name:    "invalid number",
   117  		args:    []string{"AAA"},
   118  		want:    nil,
   119  		wantErr: true,
   120  	}, {
   121  		name:    "mixed number and string",
   122  		args:    []string{"1", "2", "A"},
   123  		want:    nil,
   124  		wantErr: true,
   125  	}, {
   126  		name:    "reversed ranged number",
   127  		args:    []string{"5-1"},
   128  		want:    nil,
   129  		wantErr: true,
   130  	}}
   131  
   132  	for _, tt := range tests {
   133  		t.Run(tt.name, func(t *testing.T) {
   134  			got, err := parseStrIndices(tt.args)
   135  			if (err != nil) != tt.wantErr {
   136  				t.Errorf("parseStrIndices() error = %v, wantErr %v", err, tt.wantErr)
   137  				return
   138  			}
   139  
   140  			if !reflect.DeepEqual(got, tt.want) {
   141  				t.Errorf("parseStrIndices() = %v, want %v", got, tt.want)
   142  			}
   143  		})
   144  	}
   145  }