๐Ÿ“ฆ amake / GrandPerspective

๐Ÿ“„ SelectiveItemTest.m ยท 92 lines
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#import "SelectiveItemTest.h"

#import "FileItemTestVisitor.h"
#import "FileItem.h"


@implementation SelectiveItemTest

// Overrides designated initialiser
- (instancetype) init {
  NSAssert(NO, @"Use initWithSubItemTest:onlyFiles: instead.");
  return [self initWithSubItemTest: nil onlyFiles: YES];
}

- (instancetype) initWithSubItemTest:(FileItemTest *)subItemTest
                           onlyFiles:(BOOL)onlyFiles {
  if (self = [super init]) {
    _subItemTest = [subItemTest retain];

    _applyToFilesOnly = onlyFiles;
  }
  
  return self;
}

- (instancetype) initWithPropertiesFromDictionary:(NSDictionary *)dict {
  if (self = [super initWithPropertiesFromDictionary: dict]) {
    NSDictionary  *subTestDict = dict[@"subTest"];
    
    _subItemTest = [[FileItemTest fileItemTestFromDictionary: subTestDict] retain];
    _applyToFilesOnly = [dict[@"onlyFiles"] boolValue];
  }
  
  return self;
}

- (void) dealloc {
  [_subItemTest release];

  [super dealloc];
}


- (void) addPropertiesToDictionary:(NSMutableDictionary *)dict {
  [super addPropertiesToDictionary: dict];
  
  dict[@"class"] = @"SelectiveItemTest";
  dict[@"subTest"] = [self.subItemTest dictionaryForObject];
  dict[@"onlyFiles"] = @(self.applyToFilesOnly);
}


- (TestResult) testFileItem:(FileItem *)item context:(id) context {
  if (item.isDirectory == self.applyToFilesOnly) {
    // Test should not be applied to this type of item.
    return TEST_NOT_APPLICABLE;
  }
  
  return [self.subItemTest testFileItem: item context: context] ? TEST_PASSED : TEST_FAILED;
}

- (BOOL) appliesToDirectories {
  return !self.applyToFilesOnly;
}


- (void) acceptFileItemTestVisitor:(NSObject <FileItemTestVisitor> *)visitor {
  [visitor visitSelectiveItemTest: self];
}


- (NSString *)description {
  NSString  *format = (self.applyToFilesOnly
                       ? NSLocalizedStringFromTable(@"files: %@", @"Tests",
                                                    @"Selective test with 1: sub test")
                       : NSLocalizedStringFromTable(@"folders: %@", @"Tests",
                                                    @"Selective test with 1: sub test"));
  
  return [NSString stringWithFormat: format, self.subItemTest.description];
}


+ (FileItemTest *)fileItemTestFromDictionary:(NSDictionary *)dict { 
  NSAssert([dict[@"class"] isEqualToString: @"SelectiveItemTest"],
           @"Incorrect value for class in dictionary.");

  return [[[SelectiveItemTest alloc] initWithPropertiesFromDictionary: dict] autorelease];
}

@end