blade / tests / test_xcmp.py

# -*- coding: utf-8 -*-
'''blade comparison tests.'''

import platform

PYPY = platform.python_implementation() == 'PyPy'

from stuf.six import unittest


class TestXCmp(unittest.TestCase):

    def test_xall(self):
        from blade.xcmp import xall
        self.assertFalse(xall([True, 1, None, 'yes'], bool))
        self.assertTrue(xall([2, 4, 6, 8], lambda x: x % 2 == 0))

    def test_xany(self):
        from blade.xcmp import xany
        self.assertTrue(xany([None, 0, 'yes', False], bool))
        self.assertTrue(xany([1, 4, 5, 9], lambda x: x % 2 == 0))

    def test_xdiff(self):
        from blade.xcmp import xdiff
        self.assertEqual(
            xdiff(([1, 2, 3, 4, 5], [5, 2, 10], [10, 11, 2])), set([1, 3, 4]),
        )
        self.assertEqual(
            xdiff(([1, 3, 4, 5], [5, 2, 10], [10, 11, 2]), True),
            set([3, 1, 11, 4] if PYPY else [1, 3, 4, 11]),
        )

    def test_xintersect(self):
        from blade.xcmp import xintersect
        self.assertEqual(
            xintersect(([1, 2, 3], [101, 2, 1, 10], [2, 1])), set([1, 2]),
        )

    def test_xunion(self):
        from blade.xcmp import xunion
        self.assertEqual(
            xunion(([1, 2, 3], [101, 2, 1, 10], [2, 1])),
            set([10, 1, 2, 3, 101] if PYPY else [1, 10, 3, 2, 101]),
        )

    def test_xunique(self):
        from blade.xcmp import xunique
        self.assertEqual(
            list(xunique([1, 2, 1, 3, 1, 4])), [1, 2, 3, 4]
        )
        self.assertEqual(
            list(xunique([1, 2, 1, 3, 1, 4], round)), [1, 2, 3, 4],
        )
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.