Beispiel #1
0
def test_iteration(various_pdims):
    """Tests whether the iteration over the span's state works."""
    pd = ParamDim(default=0, values=[1, 2, 3])

    # Should start in default state
    assert pd.state == 0

    # First iteration
    assert pd.__next__() == 1
    assert pd.__next__() == 2
    assert pd.__next__() == 3
    with pytest.raises(StopIteration):
        pd.__next__()

    # Should be able to iterate again
    assert pd.__next__() == 1
    assert pd.__next__() == 2
    assert pd.__next__() == 3
    with pytest.raises(StopIteration):
        pd.__next__()

    # State should be reset to 0 now
    assert pd.state == 0

    # And as a loop
    for _ in pd:
        continue
Beispiel #2
0
def test_mask():
    """Test that masking works"""
    # Test initialization, property getter and setter, and type
    pd = ParamDim(default=0, values=[1, 2, 3, 4], mask=False)
    assert pd.mask is False
    # NOTE not trivial to test because the .mask getter _computes_ the value
    assert not any([isinstance(v, Masked) for v in pd.values])

    pd.mask = True
    assert pd.mask is True
    assert all([isinstance(v, Masked) for v in pd.values])

    # Check the string representation of masked values
    assert str(pd.values[0]).find("<1>") > -1
    assert str(pd).find("Masked object, value:") > -1

    # Now to a more complex mask
    pd.mask = (True, False, True, False)
    assert pd.mask == (True, False, True, False)

    # Test the properties that inform about the number of masked and unmasked
    # values
    assert len(pd) == 2
    assert pd.num_masked == 2

    # Setting one with a bad length should not work
    with pytest.raises(ValueError, match="container of same length as the"):
        pd.mask = (True, False)

    # Check that iteration starts at first unmasked state
    pd.enter_iteration()
    assert pd.state == 2
    assert pd.current_value == 2

    # Iterate one step, this should jump to index and value 3
    assert pd.__next__() == 4
    assert pd.state == 4

    # Setting the state manually to something masked should not work
    with pytest.raises(MaskedValueError, match="Value at index 1 is masked"):
        pd.state = 1

    # No further iteration should be possible for this one
    with pytest.raises(StopIteration):
        pd.iterate_state()
    assert pd.state == 0

    # Check iteration again
    assert list(pd) == [2, 4]

    # For fully masked, the default value should be returned. Eff. length: 1
    pd.mask = True
    assert len(pd) == 1
    assert pd.num_masked == pd.num_states - 1
    assert list(pd) == [0]

    # Try using a slice to set the mask
    pd.mask = slice(2)
    assert pd.mask == (True, True, False, False)

    pd.mask = slice(2, None)
    assert pd.mask == (False, False, True, True)

    pd.mask = slice(None, None, 2)
    assert pd.mask == (True, False, True, False)