Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract Event Filtering to its own class
- Loading branch information
1 parent
886b08e
commit 4198612
Showing
5 changed files
with
147 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// | ||
// EventFilter.h | ||
// Travis CI | ||
// | ||
// Created by Henrik Hodne on 12/1/12. | ||
// Copyright (c) 2012 Travis CI GmbH. All rights reserved. | ||
// | ||
|
||
#import <Foundation/Foundation.h> | ||
|
||
@class RACSignal; | ||
@class FilterPreferences; | ||
|
||
@interface EventFilter : NSObject | ||
|
||
// A stream of filtered `TravisEvent`. | ||
@property (nonatomic, strong, readonly) RACSignal *outputStream; | ||
|
||
// Creates a new event filter. | ||
// | ||
// inputStream - A stream of `TravisEvent` objects to filter. | ||
// filterPreferences - A `FilterPreferences` object describing how to filter the incoming `TravisEvent` objects. | ||
+ (EventFilter *)eventFilterWithInputStream:(RACSignal *)inputStream filterPreferences:(FilterPreferences *)filterPreferences; | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// | ||
// EventFilter.m | ||
// Travis CI | ||
// | ||
// Created by Henrik Hodne on 12/1/12. | ||
// Copyright (c) 2012 Travis CI GmbH. All rights reserved. | ||
// | ||
|
||
#import "EventFilter.h" | ||
#import <ReactiveCocoa/ReactiveCocoa.h> | ||
#import <libextobjc/EXTScope.h> | ||
#import "BuildEvent.h" | ||
#import "FilterPreferences.h" | ||
|
||
@interface EventFilter () | ||
@property (nonatomic, strong, readonly) RACSignal *inputStream; | ||
@property (nonatomic, strong, readonly) FilterPreferences *filterPreferences; | ||
@end | ||
|
||
@implementation EventFilter | ||
|
||
- (RACSignal *)outputStream { | ||
@weakify(self); | ||
return [[self inputStream] filter:^(BuildEvent *event) { | ||
@strongify(self); | ||
return [[self filterPreferences] matchesSlug:[event name]]; | ||
}]; | ||
} | ||
|
||
#pragma mark - Lifecycle | ||
|
||
+ (EventFilter *)eventFilterWithInputStream:(RACSignal *)inputStream filterPreferences:(FilterPreferences *)filterPreferences { | ||
return [[self alloc] initWithInputStream:inputStream filterPreferences:filterPreferences]; | ||
} | ||
|
||
- (id)initWithInputStream:(RACSignal *)inputStream filterPreferences:(FilterPreferences *)filterPreferences { | ||
self = [super init]; | ||
if (self == nil) return nil; | ||
|
||
_inputStream = inputStream; | ||
_filterPreferences = filterPreferences; | ||
|
||
return self; | ||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// | ||
// EventFilterTests.m | ||
// Travis CI | ||
// | ||
// Created by Henrik Hodne on 12/1/12. | ||
// Copyright (c) 2012 Travis CI GmbH. All rights reserved. | ||
// | ||
|
||
#import <SenTestingKit/SenTestingKit.h> | ||
|
||
#define HC_SHORTHAND | ||
#import <OCHamcrest/OCHamcrest.h> | ||
|
||
#define MOCKITO_SHORTHAND | ||
#import <OCMockito/OCMockito.h> | ||
|
||
#import "EventFilter.h" | ||
#import "BuildEvent.h" | ||
#import "FilterPreferences.h" | ||
#import <ReactiveCocoa/ReactiveCocoa.h> | ||
|
||
@interface EventFilterTests : SenTestCase | ||
@end | ||
|
||
@implementation EventFilterTests { | ||
EventFilter *_eventFilter; | ||
RACSubject *_inputStream; | ||
FilterPreferences *_filterPreferences; | ||
id<RACSubscriber> _outputStream; | ||
} | ||
|
||
- (void)setUp { | ||
_inputStream = [RACSubject subject]; | ||
_outputStream = mockProtocol(@protocol(RACSubscriber)); | ||
_filterPreferences = mock([FilterPreferences class]); | ||
_eventFilter = [EventFilter eventFilterWithInputStream:_inputStream filterPreferences:_filterPreferences]; | ||
[[_eventFilter outputStream] subscribe:_outputStream]; | ||
} | ||
|
||
#pragma mark - Tests | ||
|
||
- (void)testDoesNotRemoveMatchingSlugs { | ||
[given([_filterPreferences matchesSlug:@"travis-ci/travis-ci"]) willReturnBool:YES]; | ||
|
||
BuildEvent *event = [[BuildEvent alloc] initWithEventData:@{ @"repository": @{ @"slug": @"travis-ci/travis-ci" } }]; | ||
[_inputStream sendNext:event]; | ||
|
||
[verify(_outputStream) sendNext:event]; | ||
} | ||
|
||
- (void)testRemovesNonMatchingSlugs { | ||
[given([_filterPreferences matchesSlug:@"travis-ci/travis-ci"]) willReturnBool:NO]; | ||
|
||
BuildEvent *event = [[BuildEvent alloc] initWithEventData:@{ @"repository": @{ @"slug": @"travis-ci/travis-ci" } }]; | ||
[_inputStream sendNext:event]; | ||
|
||
[verifyCount(_outputStream, never()) sendNext:event]; | ||
} | ||
|
||
@end |