1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
|
//
// lager - library for functional interactive c++ programs
// Copyright (C) 2017 Juan Pedro Bolivar Puente
//
// This file is part of lager.
//
// lager is free software: you can redistribute it and/or modify
// it under the terms of the MIT License, as detailed in the LICENSE
// file located at the root of this source code distribution,
// or here: <https://github.com/arximboldi/lager/blob/master/LICENSE>
//
#include <catch2/catch.hpp>
#include <lager/state.hpp>
TEST_CASE("watch before assign")
{
auto c = lager::cursor<int>{};
auto called = 0;
auto value = -1;
watch(c, [&](auto x) {
++called;
value = x;
});
auto s = lager::state<int, lager::automatic_tag>(42);
c = s;
CHECK(called == 0);
CHECK(value == -1);
s.set(5);
CHECK(called == 1);
CHECK(value == 5);
CHECK(c.get() == 5);
c = lager::state<int, lager::automatic_tag>(2);
c.set(4);
CHECK(called == 2);
CHECK(value == 4);
CHECK(c.get() == 4);
}
TEST_CASE("nudge")
{
auto c = lager::state<int>(42);
auto called = 0;
auto value = -1;
c.watch([&](auto x) {
++called;
value = x;
}).nudge();
CHECK(called == 1);
CHECK(value == 42);
}
TEST_CASE("bind")
{
auto c = lager::state<int>(42);
auto called = 0;
auto value = -1;
c.bind([&](auto x) {
++called;
value = x;
});
CHECK(called == 1);
CHECK(value == 42);
}
TEST_CASE("assignment doesn't change signal bindings")
{
lager::state<int, lager::automatic_tag> data1;
lager::state<int, lager::automatic_tag> data2;
lager::reader<int> reader = data1;
int bind1_times_called = 0;
reader.bind([&bind1_times_called] (int i) { bind1_times_called++;});
CHECK(bind1_times_called == 1);
data1.set(42);
CHECK(bind1_times_called == 2);
reader = data2;
// data1 is not connected anymore!
data1.set(43);
CHECK(bind1_times_called == 2);
// but data2 is!
data2.set(44);
CHECK(bind1_times_called == 3);
int bind2_times_called = 0;
reader.bind([&bind2_times_called] (int i) { bind2_times_called++;});
CHECK(bind2_times_called == 1);
data2.set(46);
CHECK(bind1_times_called == 4);
CHECK(bind2_times_called == 2);
}
TEST_CASE("reader::unbind")
{
lager::state<int, lager::automatic_tag> data1;
lager::reader<int> reader = data1;
int bind1_times_called = 0;
reader.bind([&bind1_times_called] (int i) { bind1_times_called++;});
CHECK(bind1_times_called == 1);
data1.set(42);
CHECK(bind1_times_called == 2);
reader.unbind();
data1.set(43);
CHECK(bind1_times_called == 2);
}
|