LoginSignup
8
8

More than 5 years have passed since last update.

『レガシーコード改善ガイド』の仕様化テストを書いてみた

Last updated at Posted at 2013-07-22

仕様化テストを書いてみました。ツッコミどころがあれば、よろしくお願いします。

PopupPanelViewTest
#import "PopupPanelViewTest.h"
#import "PopupPanelView.h"

@implementation PopupPanelViewTest.m

// デフォルトはNOですが、UIのテストやメインスレッドに依存するテストを行う場合はYESにします。
- (BOOL)shouldRunOnMainThread {
    return NO;
}

// 本クラスが実行される前に呼び出されます。
- (void)setUpClass {
    NSString* a = @"first item";
    NSString* b = @"second item";
    NSString* c = @"third item";
    NSArray* items = [NSArray arrayWithObjects:a, b, c, nil];
    _view = [[PopupPanelView alloc] initWithFrame:CGRectMake(0, 0, 120, 10) andItems:items inView:nil];
}

- (void)testBorderWidth
{
    GHAssertEquals(_view.layer.borderWidth, (CGFloat)2.0, @"ふちのラインの太さは2である");
}

- (void)testCornerRadius
{
    GHAssertEquals(_view.layer.cornerRadius, (CGFloat)5.0, @"ふちのラインの角丸半径は5.0である");
}

- (void)testClipsToBounds
{
    GHAssertEquals(_view.clipsToBounds, YES, @"本ビューの中のテーブルビューが本ビューからはみ出した場合、トリムされる。");
}

- (void)testBorderColor
{
    GHAssertEquals(_view.layer.borderColor, [[UIColor blueColor] CGColor], @"ふちのラインの色は青である");
}

@end

ビューの仕様化テストはこんな感じ?

PopupPanelViewVerifyTest.m

#import "PopupPanelViewVerifyTest.h"
#import "PopupPanelView.h"

@implementation PopupPanelViewVerifyTest

// デフォルトはNOですが、UIのテストやメインスレッドに依存するテストを行う場合はYESにします。
- (BOOL)shouldRunOnMainThread {
    return NO;
}

// 本クラスが実行される前に呼び出されます。
- (void)setUpClass {
    NSString* a = @"first item";
    NSString* b = @"second item";
    NSString* c = @"third item";
    NSString* d = @"fourth item";
    NSString* e = @"fifth item";
    NSString* f = @"sixth item";
    NSString* g = @"seventh item";
    NSString* h = @"eighth item";
    NSString* i = @"nineth item";
    NSString* j = @"tenth item";
    _items = [NSArray arrayWithObjects:a, b, c, d, e, f, g, h, i, j, nil];
}

/*
 PopupPanelViewは、矩形情報と配列と親ビューを引数にして初期化される。
 配列の中身が文字列の項目として下に並ぶ。
 矩形情報の部分に描画される。
 矩形情報の高さは、フォントより小さい場合は随時高くなる。
 矩形情報の横幅が狭くて入りきらない文字は「...」と省略されて表示される。
 本ビューの中にテーブルビューが入る。
 テーブルビューのdelegate、dataSourceプロパティは自身(PopupPanelView)になる
 テーブルのセルが選択されるとselectFuncメソッドが呼ばれ、
 その中で親ビュー(PDFScrollView)のremovePopupViewメソッドが呼ばれる。
 そこで選択された項目の文字列をいくつかの定型文字列と比較して処理を行う。
 */
- (void)testInit
{
    PopupPanelView *view = [[PopupPanelView alloc] initWithFrame:CGRectMake(0, 0, 120, 10) andItems:_items inView:nil];
    view.frame = [UIScreen mainScreen].bounds;
    GHVerifyView(view);
}

/*
 PopupPanelViewの初期化の引数である矩形情報のはじめのxyは、
 親ビューに対するオフセットとなる。
 例えばxyが(100, 300)であるとき、
 親ビューの(100, 300)から右下へ向かってPopupPanelViewがつくられる。
 ただし、親ビューの下側の外へはみ出てしまう場合は
 (100, 300)の右上に向かってPopupPanelViewがつくられる。
 */
- (void)testAddedByScrollView
{
    UIScrollView *view = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 1000, 1000)];
    view.contentSize = CGSizeMake(200, 1000);
    PopupPanelView *childView = [[PopupPanelView alloc] initWithFrame:CGRectMake(100, 300, 120, 10) andItems:_items inView:view];
    [view addSubview:childView];
    view.frame = [UIScreen mainScreen].bounds;
    GHVerifyView(view);
}

/*
 ただし、親ビューの下側の外へはみ出てしまう場合は
 (100, 300)の右上に向かってPopupPanelViewがつくられる。
 */
- (void)testAddedByScrollViewAndMadeToRightUpward
{
    UIScrollView *view = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
    view.contentSize = CGSizeMake(200, 1000);
    PopupPanelView *childView = [[PopupPanelView alloc] initWithFrame:CGRectMake(100, 300, 120, 10) andItems:_items inView:view];
    [view addSubview:childView];
    view.frame = [UIScreen mainScreen].bounds;
    GHVerifyView(view);
}

@end

ブログやってます:PAPA-tronix !

8
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
8