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 ...
随机推荐
- c#.NET的事件与委托例子
原文发布时间为:2008-07-25 -- 来源于本人的百度文章 [由搬家工具导入] using System;using System.Data;using System.Configuration ...
- jquery 日期插件
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- transform与position:fixed的那些恩怨--摘抄
1. 前言 在写这篇文章之前,我理解的fixed元素是这样的:(摘自CSS布局基础) 固定定位与absolute定位类型类似,但它的相对移动的坐标是视图(屏幕内的网页窗口)本身.由于视图本身是固定的, ...
- 转 Centos下安装apahce的configure: error: APR not found. Please read the documentation解决办法
转自: http://www.cnblogs.com/Anker/p/3355573.html 今天从Apache官网上http://httpd.apache.org/下载httpd web服务器,由 ...
- 转载 linux 僵尸进程,讲的很透彻
僵尸进程的产生和避免,以及wait,waitpid的使用 在fork()/execve()过程中,假设子进程结束时父进程仍存在,而父进程fork()之前既没安装SIGCHLD信号处理函数调用waitp ...
- NERDTree
What's NERDTree : A tree explorer plugin for vim. Install : Using Vundle on .vimrc " for ...
- 删除指定文件夹下所有的.svn文件夹
sudo find /目录 -name ".svn" -exec rm -r {} \;
- 微信小程序 之wx.getLocation()获取地理信息中的小坑
提到wx.getLocation()这个方法,大家都知道是获取地理信息的 今天用这个方法获取定位经纬度后传给后台取得附近markers标记集合, 在开发工具上都正常有标记出现 ,但是在手机测试时,死活 ...
- Codeforces 798D Mike and distribution(贪心或随机化)
题目链接 Mike and distribution 题目意思很简单,给出$a_{i}$和$b_{i}$,我们需要在这$n$个数中挑选最多$n/2+1$个,使得挑选出来的 $p_{1}$,$p_{2} ...
- awk理论详解、实战
答疑解惑: 为什么用awk取IP的时候用$4? ifconfig eth0 | awk -F '[ :]+' 'NR==2{print $4}' IP第二行内容如下: inet addr:10.0.0 ...