github.com/jaypipes/ghw@v0.21.1/pkg/util/util_test.go (about)

     1  //
     2  // Use and distribution licensed under the Apache license version 2.
     3  //
     4  // See the COPYING file in the root project directory for full text.
     5  //
     6  
     7  package util_test
     8  
     9  import (
    10  	"testing"
    11  
    12  	"github.com/jaypipes/ghw/pkg/util"
    13  )
    14  
    15  // nolint: gocyclo
    16  func TestConcatStrings(t *testing.T) {
    17  	type testCase struct {
    18  		items    []string
    19  		expected string
    20  	}
    21  
    22  	testCases := []testCase{
    23  		{
    24  			items:    []string{},
    25  			expected: "",
    26  		},
    27  		{
    28  			items:    []string{"simple"},
    29  			expected: "simple",
    30  		},
    31  		{
    32  			items: []string{
    33  				"foo",
    34  				"bar",
    35  				"baz",
    36  			},
    37  			expected: "foobarbaz",
    38  		},
    39  		{
    40  			items: []string{
    41  				"foo ",
    42  				" bar ",
    43  				" baz",
    44  			},
    45  			expected: "foo  bar  baz",
    46  		},
    47  	}
    48  
    49  	for _, tCase := range testCases {
    50  		t.Run(tCase.expected, func(t *testing.T) {
    51  			got := util.ConcatStrings(tCase.items...)
    52  			if got != tCase.expected {
    53  				t.Errorf("expected %q got %q", tCase.expected, got)
    54  			}
    55  		})
    56  	}
    57  }
    58  
    59  func TestParseBool(t *testing.T) {
    60  	type testCase struct {
    61  		item     string
    62  		expected bool
    63  	}
    64  
    65  	testCases := []testCase{
    66  		{
    67  			item:     "False",
    68  			expected: false,
    69  		},
    70  		{
    71  			item:     "F",
    72  			expected: false,
    73  		},
    74  		{
    75  			item:     "1",
    76  			expected: true,
    77  		},
    78  		{
    79  			item:     "",
    80  			expected: false,
    81  		},
    82  		{
    83  			item:     "on",
    84  			expected: true,
    85  		},
    86  		{
    87  			item:     "Off",
    88  			expected: false,
    89  		},
    90  		{
    91  			item:     "Yes",
    92  			expected: true,
    93  		},
    94  		{
    95  			item:     "no",
    96  			expected: false,
    97  		},
    98  	}
    99  
   100  	for _, tCase := range testCases {
   101  		t.Run(tCase.item, func(t *testing.T) {
   102  			got, err := util.ParseBool(tCase.item)
   103  			if got != tCase.expected {
   104  				t.Errorf("expected %t got %t", tCase.expected, got)
   105  			}
   106  			if err != nil {
   107  				t.Errorf("util.ParseBool threw error %s", err)
   108  			}
   109  		})
   110  	}
   111  }