Merge lp:~jtv/maas/normalise-whitespace into lp:~maas-committers/maas/trunk

Proposed by Jeroen T. Vermeulen
Status: Merged
Approved by: Jeroen T. Vermeulen
Approved revision: no longer in the source branch.
Merged at revision: 2851
Proposed branch: lp:~jtv/maas/normalise-whitespace
Merge into: lp:~maas-committers/maas/trunk
Diff against target: 172 lines (+73/-16)
4 files modified
src/maasserver/views/tests/test_noderesult.py (+1/-5)
src/maasserver/views/tests/test_nodes.py (+9/-11)
src/provisioningserver/utils/tests/test_text.py (+41/-0)
src/provisioningserver/utils/text.py (+22/-0)
To merge this branch: bzr merge lp:~jtv/maas/normalise-whitespace
Reviewer Review Type Date Requested Status
Julian Edwards (community) Approve
Review via email: mp+232837@code.launchpad.net

Commit message

Unify implementations for normalising a string's whitespace.

Description of the change

This is something Gavin suggested while reviewing another branch.

Jeroen

To post a comment you must log in.
Revision history for this message
Julian Edwards (julian-edwards) wrote :

 review: approve

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/maasserver/views/tests/test_noderesult.py'
2--- src/maasserver/views/tests/test_noderesult.py 2014-08-20 20:24:39 +0000
3+++ src/maasserver/views/tests/test_noderesult.py 2014-09-01 03:04:59 +0000
4@@ -27,14 +27,10 @@
5 from maasserver.utils.orm import get_one
6 from maasserver.views.noderesult import NodeCommissionResultListView
7 from mock import Mock
8+from provisioningserver.utils.text import normalise_whitespace
9 from testtools.matchers import HasLength
10
11
12-def normalise_whitespace(text):
13- """Replace any whitespace sequence with just a single space."""
14- return ' '.join(text.split())
15-
16-
17 def extract_field(doc, field_name, containing_tag='span'):
18 """Get the content text from one of the <li> fields on the page.
19
20
21=== modified file 'src/maasserver/views/tests/test_nodes.py'
22--- src/maasserver/views/tests/test_nodes.py 2014-08-30 10:25:02 +0000
23+++ src/maasserver/views/tests/test_nodes.py 2014-09-01 03:04:59 +0000
24@@ -82,6 +82,7 @@
25 LLDP_OUTPUT_NAME,
26 )
27 from provisioningserver.utils.enum import map_enum
28+from provisioningserver.utils.text import normalise_whitespace
29 from testtools.matchers import ContainsAll
30
31
32@@ -592,7 +593,7 @@
33 [interface] = interfaces_list.cssselect('li')
34 self.assertEqual(
35 "%s (on %s)" % (mac.mac_address, network.name),
36- ' '.join(interface.text_content().split()))
37+ normalise_whitespace(interface.text_content()))
38 [link] = interface.cssselect('a')
39 self.assertEqual(network.name, link.text_content().strip())
40 self.assertEqual(
41@@ -615,7 +616,7 @@
42 [interface] = interfaces_list.cssselect('li')
43 self.assertEqual(
44 "%s (on %s)" % (mac.mac_address, ', '.join(sorted_names)),
45- ' '.join(interface.text_content().split()))
46+ normalise_whitespace(interface.text_content()))
47
48 def test_view_node_displays_link_to_edit_if_user_owns_node(self):
49 self.client_log_in()
50@@ -1340,10 +1341,6 @@
51 else:
52 self.fail("Found more than one link: %s" % links)
53
54- def normalise_whitespace(self, text):
55- """Return a version of `text` where all whitespace is single spaces."""
56- return ' '.join(text.split())
57-
58 def test_view_node_links_to_commissioning_results_if_appropriate(self):
59 self.client_log_in(as_admin=True)
60 result = factory.make_node_commission_result()
61@@ -1373,7 +1370,7 @@
62 link = self.get_results_link(section)
63 self.assertEqual(
64 "1 output file",
65- self.normalise_whitespace(link.text_content()))
66+ normalise_whitespace(link.text_content()))
67
68 def test_view_node_shows_commissioning_results_requires_edit_perm(self):
69 password = 'test'
70@@ -1394,7 +1391,7 @@
71 link = self.get_results_link(section)
72 self.assertEqual(
73 "1 output file",
74- self.normalise_whitespace(link.text_content()))
75+ normalise_whitespace(link.text_content()))
76
77 def test_view_node_shows_multiple_commissioning_results(self):
78 self.client_log_in(as_admin=True)
79@@ -1407,7 +1404,7 @@
80 link = self.get_results_link(section)
81 self.assertEqual(
82 "%d output files" % num_results,
83- self.normalise_whitespace(link.text_content()))
84+ normalise_whitespace(link.text_content()))
85
86 def test_view_node_shows_installing_results_only_if_present(self):
87 self.client_log_in(as_admin=True)
88@@ -1425,8 +1422,9 @@
89 section = self.request_results_display(
90 result.node, RESULT_TYPE.INSTALLING)
91 link = self.get_results_link(section)
92- self.assertIsNotNone(
93- self.normalise_whitespace(link.text_content()))
94+ self.assertNotIn(
95+ normalise_whitespace(link.text_content()),
96+ ('', None))
97
98 def test_view_node_shows_installing_results_requires_edit_perm(self):
99 password = 'test'
100
101=== added file 'src/provisioningserver/utils/tests/test_text.py'
102--- src/provisioningserver/utils/tests/test_text.py 1970-01-01 00:00:00 +0000
103+++ src/provisioningserver/utils/tests/test_text.py 2014-09-01 03:04:59 +0000
104@@ -0,0 +1,41 @@
105+# Copyright 2014 Canonical Ltd. This software is licensed under the
106+# GNU Affero General Public License version 3 (see the file LICENSE).
107+
108+"""Tests for text processing utilities."""
109+
110+from __future__ import (
111+ absolute_import,
112+ print_function,
113+ unicode_literals,
114+ )
115+
116+str = None
117+
118+__metaclass__ = type
119+__all__ = []
120+
121+from maastesting.factory import factory
122+from maastesting.testcase import MAASTestCase
123+from provisioningserver.utils.text import normalise_whitespace
124+
125+
126+class TestNormaliseWhitespace(MAASTestCase):
127+
128+ def test__preserves_text_without_whitespace(self):
129+ word = factory.make_name('word')
130+ self.assertEqual(word, normalise_whitespace(word))
131+
132+ def test__eliminates_leading_space(self):
133+ self.assertEqual('word', normalise_whitespace(' word'))
134+
135+ def test__eliminates_trailing_space(self):
136+ self.assertEqual('word', normalise_whitespace('word '))
137+
138+ def test__replaces_any_whitespace_sequence_with_single_space(self):
139+ self.assertEqual(
140+ 'one two three',
141+ normalise_whitespace('one two\t\nthree'))
142+
143+ def test__treats_punctuation_as_non_space(self):
144+ punctuation = '.?;:!'
145+ self.assertEqual(punctuation, normalise_whitespace(punctuation))
146
147=== added file 'src/provisioningserver/utils/text.py'
148--- src/provisioningserver/utils/text.py 1970-01-01 00:00:00 +0000
149+++ src/provisioningserver/utils/text.py 2014-09-01 03:04:59 +0000
150@@ -0,0 +1,22 @@
151+# Copyright 2014 Canonical Ltd. This software is licensed under the
152+# GNU Affero General Public License version 3 (see the file LICENSE).
153+
154+"""Text-processing utilities."""
155+
156+from __future__ import (
157+ absolute_import,
158+ print_function,
159+ unicode_literals,
160+ )
161+
162+str = None
163+
164+__metaclass__ = type
165+__all__ = [
166+ 'normalise_whitespace',
167+ ]
168+
169+
170+def normalise_whitespace(text):
171+ """Replace any whitespace sequence in `text` with just a single space."""
172+ return ' '.join(text.split())