等价类划分的应用 2
- 在上一个等价类划分 demo 的基础上由一个 textField 变为三个
- 将上次的检验函数 isValid 更改为由正则表达式判断的 textCheckWithRegax 函数
- 正则表达式 [A-Za-z0-9]{1,6}
- 测试过程中三个皆不符合时没有提示信息,检查半天发现把 if 里的判断条件 "=="写成了"&&".......犯了低级错误.......
if (validity1 == false && validity2 && false && validity3 == false){
message = [NSString stringWithFormat:@"三个输入框均不符合要求"];
}
- 测试用例
- |编号|第一个输入框 |第二个输入框|第三个输入框|输出|
|-|-|-|-|-|
|1|null|null|null|三个输入框均不符合要求|
|2|abc|123|ab112|三个输入框均符合要求|
|3|abc..|123|ab112|第一个输入框不符合要求|
|4|abc|123..|ab112|第二个输入框不符合要求|
|5|abc|123|ab112.|第三个输入框不符合要求|
|6|abcadlsfkja|123|ab112|第一个输入框不符合要求|
|7|abcadlsfkja|12312192 |ab112|只有第三个输入框符合要求|
|8|abcadlsfkja|12312192|ab112sfav|三个输入框均不符合要求|
测试结果截图
//
// ViewController.m
// testApp
//
// Created by trigger on 15/3/22.
// Copyright (c) 2015年 trigger. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@property (strong, nonatomic) IBOutlet UITextField *textBox3;
@property (strong, nonatomic) IBOutlet UITextField *textBox2;
@property (strong, nonatomic) IBOutlet UITextField *textBoxText;
- (IBAction)buttonPress:(id)sender;
@end
@implementation ViewController
@synthesize textBoxText;
@synthesize textBox2;
@synthesize textBox3;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self textCheckWithRegex:@""];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)buttonPress:(id)sender {
BOOL validity1, validity2, validity3;
validity1 = [self textCheckWithRegex:textBoxText.text];
validity2 = [self textCheckWithRegex:textBox2.text];
validity3 = [self textCheckWithRegex:textBox3.text];
NSString *message;
if (validity1 == false && validity2 == false && validity3 == false){
message = [NSString stringWithFormat:@"三个输入框均不符合要求"];
}
else if(validity1 == true && validity2 == true && validity3 == true){
message = [NSString stringWithFormat:@"三个输入框均符合要求"];
}
else if(validity1 == false && validity2 == true && validity3 == true){
message = [NSString stringWithFormat:@"第一个输入框不符合要求"];
}
else if(validity1 == true && validity2 == false && validity3 == true){
message = [NSString stringWithFormat:@"第二个输入框不符合要求"];
}
else if(validity1 == true && validity2 == true && validity3 == false){
message = [NSString stringWithFormat:@"第三个输入框不符合要求"];
}
else if(validity1 == true && validity2 == false && validity3 == false){
message = [NSString stringWithFormat:@"只有第一个输入框符合要求"];
}
else if(validity1 == false && validity2 == true && validity3 == false){
message = [NSString stringWithFormat:@"只有第二个输入框符合要求"];
}
else if(validity1 == false && validity2 == false && validity3 == true){
message = [NSString stringWithFormat:@"只有第三个输入框符合要求"];
}
if ([textBoxText.text isEqualToString:@""] && [textBox2.text isEqualToString:@""] && [textBox3.text isEqualToString:@""]){
message = [NSString stringWithFormat:@"三个输入框均不符合要求"];
}
[[[UIAlertView alloc]initWithTitle:@"等价类检查" message:message delegate: nil cancelButtonTitle:@"好的" otherButtonTitles:nil, nil]show];
}
- (BOOL)textCheckWithRegex:(NSString*)str{
NSString *searchText = [NSString stringWithString:str];
NSError *error = NULL;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"[A-Za-z0-9]{1,6}" options:NSRegularExpressionCaseInsensitive error:&error];
NSTextCheckingResult *result = [regex firstMatchInString:searchText options:0 range:NSMakeRange(0, [searchText length])];
searchText = [searchText substringWithRange:result.range];
if ([str isEqualToString:searchText] && ![str isEqualToString:@""]) {
return true;
}
else{
return false;
}
}
@end