summaryrefslogtreecommitdiff
path: root/cmd/rescribe/gui_test.go
blob: 3c90000ce282a3c0873dc97ba918b2e9dd9d65a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// Copyright 2022 Nick White.
// Use of this source code is governed by the GPLv3
// license that can be found in the LICENSE file.

package main

import (
	"fmt"
	"strings"
	"testing"
	"fyne.io/fyne/v2/app"
	"fyne.io/fyne/v2/widget"
)

func TestFormatProgressBar(t *testing.T) {
	cases := []struct {
		val float64
		str string
	}{
		{0.0, ""},
		{0.01, "Processing"},
		{0.11, "Downloading"},
		{0.12, "Processing PDF"},
		{0.2, "Preprocessing"},
		{0.5, "OCRing"},
		{0.55, "OCRing"},
		{0.89, "OCRing"},
		{0.9, "Analysing"},
		{1.0, "Done"},
		{1.1, "Processing"},
	}

	_ = app.New() // shouldn't be needed for test but we get a panic without it
	bar := widget.NewProgressBar()

	for _, c := range cases {
		t.Run(fmt.Sprintf("%s_%.1f", c.str, c.val), func(t *testing.T) {
			bar.Value = c.val
			got := formatProgressBar(bar)()
			if got != c.str {
				t.Fatalf("Expected %s, got %s", c.str, got)
			}
		})
	}
}

func TestUpdateProgress(t *testing.T) {
	cases := []struct {
		log string
		val float64
	}{
		{"Downloading", 0.11},
		{"Preprocessing", 0.2},
		{"Preprocessing\nOCRing", 0.5},
		{"Preprocessing\nOCRing...", 0.53},
		{"OCRing........................................", 0.89},
		{"OCRing..\nAnalysing", 0.9},
		{"Done", 1.0},
		{"Weirdness", 0.0},
	}

	_ = app.New() // shouldn't be needed for test but we get a panic without it
	bar := widget.NewProgressBar()

	for _, c := range cases {
		t.Run(c.log, func(t *testing.T) {
			l := strings.ReplaceAll("  "+c.log, "\n", "\n  ")
			bar.Value = 0.0
			updateProgress(l, bar)
			got := bar.Value
			if got != c.val {
				t.Fatalf("Expected %f, got %f", c.val, got)
			}
		})
	}
}