unit TestuntCardJson;
{

  Delphi DUnit Test Case
  ----------------------
  This unit contains a skeleton test case class generated by the Test Case Wizard.
  Modify the generated code to correctly setup and call the methods from the unit 
  being tested.

}

interface

uses
  TestFramework, Classes, JSON, untCard, untCardJson, SysUtils;

type
  // Test methods for class TCardJson

  TestTCardJson = class(TTestCase)
  strict private
    FCardJson: TCardJson;
  public
    procedure SetUp; override;
    procedure TearDown; override;
  published
    procedure TestCardToJson;
    procedure TestJsonToCard;
  end;
  // Test methods for class TPuzzleJson

  TestTPuzzleJson = class(TTestCase)
  strict private
    FPuzzleJson: TPuzzleJson;
  public
    procedure SetUp; override;
    procedure TearDown; override;
  published
    procedure TestPuzzleToJson;
    procedure TestJsonToPuzzle;
  end;

implementation

uses
  untArrayHelper;

procedure TestTCardJson.SetUp;
begin
  FCardJson := TCardJson.Create;
end;

procedure TestTCardJson.TearDown;
begin
  FCardJson.Free;
  FCardJson := nil;
end;

procedure TestTCardJson.TestCardToJson;
var
  ReturnValue: TJSONValue;
  ACard: ICard;
  c1, c2, c3: ICard;
begin

  c1 := TCard.Create;
  c1.SetCardId(6666);
  c2 := TCard.Create;
  c2.SetCardId(7777);
  c3 := TCard.Create;
  c3.SetCardId(8888);
  ACard := TCard.Create;
  ACard.SetCardId(1);
  ACard.setCardName('23333');
  ACard.SetEquipTo(c1);
  ACard.SetContinousEffectTo(ArrayOfCard([c2, c3]));

  // TODO: Setup method call parameters
  ReturnValue := FCardJson.CardToJson(ACard);
  // TODO: Validate method results
  Writeln(ReturnValue.ToJSON([]));
  ReturnValue.Free;
end;

procedure TestTCardJson.TestJsonToCard;
var
  ReturnValue: ICard;
  AJson: TJSONValue;
  AJsonStr: string;
  i: Integer;
begin
  AJsonStr := '{"cardId":1,"cardName":"23333","cardType":"monster","continousEffectTo":[{"cardId":7777,"cardName":"","cardType":"monster","continousEffectTo":[],"equipTo":null,"indicatorCount":0,"isLinkMonster":false,"isXyzMonster":false,"location":"deck","owner":"self","player":"self","position":"upAtk","proc":false,"seq":0,"xyzElements":[],"workAs":"none"},{"cardId":8888,"cardName":"","cardType":"monster","continousEffectTo":[],"equipTo":null,"indicatorCount":0,"isLinkMonster":false,"isXyzMonster":false,"location":"deck","owner":"self","player":"self","position":"upAtk","proc":false,"seq":0,"xyzElements":[],"workAs":"none"}],"equipTo":{"cardId":6666,"cardName":"","cardType":"monster","continousEffectTo":[],"equipTo":null,"indicatorCount":0,"isLinkMonster":false,"isXyzMonster":false,"location":"deck","owner":"self","player":"self","position":"upAtk","proc":false,"seq":0,"xyzElements":[],"workAs":"none"},"indicatorCount":0,"isLinkMonster":false,"isXyzMonster":false,"location":"deck","owner":"self","player":"self","position":"upAtk","proc":false,"seq":0,"xyzElements":[],"workAs":"none"}';
  // TODO: Setup method call parameters
  AJson := TJSONValue.ParseJSONValue(AJsonStr, True);
  ReturnValue := FCardJson.JsonToCard(AJson);
  // TODO: Validate method results
  Writeln(ReturnValue.GetCardName);
  Writeln(ReturnValue.GetContinousEffectTo.Count);
  for i := 0 to ReturnValue.GetContinousEffectTo.Count - 1 do begin
    Writeln(ReturnValue.GetContinousEffectTo[i].GetCardId);
  end;
  AJson.Free;
end;

procedure TestTPuzzleJson.SetUp;
begin
  FPuzzleJson := TPuzzleJson.Create;
end;

procedure TestTPuzzleJson.TearDown;
begin
  FPuzzleJson.Free;
  FPuzzleJson := nil;
end;

procedure TestTPuzzleJson.TestPuzzleToJson;
var
  ReturnValue: string;
  APuzzle: TPuzzleField;
  c1, c2, c3: ICard;
begin
  APuzzle := TPuzzleField.Create;
  APuzzle.PuzzleName := 'Sample';
  c1 := TCard.Create;
  c1.SetCardId(1);
  c2 := TCard.Create;
  c2.SetCardId(2);
  c3 := TCard.Create;
  c3.SetCardId(3);

  APuzzle.MCard[0, 0] := c1;
  APuzzle.P0Deck.Add(c2);
  APuzzle.P0Deck.Add(c3);
  // TODO: Setup method call parameters
  ReturnValue := FPuzzleJson.PuzzleToJson(APuzzle);
  // TODO: Validate method results

  APuzzle.Free;
  Writeln(ReturnValue);
end;

procedure TestTPuzzleJson.TestJsonToPuzzle;
var
  ReturnValue: TPuzzleField;
  AJsonString: string;
  i: Integer;
begin
  // TODO: Setup method call parameters
  AJsonString := '{"puzzleId":0,"puzzleName":"Sample","aiName":"\u53EA\u662F\u4E00\u4E2AAI","p0MCards":[{"cardId":1,"cardName":"","cardType":"monster","continousEffectTo":[],"equipTo":null,"indicatorCount":0,"isLinkMonster":false,"isXyzMonster":false,"location":"deck","owner":"self","player":"self","position":"upAtk","proc":false,"seq":0,"xyzElements":[],"workAs":"none"},null,null,null,null],"p1MCards":[null,null,null,null,null],"p0SCards":[null,null,null,null,null,null],"p1SCards":[null,null,null,null,null,null],"extraMonsters":[null,null],"p0Deck":[{"cardId":2,"cardName":"","cardType":"monster","continousEffectTo":[],"equipTo":null,"indicatorCount":0,"isLinkMonster":false,"isXyzMonster":false,"location":"deck","owner":"self","player":"self","position":"upAtk","proc":false,"seq":0,"xyzElements":[],"workAs":"none"},{"cardId":3,"cardName":"","cardType":"monster","continousEffectTo":[],"equipTo":null,"indicatorCount":0,"isLinkMonster":false,"isXyzMonster":false,"location":"deck","owner":"self","player":"self","position":"upAtk","proc":false,"seq":0,"xyzElements":[],"workAs":"none"}],"p1Deck":[],"p0ExtraDeck":[],"p1ExtraDeck":[],"p0Grave":[],"p1Grave":[],"p0Hand":[],"p1Hand":[],"p0Removed":[],"p1Removed":[],"p0life":8000,"p1life":8000,"message":"","solution":""}';
  ReturnValue := FPuzzleJson.JsonToPuzzle(AJsonString);
  // TODO: Validate method results
  Writeln(ReturnValue.PuzzleName);
  for i := 0 to ReturnValue.P0Deck.Count - 1 do begin
    Writeln(ReturnValue.P0Deck[i].GetCardId);
  end;
  ReturnValue.Free;
end;

initialization
  // Register any test cases with the test runner
  // RegisterTest(TestTCardJson.Suite);
  RegisterTest(TestTPuzzleJson.Suite);
end.

