Source

pida-main / tests / core / test_addtypes.py

# -*- coding: utf-8 -*-
"""
    :copyright: 2005-2008 by The PIDA Project
    :license: GPL 2 or later (see README/COPYING/LICENSE)
"""

from pida.utils.addtypes import Enumeration, PriorityList
#from pida.core.testing import test, assert_equal, assert_notequal
import pytest
class TestEnumeration:

    def test_enum(self):
        test = Enumeration('test', ('A', 'B', 'C'))
        assert test.A == 0
        assert test.B == 1
        assert test.C == 2

    def test_tupel(self):
        test = Enumeration('test', (('A', 1), ('B', 0), ('C', 10)))
        assert test.A == 1
        assert test.B == 0
        assert test.C == 10

    def test_change(self):
        test = Enumeration('test', ('A', 'B', 'C'))
        assert test.A == 0
        pytest.raises(AttributeError, setattr, test, 'A', 5)
        pytest.raises(AttributeError, setattr, test, 'X', 9)

    def test_missing(self):
        test = Enumeration('test', ('A', 'B', 'C'))
        with pytest.raises(AttributeError):
            test.D


class TestPriorityList:

    def test_list(self):
        pl = PriorityList(1, 2, 3)
        assert pl == [1, 2, 3]
        pl.sort(reverse=True)
        assert pl == [3, 2, 1]
        pl.add(4)
        assert pl == [1, 2, 3, 4]
        pl.sort(reverse=True)
        assert pl == [4, 3, 2, 1]

    def test_prio_list(self):
        pl = PriorityList(1, 2, 3, 4, 'test',
                sort_list=(2, 3, 'test', 1, 4))
        assert pl == [2, 3, 'test', 1, 4]

    def test_prio_list2(self):
        pl = PriorityList('test', 1, 2, 3, 4,
                sort_list=(2, 3, 7, 'test', 1, 4, 6))
        assert pl == [2, 3, 'test', 1, 4]
        pl.add(6)
        assert pl == [2, 3, 'test', 1, 4, 6]
        pl.add(7)
        assert pl == [2, 3, 7, 'test', 1, 4, 6]