diff --git a/draw_test.go b/draw_test.go index c667af0..3fc284c 100644 --- a/draw_test.go +++ b/draw_test.go @@ -451,39 +451,151 @@ }, 99: { { - style: BarStyle().Lbound("[").Filler("の").Tip("だ").Padding("つ").Rbound("]"), - name: `t,c{100,1}Tip("だ")`, + style: BarStyle(), + name: "t,c{100,1}", total: 100, current: 1, - want: " [だつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつ…] ", - }, - { - style: BarStyle().Lbound("[").Filler("の").Tip("だ").Padding("つ").Rbound("]"), - name: `t,c{100,2}Tip("だ")`, - total: 100, - current: 2, - want: " [だつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつ…] ", - }, - { - style: BarStyle().Lbound("[").Filler("の").Tip("だ").Padding("つ").Rbound("]"), - name: `t,c{100,99}Tip("だ")`, + want: " [>----------------------------------------------------------------------------------------------] ", + }, + { + style: BarStyle(), + name: "t,c{100,1}trim", + total: 100, + current: 1, + trim: true, + want: "[>------------------------------------------------------------------------------------------------]", + }, + { + style: BarStyle(), + name: "t,c,r{100,40,33}", + total: 100, + current: 40, + refill: 33, + want: " [+++++++++++++++++++++++++++++++======>---------------------------------------------------------] ", + }, + { + style: BarStyle(), + name: "t,c,r{100,40,33}trim", + total: 100, + current: 40, + refill: 33, + trim: true, + want: "[++++++++++++++++++++++++++++++++======>----------------------------------------------------------]", + }, + { + style: BarStyle().Tip("<").Reverse(), + name: "t,c,r{100,40,33},rev", + total: 100, + current: 40, + refill: 33, + want: " [---------------------------------------------------------<======+++++++++++++++++++++++++++++++] ", + }, + { + style: BarStyle().Tip("<").Reverse(), + name: "t,c,r{100,40,33}trim,rev", + total: 100, + current: 40, + refill: 33, + trim: true, + want: "[----------------------------------------------------------<======++++++++++++++++++++++++++++++++]", + }, + { + style: BarStyle(), + name: "t,c{100,99}", total: 100, current: 99, - want: " [ののののののののののののののののののののののののののののののののののののののののののののののだ…] ", - }, - { - style: BarStyle().Lbound("[").Filler("の").Tip("だ").Padding("つ").Rbound("]"), - name: `t,c{100,100}Tip("だ")`, - total: 100, - current: 100, - want: " [ののののののののののののののののののののののののののののののののののののののののののののののの…] ", - }, - { - style: BarStyle().Lbound("[").Filler("の").Tip("だ").Padding("つ").Rbound("]").Reverse(), - name: `t,c{100,100}Tip("だ")rev`, - total: 100, - current: 100, - want: " […ののののののののののののののののののののののののののののののののののののののののののののののの] ", + want: " [=============================================================================================>-] ", + }, + { + style: BarStyle(), + name: "t,c{100,99}trim", + total: 100, + current: 99, + trim: true, + want: "[===============================================================================================>-]", + }, + { + style: BarStyle(), + name: "t,c{100,100}", + total: 100, + current: 100, + want: " [===============================================================================================] ", + }, + { + style: BarStyle(), + name: "t,c{100,100}trim", + total: 100, + current: 100, + trim: true, + want: "[=================================================================================================]", + }, + { + style: BarStyle(), + name: "t,c,r{100,100,99}", + total: 100, + current: 100, + refill: 99, + want: " [++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++=] ", + }, + { + style: BarStyle(), + name: "t,c,r{100,100,99}trim", + total: 100, + current: 100, + refill: 99, + trim: true, + want: "[++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++=]", + }, + { + style: BarStyle().Tip("<").Reverse(), + name: "t,c,r{100,100,99}rev", + total: 100, + current: 100, + refill: 99, + want: " [=++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++] ", + }, + { + style: BarStyle().Tip("<").Reverse(), + name: "t,c,r{100,100,99}trim,rev", + total: 100, + current: 100, + refill: 99, + trim: true, + want: "[=++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++]", + }, + { + style: BarStyle(), + name: "t,c,r{100,100,100}", + total: 100, + current: 100, + refill: 100, + want: " [+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++] ", + }, + { + style: BarStyle(), + name: "t,c,r{100,100,100}trim", + total: 100, + current: 100, + refill: 100, + trim: true, + want: "[+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++]", + }, + { + style: BarStyle().Tip("<").Reverse(), + name: "t,c,r{100,100,100}rev", + total: 100, + current: 100, + refill: 100, + want: " [+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++] ", + }, + { + style: BarStyle().Tip("<").Reverse(), + name: "t,c,r{100,100,100}trim", + total: 100, + current: 100, + refill: 100, + trim: true, + want: "[+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++]", }, }, 100: { @@ -582,7 +694,7 @@ want: "[++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++]", }, { - style: BarStyle().Tip("", "<").Reverse(), + style: BarStyle().Tip("<").Reverse(), name: "t,c,r{100,100,99}rev", total: 100, current: 100, @@ -590,7 +702,7 @@ want: " [=+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++] ", }, { - style: BarStyle().Tip("", "<").Reverse(), + style: BarStyle().Tip("<").Reverse(), name: "t,c,r{100,100,99}trim,rev", total: 100, current: 100, @@ -599,7 +711,7 @@ want: "[=+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++]", }, { - style: BarStyle().Tip("", "<").Reverse(), + style: BarStyle().Tip("<").Reverse(), name: "t,c,r{100,100,100}rev", total: 100, current: 100, @@ -607,7 +719,7 @@ want: " [++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++] ", }, { - style: BarStyle().Tip("", "<").Reverse(), + style: BarStyle().Tip("<").Reverse(), name: "t,c,r{100,100,100}trim", total: 100, current: 100, @@ -1121,25 +1233,151 @@ }, 99: { { - style: BarStyle().Lbound("[").Filler("の").Tip("だ").TipOnComplete("だ").Padding("つ").Rbound("]"), - name: `t,c{100,99}Tip("だ").TipOnComplete("だ")`, + style: BarStyle().TipOnComplete(">"), + name: "t,c{100,1}", + total: 100, + current: 1, + want: " [>----------------------------------------------------------------------------------------------] ", + }, + { + style: BarStyle().TipOnComplete(">"), + name: "t,c{100,1}trim", + total: 100, + current: 1, + trim: true, + want: "[>------------------------------------------------------------------------------------------------]", + }, + { + style: BarStyle().TipOnComplete(">"), + name: "t,c,r{100,40,33}", + total: 100, + current: 40, + refill: 33, + want: " [+++++++++++++++++++++++++++++++======>---------------------------------------------------------] ", + }, + { + style: BarStyle().TipOnComplete(">"), + name: "t,c,r{100,40,33}trim", + total: 100, + current: 40, + refill: 33, + trim: true, + want: "[++++++++++++++++++++++++++++++++======>----------------------------------------------------------]", + }, + { + style: BarStyle().Tip("<").Reverse().TipOnComplete(">"), + name: "t,c,r{100,40,33},rev", + total: 100, + current: 40, + refill: 33, + want: " [---------------------------------------------------------<======+++++++++++++++++++++++++++++++] ", + }, + { + style: BarStyle().Tip("<").Reverse().TipOnComplete(">"), + name: "t,c,r{100,40,33}trim,rev", + total: 100, + current: 40, + refill: 33, + trim: true, + want: "[----------------------------------------------------------<======++++++++++++++++++++++++++++++++]", + }, + { + style: BarStyle().TipOnComplete(">"), + name: "t,c{100,99}", total: 100, current: 99, - want: " [ののののののののののののののののののののののののののののののののののののののののののののののだ…] ", - }, - { - style: BarStyle().Lbound("[").Filler("の").Tip("だ").TipOnComplete("だ").Padding("つ").Rbound("]"), - name: `t,c{100,100}Tip("だ").TipOnComplete("だ")`, - total: 100, - current: 100, - want: " [ののののののののののののののののののののののののののののののののののののののののののののののだ…] ", - }, - { - style: BarStyle().Lbound("[").Filler("の").Tip("だ").TipOnComplete("だ").Padding("つ").Rbound("]").Reverse(), - name: `t,c{100,100}Tip("だ").TipOnComplete("だ")rev`, - total: 100, - current: 100, - want: " […だのののののののののののののののののののののののののののののののののののののののののののののの] ", + want: " [=============================================================================================>-] ", + }, + { + style: BarStyle().TipOnComplete(">"), + name: "t,c{100,99}trim", + total: 100, + current: 99, + trim: true, + want: "[===============================================================================================>-]", + }, + { + style: BarStyle().TipOnComplete(">"), + name: "t,c{100,100}", + total: 100, + current: 100, + want: " [==============================================================================================>] ", + }, + { + style: BarStyle().TipOnComplete(">"), + name: "t,c{100,100}trim", + total: 100, + current: 100, + trim: true, + want: "[================================================================================================>]", + }, + { + style: BarStyle().TipOnComplete(">"), + name: "t,c,r{100,100,99}", + total: 100, + current: 100, + refill: 99, + want: " [++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>] ", + }, + { + style: BarStyle().TipOnComplete(">"), + name: "t,c,r{100,100,99}trim", + total: 100, + current: 100, + refill: 99, + trim: true, + want: "[++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>]", + }, + { + style: BarStyle().TipOnComplete("<").Reverse(), + name: `t,c,r{100,100,99}TipOnComplete("<").Reverse()`, + total: 100, + current: 100, + refill: 99, + want: " [<++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++] ", + }, + { + style: BarStyle().TipOnComplete("<").Reverse(), + name: `t,c,r{100,100,99}TipOnComplete("<").Reverse()trim`, + total: 100, + current: 100, + refill: 99, + trim: true, + want: "[<++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++]", + }, + { + style: BarStyle().TipOnComplete(">"), + name: "t,c,r{100,100,100}", + total: 100, + current: 100, + refill: 100, + want: " [++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>] ", + }, + { + style: BarStyle().TipOnComplete(">"), + name: "t,c,r{100,100,100}trim", + total: 100, + current: 100, + refill: 100, + trim: true, + want: "[++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>]", + }, + { + style: BarStyle().Tip("<").TipOnComplete("<").Reverse(), + name: `t,c,r{100,100,100}Tip("<").TipOnComplete("<").Reverse()`, + total: 100, + current: 100, + refill: 100, + want: " [<++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++] ", + }, + { + style: BarStyle().Tip("<").Reverse().TipOnComplete("<"), + name: "t,c,r{100,100,100}trim", + total: 100, + current: 100, + refill: 100, + trim: true, + want: "[<++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++]", }, }, 100: { @@ -1304,6 +1542,133 @@ refill: 33, trim: true, want: "[-----------------------------------------------------------<======++++++++++++++++++++++++++++++++]", + }, + }, + } + + var tmpBuf bytes.Buffer + for tw, cases := range testSuite { + for _, tc := range cases { + s := newTestState(NewBarFiller(tc.style)) + s.reqWidth = tc.barWidth + s.total = tc.total + s.current = tc.current + s.trimSpace = tc.trim + s.refill = tc.refill + tmpBuf.Reset() + tmpBuf.ReadFrom(s.draw(newStatistics(tw, s))) + by := tmpBuf.Bytes() + + got := string(by[:len(by)-1]) + if !utf8.ValidString(got) { + t.Fail() + } + if got != tc.want { + t.Errorf("termWidth:%d %q want: %q %d, got: %q %d\n", tw, tc.name, tc.want, utf8.RuneCountInString(tc.want), got, utf8.RuneCountInString(got)) + } + } + } +} + +func TestDrawDoubleWidth(t *testing.T) { + // key is termWidth + testSuite := map[int][]struct { + style BarStyleComposer + name string + total int64 + current int64 + refill int64 + barWidth int + trim bool + want string + }{ + 99: { + { + style: BarStyle().Lbound("の").Rbound("の"), + name: `t,c{100,1}.Lbound("の").Rbound("の")`, + total: 100, + current: 1, + want: " の>--------------------------------------------------------------------------------------------の ", + }, + { + style: BarStyle().Lbound("の").Rbound("の"), + name: `t,c{100,1}.Lbound("の").Rbound("の")`, + total: 100, + current: 2, + want: " の=>-------------------------------------------------------------------------------------------の ", + }, + { + style: BarStyle().Lbound("[").Filler("の").Tip("だ").Padding("つ").Rbound("]"), + name: `t,c{100,1}Tip("だ")`, + total: 100, + current: 1, + want: " [だつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつ…] ", + }, + { + style: BarStyle().Lbound("[").Filler("の").Tip("だ").Padding("つ").Rbound("]"), + name: `t,c{100,2}Tip("だ")`, + total: 100, + current: 2, + want: " [だつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつつ…] ", + }, + { + style: BarStyle().Lbound("[").Filler("の").Tip("だ").Padding("つ").Rbound("]"), + name: `t,c{100,99}Tip("だ")`, + total: 100, + current: 99, + want: " [ののののののののののののののののののののののののののののののののののののののののののののののだ…] ", + }, + { + style: BarStyle().Lbound("[").Filler("の").Tip("だ").Padding("つ").Rbound("]"), + name: `t,c{100,100}Tip("だ")`, + total: 100, + current: 100, + want: " […ののののののののののののののののののののののののののののののののののののののののののののののの] ", + }, + { + style: BarStyle().Lbound("[").Filler("の").Tip("だ").Padding("つ").Rbound("]").Reverse(), + name: `t,c{100,100}Tip("だ")rev`, + total: 100, + current: 100, + want: " [ののののののののののののののののののののののののののののののののののののののののののののののの…] ", + }, + { + style: BarStyle().Lbound("[").Filler("の").Tip("だ").TipOnComplete("だ").Padding("つ").Rbound("]"), + name: `t,c{100,99}Tip("だ").TipOnComplete("だ")`, + total: 100, + current: 99, + want: " [ののののののののののののののののののののののののののののののののののののののののののののののだ…] ", + }, + { + style: BarStyle().Lbound("[").Filler("の").Tip("だ").TipOnComplete("だ").Padding("つ").Rbound("]"), + name: `t,c{100,100}Tip("だ").TipOnComplete("だ")`, + total: 100, + current: 100, + want: " […ののののののののののののののののののののののののののののののののののののののののののののののだ] ", + }, + { + style: BarStyle().Lbound("[").Filler("の").Tip("だ").TipOnComplete("だ").Padding("つ").Rbound("]").Reverse(), + name: `t,c{100,100}Tip("だ").TipOnComplete("だ")rev`, + total: 100, + current: 100, + want: " [だのののののののののののののののののののののののののののののののののののののののののののののの…] ", + }, + { + style: BarStyle().Refiller("の"), + name: `t,c,r{100,100,99}Refiller("の")`, + total: 100, + current: 100, + refill: 99, + want: " [ののののののののののののののののののののののののののののののののののののののののののののののの=] ", + }, + { + style: BarStyle().Refiller("の"), + name: `t,c,r{100,100,99}Refiller("の")trim`, + total: 100, + current: 100, + refill: 99, + trim: true, + want: "[のののののののののののののののののののののののののののののののののののののののののののののののの=]", }, }, }