#include "tile/base/deferred.h"
#include "gtest/gtest.h"

namespace tile {
TEST(ScopedDefered, All)
{
    bool f = false;
    {
        ScopedDeferred scoped_defered([&] { f = true; });
        ASSERT_FALSE(f);
    }
    ASSERT_TRUE(f);
}

TEST(Defered, All)
{
    bool f1 = false;
    bool f2 = false;
    {
        Deferred defer([&] { f1 = true; });
        ASSERT_FALSE(f1);
        Deferred defer2([&] { f2 = true; });
        defer2.Dimiss();
        ASSERT_FALSE(f2);
    }
    ASSERT_TRUE(f1);
    ASSERT_FALSE(f2);

    bool f3 = false;
    Deferred defer3([&] { f3 = true; });
    ASSERT_FALSE(f3);
    defer3.Fire();
    ASSERT_TRUE(f3);

    bool f4 = false;
    Deferred defer5;
    {
        Deferred defer4([&] { f4 = true; });
        ASSERT_FALSE(f4);
        defer5 = std::move(defer4);
    }
    ASSERT_FALSE(f4);
    auto defer6 = std::move(defer5);
    defer5.Fire();
    ASSERT_FALSE(defer5);
    defer6.Fire();
    ASSERT_TRUE(f4);

    Deferred().Fire();
    Deferred().Dimiss();
}
}// namespace tile
