《重构》 — Delphi示例:影片出租店程序(5、单元测试)

示例:影片出租店程序(单元测试)

代码:

unit uMovieTest;

 

interface

 

uses

    Windows,SysUtils,Classes,TestFramework,TestExtensions;

 

type

    TTest = class(TTestCase)

    published

        procedure Test;

        procedure TestHtml;

    end;

 

implementation

 

uses uMovie1;

 

procedure TTest.Test;

const

    CNT_Text = 'Rental Record for ZhangSan'#$D#$A

        + 'aaa 2'#$D#$A

        + 'bbb 6'#$D#$A

        + 'ccc 1.5'#$D#$A

        + 'Amount owed is 9.5'#$D#$A

        + 'You earned 4 frequent renter points';

var

    ACustomer: TCustomer;

    AMovie1,AMovie2,AMovie3: TMovie;

    AText: string;

begin

    AMovie1 := TMovie.Create('aaa',REGULAR);

    AMovie2 := TMovie.Create('bbb',NEW_RELEASE);

    AMovie3 := TMovie.Create('ccc',CHILDRENS);

    try

        ACustomer := TCustomer.Create('ZhangSan');

        try

            with ACustomer do

            begin

                AddRental(TRental.Create(AMovie1,1));

                AddRental(TRental.Create(AMovie2,2));

                AddRental(TRental.Create(AMovie3,3));

            end;

            AText := ACustomer.Statement;

            Check(AText = CNT_Text, 'Test fail');

        finally

            ACustomer.Free;

        end;

    finally

        AMovie1.Free;

        AMovie2.Free;

        AMovie3.Free;

    end;

end;

 

procedure TTest.TestHtml;

const

    CNT_Text = '<H1>Rental for <EM>ZhangSan</EM></H1><P>'#$D#$A

        + 'aaa:2<BR>'#$D#$A

        + 'bbb:6<BR>'#$D#$A

        + 'ccc:1.5<BR>'#$D#$A

        + '<P>You owe <EM>9.5</EM><P>'#$D#$A

        + 'On this Rental you earned <EM>4</EM> frequent renter points<P>';

var

    ACustomer: TCustomer;

    AMovie1,AMovie2,AMovie3: TMovie;

    AText: string;

begin

    AMovie1 := TMovie.Create('aaa',REGULAR);

    AMovie2 := TMovie.Create('bbb',NEW_RELEASE);

    AMovie3 := TMovie.Create('ccc',CHILDRENS);

    try

        ACustomer := TCustomer.Create('ZhangSan');

        try

            with ACustomer do

            begin

                AddRental(TRental.Create(AMovie1,1));

                AddRental(TRental.Create(AMovie2,2));

                AddRental(TRental.Create(AMovie3,3));

            end;

            AText := ACustomer.HtmlStatement;

            Check(AText = CNT_Text, 'Test fail');

        finally

            ACustomer.Free;

        end;

    finally

        AMovie1.Free;

        AMovie2.Free;

        AMovie3.Free;

    end;

end;

 

initialization

    TestFramework.RegisterTest(TTest.Suite);

 

end.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值