1
exercism fetch objective-c robot-name

RobotNameTest.m

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
#import <XCTest/XCTest.h>

#if __has_include("RobotNameExample.h")
# import "RobotNameExample.h"
# else
# import "RobotName.h"
#endif

NS_ASSUME_NONNULL_BEGIN

@interface RobotNameTest : XCTestCase

@end

@implementation RobotNameTest

- (BOOL)string:(NSString *)string matches:(NSString *)regexString {

  NSRegularExpression *expression = [NSRegularExpression regularExpressionWithPattern:regexString
                                                                              options:NSRegularExpressionCaseInsensitive
                                                                                error:NULL];

  NSArray *matches = [expression matchesInString:string options:0 range:NSMakeRange(0,[string length])];

  return [matches count] >= 1;
}

- (void)testHasName {
  Robot *robot = [[Robot alloc] init];

  NSString *name = [robot name];
  XCTAssert([self string:name matches:@"\\A\\w{2}\\d{3}\\z"], @"%@ is not a valid robot name", name);
}

- (void)testNameSticks {
  Robot *robot = [[Robot alloc] init];
  [robot name];
  XCTAssertEqualObjects([robot name], [robot name]);
}

- (void)testDifferentRobotsHaveDifferentNames {
  Robot *firstRobot = [[Robot alloc] init];
  Robot *secondRobot = [[Robot alloc] init];
  XCTAssertNotEqualObjects([firstRobot name], [secondRobot name]);
}

- (void)testResetName {
  Robot *robot = [[Robot alloc] init];
  NSString *firstName = [robot name];
  [robot reset];
  NSString *secondName = [robot name];

  XCTAssertNotEqualObjects(firstName, secondName);
}

@end
NS_ASSUME_NONNULL_END