コード例 #1
0
def test_assert_with_dependencies(message):
    with pf.Graph() as graph:
        x = pf.placeholder(name='x')
        if message:
            assertion = pf.assert_(x < 10, message, 10, x)
        else:
            assertion = pf.assert_(x < 10)
        with pf.control_dependencies([assertion]):
            y = 2 * x

    assert len(y.dependencies) == 1
    assert graph(y, x=9) == 18
    with pytest.raises(AssertionError) as exc_info:
        graph(y, x=11)

    if message:
        exc_info.match(message % (10, 11))
コード例 #2
0
def test_assert_with_value():
    with pf.Graph() as graph:
        x = pf.placeholder(name='x')
        assertion = pf.assert_(x < 10, value=2 * x)

    assert graph(assertion, x=9) == 18
    with pytest.raises(AssertionError):
        graph(assertion, x=11)
コード例 #3
0
def test_try_callback():
    with pf.Graph() as graph:
        a = pf.placeholder('a')
        b = pf.assert_((a > 0).set_name('condition'), value=a, name='b')
        c = pf.try_(
            b, [(AssertionError,
                 (pf.constant(41, name='41') + 1).set_name('alternative'))])

    tracer = pf.Profiler()
    graph(c, {a: 3}, callback=tracer) == 3
    assert len(tracer.times) == 3

    graph(c, {a: -2}, callback=tracer) == 42
    assert len(tracer.times) == 5