Skip to content
Newer
Older
100644 145 lines (120 sloc) 2.93 KB
11ce6af @tobi Added Bounds as own struct
authored May 31, 2012
1 package mogrify
2
3 import (
476b3b6 @aybabtme Run go-fmt on the repo.
aybabtme authored Apr 19, 2014
4 "testing"
11ce6af @tobi Added Bounds as own struct
authored May 31, 2012
5 )
6
7 func TestBounds(t *testing.T) {
8
476b3b6 @aybabtme Run go-fmt on the repo.
aybabtme authored Apr 20, 2014
9 bounds := Bounds{100, 50}
10 if bounds.Width != 100 {
11 t.FailNow()
12 }
11ce6af @tobi Added Bounds as own struct
authored May 31, 2012
13
476b3b6 @aybabtme Run go-fmt on the repo.
aybabtme authored Apr 20, 2014
14 if bounds.Height != 50 {
15 t.FailNow()
16 }
11ce6af @tobi Added Bounds as own struct
authored May 31, 2012
17
18 }
19
20 func TestProportionalOperation(t *testing.T) {
476b3b6 @aybabtme Run go-fmt on the repo.
aybabtme authored Apr 20, 2014
21 // half
22 bounds := Bounds{100, 50}.ScaleProportionally(50, 50)
23 if bounds.Width != 50 {
24 t.FailNow()
25 }
26 if bounds.Height != 25 {
27 t.FailNow()
28 }
29
30 // no changes
31 bounds = Bounds{100, 50}.ScaleProportionally(100, 100)
32 if bounds.Width != 100 {
33 t.FailNow()
34 }
35 if bounds.Height != 50 {
36 t.FailNow()
37 }
38
39 // no changes
40 bounds = Bounds{100, 50}.ScaleProportionally(100, 100000000)
41 if bounds.Width != 100 {
42 t.FailNow()
43 }
44 if bounds.Height != 50 {
45 t.FailNow()
46 }
47
48 // scale up
49 bounds = Bounds{100, 50}.ScaleProportionally(1000, 1000)
50 if bounds.Width != 1000 {
51 t.FailNow()
52 }
53 if bounds.Height != 500 {
54 t.FailNow()
55 }
11ce6af @tobi Added Bounds as own struct
authored May 31, 2012
56 }
57
58 func TestShrink(t *testing.T) {
476b3b6 @aybabtme Run go-fmt on the repo.
aybabtme authored Apr 20, 2014
59 // half
60 bounds := Bounds{100, 50}.ShrinkProportionally(50, 50)
61 if bounds.Width != 50 {
62 t.Errorf("Width is wrong: %d", bounds.Width)
63 }
64 if bounds.Height != 25 {
65 t.Errorf("Height is wrong: %d", bounds.Height)
66 }
67
68 // no changes
69 bounds = Bounds{100, 50}.ShrinkProportionally(100000, 100000)
70 if bounds.Width != 100 {
71 t.Errorf("Width is wrong: %d", bounds.Width)
72 }
73 if bounds.Height != 50 {
74 t.Errorf("Height is wrong: %d", bounds.Height)
75 }
11ce6af @tobi Added Bounds as own struct
authored May 31, 2012
76 }
77
78 func TestGrow(t *testing.T) {
476b3b6 @aybabtme Run go-fmt on the repo.
aybabtme authored Apr 20, 2014
79 // no changes
80 bounds := Bounds{100, 50}.GrowProportionally(50, 50)
81 if bounds.Width != 100 {
82 t.Errorf("Width is wrong: %d", bounds.Width)
83 }
84 if bounds.Height != 50 {
85 t.Errorf("Height is wrong: %d", bounds.Height)
86 }
87
88 // no changes
89 bounds = Bounds{100, 50}.GrowProportionally(100000, 100000)
90 if bounds.Width != 100000 {
91 t.Errorf("Width is wrong: %d", bounds.Width)
92 }
93 if bounds.Height != 50000 {
94 t.Errorf("Height is wrong: %d", bounds.Height)
95 }
11ce6af @tobi Added Bounds as own struct
authored May 31, 2012
96 }
793fe1a instantiate Bounds from string
Tom Burns authored Sep 13, 2012
97
0709a6f @aybabtme Error handling instead of nil-checks. Stub errors need to be improved.
aybabtme authored Apr 19, 2014
98 var stringTests = []struct {
99 name string
100 bounds string
101 want *Bounds
102 shouldErr bool
103 }{
104 {"Two proper sizes", "100x50", &Bounds{100, 50}, false},
105 {"Only height", "x50", &Bounds{0, 50}, false},
106 {"Only width", "100x", &Bounds{100, 0}, false},
107 {"An invalid bound", "not a bound", nil, true},
108 }
109
793fe1a instantiate Bounds from string
Tom Burns authored Sep 13, 2012
110 func TestFromString(t *testing.T) {
0709a6f @aybabtme Error handling instead of nil-checks. Stub errors need to be improved.
aybabtme authored Apr 20, 2014
111 for _, tt := range stringTests {
112 t.Logf("Extracting bounds: %s", tt.name)
113 bounds, err := BoundsFromString(tt.bounds)
222fcf9 Allow single-dimension bounds from strings
Tom Burns authored Sep 18, 2012
114
0709a6f @aybabtme Error handling instead of nil-checks. Stub errors need to be improved.
aybabtme authored Apr 20, 2014
115 switch {
222fcf9 Allow single-dimension bounds from strings
Tom Burns authored Sep 18, 2012
116
0709a6f @aybabtme Error handling instead of nil-checks. Stub errors need to be improved.
aybabtme authored Apr 20, 2014
117 case tt.shouldErr && err == nil:
118 t.Error("want an error, got nothing")
222fcf9 Allow single-dimension bounds from strings
Tom Burns authored Sep 18, 2012
119
0709a6f @aybabtme Error handling instead of nil-checks. Stub errors need to be improved.
aybabtme authored Apr 20, 2014
120 case !tt.shouldErr && err != nil:
121 t.Errorf("want no error, got '%v'", err)
122
123 case tt.want == nil && bounds != nil:
124 t.Errorf("want nil bound, got '%#v'", bounds)
125
126 case tt.want != nil && bounds == nil:
127 t.Errorf("want '%#v', got nothing", tt.want)
793fe1a instantiate Bounds from string
Tom Burns authored Sep 13, 2012
128
0709a6f @aybabtme Error handling instead of nil-checks. Stub errors need to be improved.
aybabtme authored Apr 20, 2014
129 }
130
131 if tt.shouldErr {
132 continue // tt.bounds will be nil
133 }
134
135 // not an error case, bounds is not nil
136 if tt.want.Width != bounds.Width {
137 t.Errorf("want width %d, got %d", tt.want.Width, bounds.Width)
138 }
139
140 if tt.want.Height != bounds.Height {
141 t.Errorf("want height %d, got %d", tt.want.Height, bounds.Height)
142 }
143 }
144 }
Something went wrong with that request. Please try again.