<commit_before>unit Delphi.Mocks.Tests.Behavior;

interface

uses
  TestFramework,
  Rtti,
  Delphi.Mocks,
  Delphi.Mocks.InterfaceProxy;

type
  TTestBehaviors = class(TTestCase)
  private
    FContext : TRttiContext;
  protected
    procedure SetUp; override;

  published
    procedure Test_WillReturnBehavior_Match;
    procedure Test_WillReturnBehavior_NoMatch;
    procedure Test_WillReturnBehavior_Default;
    procedure Test_WillExecute;
  end;

implementation

uses
  SysUtils,
  Delphi.Mocks.Helpers,
  Delphi.Mocks.Interfaces,
  Delphi.Mocks.Behavior,
  classes;

{ TTestBehaviors }


procedure TTestBehaviors.SetUp;
begin
  inherited;
  FContext := TRttiContext.Create;
end;

procedure TTestBehaviors.Test_WillExecute;
var
  behavior : IBehavior;
  returnValue : TValue;
  args : TArray<TValue>;
  rType : TRttiType;
begin
  SetLength(args,1);
  args[0] := 999;
  rType := FContext.GetType(TypeInfo(string));
  behavior := TBehavior.CreateWillExecute(
    function (const args : TArray<TValue>; const returnType : TRttiType) : TValue
    begin
      result := 'hello world';
    end
    );
  returnValue := behavior.Execute(args,rType);
  CheckTrue(SameText(returnValue.AsString,'hello world'));

end;

procedure TTestBehaviors.Test_WillReturnBehavior_Default;
var
  behavior : IBehavior;
  args : TArray<TValue>;
  returnValue : TValue;
  rType : TRttiType;
begin
  returnValue := 123;
  behavior := TBehavior.CreateReturnDefault(returnValue);
  SetLength(args,3);
  args[0] := 1;
  args[1] := 2;
  args[2] := 3;
  rType := FContext.GetType(TypeInfo(Int64));
  returnValue := behavior.Execute(args,rType);
  CheckTrue(returnValue.AsInt64 = 123);
end;

procedure TTestBehaviors.Test_WillReturnBehavior_Match;
var
  behavior : IBehavior;
  args : TArray<TValue>;
  returnValue : TValue;
begin
  SetLength(args,3);
  args[0] := 1;
  args[1] := 2;
  args[2] :=  'hello';
  behavior := TBehavior.CreateWillReturnWhen(args,returnValue);
  args[0] := 1;
  args[1] := 2;
  args[2] :=  'hello';
  CheckTrue(behavior.Match(args));
end;

procedure TTestBehaviors.Test_WillReturnBehavior_NoMatch;
var
  behavior : IBehavior;
  args : TArray<TValue>;
  returnValue : TValue;
begin
  SetLength(args,3);
  args[0] := 1;
  args[1] := 2;
  args[2] := 'hello';
  behavior := TBehavior.CreateWillReturnWhen(args,returnValue);
  args[0] := 1;
  args[1] := 2;
  args[2] :=  'hello world';
  CheckFalse(behavior.Match(args));
end;

initialization
  TestFramework.RegisterTest(TTestBehaviors.Suite);
end.<commit_msg>Added Behavior WillRaise Execute tests<commit_after>unit Delphi.Mocks.Tests.Behavior;

interface

uses
  SysUtils,
  TestFramework,
  Rtti,
  Delphi.Mocks,
  Delphi.Mocks.InterfaceProxy;

type
  ETestBehaviourException = class(Exception);

  TTestBehaviors = class(TTestCase)
  private
    FContext : TRttiContext;
  protected
    procedure SetUp; override;
  published
    procedure Test_WillReturnBehavior_Match;
    procedure Test_WillReturnBehavior_NoMatch;
    procedure Test_WillReturnBehavior_Default;
    procedure Test_WillExecute;

    // WillRaise-Execute tests
    procedure CreateWillRaise_Execute__Raises_Exception_Of_Our_Choice;
    procedure CreateWillRaise_Execute__Raises_Exception_Message_Of_Our_Choice;
    procedure CreateWillRaise_Execute__Raises_No_Exception_If_Passed_Nil_For_Exception_Class;
    procedure CreateWillRaise_Execute__Raises_Exception_Of_Our_Choice_With_Default_Message;
  end;

implementation

uses
  Delphi.Mocks.Helpers,
  Delphi.Mocks.Interfaces,
  Delphi.Mocks.Behavior,
  classes;

{ TTestBehaviors }


