Exemple #1
0
    public void DisableHandlerTest()
    {
        var c = new BasicComponent();
        var e = new BasicEvent();
        var x = new EventExchange(new BasicLogExchange());

        c.AddHandler <BasicEvent>(_ => { });

        c.Attach(x);
        Assert.Equal(0, c.Handled);

        c.Receive(e, this);
        Assert.Equal(1, c.Handled);

        c.AddHandler <BasicEvent>(_ => { });
        c.Receive(e, this);
        Assert.Equal(2, c.Handled);

        c.RemoveHandler <BasicEvent>();
        c.Receive(e, this);
        Assert.Equal(2, c.Handled);

        c.AddHandler <BasicEvent>(_ => { });
        c.Receive(e, this);
        Assert.Equal(3, c.Handled);

        c.AddHandler <BasicEvent>(_ => throw new InvalidOperationException()); // Registering a handler for an event that's already handled should be a no-op
        c.Receive(e, this);
        Assert.Equal(4, c.Handled);
    }
Exemple #2
0
    public void BasicComponentTest()
    {
        var c = new BasicComponent();

        c.AddHandler <BasicEvent>(_ => { });
        var e = new BasicEvent();
        var x = new EventExchange(new BasicLogExchange());

        Assert.Equal(0, c.Handled);
        Assert.True(c.IsActive);

        c.Receive(e, this);
        Assert.Equal(0, c.Handled); // Handlers don't fire if component isn't attached

        c.Attach(x);
        c.Receive(e, this);
        Assert.Equal(1, c.Handled);

        c.Attach(x);
        c.Attach(x);
        c.Attach(x);
        c.Attach(x);
        c.Receive(e, this);
        Assert.Equal(2, c.Handled);

        c.Remove();
        c.Receive(e, this);
        Assert.Equal(2, c.Handled);

        c.Remove();
        c.Remove();
        c.Receive(e, this);
        Assert.Equal(2, c.Handled);
    }