webkitpy.layout_tests.port.mock_drt_unittest.MockChromiumDRTTest has been failing...
[WebKit-https.git] / Tools / Scripts / webkitpy / layout_tests / models / test_expectations_unittest.py
1 #!/usr/bin/python
2 # Copyright (C) 2010 Google Inc. All rights reserved.
3 #
4 # Redistribution and use in source and binary forms, with or without
5 # modification, are permitted provided that the following conditions are
6 # met:
7 #
8 #     * Redistributions of source code must retain the above copyright
9 # notice, this list of conditions and the following disclaimer.
10 #     * Redistributions in binary form must reproduce the above
11 # copyright notice, this list of conditions and the following disclaimer
12 # in the documentation and/or other materials provided with the
13 # distribution.
14 #     * Neither the name of Google Inc. nor the names of its
15 # contributors may be used to endorse or promote products derived from
16 # this software without specific prior written permission.
17 #
18 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30 import unittest
31
32 from webkitpy.common.host_mock import MockHost
33
34 from webkitpy.layout_tests.models.test_configuration import *
35 from webkitpy.layout_tests.models.test_expectations import *
36 from webkitpy.layout_tests.models.test_configuration import *
37
38
39 class MockBugManager(object):
40     def close_bug(self, bug_id, reference_bug_id=None):
41         pass
42
43     def create_bug(self):
44         return "BUG_NEWLY_CREATED"
45
46
47 class FunctionsTest(unittest.TestCase):
48     def test_result_was_expected(self):
49         # test basics
50         self.assertEquals(result_was_expected(PASS, set([PASS]),
51                                               False, False), True)
52         self.assertEquals(result_was_expected(TEXT, set([PASS]),
53                                               False, False), False)
54
55         # test handling of FAIL expectations
56         self.assertEquals(result_was_expected(IMAGE_PLUS_TEXT, set([FAIL]),
57                                               False, False), True)
58         self.assertEquals(result_was_expected(IMAGE, set([FAIL]),
59                                               False, False), True)
60         self.assertEquals(result_was_expected(TEXT, set([FAIL]),
61                                               False, False), True)
62         self.assertEquals(result_was_expected(CRASH, set([FAIL]),
63                                               False, False), False)
64
65         # test handling of SKIPped tests and results
66         self.assertEquals(result_was_expected(SKIP, set([CRASH]),
67                                               False, True), True)
68         self.assertEquals(result_was_expected(SKIP, set([CRASH]),
69                                               False, False), False)
70
71         # test handling of MISSING results and the REBASELINE modifier
72         self.assertEquals(result_was_expected(MISSING, set([PASS]),
73                                               True, False), True)
74         self.assertEquals(result_was_expected(MISSING, set([PASS]),
75                                               False, False), False)
76
77     def test_remove_pixel_failures(self):
78         self.assertEquals(remove_pixel_failures(set([TEXT])),
79                           set([TEXT]))
80         self.assertEquals(remove_pixel_failures(set([PASS])),
81                           set([PASS]))
82         self.assertEquals(remove_pixel_failures(set([IMAGE])),
83                           set([PASS]))
84         self.assertEquals(remove_pixel_failures(set([IMAGE_PLUS_TEXT])),
85                           set([TEXT]))
86         self.assertEquals(remove_pixel_failures(set([PASS, IMAGE, CRASH])),
87                           set([PASS, CRASH]))
88
89
90 class Base(unittest.TestCase):
91     # Note that all of these tests are written assuming the configuration
92     # being tested is Windows XP, Release build.
93
94     def __init__(self, testFunc, setUp=None, tearDown=None, description=None):
95         host = MockHost()
96         self._port = host.port_factory.get('test-win-xp', None)
97         self._exp = None
98         unittest.TestCase.__init__(self, testFunc)
99
100     def get_test(self, test_name):
101         # FIXME: Remove this routine and just reference test names directly.
102         return test_name
103
104     def get_basic_tests(self):
105         return [self.get_test('failures/expected/text.html'),
106                 self.get_test('failures/expected/image_checksum.html'),
107                 self.get_test('failures/expected/crash.html'),
108                 self.get_test('failures/expected/missing_text.html'),
109                 self.get_test('failures/expected/image.html'),
110                 self.get_test('passes/text.html')]
111
112     def get_basic_expectations(self):
113         return """
114 BUG_TEST : failures/expected/text.html = TEXT
115 BUG_TEST WONTFIX SKIP : failures/expected/crash.html = CRASH
116 BUG_TEST REBASELINE : failures/expected/missing_image.html = MISSING
117 BUG_TEST WONTFIX : failures/expected/image_checksum.html = IMAGE
118 BUG_TEST WONTFIX MAC : failures/expected/image.html = IMAGE
119 """
120
121     def parse_exp(self, expectations, overrides=None, is_lint_mode=False):
122         test_config = self._port.test_configuration()
123         self._exp = TestExpectations(self._port,
124              tests=self.get_basic_tests(),
125              expectations=expectations,
126              test_config=test_config,
127              is_lint_mode=is_lint_mode,
128              overrides=overrides)
129
130     def assert_exp(self, test, result):
131         self.assertEquals(self._exp.get_expectations(self.get_test(test)),
132                           set([result]))
133
134
135 class BasicTests(Base):
136     def test_basic(self):
137         self.parse_exp(self.get_basic_expectations())
138         self.assert_exp('failures/expected/text.html', TEXT)
139         self.assert_exp('failures/expected/image_checksum.html', IMAGE)
140         self.assert_exp('passes/text.html', PASS)
141         self.assert_exp('failures/expected/image.html', PASS)
142
143
144 class MiscTests(Base):
145     def test_multiple_results(self):
146         self.parse_exp('BUGX : failures/expected/text.html = TEXT CRASH')
147         self.assertEqual(self._exp.get_expectations(
148             self.get_test('failures/expected/text.html')),
149             set([TEXT, CRASH]))
150
151     def test_category_expectations(self):
152         # This test checks unknown tests are not present in the
153         # expectations and that known test part of a test category is
154         # present in the expectations.
155         exp_str = """
156 BUGX WONTFIX : failures/expected = IMAGE
157 """
158         self.parse_exp(exp_str)
159         test_name = 'failures/expected/unknown-test.html'
160         unknown_test = self.get_test(test_name)
161         self.assertRaises(KeyError, self._exp.get_expectations,
162                           unknown_test)
163         self.assert_exp('failures/expected/crash.html', IMAGE)
164
165     def test_get_modifiers(self):
166         self.parse_exp(self.get_basic_expectations())
167         self.assertEqual(self._exp.get_modifiers(
168                          self.get_test('passes/text.html')), [])
169
170     def test_get_expectations_string(self):
171         self.parse_exp(self.get_basic_expectations())
172         self.assertEquals(self._exp.get_expectations_string(
173                           self.get_test('failures/expected/text.html')),
174                           'TEXT')
175
176     def test_expectation_to_string(self):
177         # Normal cases are handled by other tests.
178         self.parse_exp(self.get_basic_expectations())
179         self.assertRaises(ValueError, self._exp.expectation_to_string,
180                           -1)
181
182     def test_get_test_set(self):
183         # Handle some corner cases for this routine not covered by other tests.
184         self.parse_exp(self.get_basic_expectations())
185         s = self._exp.get_test_set(WONTFIX)
186         self.assertEqual(s,
187             set([self.get_test('failures/expected/crash.html'),
188                  self.get_test('failures/expected/image_checksum.html')]))
189         s = self._exp.get_test_set(WONTFIX, CRASH)
190         self.assertEqual(s,
191             set([self.get_test('failures/expected/crash.html')]))
192         s = self._exp.get_test_set(WONTFIX, CRASH, include_skips=False)
193         self.assertEqual(s, set([]))
194
195     def test_parse_error_fatal(self):
196         try:
197             self.parse_exp("""FOO : failures/expected/text.html = TEXT
198 SKIP : failures/expected/image.html""")
199             self.assertFalse(True, "ParseError wasn't raised")
200         except ParseError, e:
201             self.assertTrue(e.fatal)
202             exp_errors = [u"FAILURES FOR %s in %s" % (self._port.test_configuration(), self._port.path_to_test_expectations_file()),
203                           u"Line:1 Unrecognized modifier 'foo' failures/expected/text.html",
204                           u"Line:2 Missing expectations SKIP : failures/expected/image.html"]
205             self.assertEqual(str(e), '\n'.join(map(str, exp_errors)))
206             self.assertEqual(e.errors, exp_errors)
207
208     def test_parse_error_nonfatal(self):
209         try:
210             self.parse_exp('SKIP : failures/expected/text.html = TEXT',
211                            is_lint_mode=True)
212             self.assertFalse(True, "ParseError wasn't raised")
213         except ParseError, e:
214             self.assertFalse(e.fatal)
215             exp_errors = [u'FAILURES FOR %s in %s' % (self._port.test_configuration(), self._port.path_to_test_expectations_file()),
216                           u'Line:1 Test lacks BUG modifier. failures/expected/text.html']
217             self.assertEqual(str(e), '\n'.join(map(str, exp_errors)))
218             self.assertEqual(e.errors, exp_errors)
219
220     def test_overrides(self):
221         self.parse_exp("BUG_EXP: failures/expected/text.html = TEXT",
222                        "BUG_OVERRIDE : failures/expected/text.html = IMAGE")
223         self.assert_exp('failures/expected/text.html', IMAGE)
224
225     def test_overrides__duplicate(self):
226         self.assertRaises(ParseError, self.parse_exp,
227              "BUG_EXP: failures/expected/text.html = TEXT",
228              """
229 BUG_OVERRIDE : failures/expected/text.html = IMAGE
230 BUG_OVERRIDE : failures/expected/text.html = CRASH
231 """)
232
233     def test_pixel_tests_flag(self):
234         def match(test, result, pixel_tests_enabled):
235             return self._exp.matches_an_expected_result(
236                 self.get_test(test), result, pixel_tests_enabled)
237
238         self.parse_exp(self.get_basic_expectations())
239         self.assertTrue(match('failures/expected/text.html', TEXT, True))
240         self.assertTrue(match('failures/expected/text.html', TEXT, False))
241         self.assertFalse(match('failures/expected/text.html', CRASH, True))
242         self.assertFalse(match('failures/expected/text.html', CRASH, False))
243         self.assertTrue(match('failures/expected/image_checksum.html', IMAGE,
244                               True))
245         self.assertTrue(match('failures/expected/image_checksum.html', PASS,
246                               False))
247         self.assertTrue(match('failures/expected/crash.html', SKIP, False))
248         self.assertTrue(match('passes/text.html', PASS, False))
249
250     def test_more_specific_override_resets_skip(self):
251         self.parse_exp("BUGX SKIP : failures/expected = TEXT\n"
252                        "BUGX : failures/expected/text.html = IMAGE\n")
253         self.assert_exp('failures/expected/text.html', IMAGE)
254         self.assertFalse(self._port._filesystem.join(self._port.layout_tests_dir(),
255                                                      'failures/expected/text.html') in
256                          self._exp.get_tests_with_result_type(SKIP))
257
258     def test_add_skipped_tests(self):
259         port = MockHost().port_factory.get('qt')
260         port._filesystem.files[port._filesystem.join(port.layout_tests_dir(), 'platform/qt/Skipped')] = 'failures/expected/text.html'
261         port._filesystem.files[port._filesystem.join(port.layout_tests_dir(), 'failures/expected/text.html')] = 'foo'
262         self.assertRaises(ParseError, TestExpectations, port, 'failures/expected/text.html\n', 'BUGX : failures/expected/text.html = text\n', None, True)
263
264
265 class ExpectationSyntaxTests(Base):
266     def test_missing_expectation(self):
267         # This is missing the expectation.
268         self.assertRaises(ParseError, self.parse_exp,
269                           'BUG_TEST: failures/expected/text.html')
270
271     def test_missing_colon(self):
272         # This is missing the modifiers and the ':'
273         self.assertRaises(ParseError, self.parse_exp,
274                           'failures/expected/text.html = TEXT')
275
276     def disabled_test_too_many_colons(self):
277         # FIXME: Enable this test and fix the underlying bug.
278         self.assertRaises(ParseError, self.parse_exp,
279                           'BUG_TEST: failures/expected/text.html = PASS :')
280
281     def test_too_many_equals_signs(self):
282         self.assertRaises(ParseError, self.parse_exp,
283                           'BUG_TEST: failures/expected/text.html = TEXT = IMAGE')
284
285     def test_unrecognized_expectation(self):
286         self.assertRaises(ParseError, self.parse_exp,
287                           'BUG_TEST: failures/expected/text.html = UNKNOWN')
288
289     def test_macro(self):
290         exp_str = """
291 BUG_TEST WIN : failures/expected/text.html = TEXT
292 """
293         self.parse_exp(exp_str)
294         self.assert_exp('failures/expected/text.html', TEXT)
295
296
297 class SemanticTests(Base):
298     def test_bug_format(self):
299         self.assertRaises(ParseError, self.parse_exp, 'BUG1234 : failures/expected/text.html = TEXT')
300
301     def test_missing_bugid(self):
302         # This should log a non-fatal error.
303         self.parse_exp('SLOW : failures/expected/text.html = TEXT')
304         self.assertTrue(self._exp.has_warnings())
305
306     def test_slow_and_timeout(self):
307         # A test cannot be SLOW and expected to TIMEOUT.
308         self.assertRaises(ParseError, self.parse_exp,
309             'BUG_TEST SLOW : failures/expected/timeout.html = TIMEOUT')
310
311     def test_rebaseline(self):
312         # Can't lint a file w/ 'REBASELINE' in it.
313         self.assertRaises(ParseError, self.parse_exp,
314             'BUG_TEST REBASELINE : failures/expected/text.html = TEXT',
315             is_lint_mode=True)
316
317     def test_duplicates(self):
318         self.assertRaises(ParseError, self.parse_exp, """
319 BUG_EXP : failures/expected/text.html = TEXT
320 BUG_EXP : failures/expected/text.html = IMAGE""")
321
322         self.assertRaises(ParseError, self.parse_exp,
323             self.get_basic_expectations(), overrides="""
324 BUG_OVERRIDE : failures/expected/text.html = TEXT
325 BUG_OVERRIDE : failures/expected/text.html = IMAGE""", )
326
327     def test_missing_file(self):
328         # This should log a non-fatal error.
329         self.parse_exp('BUG_TEST : missing_file.html = TEXT')
330         self.assertTrue(self._exp.has_warnings(), 1)
331
332
333 class PrecedenceTests(Base):
334     def test_file_over_directory(self):
335         # This tests handling precedence of specific lines over directories
336         # and tests expectations covering entire directories.
337         exp_str = """
338 BUGX : failures/expected/text.html = TEXT
339 BUGX WONTFIX : failures/expected = IMAGE
340 """
341         self.parse_exp(exp_str)
342         self.assert_exp('failures/expected/text.html', TEXT)
343         self.assert_exp('failures/expected/crash.html', IMAGE)
344
345         exp_str = """
346 BUGX WONTFIX : failures/expected = IMAGE
347 BUGX : failures/expected/text.html = TEXT
348 """
349         self.parse_exp(exp_str)
350         self.assert_exp('failures/expected/text.html', TEXT)
351         self.assert_exp('failures/expected/crash.html', IMAGE)
352
353     def test_ambiguous(self):
354         self.assertRaises(ParseError, self.parse_exp, """
355 BUG_TEST RELEASE : passes/text.html = PASS
356 BUG_TEST WIN : passes/text.html = FAIL
357 """)
358
359     def test_more_modifiers(self):
360         exp_str = """
361 BUG_TEST RELEASE : passes/text.html = PASS
362 BUG_TEST WIN RELEASE : passes/text.html = TEXT
363 """
364         self.assertRaises(ParseError, self.parse_exp, exp_str)
365
366     def test_order_in_file(self):
367         exp_str = """
368 BUG_TEST WIN RELEASE : passes/text.html = TEXT
369 BUG_TEST RELEASE : passes/text.html = PASS
370 """
371         self.assertRaises(ParseError, self.parse_exp, exp_str)
372
373     def test_macro_overrides(self):
374         exp_str = """
375 BUG_TEST WIN : passes/text.html = PASS
376 BUG_TEST XP : passes/text.html = TEXT
377 """
378         self.assertRaises(ParseError, self.parse_exp, exp_str)
379
380
381 class RebaseliningTest(Base):
382     """Test rebaselining-specific functionality."""
383     def assertRemove(self, input_expectations, tests, expected_expectations):
384         self.parse_exp(input_expectations)
385         actual_expectations = self._exp.remove_rebaselined_tests(tests)
386         self.assertEqual(expected_expectations, actual_expectations)
387
388     def test_remove(self):
389         self.assertRemove('BUGX REBASELINE : failures/expected/text.html = TEXT\n'
390                           'BUGY : failures/expected/image.html = IMAGE\n'
391                           'BUGZ REBASELINE : failures/expected/crash.html = CRASH\n',
392                           ['failures/expected/text.html'],
393                           'BUGY : failures/expected/image.html = IMAGE\n'
394                           'BUGZ REBASELINE : failures/expected/crash.html = CRASH\n')
395
396     def test_no_get_rebaselining_failures(self):
397         self.parse_exp(self.get_basic_expectations())
398         self.assertEqual(len(self._exp.get_rebaselining_failures()), 0)
399
400
401 class TestExpectationParserTests(unittest.TestCase):
402     def test_tokenize_blank(self):
403         expectation = TestExpectationParser.tokenize('')
404         self.assertEqual(expectation.is_malformed(), False)
405         self.assertEqual(expectation.comment, None)
406         self.assertEqual(len(expectation.errors), 0)
407
408     def test_tokenize_missing_colon(self):
409         expectation = TestExpectationParser.tokenize('Qux.')
410         self.assertEqual(expectation.is_malformed(), True)
411         self.assertEqual(str(expectation.errors), '["Missing a \':\'"]')
412
413     def test_tokenize_extra_colon(self):
414         expectation = TestExpectationParser.tokenize('FOO : : bar')
415         self.assertEqual(expectation.is_malformed(), True)
416         self.assertEqual(str(expectation.errors), '["Extraneous \':\'"]')
417
418     def test_tokenize_empty_comment(self):
419         expectation = TestExpectationParser.tokenize('//')
420         self.assertEqual(expectation.is_malformed(), False)
421         self.assertEqual(expectation.comment, '')
422         self.assertEqual(len(expectation.errors), 0)
423
424     def test_tokenize_comment(self):
425         expectation = TestExpectationParser.tokenize('//Qux.')
426         self.assertEqual(expectation.is_malformed(), False)
427         self.assertEqual(expectation.comment, 'Qux.')
428         self.assertEqual(len(expectation.errors), 0)
429
430     def test_tokenize_missing_equal(self):
431         expectation = TestExpectationParser.tokenize('FOO : bar')
432         self.assertEqual(expectation.is_malformed(), True)
433         self.assertEqual(str(expectation.errors), "['Missing expectations\']")
434
435     def test_tokenize_extra_equal(self):
436         expectation = TestExpectationParser.tokenize('FOO : bar = BAZ = Qux.')
437         self.assertEqual(expectation.is_malformed(), True)
438         self.assertEqual(str(expectation.errors), '["Extraneous \'=\'"]')
439
440     def test_tokenize_valid(self):
441         expectation = TestExpectationParser.tokenize('FOO : bar = BAZ')
442         self.assertEqual(expectation.is_malformed(), False)
443         self.assertEqual(expectation.comment, None)
444         self.assertEqual(len(expectation.errors), 0)
445
446     def test_tokenize_valid_with_comment(self):
447         expectation = TestExpectationParser.tokenize('FOO : bar = BAZ //Qux.')
448         self.assertEqual(expectation.is_malformed(), False)
449         self.assertEqual(expectation.comment, 'Qux.')
450         self.assertEqual(str(expectation.modifiers), '[\'foo\']')
451         self.assertEqual(str(expectation.expectations), '[\'baz\']')
452         self.assertEqual(len(expectation.errors), 0)
453
454     def test_tokenize_valid_with_multiple_modifiers(self):
455         expectation = TestExpectationParser.tokenize('FOO1 FOO2 : bar = BAZ //Qux.')
456         self.assertEqual(expectation.is_malformed(), False)
457         self.assertEqual(expectation.comment, 'Qux.')
458         self.assertEqual(str(expectation.modifiers), '[\'foo1\', \'foo2\']')
459         self.assertEqual(str(expectation.expectations), '[\'baz\']')
460         self.assertEqual(len(expectation.errors), 0)
461
462     def test_parse_empty_string(self):
463         host = MockHost()
464         test_port = host.port_factory.get('test-win-xp', None)
465         test_port.test_exists = lambda test: True
466         test_config = test_port.test_configuration()
467         full_test_list = []
468         expectation_line = TestExpectationParser.tokenize('')
469         parser = TestExpectationParser(test_port, full_test_list, allow_rebaseline_modifier=False)
470         parser.parse(expectation_line)
471         self.assertFalse(expectation_line.is_invalid())
472
473
474 class TestExpectationSerializerTests(unittest.TestCase):
475     def __init__(self, testFunc):
476         host = MockHost()
477         test_port = host.port_factory.get('test-win-xp', None)
478         self._converter = TestConfigurationConverter(test_port.all_test_configurations(), test_port.configuration_specifier_macros())
479         self._serializer = TestExpectationSerializer(self._converter)
480         unittest.TestCase.__init__(self, testFunc)
481
482     def assert_round_trip(self, in_string, expected_string=None):
483         expectation = TestExpectationParser.tokenize(in_string)
484         if expected_string is None:
485             expected_string = in_string
486         self.assertEqual(expected_string, self._serializer.to_string(expectation))
487
488     def assert_list_round_trip(self, in_string, expected_string=None):
489         expectations = TestExpectationParser.tokenize_list(in_string)
490         if expected_string is None:
491             expected_string = in_string
492         self.assertEqual(expected_string, TestExpectationSerializer.list_to_string(expectations, self._converter))
493
494     def test_unparsed_to_string(self):
495         expectation = TestExpectationLine()
496         serializer = TestExpectationSerializer()
497
498         self.assertEqual(serializer.to_string(expectation), '')
499         expectation.comment = 'Qux.'
500         self.assertEqual(serializer.to_string(expectation), '//Qux.')
501         expectation.name = 'bar'
502         self.assertEqual(serializer.to_string(expectation), ' : bar =  //Qux.')
503         expectation.modifiers = ['foo']
504         self.assertEqual(serializer.to_string(expectation), 'FOO : bar =  //Qux.')
505         expectation.expectations = ['bAz']
506         self.assertEqual(serializer.to_string(expectation), 'FOO : bar = BAZ //Qux.')
507         expectation.expectations = ['bAz1', 'baZ2']
508         self.assertEqual(serializer.to_string(expectation), 'FOO : bar = BAZ1 BAZ2 //Qux.')
509         expectation.modifiers = ['foo1', 'foO2']
510         self.assertEqual(serializer.to_string(expectation), 'FOO1 FOO2 : bar = BAZ1 BAZ2 //Qux.')
511         expectation.errors.append('Oh the horror.')
512         self.assertEqual(serializer.to_string(expectation), '')
513         expectation.original_string = 'Yes it is!'
514         self.assertEqual(serializer.to_string(expectation), 'Yes it is!')
515
516     def test_unparsed_list_to_string(self):
517         expectation = TestExpectationLine()
518         expectation.comment = 'Qux.'
519         expectation.name = 'bar'
520         expectation.modifiers = ['foo']
521         expectation.expectations = ['bAz1', 'baZ2']
522         self.assertEqual(TestExpectationSerializer.list_to_string([expectation]), 'FOO : bar = BAZ1 BAZ2 //Qux.')
523
524     def test_parsed_to_string(self):
525         expectation_line = TestExpectationLine()
526         expectation_line.parsed_bug_modifiers = ['BUGX']
527         expectation_line.name = 'test/name/for/realz.html'
528         expectation_line.parsed_expectations = set([IMAGE])
529         self.assertEqual(self._serializer.to_string(expectation_line), None)
530         expectation_line.matching_configurations = set([TestConfiguration('xp', 'x86', 'release', 'cpu')])
531         self.assertEqual(self._serializer.to_string(expectation_line), 'BUGX XP RELEASE CPU : test/name/for/realz.html = IMAGE')
532         expectation_line.matching_configurations = set([TestConfiguration('xp', 'x86', 'release', 'cpu'), TestConfiguration('xp', 'x86', 'release', 'gpu')])
533         self.assertEqual(self._serializer.to_string(expectation_line), 'BUGX XP RELEASE : test/name/for/realz.html = IMAGE')
534         expectation_line.matching_configurations = set([TestConfiguration('xp', 'x86', 'release', 'cpu'), TestConfiguration('xp', 'x86', 'debug', 'gpu')])
535         self.assertEqual(self._serializer.to_string(expectation_line), 'BUGX XP RELEASE CPU : test/name/for/realz.html = IMAGE\nBUGX XP DEBUG GPU : test/name/for/realz.html = IMAGE')
536
537     def test_parsed_expectations_string(self):
538         expectation_line = TestExpectationLine()
539         expectation_line.parsed_expectations = set([])
540         self.assertEqual(self._serializer._parsed_expectations_string(expectation_line), '')
541         expectation_line.parsed_expectations = set([IMAGE_PLUS_TEXT])
542         self.assertEqual(self._serializer._parsed_expectations_string(expectation_line), 'image+text')
543         expectation_line.parsed_expectations = set([PASS, FAIL])
544         self.assertEqual(self._serializer._parsed_expectations_string(expectation_line), 'pass fail')
545         expectation_line.parsed_expectations = set([FAIL, PASS])
546         self.assertEqual(self._serializer._parsed_expectations_string(expectation_line), 'pass fail')
547
548     def test_parsed_modifier_string(self):
549         expectation_line = TestExpectationLine()
550         expectation_line.parsed_bug_modifiers = ['garden-o-matic']
551         expectation_line.parsed_modifiers = ['for', 'the']
552         self.assertEqual(self._serializer._parsed_modifier_string(expectation_line, []), 'garden-o-matic for the')
553         self.assertEqual(self._serializer._parsed_modifier_string(expectation_line, ['win']), 'garden-o-matic for the win')
554         expectation_line.parsed_bug_modifiers = []
555         expectation_line.parsed_modifiers = []
556         self.assertEqual(self._serializer._parsed_modifier_string(expectation_line, []), '')
557         self.assertEqual(self._serializer._parsed_modifier_string(expectation_line, ['win']), 'win')
558         expectation_line.parsed_bug_modifiers = ['garden-o-matic', 'total', 'is']
559         self.assertEqual(self._serializer._parsed_modifier_string(expectation_line, ['win']), 'garden-o-matic is total win')
560         expectation_line.parsed_bug_modifiers = []
561         expectation_line.parsed_modifiers = ['garden-o-matic', 'total', 'is']
562         self.assertEqual(self._serializer._parsed_modifier_string(expectation_line, ['win']), 'garden-o-matic is total win')
563
564     def test_format_result(self):
565         self.assertEqual(TestExpectationSerializer._format_result('modifiers', 'name', 'expectations', 'comment'), 'MODIFIERS : name = EXPECTATIONS //comment')
566         self.assertEqual(TestExpectationSerializer._format_result('modifiers', 'name', 'expectations', None), 'MODIFIERS : name = EXPECTATIONS')
567
568     def test_string_roundtrip(self):
569         self.assert_round_trip('')
570         self.assert_round_trip('FOO')
571         self.assert_round_trip(':')
572         self.assert_round_trip('FOO :')
573         self.assert_round_trip('FOO : bar')
574         self.assert_round_trip('  FOO :')
575         self.assert_round_trip('    FOO : bar')
576         self.assert_round_trip('FOO : bar = BAZ')
577         self.assert_round_trip('FOO : bar = BAZ //Qux.')
578         self.assert_round_trip('FOO : bar = BAZ // Qux.')
579         self.assert_round_trip('FOO : bar = BAZ // Qux.     ')
580         self.assert_round_trip('FOO : bar = BAZ //        Qux.     ')
581         self.assert_round_trip('FOO : : bar = BAZ')
582         self.assert_round_trip('FOO : : bar = BAZ')
583         self.assert_round_trip('FOO : : bar ==== BAZ')
584         self.assert_round_trip('=')
585         self.assert_round_trip('//')
586         self.assert_round_trip('// ')
587         self.assert_round_trip('// Foo')
588         self.assert_round_trip('// Foo')
589         self.assert_round_trip('// Foo :')
590         self.assert_round_trip('// Foo : =')
591
592     def test_list_roundtrip(self):
593         self.assert_list_round_trip('')
594         self.assert_list_round_trip('\n')
595         self.assert_list_round_trip('\n\n')
596         self.assert_list_round_trip('bar')
597         self.assert_list_round_trip('bar\n//Qux.')
598         self.assert_list_round_trip('bar\n//Qux.\n')
599
600     def test_reconstitute_only_these(self):
601         lines = []
602         reconstitute_only_these = []
603
604         def add_line(matching_configurations, reconstitute):
605             expectation_line = TestExpectationLine()
606             expectation_line.original_string = "Nay"
607             expectation_line.parsed_bug_modifiers = ['BUGX']
608             expectation_line.name = 'Yay'
609             expectation_line.parsed_expectations = set([IMAGE])
610             expectation_line.matching_configurations = matching_configurations
611             lines.append(expectation_line)
612             if reconstitute:
613                 reconstitute_only_these.append(expectation_line)
614
615         add_line(set([TestConfiguration('xp', 'x86', 'release', 'cpu')]), False)
616         add_line(set([TestConfiguration('xp', 'x86', 'release', 'cpu'), TestConfiguration('xp', 'x86', 'release', 'gpu')]), True)
617         add_line(set([TestConfiguration('xp', 'x86', 'release', 'cpu'), TestConfiguration('xp', 'x86', 'debug', 'gpu')]), False)
618         serialized = TestExpectationSerializer.list_to_string(lines, self._converter)
619         self.assertEquals(serialized, "BUGX XP RELEASE CPU : Yay = IMAGE\nBUGX XP RELEASE : Yay = IMAGE\nBUGX XP RELEASE CPU : Yay = IMAGE\nBUGX XP DEBUG GPU : Yay = IMAGE")
620         serialized = TestExpectationSerializer.list_to_string(lines, self._converter, reconstitute_only_these=reconstitute_only_these)
621         self.assertEquals(serialized, "Nay\nBUGX XP RELEASE : Yay = IMAGE\nNay")
622
623     def test_string_whitespace_stripping(self):
624         self.assert_round_trip('\n', '')
625         self.assert_round_trip('  FOO : bar = BAZ', 'FOO : bar = BAZ')
626         self.assert_round_trip('FOO    : bar = BAZ', 'FOO : bar = BAZ')
627         self.assert_round_trip('FOO : bar = BAZ       // Qux.', 'FOO : bar = BAZ // Qux.')
628         self.assert_round_trip('FOO : bar =        BAZ // Qux.', 'FOO : bar = BAZ // Qux.')
629         self.assert_round_trip('FOO :       bar =    BAZ // Qux.', 'FOO : bar = BAZ // Qux.')
630         self.assert_round_trip('FOO :       bar     =    BAZ // Qux.', 'FOO : bar = BAZ // Qux.')
631
632
633 if __name__ == '__main__':
634     unittest.main()