Source for: test_justify_row.py [raw]
1import pytest
2
3import boxes
4
5
6def test_justify_simple():
7 """Test a simple use case."""
8 # First setup what we will use, our "test case"
9 row = [boxes.Box(x=i, w=1, h=1, letter="a") for i in range(10)]
10 page = boxes.Box(w=50, h=50)
11 separation = .1
12
13 # Check expected characteristics
14 assert len(row) == 10
15 assert row[-1].x + row[-1].w == 10
16
17 # Do the thing
18 boxes.justify_row(row, page, separation)
19
20 # Check the expected behavior
21
22 # Should have the same number of elements
23 assert len(row) == 10
24 # The first element should be flushed-left
25 assert row[0].x == page.x
26 # The last element should be flushed-right
27 assert row[-1].x + row[-1].w == page.x + page.w
28
29
30def test_justify_with_spaces():
31 """Test a simple use case with spaces."""
32 row = [boxes.Box(x=i, w=1, h=1, letter="a") for i in range(10)]
33 row[5].letter = " "
34 row[5].stretchy = True
35 page = boxes.Box(w=50, h=50)
36 separation = .1
37
38 assert len(row) == 10
39 assert row[-1].x + row[-1].w == 10
40
41 boxes.justify_row(row, page, separation)
42
43 # Should have the same number of elements
44 assert len(row) == 10
45 # The first element should be flushed-left
46 assert row[0].x == page.x
47 # The last element should be flushed-right
48 # Use approx() because floating point adds a tiny error here
49 assert pytest.approx(row[-1].x + row[-1].w) == page.x + page.w
50 # The element in position 5 must have absorbed all the slack
51 # So is 1 (it's width) + 40 (slack) units wide
52 assert row[5].w == 41
53
54
55@pytest.mark.xfail # FIXME: justify doesn't handle newlines yet!
56def test_justify_ends_with_newline():
57 """Test a use case with a newline."""
58 row = [boxes.Box(x=i, w=1, h=1, letter="a") for i in range(10)]
59 row[-1].letter = "\n"
60 page = boxes.Box(w=50, h=50)
61 separation = .1
62
63 assert len(row) == 10
64 assert row[-1].x + row[-1].w == 10
65
66 boxes.justify_row(row, page, separation)
67
68 # Should have the same number of elements
69 assert len(row) == 10
70 # The first element should be flushed-left
71 assert row[0].x == page.x
72 # The last element should NOT be flushed-right
73 assert row[-1].x + row[-1].w == 10
74
75
76def test_justify_trailing_spaces():
77 """Test a use case with traling spaces."""
78 row = [boxes.Box(x=i, w=1, h=1, letter="a") for i in range(10)]
79 row[-1].letter = " "
80 row[-2].letter = " "
81 page = boxes.Box(w=50, h=50)
82 separation = .1
83
84 assert len(row) == 10
85 assert row[-1].x + row[-1].w == 10
86
87 boxes.justify_row(row, page, separation)
88
89 # Should have lost the 2 trailing spaces
90 assert len(row) == 8
91 # The first element should be flushed-left
92 assert row[0].x == page.x
93 # The last element should be flushed-right
94 assert row[-1].x + row[-1].w == page.x + page.w
95
96
97def test_justify_puts_things_in_a_row():
98 """Test a simple use case with spaces."""
99 row = [boxes.Box(x=i, w=1, h=1, letter="a") for i in range(10)]
100 row[5].letter = " "
101 row[5].stretchy = True
102 page = boxes.Box(w=50, h=50)
103 separation = .1
104
105 assert len(row) == 10
106 assert row[-1].x + row[-1].w == 10
107
108 boxes.justify_row(row, page, separation)
109
110 # Should have the same number of elements
111 assert len(row) == 10
112 # The first element should be flushed-left
113 assert row[0].x == page.x
114 # The last element should be flushed-right
115 # Use approx() because floating point adds a tiny error here
116 assert pytest.approx(row[-1].x + row[-1].w) == page.x + page.w
117 # All elements should be separated correctly.
118 separations = [
119 separation - (row[i].x - (row[i - 1].x + row[i - 1].w))
120 for i in range(1, len(row))
121 ]
122 # Again, floating point is inaccurate
123 assert max(separations) < 0.00001
124 assert min(separations) > -0.00001
125