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 ...
随机推荐
- linux 下 异步IO
方法一:使用fcntl来置O_ASYNC位. 这个方法的效果是,当输入缓存中的输入数据就绪时(输入数据可读),内核向用F_SETOWN来绑定的那个进程发送SIGIO信号.此时程序应该用getchar等 ...
- 计算机图形——OpenGL
荒废了太久,趁着"寒假"死磕了两周,验证了不少想法,解开了不少疑惑,代码质量当然是没有的,一切只为看到结果. 有空了再写每一项的细节. 源码地址 2019/5/12 更新 延迟渲染 ...
- GNOME 3.x下安装配置小企鹅输入法框架及SunPinYin插件
fcitx 小企鹅输入法框架已经越来越成熟,并且具备极高的性能,配合 Sun PinYin 智能输入法就和 Windows 下的搜狗百度等输入法几乎无二了.事实上,现在Linux版本的搜狗输入法正是基 ...
- Ajax的post方式提交数据
最新需要学习如何使用 POST 提交方法的接口,正好看到了Ajax 版本的感觉不错分享给大家,欢迎高手指点. <SCRIPT LANGUAGE=”javascript”> <!– f ...
- 神秘的FrontCache
用jmap -histo的时候,发现堆内存中有很多奇怪的对象,其class name为 java.util.HashMap$FrontCache 跳转到HashMap的源码中,直接搜索FrontCac ...
- BZOJ——2134: 单选错位
http://www.lydsy.com/JudgeOnline/problem.php?id=2134 Time Limit: 10 Sec Memory Limit: 259 MBSubmit: ...
- 利用例子来理解spring的面向切面编程(使用@Aspect)
上篇的例子,自动装配和自动检测Bean是使用注解的方式处理的,而面向切面编程是使用aop标签处理的,给我感觉就像中西医参合一样. 现在就来优化优化,全部使用注解的方式处理. 1.工程图:
- CodeForces - 393E Yet Another Number Sequence
Discription Everyone knows what the Fibonacci sequence is. This sequence can be defined by the recur ...
- mysql 设置默认id自增开始下标
alter table 表名 AUTO_INCREMENT 此处写你想让id从几开始增长的数字:
- 编译lua
http://www.lua.org/ 新建一个 static library 工程,把解压得到的目录下的src子目录中的所有.h和.c文件拷贝到新工程目录下. 工程中删除自动生成的 main.c 文 ...