github.com/turbot/steampipe@v1.7.0-rc.0.0.20240517123944-7cef272d4458/pkg/control/controldisplay/group_counter_test.go (about)

     1  package controldisplay
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/logrusorgru/aurora"
     8  )
     9  
    10  var Gray = aurora.Gray
    11  var Red = aurora.Red
    12  
    13  type counterTest struct {
    14  	failedControls    int
    15  	totalControls     int
    16  	maxFailedControls int
    17  	maxTotalControls  int
    18  
    19  	expected string
    20  }
    21  
    22  func testCasesCounter() map[string]counterTest {
    23  	return map[string]counterTest{
    24  		"1/10 max 1/10": {
    25  			failedControls:    1,
    26  			totalControls:     10,
    27  			maxFailedControls: 1,
    28  			maxTotalControls:  10,
    29  
    30  			expected: fmt.Sprintf("%d %s %d ", ControlColors.CountFail(1), ControlColors.CountDivider("/"), ControlColors.CountTotal(10)),
    31  		},
    32  		"1/10 max 10/10": {
    33  			/*
    34  				" 1 / 10" <
    35  				"10 / 10"
    36  			*/
    37  			failedControls:    1,
    38  			totalControls:     10,
    39  			maxFailedControls: 10,
    40  			maxTotalControls:  10,
    41  
    42  			expected: fmt.Sprintf("%2d %s %d ", ControlColors.CountFail(1), ControlColors.CountDivider("/"), ControlColors.CountTotal(10)),
    43  		},
    44  		"1/10 max 10/100": {
    45  			/*
    46  				" 1 /  10" <
    47  				"10 / 100"
    48  			*/
    49  			failedControls:    1,
    50  			totalControls:     10,
    51  			maxFailedControls: 10,
    52  			maxTotalControls:  100,
    53  
    54  			expected: fmt.Sprintf("%2d %s %3d ", ControlColors.CountFail(1), ControlColors.CountDivider("/"), ControlColors.CountTotal(10)),
    55  		},
    56  		"1/10 max 100/1000": {
    57  			/*
    58  				"  1 /    10" <
    59  				"100 / 1,000"
    60  			*/
    61  			failedControls:    1,
    62  			totalControls:     10,
    63  			maxFailedControls: 100,
    64  			maxTotalControls:  1000,
    65  
    66  			expected: fmt.Sprintf("%3d %s %5d ", ControlColors.CountFail(1), ControlColors.CountDivider("/"), ControlColors.CountTotal(10)),
    67  		},
    68  		"10/500 max 100/1000": {
    69  			/*
    70  				" 10 /   500" <
    71  				"100 / 1,000"
    72  			*/
    73  			failedControls:    10,
    74  			totalControls:     500,
    75  			maxFailedControls: 100,
    76  			maxTotalControls:  1000,
    77  
    78  			expected: fmt.Sprintf("%3d %s %5d ", ControlColors.CountFail(10), ControlColors.CountDivider("/"), ControlColors.CountTotal(500)),
    79  		},
    80  		"10/1000 max 100/1000": {
    81  			/*
    82  				" 10 / 1,000" <
    83  				"100 / 1,000"
    84  			*/
    85  			failedControls:    10,
    86  			totalControls:     1000,
    87  			maxFailedControls: 100,
    88  			maxTotalControls:  1000,
    89  
    90  			expected: fmt.Sprintf("%3d %s %s ", ControlColors.CountFail(10), ControlColors.CountDivider("/"), ControlColors.CountTotal("1,000")),
    91  		},
    92  		"0/1000 max 100/1000": {
    93  			/*
    94  				"  0 / 1,000" <
    95  				"100 / 1,000"
    96  			*/
    97  			failedControls:    0,
    98  			totalControls:     1000,
    99  			maxFailedControls: 100,
   100  			maxTotalControls:  1000,
   101  
   102  			expected: fmt.Sprintf("%3d %s %s ", ControlColors.CountZeroFail(0), ControlColors.CountZeroFailDivider("/"), ControlColors.CountTotalAllPassed("1,000")),
   103  		},
   104  	}
   105  }
   106  
   107  func TestCounter(t *testing.T) {
   108  	themeDef := ColorSchemes["plain"]
   109  	scheme, _ := NewControlColorScheme(themeDef)
   110  	ControlColors = scheme
   111  
   112  	for name, test := range testCasesCounter() {
   113  		counter := NewCounterRenderer(test.failedControls, test.totalControls, test.maxFailedControls, test.maxTotalControls, CounterRendererOptions{AddLeadingSpace: true})
   114  		output := counter.Render()
   115  
   116  		if output != test.expected {
   117  			t.Errorf("Test: '%s'' FAILED : \nexpected:\n%s \ngot:\n%s\n", name, test.expected, output)
   118  		}
   119  	}
   120  }
   121  
   122  //func TestColor(t *testing.T) {
   123  //
   124  //	// 	72
   125  //	for i := uint8(16); i != 15; i += 36 {
   126  //		if i > 231 {
   127  //			i -= 231
   128  //		}
   129  //		fmt.Println(aurora.Index(i, fmt.Sprintf("COLOR %d", i)))
   130  //	}
   131  //
   132  //}