github.com/wtfutil/wtf@v0.43.0/view/bargraph_test.go (about)

     1  package view
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/olebedev/config"
     7  	"github.com/rivo/tview"
     8  	"github.com/stretchr/testify/assert"
     9  	"github.com/wtfutil/wtf/cfg"
    10  )
    11  
    12  // MakeData - Create sample data
    13  func makeData() []Bar {
    14  	//this could come from config
    15  	const lineCount = 3
    16  	var stats [lineCount]Bar
    17  
    18  	stats[0] = Bar{
    19  		Label:   "Jun 27, 2018",
    20  		Percent: 20,
    21  	}
    22  
    23  	stats[1] = Bar{
    24  		Label:      "Jul 09, 2018",
    25  		Percent:    80,
    26  		LabelColor: "red",
    27  	}
    28  
    29  	stats[2] = Bar{
    30  		Label:      "Jul 09, 2018",
    31  		Percent:    80,
    32  		LabelColor: "green",
    33  	}
    34  
    35  	return stats[:]
    36  }
    37  
    38  func newTestGraph(graphStars int, graphIcon string) *BarGraph {
    39  	widget := NewBarGraph(
    40  		tview.NewApplication(),
    41  		make(chan bool),
    42  		"testapp",
    43  		&cfg.Common{
    44  			Config: &config.Config{
    45  				Root: map[string]interface{}{
    46  					"graphStars": graphStars,
    47  					"graphIcon":  graphIcon,
    48  				},
    49  			},
    50  		},
    51  	)
    52  	return &widget
    53  }
    54  
    55  func Test_NewBarGraph(t *testing.T) {
    56  	widget := newTestGraph(15, "|")
    57  
    58  	assert.NotNil(t, widget.View)
    59  	assert.Equal(t, 15, widget.maxStars)
    60  	assert.Equal(t, "|", widget.starChar)
    61  }
    62  
    63  func Test_BuildBars(t *testing.T) {
    64  	widget := newTestGraph(15, "|")
    65  
    66  	before := widget.View.GetText(false)
    67  	widget.BuildBars(makeData())
    68  	after := widget.View.GetText(false)
    69  
    70  	assert.NotEqual(t, before, after)
    71  }
    72  
    73  func Test_TextView(t *testing.T) {
    74  	widget := newTestGraph(15, "|")
    75  
    76  	assert.NotNil(t, widget.TextView())
    77  }
    78  
    79  func Test_BuildStars(t *testing.T) {
    80  	result := BuildStars(makeData(), 20, "*")
    81  	assert.Equal(t,
    82  		"Jun 27, 2018[[default]****[default]                ] 20\nJul 09, 2018[[red]****************[default]    ] 80\nJul 09, 2018[[green]****************[default]    ] 80\n",
    83  		result,
    84  	)
    85  }