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));
- }
iOS开发中的单元测试(三)——URLManager中的测试用例解析的更多相关文章
- iOS开发Swift篇—(三)字符串和数据类型
iOS开发Swift篇—(三)字符串和数据类型 一.字符串 字符串是String类型的数据,用双引号""包住文字内容 let website = "http://www ...
- iOS 开发问题集锦(三)
iOS 开发问题集锦(三) 介于群里大部分童鞋都是新手,为了大家能够更好的提问,并且提的问题能更好的得到回答,下面写几点提问时的注意事项: 1.认真对待你的问题,在提问题前有过认真的思考: 2.先在 ...
- iOS开发RunnLoop学习二:GCD中的定时器
#import "ViewController.h" @interface ViewController () /** 注释 */ @property (nonatomic, st ...
- iOS开发:XCTest单元测试(附上一个单例的测试代码)
测试驱动开发并不是一个很新鲜的概念了.在我最开始学习程序编写时,最喜欢干的事情就是编写一段代码,然后运行观察结果是否正确.我所学习第一门语言是c语言,用的最多的是在算法设计上,那时候最常做的事情就是编 ...
- iOS开发之窥探UICollectionViewController(三) --使用UICollectionView自定义瀑布流
上篇博客的实例是自带的UICollectionViewDelegateFlowLayout布局基础上来做的Demo, 详情请看<iOS开发之窥探UICollectionViewControlle ...
- iOS开发小技巧--TableView Group样式中控制每个section之间的距离
一.TableView的Group样式中,默认的每个section都有sectionHeader和sectionFooter,只要调整这两个的大小就可以实现section之前的间距扩大或缩小 二.项目 ...
- iOS开发系列--扩展--播放音乐库中的音乐
众所周知音乐是iOS的重要组成播放,无论是iPod.iTouch.iPhone还是iPad都可以在iTunes购买音乐或添加本地音乐到音乐 库中同步到你的iOS设备.在MediaPlayer.fram ...
- iOS开发-从16进制颜色中获取UIColor
目前iOS中设置UIColor只能使用其枚举值.RGB等方法,不能直接将常用的16进制颜色值直接转为UIColor对象,所以写了点代码,将16进制颜色值转为UIColor. 代码如下, //头文件#i ...
- iOS开发读取plist文件、iphone中plist文件的
在Xcode中建立一个iOS项目后,会自己产生一个.plist文件,点击时会看见它显示的是类似于excel表格: 但是,如果打开方式选择Source Code,你会看见它其实是一个xml文件. 我们会 ...
- IOS开发中将定时器添加到runLoop中
runLoop主要就是为线程而生的.他能够让线程在有任务的时候保持工作状态,没有任务的时候让线程处于休眠待备状态. 主线程的runloop默认是开启的.主线程上创建的定时器已经默认添加到runLoop ...
随机推荐
- bzoj 2794 [Poi2012]Cloakroom 离线+背包
题目大意 有n件物品,每件物品有三个属性a[i], b[i], c[i] (a[i]<b[i]). 再给出q个询问,每个询问由非负整数m, k, s组成,问是否能够选出某些物品使得: 对于每个选 ...
- 16深入理解C指针之---迷途指针
一.若程序中存在迷途指针,轻则导致程序退出,重则使程序出现重大逻辑错误 1.定义:内存已释放,指针依旧指向原始内存,这种指针就是迷途指针 2.迷途指针和指针别名: 1).指针依旧指向已释放的内存,无法 ...
- C++拷贝(复制)构造函数详解
原文:http://blog.csdn.net/lwbeyond/article/details/6202256/[侵删] 一. 什么是拷贝构造函数 首先对于普通类型的对象来说,它们之间的复制是很简单 ...
- Yii CActiveForm 客户端验证(enableClientValidation)和自定义验证
使用Yii的CActiveForm默认使用服务器端模型(model)的rules规则验证数据. 但这会导致无谓的请求提交,比较好的方式是为了用户体验在客户端也验证,而为了安全性,在服务器端和数据库也做 ...
- hdu 5459(递推好题)
Jesus Is Here Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 65535/102400 K (Java/Others)To ...
- Scrollview总结:滑动问题、监听Scrollview实现头部局改变
ScrollView就是一个可以滚动的View,这个滚动的方向是垂直方向的,而HorizontalScrollView则是一个水平方向的可以滚动的View. ScrollView的简单介绍 Scrol ...
- 2016-2017 ACM-ICPC Southeastern European Regional Programming Contest (SEERC 2016)
题目链接 Codefores_Gym_101164 Solved 6/11 Penalty Problem A Problem B Problem C Problem D Problem E Pr ...
- POJ 3107 Godfather (树重心)
题目链接:http://poj.org/problem?id=3107 题意: 数重心,并按从小到大输出. 思路: dfs #include <iostream> #include < ...
- 平衡树与可持久化treap
平衡树(二叉树) 线段树不支持插入or删除一个数于是平衡树产生了 常见平衡树:treap(比sbt慢,好写吧),SBT(快,比较好写,有些功能不支持),splay(特别慢,复杂度当做根号n来用,功能强 ...
- 【hibernate】Hibernate中save, saveOrUpdate, persist, merge, update 区别
Hibernate Save hibernate save()方法能够保存实体到数据库,正如方法名称save这个单词所表明的意思.我们能够在事务之外调用这个方法,这也是我不喜欢使用这个方法保存数据的原 ...