<reponame>curoky/my-own-x
# Copyright (c) 2018-2022 curoky(<EMAIL>).
#
# This file is part of my-own-x.
# See https://github.com/curoky/my-own-x for further info.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from .count_down import Countdown


class TestCountDown(object):

    def test_limit(self):
        cd = Countdown.makeWithSize(10)
        assert cd.left() == 10
        cd.loop()
        assert cd.left() == 9
        cd.loop(8)
        assert cd.left() == 1
        cd.loop(1)
        assert cd.left() == 10
        cd.loop(11)
        assert cd.left() == 10

    def test_callback(self):
        cb_limit = 3

        def cb():
            nonlocal cb_limit
            cb_limit -= 1
            return cb_limit

        cd = Countdown.makeWithCallback(cb)
        assert cd.left() == 2
        cd.loop(2)
        assert cd.left() == 1

    def test_loop_callback(self):
        called = False

        def do_call():
            nonlocal called
            called = True

        cd = Countdown.makeWithSize(10)
        cd.loop(10, do_call)
        assert cd.left() == 10
        assert called == True

# Copyright (c) 2018-2022 curoky(<EMAIL>).
#
# This file is part of my-own-x.
# See https://github.com/curoky/my-own-x for further info.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from .count_down import Countdown


class TestCountDown(object):

    def test_limit(self):
        cd = Countdown.makeWithSize(10)
        assert cd.left() == 10
        cd.loop()
        assert cd.left() == 9
        cd.loop(8)
        assert cd.left() == 1
        cd.loop(1)
        assert cd.left() == 10
        cd.loop(11)
        assert cd.left() == 10

    def test_callback(self):
        cb_limit = 3

        def cb():
            nonlocal cb_limit
            cb_limit -= 1
            return cb_limit

        cd = Countdown.makeWithCallback(cb)
        assert cd.left() == 2
        cd.loop(2)
        assert cd.left() == 1

    def test_loop_callback(self):
        called = False

        def do_call():
            nonlocal called
            called = True

        cd = Countdown.makeWithSize(10)
        cd.loop(10, do_call)
        assert cd.left() == 10
        assert called == True
# Copyright (c) 2018-2022 curoky(<EMAIL>). # # This file is part of my-own-x. # See https://github.com/curoky/my-own-x for further info. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # #     http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License.