iOS开发中的单元测试(三)——URLManager中的测试用例解析
本文转载至 http://www.cocoachina.com/cms/plus/view.php?aid=8088
- #import
- @interface UMTestCase : GHTestCase
- @end
- 代码2,定义属性// 普通字符串,带有字母和数字
- @property (strong, nonatomic) NSString *string;
- // 普通字符串,仅带有字母
- @property (strong, nonatomic) NSString *stringWithoutNumber;
- // 将被做URLEncode的字符串,含有特殊字符和汉字
- @property (strong, nonatomic) NSString *toBeEncode;
- // 把 toBeEncode 编码后的串
- @property (strong, nonatomic) NSString *encoded;
- // 普通的URL,带有QueryString
- @property (strong, nonatomic) NSURL *url;
- // 去掉上边一个URL的QueryString
- @property (strong, nonatomic) NSURL *noQueryUrl;
- // 一个普通的UIView
- @property (strong, nonatomic) UIView *view;
- (void)setUpClass
- {
- self.string = @"NSString For Test with a number 8848.";
- self.stringWithoutNumber = @"NSString For Test.";
- self.toBeEncode = @"~!@#$%^&*()_+=-[]{}:;\"‘<>.,/?123qwe汉字";
- self.encoded = @"%7E%21%40%23%24%25%5E%26%2A%28%29_%2B%3D-%5B%5D%
- 7B%7D%3A%3B%22%27%3C%3E.%2C%2F%3F123qwe%E6%B1%89%E5%AD%97";
- self.url = [NSURL URLWithString:@"http://example.com
- /patha/pathb/?p2=v2&p1=v1"];
- self.noQueryUrl = [NSURL URLWithString:@"http://example.com
- /patha/pathb/"];
- self.view = [[UIView alloc] initWithFrame:CGRectMake(10.0f,
- 10.0f, 100.0f, 100.f)];
- }
- #pragma mark - UMString
- - (void)testUMStringContainsString
- {
- NSString *p = @"For";
- NSString *np = @"BAD";
- GHAssertTrue([self.string containsString:p],
- @"\"%@\" should contains \"%@\".",
- self.string, p);
- GHAssertFalse([self.string containsString:np],
- @"\"%@\" should not contain \"%@\".",
- self.string, p);
- (void)testUrlencode
- {
- GHAssertEqualStrings([self.toBeEncode urlencode], self.encoded,
- @"URLEncode Error.",
- self.toBeEncode, self.encoded);
- GHAssertEqualStrings([self.encoded urldecode], self.toBeEncode,
- @"URLDecode Error.",
- self.encoded, self.toBeEncode);
- }
- #pragma mark - UMURL
- - (void)testAddParams
- {
- NSURL *queryUrl = [self.noQueryUrl addParams:@{@"p1":@"v1",@"p2":@"v2"}];
- HC_assertThat(queryUrl.absoluteString, HC_containsString(@"p1=v1"));
- HC_assertThat(queryUrl.absoluteString, HC_containsString(@"p2=v2"));
- }
- (void)testRemoveAllSubviews
- {
- UIView *subViewA = [[UIView alloc] init];
- UIView *subViewB = [[UIView alloc] init];
- [self.view addSubview:subViewA];
- [self.view addSubview:subViewB];
- HC_assertThat(self.view.subviews, HC_containsInAnyOrder(subViewA, subViewB, nil));
- [self.view removeAllSubviews];
- if (nil != self.view.subviews) {
- HC_assertThat(self.view.subviews, HC_empty());
- }
- }
- (void)testAddConfig
- {
- [UMNavigationController setViewControllerName:@"ViewControllerA" forURL:@"
- um://viewa2"];
- NSMutableDictionary *config = [UMNavigationController config];
- NSLog(@"%@", [config allKeys]);
- HC_assertThat([config allKeys],
- HC_containsInAnyOrder(HC_equalTo(@"um://viewa2"), HC_equalTo(@"
- um://viewa"),
- HC_equalTo(@"um://viewb"), nil));
- GHAssertEqualStrings(config[@"um://viewa2"], @"ViewControllerA",
- @"config set error.");
- }
- (BOOL)matches:(id)item
- {
- NSUInteger index = 0;
- for (id matcher in matchers)
- {
- if ([matcher matches:item])
- {
- [matchers removeObjectAtIndex:index];
- return YES;
- }
- ++index;
- }
- [[mismatchDescription appendText:@"not matched: "] appendDescriptionOf:item];
- return NO;
- }
- - (BOOL)matches:(id)collection describingMismatchTo:(id)
- mismatchDescription
- {
- if (![collection conformsToProtocol:@protocol(NSFastEnumeration)])
- {
- [super describeMismatchOf:collection to:mismatchDescription];
- return NO;
- }
- HCMatchingInAnyOrder *matchSequence =
- [[HCMatchingInAnyOrder alloc] initWithMatchers:matchers
- mismatchDescription:mismatchDescription];
- for (id item in collection)
- if (![matchSequence matches:item])
- return NO;
- return [matchSequence isFinishedWith:collection];
- }
- @implementation HCIsCollectionHavingInAnyOrder
- - (BOOL)matches:(id)collection describingMismatchTo:(id)
- mismatchDescription
- {
- if (![collection conformsToProtocol:@protocol(NSFastEnumeration)])
- {
- [super describeMismatchOf:collection to:mismatchDescription];
- return NO;
- }
- HCMatchingInAnyOrderEx *matchSequence =
- [[HCMatchingInAnyOrderEx alloc] initWithMatchers:matchers
- mismatchDescription:mismatchDescription];
- for (id item in collection)
- if (![matchSequence matches:item])
- return NO;
- return [matchSequence isFinishedWith:collection];
- }
- @end
- id HC_hasInAnyOrder(id itemMatch, ...)
- {
- NSMutableArray *matchers = [NSMutableArray arrayWithObject:HCWrapInMatcher
- (itemMatch)];
- va_list args;
- va_start(args, itemMatch);
- itemMatch = va_arg(args, id);
- while (itemMatch != nil)
- {
- [matchers addObject:HCWrapInMatcher(itemMatch)];
- itemMatch = va_arg(args, id);
- }
- va_end(args);
- return [HCIsCollectionHavingInAnyOrder isCollectionContainingInAnyOrder:matchers];
- }
- (BOOL)matches:(id)item
- {
- NSUInteger index = 0;
- BOOL matched = (0 >= [self.matchers count]);
- for (id matcher in self.matchers)
- {
- if ([matcher matches:item]) {
- [self.matchers removeObjectAtIndex:index];
- matched = YES;
- return YES;
- }
- ++index;
- }
- return matched;
- }
- (void)testAddConfig
- {
- [UMNavigationController setViewControllerName:@"ViewControllerA" forURL:@"um://
- viewa2"];
- NSMutableDictionary *config = [UMNavigationController config];
- HC_assertThat([config allKeys],
- HC_hasInAnyOrder(HC_equalTo(@"um://viewa2"), nil));
- GHAssertEqualStrings(config[@"um://viewa2"], @"ViewControllerA",
- @"config set error.");
- }
- #pragma mark - UMView
- HC_assertThat(NSStringFromCGSize(self.view.size),
- HC_equalToSize(self.view.frame.size));
- HC_assertThat(NSStringFromCGPoint(self.view.origin),
- HC_equalToPoint(CGPointMake(self.view.frame.origin.x, self.
- view.frame.origin.y)));
- #import
- OBJC_EXPORT id HC_equalToPoint(CGPoint point);
- #ifdef HC_SHORTHAND
- #define equalToPoint HC_equalToPoint
- #endif
- @interface HCIsEqualToPoint : HCBaseMatcher
- + (id)equalToPoint:(CGPoint)point;
- - (id)initWithPoint:(CGPoint)point;
- @property (nonatomic, assign) CGFloat x;
- @property (nonatomic, assign) CGFloat y;
- @end
- #import "HCIsEqualToPoint.h"
- #import
- id HC_equalToPoint(CGPoint point)
- {
- return [HCIsEqualToPoint equalToPoint:point];
- }
- @implementation HCIsEqualToPoint
- + (id)equalToPoint:(CGPoint)point
- {
- return [[self alloc] initWithPoint:point];
- }
- - (id)initWithPoint:(CGPoint)point
- {
- self = [super init];
- if (self) {
- self.x = point.x;
- self.y = point.y;
- }
- return self;
- }
- - (BOOL)matches:(id)item
- {
- if (! [item isKindOfClass:[NSString class]]) {
- return NO;
- }
- CGPoint point = CGPointFromString((NSString *)item);
- return (point.x == self.x && point.y == self.y);
- }
- - (void)describeTo:(id)description
- {
- [description appendText:@"Point not equaled."];
- }
- @end
- (void)testViewControllerForSimpleURL
- {
- self.viewControllerA = (ViewControllerA *)[self.navigator
- viewControllerForURL:
- [NSURL URLWithString:@"um://viewa"]
- withQuery:nil];
- HC_assertThat(self.viewControllerA, HC_instanceOf([UMViewController class]));
- HC_assertThat(self.viewControllerA, HC_isA([ViewControllerA class]));
- }
- - (void)testViewControllerForURLWithArgs
- {
- self.viewControllerA = (ViewControllerA *)[self.navigator
- viewControllerForURL:[NSURL URLWithString:@"um://viewa?
- p1=v1&p2=v2"]
- withQuery:nil];
- HC_assertThat(self.viewControllerA, HC_instanceOf([UMViewController class]));
- HC_assertThat(self.viewControllerA, HC_isA([ViewControllerA class]));
- HC_assertThat([self.viewControllerA.params allKeys], HC_containsInAnyOrder
- (@"p1", @"p2", nil));
- GHAssertEqualStrings(self.viewControllerA.params[@"p1"], @"v1", @"param error.");
- GHAssertEqualStrings(self.viewControllerA.params[@"p2"], @"v2", @"param error.");
- }
- - (void)testViewControllerWithQuery
- {
- self.viewControllerA = (ViewControllerA *)[self.navigator
- viewControllerForURL:
- [NSURL URLWithString:@"um://viewa"]
- withQuery:@{@"k1":@"v1", @"k2":@"v2"}];
- HC_assertThat([self.viewControllerA.query allKeys], HC_containsInAnyOrder
- (@"k1", @"k2", nil));
- GHAssertEqualStrings(self.viewControllerA.query[@"k1"], @"v1", @"param error.");
- GHAssertEqualStrings(self.viewControllerA.query[@"k2"], @"v2", @"param error.");
- }
- - (void)testViewControllerForURLAndQuery
- {
- self.viewControllerA = (ViewControllerA *)[self.navigator
- viewControllerForURL:
- [NSURL URLWithString:@"um://viewa?p1=v1&p2=v2"]
- withQuery:@{@"k1":@"v1", @"k2":@"v2"}];
- HC_assertThat([self.viewControllerA.params allKeys], HC_containsInAnyOrder
- (@"p1", @"p2", nil));
- GHAssertEqualStrings(self.viewControllerA.params[@"p1"], @"v1", @"param error.");
- GHAssertEqualStrings(self.viewControllerA.params[@"p2"], @"v2", @"param error.");
- HC_assertThat([self.viewControllerA.query allKeys], HC_containsInAnyOrder
- (@"k1", @"k2", nil));
- GHAssertEqualStrings(self.viewControllerA.query[@"k1"], @"v1", @"param error.");
- GHAssertEqualStrings(self.viewControllerA.query[@"k2"], @"v2", @"param error.");
- }
- (void)testInitWihtRootViewControllerURL
- {
- UMNavigationController *navigator = [[UMNavigationController alloc]
- initWithRootViewControllerURL:[NSURL URLWithString:@"um://viewb"]];
- HC_assertThat(navigator, HC_instanceOf([UINavigationController class]));
- HC_assertThat(navigator, HC_isA([UMNavigationController class]));
- HC_assertThat(navigator.rootViewController,
- HC_instanceOf([UMViewController class]));
- HC_assertThat(navigator.rootViewController, HC_isA([ViewControllerB class]));
- HC_assertThatInteger(navigator.viewControllers.count, HC_equalToInteger(1));
- HC_assertThat(navigator.viewControllers,
- HC_hasInAnyOrder(HC_instanceOf([UMViewController class]), nil));
- HC_assertThat(navigator.viewControllers,
- HC_hasInAnyOrder(HC_isA([ViewControllerB class]), nil));
- HC_assertThat(navigator.viewControllers,
- HC_hasInAnyOrder(HC_is(navigator.rootViewController), nil));
- }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。