github.com/u-root/u-root@v7.0.1-0.20200915234505-ad7babab0a8e+incompatible/pkg/gzip/options_test.go (about)

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