TestCustomFormat
Vladimir Bauer
9 years ago
| 3 | 3 | "bytes" |
| 4 | 4 | "fmt" |
| 5 | 5 | "math/rand" |
| 6 | "strings" | |
| 7 | 6 | "sync" |
| 8 | 7 | "testing" |
| 9 | 8 | "time" |
| 15 | 14 | func TestDefaultWidth(t *testing.T) { |
| 16 | 15 | var buf bytes.Buffer |
| 17 | 16 | p := mpb.New().SetOut(&buf) |
| 18 | bar := p.AddBar(100) | |
| 17 | bar := p.AddBar(100).TrimLeftSpace().TrimRightSpace() | |
| 19 | 18 | for i := 0; i < 100; i++ { |
| 20 | 19 | bar.Incr(1) |
| 21 | 20 | } |
| 22 | 21 | p.Stop() |
| 23 | runeCount := utf8.RuneCountInString(strings.TrimSpace(buf.String())) | |
| 24 | defWidth := 80 | |
| 22 | runeCount := utf8.RuneCountInString(buf.String()) | |
| 23 | defWidth := 81 // + 1 for new line | |
| 25 | 24 | if runeCount != defWidth { |
| 26 | defWidth = 78 // when testing with ./... | |
| 27 | if runeCount != defWidth { | |
| 28 | t.Errorf("Expected default width: %d, got: %d\n", defWidth, runeCount) | |
| 29 | } | |
| 25 | t.Errorf("Expected default width: %d, got: %d\n", defWidth, runeCount) | |
| 30 | 26 | } |
| 31 | 27 | } |
| 32 | 28 | |
| 34 | 30 | customWidth := 60 |
| 35 | 31 | var buf bytes.Buffer |
| 36 | 32 | p := mpb.New().SetWidth(customWidth).SetOut(&buf) |
| 37 | bar := p.AddBar(100) | |
| 33 | bar := p.AddBar(100).TrimLeftSpace().TrimRightSpace() | |
| 38 | 34 | for i := 0; i < 100; i++ { |
| 39 | 35 | bar.Incr(1) |
| 40 | 36 | } |
| 41 | 37 | p.Stop() |
| 42 | runeCount := utf8.RuneCountInString(strings.TrimSpace(buf.String())) | |
| 43 | if runeCount != customWidth { | |
| 44 | customWidth = 58 // when testing with ./... | |
| 45 | if runeCount != customWidth { | |
| 46 | t.Errorf("Expected default width: %d, got: %d\n", customWidth, runeCount) | |
| 47 | } | |
| 38 | runeCount := utf8.RuneCountInString(buf.String()) | |
| 39 | if runeCount != customWidth+1 { // +1 for new line | |
| 40 | t.Errorf("Expected default width: %d, got: %d\n", customWidth, runeCount) | |
| 48 | 41 | } |
| 49 | 42 | } |
| 50 | 43 | |
| 154 | 147 | _ = mpb.New().WithCancel(nil) |
| 155 | 148 | } |
| 156 | 149 | |
| 157 | func TestFormat(t *testing.T) { | |
| 150 | func TestCustomFormat(t *testing.T) { | |
| 158 | 151 | var buf bytes.Buffer |
| 159 | 152 | cancel := make(chan struct{}) |
| 160 | 153 | shutdown := make(chan struct{}) |
| 161 | 154 | customFormat := "╢▌▌░╟" |
| 162 | p := mpb.New().Format(customFormat) | |
| 163 | p.WithCancel(cancel) | |
| 164 | p.ShutdownNotify(shutdown) | |
| 165 | p.SetOut(&buf) | |
| 155 | p := mpb.New().Format(customFormat). | |
| 156 | WithCancel(cancel). | |
| 157 | ShutdownNotify(shutdown). | |
| 158 | SetOut(&buf) | |
| 166 | 159 | bar := p.AddBar(100).TrimLeftSpace().TrimRightSpace() |
| 167 | 160 | |
| 168 | 161 | go func() { |
| 169 | 162 | for i := 0; i < 100; i++ { |
| 163 | time.Sleep(10 * time.Millisecond) | |
| 170 | 164 | bar.Incr(1) |
| 171 | time.Sleep(10 * time.Millisecond) | |
| 172 | if i == 42 { | |
| 173 | close(cancel) | |
| 174 | } | |
| 175 | 165 | } |
| 176 | 166 | }() |
| 177 | 167 | |
| 178 | // p.Stop() | |
| 179 | // time.Sleep(300 * time.Millisecond) | |
| 168 | time.Sleep(300 * time.Millisecond) | |
| 169 | close(cancel) | |
| 170 | p.Stop() | |
| 180 | 171 | |
| 181 | // gotBar := strings.TrimSpace(buf.String()) | |
| 182 | gotBar := buf.String() | |
| 172 | bytes := buf.Bytes() | |
| 173 | _, size := utf8.DecodeLastRune(bytes) | |
| 174 | bytes = bytes[:len(bytes)-size] // removing new line | |
| 175 | ||
| 183 | 176 | seen := make(map[rune]bool) |
| 184 | for _, r := range gotBar { | |
| 177 | for _, r := range string(bytes) { | |
| 185 | 178 | if !seen[r] { |
| 186 | 179 | seen[r] = true |
| 187 | 180 | } |
| 188 | 181 | } |
| 189 | fmt.Println(gotBar) | |
| 190 | for r, _ := range seen { | |
| 191 | fmt.Printf("%#U\n", r) | |
| 182 | for _, r := range customFormat { | |
| 183 | if !seen[r] { | |
| 184 | t.Errorf("Rune %#U not found in bar\n", r) | |
| 185 | } | |
| 192 | 186 | } |
| 193 | // expectBar := "╢▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌╟" | |
| 194 | // if gotBar != expectBar { | |
| 195 | // t.Errorf("Expected for") | |
| 196 | // } | |
| 197 | 187 | } |