github.com/ratrocket/u-root@v0.0.0-20180201221235-1cf9f48ee2cf/pkg/gzip/options_test.go (about) 1 package gzip 2 3 import ( 4 "flag" 5 "runtime" 6 "testing" 7 8 "github.com/klauspost/pgzip" 9 ) 10 11 func TestOptions_ParseArgs(t *testing.T) { 12 type fields struct { 13 Blocksize int 14 Level int 15 Processes int 16 Decompress bool 17 Force bool 18 Help bool 19 Keep bool 20 Quiet bool 21 Stdin bool 22 Stdout bool 23 Test bool 24 Verbose bool 25 Suffix string 26 } 27 type args struct { 28 args []string 29 cmdLine *flag.FlagSet 30 } 31 tests := []struct { 32 name string 33 fields fields 34 args args 35 wantErr bool 36 }{ 37 // TODO: Add test cases. 38 } 39 for _, tt := range tests { 40 t.Run(tt.name, func(t *testing.T) { 41 o := &Options{ 42 Blocksize: tt.fields.Blocksize, 43 Level: tt.fields.Level, 44 Processes: tt.fields.Processes, 45 Decompress: tt.fields.Decompress, 46 Force: tt.fields.Force, 47 Help: tt.fields.Help, 48 Keep: tt.fields.Keep, 49 Quiet: tt.fields.Quiet, 50 Stdin: tt.fields.Stdin, 51 Stdout: tt.fields.Stdout, 52 Test: tt.fields.Test, 53 Verbose: tt.fields.Verbose, 54 Suffix: tt.fields.Suffix, 55 } 56 if err := o.ParseArgs(tt.args.args, tt.args.cmdLine); (err != nil) != tt.wantErr { 57 t.Errorf("Options.ParseArgs() error = %v, wantErr %v", err, tt.wantErr) 58 } 59 }) 60 } 61 } 62 func TestOptions_validate(t *testing.T) { 63 type fields struct { 64 Blocksize int 65 Level int 66 Processes int 67 Decompress bool 68 Force bool 69 Help bool 70 Keep bool 71 Quiet bool 72 Stdin bool 73 Stdout bool 74 Test bool 75 Verbose bool 76 Suffix string 77 } 78 type args struct { 79 moreArgs bool 80 } 81 tests := []struct { 82 name string 83 args args 84 fields fields 85 wantErr bool 86 }{ 87 { 88 name: "Default values no args", 89 fields: fields{Blocksize: 128, Level: -1, Processes: runtime.NumCPU(), Decompress: false, Force: false, Help: false}, 90 args: args{moreArgs: false}, 91 wantErr: true, 92 }, 93 } 94 for _, tt := range tests { 95 t.Run(tt.name, func(t *testing.T) { 96 o := &Options{ 97 Blocksize: tt.fields.Blocksize, 98 Level: tt.fields.Level, 99 Processes: tt.fields.Processes, 100 Decompress: tt.fields.Decompress, 101 Force: tt.fields.Force, 102 Help: tt.fields.Help, 103 Keep: tt.fields.Keep, 104 Quiet: tt.fields.Quiet, 105 Stdin: tt.fields.Stdin, 106 Stdout: tt.fields.Stdout, 107 Test: tt.fields.Test, 108 Verbose: tt.fields.Verbose, 109 Suffix: tt.fields.Suffix, 110 } 111 112 if err := o.validate(tt.args.moreArgs); (err != nil) != tt.wantErr { 113 t.Errorf("Options.validate() error = %v, wantErr %v", err, tt.wantErr) 114 } 115 }) 116 } 117 } 118 119 func Test_parseLevels(t *testing.T) { 120 type args struct { 121 levels [10]bool 122 } 123 tests := []struct { 124 name string 125 args args 126 want int 127 wantErr bool 128 }{ 129 { 130 name: "No level specified", 131 args: args{levels: [10]bool{}}, 132 want: pgzip.DefaultCompression, 133 wantErr: false, 134 }, 135 { 136 name: "Level 1", 137 args: args{levels: [10]bool{false, true, false, false, false, false, false, false, false, false}}, 138 want: 1, 139 wantErr: false, 140 }, 141 { 142 name: "Level 9", 143 args: args{levels: [10]bool{false, false, false, false, false, false, false, false, false, true}}, 144 want: 9, 145 wantErr: false, 146 }, 147 { 148 name: "Multuple levels specified", 149 args: args{levels: [10]bool{false, true, false, false, false, false, false, false, false, true}}, 150 want: 0, 151 wantErr: true, 152 }, 153 } 154 for _, tt := range tests { 155 t.Run(tt.name, func(t *testing.T) { 156 got, err := parseLevels(tt.args.levels) 157 if (err != nil) != tt.wantErr { 158 t.Errorf("parseLevels() error = %v, wantErr %v", err, tt.wantErr) 159 return 160 } 161 if got != tt.want { 162 t.Errorf("parseLevels() = %v, want %v", got, tt.want) 163 } 164 }) 165 } 166 }