1package entity
2
3import (
4 "math"
5 "testing"
6)
7
8func TestZoomPercentageRoundsDisplayedValue(t *testing.T) {
9 tests := []struct {
10 name string
11 factor float64
12 want int
13 }{
14 {name: "default_zoom", factor: 1.0, want: 100},
15 {name: "accumulated_default_error", factor: 0.9999999999999997, want: 100},
16 {name: "accumulated_above_default", factor: 1.0000000000000002, want: 100},
17 {name: "ordinary_percent", factor: 1.5, want: 150},
18 {name: "minimum_zoom", factor: ZoomMin, want: 25},
19 {name: "maximum_zoom", factor: ZoomMax, want: 500},
20 {name: "rounds_up", factor: 1.006, want: 101},
21 {name: "rounds_down", factor: 1.004, want: 100},
22 {name: "three_digit_percent_below_half", factor: 2.344, want: 234},
23 }
24
25 for _, tt := range tests {
26 t.Run(tt.name, func(t *testing.T) {
27 zoom := NewZoomLevel("example.com", tt.factor)
28 if got := zoom.Percentage(); got != tt.want {
29 t.Fatalf("Percentage()=%d, want %d for factor %.16f", got, tt.want, tt.factor)
30 }
31 })
32 }
33}
34
35func TestZoomPercentageSurvivesRepeatedSteps(t *testing.T) {
36 zoom := NewZoomLevel("example.com", ZoomDefault)
37
38 for range 10 {
39 zoom.ZoomIn()
40 }
41 if got := zoom.Percentage(); got != 200 {
42 t.Fatalf("after 10 zoom-in steps Percentage()=%d, want 200", got)
43 }
44
45 for range 10 {
46 zoom.ZoomOut()
47 }
48 if got := zoom.Percentage(); got != 100 {
49 t.Fatalf("after 10 zoom-out steps Percentage()=%d, want 100", got)
50 }
51 if math.Abs(zoom.ZoomFactor-ZoomDefault) > 1e-9 {
52 t.Fatalf("persisted factor drifted to %.16f, want default", zoom.ZoomFactor)
53 }
54 if !zoom.IsDefault() {
55 t.Fatal("factor behind a 100%% display must still compare equal to the default zoom")
56 }
57}