File: test_compare.py

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (96 lines) | stat: -rw-r--r-- 4,048 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# test_compare.py - Unit tests for xctest_checker.compare -*- python -*-
#
# This source file is part of the Swift.org open source project
#
# Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
# Licensed under Apache License v2.0 with Runtime Library Exception
#
# See http://swift.org/LICENSE.txt for license information
# See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors

import tempfile
import unittest

from xctest_checker import compare
from xctest_checker.error import XCTestCheckerError


def _tmpfile(content):
    """Returns the path to a temp file with the given contents."""
    tmp = tempfile.mkstemp()[1]
    with open(tmp, 'w') as f:
        f.write(content)
    return tmp


class CompareTestCase(unittest.TestCase):
    def test_no_match_raises(self):
        actual = _tmpfile('foo\nbar\nbaz\n')
        expected = _tmpfile('c: foo\nc: baz\nc: bar\n')
        with self.assertRaises(XCTestCheckerError):
            compare.compare(open(actual, 'r'), expected, check_prefix='c: ')

    def test_too_few_expected_raises_and_first_line_in_error(self):
        actual = _tmpfile('foo\nbar\nbaz\n')
        expected = _tmpfile('c: foo\nc: bar\n')
        with self.assertRaises(XCTestCheckerError) as cm:
            compare.compare(open(actual, 'r'), expected, check_prefix='c: ')

        self.assertIn('{}:{}'.format(expected, 1), str(cm.exception))

    def test_too_many_expected_raises_and_excess_check_line_in_error(self):
        actual = _tmpfile('foo\nbar\n')
        expected = _tmpfile('c: foo\nc: bar\nc: baz\n')
        with self.assertRaises(XCTestCheckerError) as cm:
            compare.compare(open(actual, 'r'), expected, check_prefix='c: ')

        self.assertIn('{}:{}'.format(expected, 3), str(cm.exception))

    def test_match_does_not_raise(self):
        actual = _tmpfile('foo\nbar\nbaz\n')
        expected = _tmpfile('c: foo\nc: bar\nc: baz\n')
        compare.compare(open(actual, 'r'), expected, check_prefix='c: ')

    def test_match_with_inline_check_does_not_raise(self):
        actual = _tmpfile('bling\nblong\n')
        expected = _tmpfile('meep meep // c: bling\nmeep\n// c: blong\n')
        compare.compare(open(actual, 'r'), expected, check_prefix='// c: ')

    def test_check_prefix_twice_in_the_same_line_raises_with_line(self):
        actual = _tmpfile('blorp\nbleep\n')
        expected = _tmpfile('c: blorp\nc: bleep c: blammo\n')
        with self.assertRaises(XCTestCheckerError) as cm:
            compare.compare(open(actual, 'r'), expected, check_prefix='c: ')

        self.assertIn('{}:{}'.format(expected, 2), str(cm.exception))

    def test_check_prefix_in_run_line_ignored(self):
        actual = _tmpfile('flim\n')
        expected = _tmpfile('// RUN: xctest_checker --prefix "c: "\nc: flim\n')
        compare.compare(open(actual, 'r'), expected, check_prefix='c: ')

    def test_includes_file_name_and_line_of_expected_in_error(self):
        actual = _tmpfile('foo\nbar\nbaz\n')
        expected = _tmpfile('c: foo\nc: baz\nc: bar\n')
        with self.assertRaises(XCTestCheckerError) as cm:
            compare.compare(open(actual, 'r'), expected, check_prefix='c: ')

        self.assertIn("{}:{}:".format(expected, 2), str(cm.exception))

    def test_matching_ignores_leading_and_trailing_whitespace(self):
        actual = _tmpfile('foo\nbar\nbaz\n')
        expected = _tmpfile('c:  foo\nc: bar \nc: baz\n')
        compare.compare(open(actual, 'r'), expected, check_prefix='c:')

    def test_can_explicitly_match_leading_and_trailing_whitespace(self):
        actual = _tmpfile('foo\n bar\nbaz \n')
        expected = _tmpfile('c: foo\nc: ^ bar \nc: baz $\n')
        compare.compare(open(actual, 'r'), expected, check_prefix='c:')

    def test_line_number_substitution(self):
        actual = _tmpfile('beep 1\nboop 5\n')
        expected = _tmpfile('c: beep [[@LINE]]\nc: boop [[@LINE+3]]')
        compare.compare(open(actual, 'r'), expected, check_prefix='c: ')

if __name__ == "__main__":
    unittest.main()