package main import ( "errors" "testing" "github.com/distatus/battery" "github.com/stretchr/testify/assert" ) func TestBattery(t *testing.T) { env := &environment{} b := &batt{ env: env, props: &properties{}, } val := b.string() assert.NotEmpty(t, val) } func setupBatteryTests(state battery.State, batteryLevel float64, props *properties) *batt { env := &MockedEnvironment{} bt := &battery.Battery{ State: state, Full: 100, Current: batteryLevel, } env.On("getBatteryInfo", nil).Return(bt, nil) return &batt{ props: props, env: env, } } func TestBatteryCharging(t *testing.T) { props := &properties{ values: map[Property]interface{}{ ChargingIcon: "charging ", }, } b := setupBatteryTests(battery.Charging, 80, props) assert.Equal(t, "charging 80", b.string()) } func TestBatteryCharged(t *testing.T) { props := &properties{ values: map[Property]interface{}{ ChargedIcon: "charged ", }, } b := setupBatteryTests(battery.Full, 100, props) assert.Equal(t, "charged 100", b.string()) } func TestBatteryDischarging(t *testing.T) { props := &properties{ values: map[Property]interface{}{ DischargingIcon: "going down ", }, } b := setupBatteryTests(battery.Discharging, 70, props) assert.Equal(t, "going down 70", b.string()) } func TestBatteryBackgroundColor(t *testing.T) { expected := "#768954" props := &properties{ background: "#111111", values: map[Property]interface{}{ DischargingIcon: "going down ", ColorBackground: true, DischargingColor: expected, }, } b := setupBatteryTests(battery.Discharging, 70, props) b.string() assert.Equal(t, expected, props.background) } func TestBatteryBackgroundColorInvalid(t *testing.T) { expected := "#768954" props := &properties{ background: expected, values: map[Property]interface{}{ DischargingIcon: "going down ", ColorBackground: true, DischargingColor: "derp", }, } b := setupBatteryTests(battery.Discharging, 70, props) b.string() assert.Equal(t, expected, props.background) } func TestBatteryForegroundColor(t *testing.T) { expected := "#768954" props := &properties{ foreground: "#111111", values: map[Property]interface{}{ DischargingIcon: "going down ", ColorBackground: false, DischargingColor: expected, }, } b := setupBatteryTests(battery.Discharging, 70, props) b.string() assert.Equal(t, expected, props.foreground) } func TestBatteryForegroundColorInvalid(t *testing.T) { expected := "#768954" props := &properties{ foreground: expected, values: map[Property]interface{}{ DischargingIcon: "going down ", ColorBackground: false, DischargingColor: "derp", }, } b := setupBatteryTests(battery.Discharging, 70, props) b.string() assert.Equal(t, expected, props.foreground) } func TestBatteryError(t *testing.T) { env := &MockedEnvironment{} err := errors.New("oh snap") env.On("getBatteryInfo", nil).Return(&battery.Battery{}, err) b := &batt{ props: nil, env: env, } assert.Equal(t, "BATT ERR", b.string()) }