github.com/xztaityozx/sel@v1.1.10/internal/option/option_test.go (about) 1 package option_test 2 3 import ( 4 "fmt" 5 "os" 6 "path/filepath" 7 "reflect" 8 "runtime" 9 "testing" 10 11 "github.com/spf13/viper" 12 13 "github.com/stretchr/testify/assert" 14 "github.com/xztaityozx/sel/internal/option" 15 ) 16 17 func TestInputFiles_Enumerate(t *testing.T) { 18 as := assert.New(t) 19 20 t.Run("配列がnilならエラーが返されるべき", func(t *testing.T) { 21 _, err := option.InputFiles{Files: nil}.Enumerate() 22 as.Error(err) 23 }) 24 25 t.Run("配列の長さが0ならエラーが返されるべき", func(t *testing.T) { 26 _, err := option.InputFiles{Files: []string{}}.Enumerate() 27 as.Error(err) 28 }) 29 30 base := filepath.Join(os.TempDir(), "sel_test") 31 _ = os.MkdirAll(base, 0755) 32 _ = os.Chdir(base) 33 t.Run("Open出来ないファイルがあると例外が投げられる", func(t *testing.T) { 34 actual, err := option.InputFiles{Files: []string{"ないわよ"}}.Enumerate() 35 as.Error(err) 36 as.Nil(actual) 37 38 actual, err = option.InputFiles{Files: []string{"ないわよ"}}.Enumerate() 39 as.Error(err) 40 as.Nil(actual) 41 }) 42 43 t.Run("OpenできるファイルのみならOK", func(t *testing.T) { 44 var files []string 45 for i := 0; i < 10; i++ { 46 f := filepath.Join(base, fmt.Sprint(i)) 47 files = append(files, f) 48 _ = os.WriteFile(f, []byte("はい"), 0644) 49 } 50 51 a, err := option.InputFiles{Files: files}.Enumerate() 52 53 as.Nil(err) 54 as.Equal(10, len(a)) 55 56 for i, v := range a { 57 as.Equal(filepath.Join(base, fmt.Sprint(i)), v) 58 } 59 }) 60 61 t.Run("ディレクトリはOpenできない", func(t *testing.T) { 62 _, err := option.InputFiles{Files: []string{base}}.Enumerate() 63 64 as.Error(err) 65 }) 66 67 t.Run("スペシャルファイルはOpenできない", func(t *testing.T) { 68 if //goland:noinspection GoBoolExpressions 69 runtime.GOOS == "windows" { 70 t.Skip() 71 } 72 _, err := option.InputFiles{Files: []string{"/dev/null"}}.Enumerate() 73 as.Error(err) 74 }) 75 76 t.Run("Globもいける", func(t *testing.T) { 77 a, err := option.InputFiles{Files: []string{filepath.Join(base, "*")}}.Enumerate() 78 79 as.Nil(err) 80 as.Equal(10, len(a)) 81 82 for i, v := range a { 83 as.Equal(filepath.Join(base, fmt.Sprint(i)), v) 84 } 85 }) 86 87 _ = os.RemoveAll(base) 88 } 89 90 func TestGetOptionNames(t *testing.T) { 91 tests := []struct { 92 name string 93 want []string 94 }{ 95 {name: "とれてますか", want: []string{ 96 option.NameInputFiles, 97 option.NameInputDelimiter, 98 option.NameOutPutDelimiter, 99 option.NameUseRegexp, 100 option.NameRemoveEmpty, 101 option.NameSplitBefore, 102 option.NameFieldSplit, 103 option.NameCsv, 104 option.NameTsv, 105 option.NameTemplate, 106 }}, 107 } 108 for _, tt := range tests { 109 t.Run(tt.name, func(t *testing.T) { 110 if got := option.GetOptionNames(); !reflect.DeepEqual(got, tt.want) { 111 t.Errorf("GetOptionNames() = %v, want %v", got, tt.want) 112 } 113 }) 114 } 115 } 116 117 func TestNewOption(t *testing.T) { 118 type args struct { 119 v *viper.Viper 120 } 121 tests := []struct { 122 name string 123 args args 124 want option.Option 125 }{ 126 { 127 name: "noname", args: args{ 128 v: func() *viper.Viper { 129 v := viper.New() 130 v.Set(option.NameInputFiles, []string{"abc", "def"}) 131 v.Set(option.NameInputDelimiter, "i") 132 v.Set(option.NameOutPutDelimiter, "o") 133 v.Set(option.NameRemoveEmpty, true) 134 v.Set(option.NameUseRegexp, true) 135 v.Set(option.NameSplitBefore, true) 136 return v 137 }(), 138 }, 139 want: option.Option{ 140 InputFiles: option.InputFiles{Files: []string{"abc", "def"}}, 141 DelimiterOption: option.DelimiterOption{ 142 InputDelimiter: "i", 143 OutPutDelimiter: "o", 144 RemoveEmpty: true, 145 UseRegexp: true, 146 SplitBefore: true, 147 }, 148 }, 149 }, 150 } 151 for _, tt := range tests { 152 t.Run(tt.name, func(t *testing.T) { 153 if got, err := option.NewOption(tt.args.v); !reflect.DeepEqual(got, tt.want) { 154 t.Errorf("NewOption() = %v, want %v", got, tt.want) 155 } else { 156 assert.NoError(t, err) 157 } 158 }) 159 } 160 } 161 162 func TestXsv_IsXsv(t *testing.T) { 163 as := assert.New(t) 164 type fields struct { 165 csv bool 166 tsv bool 167 } 168 169 type wants struct { 170 ok bool 171 comma rune 172 } 173 174 tests := []struct { 175 name string 176 fields fields 177 wants wants 178 }{ 179 {"CSV", fields{true, false}, wants{true, ','}}, 180 {"TSV", fields{false, true}, wants{true, '\t'}}, 181 {"CSV && TSV to be CSV", fields{true, true}, wants{true, ','}}, 182 {"not XSV", fields{false, false}, wants{false, ','}}, 183 } 184 185 for _, tt := range tests { 186 t.Run(tt.name, func(t *testing.T) { 187 gotOk, gotComma := option.Xsv{Csv: tt.fields.csv, Tsv: tt.fields.tsv}.IsXsv() 188 as.Equal(tt.wants.ok, gotOk) 189 as.Equal(tt.wants.comma, gotComma) 190 }) 191 } 192 }