import time
import pytest
import progressbar2


def test_list():
    '''Progressbar can guess max_value automatically.'''
    p = progressbar2.ProgressBar()
    for i in p(range(10)):
        time.sleep(0.001)


def test_iterator_with_max_value():
    '''Progressbar can't guess max_value.'''
    p = progressbar2.ProgressBar(max_value=10)
    for i in p((i for i in range(10))):
        time.sleep(0.001)


def test_iterator_without_max_value_error():
    '''Progressbar can't guess max_value.'''
    p = progressbar2.ProgressBar()

    for i in p((i for i in range(10))):
        time.sleep(0.001)

    assert p.max_value is progressbar2.UnknownLength


def test_iterator_without_max_value():
    '''Progressbar can't guess max_value.'''
    p = progressbar2.ProgressBar(widgets=[
        progressbar2.AnimatedMarker(),
        progressbar2.FormatLabel('%(value)d'),
        progressbar2.BouncingBar(),
        progressbar2.BouncingBar(marker=progressbar2.RotatingMarker()),
    ])
    for i in p((i for i in range(10))):
        time.sleep(0.001)


def test_iterator_with_incorrect_max_value():
    '''Progressbar can't guess max_value.'''
    p = progressbar2.ProgressBar(max_value=10)
    with pytest.raises(ValueError):
        for i in p((i for i in range(20))):
            time.sleep(0.001)


def test_adding_value():
    p = progressbar2.ProgressBar(max_value=10)
    p.start()
    p.update(5)
    p += 2
    p.increment(2)
    with pytest.raises(ValueError):
        p += 5