procedure TTestBehaviors.SetUp;
begin
  inherited;
  FContext := TRttiContext.Create;
end;

procedure TTestBehaviors.Test_WillExecute;
var
  behavior : IBehavior;
  returnValue : TValue;
  args : TArray<TValue>;
  rType : TRttiType;
begin
  SetLength(args,1);
  args[0] := 999;
  rType := FContext.GetType(TypeInfo(string));

  behavior := TBehavior.CreateWillExecute(
    function (const args : TArray<TValue>; const returnType : TRttiType) : TValue
    begin
      result := 'hello world';
    end
    );

  returnValue := behavior.Execute(args,rType);

  CheckTrue(SameText(returnValue.AsString,'hello world'));
end;

procedure TTestBehaviors.Test_WillReturnBehavior_Default;
var
  behavior : IBehavior;
  args : TArray<TValue>;
  returnValue : TValue;
  rType : TRttiType;
begin
  returnValue := 123;
  behavior := TBehavior.CreateReturnDefault(returnValue);
  SetLength(args,3);
  args[0] := 1;
  args[1] := 2;
  args[2] := 3;
  rType := FContext.GetType(TypeInfo(Int64));
  returnValue := behavior.Execute(args,rType);
  CheckTrue(returnValue.AsInt64 = 123);
end;

procedure TTestBehaviors.Test_WillReturnBehavior_Match;
var
  behavior : IBehavior;
  args : TArray<TValue>;
  returnValue : TValue;
begin
  SetLength(args,3);
  args[0] := 1;
  args[1] := 2;
  args[2] :=  'hello';
  behavior := TBehavior.CreateWillReturnWhen(args,returnValue);
  args[0] := 1;
  args[1] := 2;
  args[2] :=  'hello';
  CheckTrue(behavior.Match(args));
end;

procedure TTestBehaviors.Test_WillReturnBehavior_NoMatch;
var
  behavior : IBehavior;
  args : TArray<TValue>;
  returnValue : TValue;
begin
  SetLength(args,3);
  args[0] := 1;
  args[1] := 2;
  args[2] := 'hello';
  behavior := TBehavior.CreateWillReturnWhen(args,returnValue);
  args[0] := 1;
  args[1] := 2;
  args[2] :=  'hello world';
  CheckFalse(behavior.Match(args));
end;

procedure TTestBehaviors.CreateWillRaise_Execute__Raises_Exception_Message_Of_Our_Choice;
var
  behavior: IBehavior;
const
  EXCEPTION_STRING = 'Exception!';
begin
  behavior := TBehavior.CreateWillRaise(ETestBehaviourException, EXCEPTION_STRING);

  StartExpectingException(ETestBehaviourException);

  //Passing nils as we don't care about these values for the exception
  behavior.Execute(nil, nil);

  StopExpectingException('');
end;

procedure TTestBehaviors.CreateWillRaise_Execute__Raises_Exception_Of_Our_Choice;
var
  behavior: IBehavior;
begin
  behavior := TBehavior.CreateWillRaise(ETestBehaviourException, '');

  StartExpectingException(ETestBehaviourException);

  behavior.Execute(nil, nil);

  StopExpectingException;
end;


procedure TTestBehaviors.CreateWillRaise_Execute__Raises_Exception_Of_Our_Choice_With_Default_Message;
var
  behavior: IBehavior;
const
  EXCEPTION_STRING = 'raised by mock';
begin
  behavior := TBehavior.CreateWillRaise(ETestBehaviourException, '');

  StartExpectingException(ETestBehaviourException);

  //Passing nils as we don't care about these values for the exception
  behavior.Execute(nil, nil);

  StopExpectingException(EXCEPTION_STRING);
end;

procedure TTestBehaviors.CreateWillRaise_Execute__Raises_No_Exception_If_Passed_Nil_For_Exception_Class;
var
  behavior: IBehavior;
begin
  behavior := TBehavior.CreateWillRaise(nil, '');

  //No exception coverage. Therefore we shouldn't get an exception
  behavior.Execute(nil, nil);

  //If we have gotten here no exception was recieved.
  Check(True);
end;

initialization
  TestFramework.RegisterTest(TTestBehaviors.Suite);
end.
{'fraction_non_alphanumeric': 0.10463292943692089, 'fraction_numerical': 0.012544547398431931, 'mean_word_length': 5.208849557522124, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3730606', 'n_tokens_mistral': 2438, 'n_tokens_neox': 2346, 'n_words': 453}