Skip to content

Commit

Permalink
update tests to allow some volume error (for amixer on Linux)
Browse files Browse the repository at this point in the history
  • Loading branch information
itchyny committed May 20, 2018
1 parent 736a75e commit caf66b4
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 10 deletions.
41 changes: 34 additions & 7 deletions cmd/volume/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package main
import (
"bytes"
"fmt"
"math"
"os"
"strconv"
"strings"
"testing"

Expand Down Expand Up @@ -60,9 +62,18 @@ func TestRunStatus(t *testing.T) {
if err := run([]string{"status"}, out); err != nil {
t.Errorf("status failed: %+v", err)
}
expected := "volume: 17\nmuted: false\n"
if out.String() != expected {
t.Errorf("set volume failed: (got: %+v, expected: %+v)", out.String(), expected)
got := out.String()
expected := []string{"volume: 17\n", "muted: false\n"}
if !strings.Contains(got, expected[0]) {
v, _ := strconv.Atoi(strings.Split(got, "\n")[0][8:])
if vol := 17; math.Abs(float64(v-vol)) > 4 {
t.Errorf("get status failed: (got: %+v, expected: %+v)", got, strings.Join(expected, ""))
} else {
t.Logf("get status difference (possibly amixer on Linux): (got: %+v, expected: %+v)", got, strings.Join(expected, ""))
}
}
if !strings.Contains(got, expected[1]) {
t.Errorf("get status failed: %q should contain %q", got, strings.Join(expected, ""))
}
}

Expand All @@ -89,7 +100,11 @@ func TestRunUp(t *testing.T) {
vol, _ := volume.GetVolume()
expected := 17 + 6
if vol != expected {
t.Errorf("up volume failed: (got: %+v, expected: %+v)", vol, expected)
if math.Abs(float64(vol-expected)) > 4 {
t.Errorf("up volume failed: (got: %+v, expected: %+v)", vol, expected)
} else {
t.Logf("up volume difference (possibly amixer on Linux): (got: %+v, expected: %+v)", vol, expected)
}
}
}
{
Expand All @@ -99,7 +114,11 @@ func TestRunUp(t *testing.T) {
vol, _ := volume.GetVolume()
expected := 17 + 6 + 3
if vol != expected {
t.Errorf("up volume failed: (got: %+v, expected: %+v)", vol, expected)
if math.Abs(float64(vol-expected)) > 4 {
t.Errorf("up volume failed: (got: %+v, expected: %+v)", vol, expected)
} else {
t.Logf("up volume difference (possibly amixer on Linux): (got: %+v, expected: %+v)", vol, expected)
}
}
}
}
Expand All @@ -113,7 +132,11 @@ func TestRunDown(t *testing.T) {
vol, _ := volume.GetVolume()
expected := 17 - 6
if vol != expected {
t.Errorf("down volume failed: (got: %+v, expected: %+v)", vol, expected)
if math.Abs(float64(vol-expected)) > 4 {
t.Errorf("down volume failed: (got: %+v, expected: %+v)", vol, expected)
} else {
t.Logf("down volume difference (possibly amixer on Linux): (got: %+v, expected: %+v)", vol, expected)
}
}
}
{
Expand All @@ -123,7 +146,11 @@ func TestRunDown(t *testing.T) {
vol, _ := volume.GetVolume()
expected := 17 - 6 - 3
if vol != expected {
t.Errorf("down volume failed: (got: %+v, expected: %+v)", vol, expected)
if math.Abs(float64(vol-expected)) > 4 {
t.Errorf("down volume failed: (got: %+v, expected: %+v)", vol, expected)
} else {
t.Logf("down volume difference (possibly amixer on Linux): (got: %+v, expected: %+v)", vol, expected)
}
}
}
}
Expand Down
19 changes: 16 additions & 3 deletions volume_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package volume

import (
"fmt"
"math"
"os"
"testing"
)
Expand Down Expand Up @@ -45,7 +46,11 @@ func TestSetVolume(t *testing.T) {
t.Errorf("get volume failed: %+v", err)
}
if vol != v {
t.Errorf("set volume failed: (got: %+v, expected: %+v)", v, vol)
if math.Abs(float64(v-vol)) > 4 {
t.Errorf("set volume failed: (got: %+v, expected: %+v)", v, vol)
} else {
t.Logf("set volume difference (possibly amixer on Linux): (got: %+v, expected: %+v)", v, vol)
}
}
}
}
Expand All @@ -66,7 +71,11 @@ func TestIncreaseVolume(t *testing.T) {
t.Errorf("get volume failed: %+v", err)
}
if v != vol+diff {
t.Errorf("increase volume failed: (got: %+v, expected: %+v)", v, vol+diff)
if vol := vol + diff; math.Abs(float64(v-vol)) > 4 {
t.Errorf("increase volume failed: (got: %+v, expected: %+v)", v, vol)
} else {
t.Logf("increase volume difference (possibly amixer on Linux): (got: %+v, expected: %+v)", v, vol)
}
}
err = IncreaseVolume(-diff)
if err != nil {
Expand All @@ -77,7 +86,11 @@ func TestIncreaseVolume(t *testing.T) {
t.Errorf("get volume failed: %+v", err)
}
if v != vol {
t.Errorf("increase volume failed: (got: %+v, expected: %+v)", v, vol)
if math.Abs(float64(v-vol)) > 4 {
t.Errorf("increase volume failed: (got: %+v, expected: %+v)", v, vol)
} else {
t.Logf("increase volume difference (possibly amixer on Linux): (got: %+v, expected: %+v)", v, vol)
}
}
err = IncreaseVolume(-100)
if err != nil {
Expand Down

0 comments on commit caf66b4

Please sign in to comment.