package door import ( "strings" "testing" ) func pctUpdate(pct *int64) func() int64 { return func() int64 { return *pct } } func TestBarsMatch(t *testing.T) { // verify that the BARS_CP437 matches BARS_UNICODE // When translated via CP437_to_Unicode(). cp437 := BARS_CP437.Solid convert := CP437_to_Unicode(cp437) unicode := BARS_UNICODE.Solid if convert != unicode { t.Errorf("BARS solid: %#v != %#v\n", unicode, convert) } for half := 0; half < 2; half++ { cp437 := BARS_CP437.Half[half] convert := CP437_to_Unicode(cp437) unicode := BARS_UNICODE.Half[half] if convert != unicode { t.Errorf("BARS half[%d]: %#v != %#v\n", half, unicode, convert) } } for grad := 0; grad < 4; grad++ { cp437 := BARS_CP437.Gradient[grad] convert := CP437_to_Unicode(cp437) unicode := BARS_UNICODE.Gradient[grad] if convert != unicode { t.Errorf("BARS gradient[%d]: %#v != %#v\n", grad, unicode, convert) } } } func TestBarsUnicode(t *testing.T) { // Init as if we're in Unicode mode Unicode = true CP437 = false BARS = &BARS_UNICODE testBars(t) } func TestBarsCP437(t *testing.T) { // Init as if we're in CP437 mode Unicode = false CP437 = true BARS = &BARS_CP437 testBars(t) } func testBars(t *testing.T) { // Generic function that tests output by building results // from BARS structure. (Can test Unicode and CP437.) BarColor := ColorText("BLUE") Bar25 := ColorText("RED") Bar50 := ColorText("BROWN") Bar75 := ColorText("BOLD YEL") Bar95 := ColorText("GREEN") Bar100 := ColorText("BRI GREEN") bar := BarLine{Line: Line{DefaultColor: BarColor}, Width: 10, Style: SOLID} bar.ColorRange = []BarRange{ {2500, Bar25}, {5000, Bar50}, {7500, Bar75}, {9500, Bar95}, {10100, Bar100}} BarSolid := map[int]string{0: string(Bar25) + " ", 5: string(Bar25) + " ", 10: string(Bar25) + BARS.Solid + " ", 20: string(Bar25) + strings.Repeat(BARS.Solid, 2) + " ", 25: string(Bar25) + strings.Repeat(BARS.Solid, 2) + " ", 30: string(Bar50) + strings.Repeat(BARS.Solid, 3) + " ", 50: string(Bar50) + strings.Repeat(BARS.Solid, 5) + " ", 75: string(Bar75) + strings.Repeat(BARS.Solid, 7) + " ", 90: string(Bar95) + strings.Repeat(BARS.Solid, 9) + " ", 95: string(Bar95) + strings.Repeat(BARS.Solid, 9) + " ", 100: string(Bar100) + strings.Repeat(BARS.Solid, 10) + "", } for pct, text := range BarSolid { bar.Percent = int64(pct * 100) got := string(bar.Output()) if got != text { t.Errorf("BarSolidRange: Expected %#v (%d%%), got %#v", text, pct, got) } } BarColor = ColorText("BLA ON WHI") bar = BarLine{Line: Line{DefaultColor: BarColor}, Width: 10, Style: HALF_STEP, PercentStyle: PERCENT_SPACE} BarHalf := map[int]string{0: string(BarColor) + " 0% ", 5: string(BarColor) + BARS.Half[1] + " 5% ", 6: string(BarColor) + BARS.Half[1] + " 6% ", 10: string(BarColor) + BARS.Half[0] + " 10% ", 20: string(BarColor) + BARS.Half[0] + BARS.Half[0] + " 20% ", 25: string(BarColor) + BARS.Half[0] + BARS.Half[0] + BARS.Half[1] + " 25% ", 26: string(BarColor) + BARS.Half[0] + BARS.Half[0] + BARS.Half[1] + " 26% ", 50: string(BarColor) + strings.Repeat(BARS.Half[0], 3) + " 50% ", 75: string(BarColor) + strings.Repeat(BARS.Half[0], 3) + " 75% ", 90: string(BarColor) + strings.Repeat(BARS.Half[0], 3) + " 90% " + BARS.Half[0] + " ", 100: string(BarColor) + strings.Repeat(BARS.Half[0], 3) + " 100 " + strings.Repeat(BARS.Half[0], 2), } for pct, text := range BarHalf { bar.Percent = int64(pct * 100) got := string(bar.Output()) if got != text { t.Errorf("BarHalf: Expected %#v (%d%%), got %#v", text, pct, got) } } BarColor = ColorText("RED") bar = BarLine{Line: Line{DefaultColor: BarColor}, Width: 10, Style: GRADIENT} BarGrad := map[int]string{0: string(BarColor) + " ", 3: string(BarColor) + BARS.Gradient[1] + " ", 5: string(BarColor) + BARS.Gradient[2] + " ", 8: string(BarColor) + BARS.Gradient[3] + " ", 10: string(BarColor) + BARS.Gradient[0] + " ", 20: string(BarColor) + BARS.Gradient[0] + BARS.Gradient[0] + " ", 25: string(BarColor) + BARS.Gradient[0] + BARS.Gradient[0] + BARS.Gradient[2] + " ", 50: string(BarColor) + strings.Repeat(BARS.Gradient[0], 5) + " ", 75: string(BarColor) + strings.Repeat(BARS.Gradient[0], 7) + BARS.Gradient[2] + " ", 100: string(BarColor) + strings.Repeat(BARS.Gradient[0], 10), } var percent int64 bar.UpdateP = pctUpdate(&percent) for pct, text := range BarGrad { percent = int64(pct * 100) got := string(bar.Output()) if got != text { t.Errorf("BarGradient: Expected %#v (%d%%), got %#v", text, pct, got) } } } func BenchmarkBar(b *testing.B) { Unicode = false BarColor := ColorText("BLUE") Bar25 := ColorText("RED") Bar50 := ColorText("BROWN") Bar75 := ColorText("BOLD YEL") Bar95 := ColorText("GREEN") Bar100 := ColorText("BRI GREEN") bar := BarLine{Line: Line{DefaultColor: BarColor}, Width: 10, Style: SOLID} bar.ColorRange = []BarRange{ {2500, Bar25}, {5000, Bar50}, {7500, Bar75}, {9500, Bar95}, {10100, Bar100}} BarColor2 := ColorText("BLA ON WHI") bar2 := BarLine{Line: Line{DefaultColor: BarColor2}, Width: 10, Style: HALF_STEP, PercentStyle: PERCENT_SPACE} BarColor3 := ColorText("RED") bar3 := BarLine{Line: Line{DefaultColor: BarColor3}, Width: 10, Style: GRADIENT} var percent int64 var upFunc = func() int64 { return percent } bar.UpdateP = upFunc bar2.UpdateP = upFunc bar3.UpdateP = upFunc for n := 0; n < b.N; n++ { percent = (int64(n) * 100) / int64(b.N) bar.Output() bar2.Output() bar3.Output() } }