将以太坊地址添加到动态数组

时间:2018-01-27 11:19:10

标签: arrays ethereum solidity truffle

我正在尝试动态地将地址添加到具有Solidity的地址数组中。但是,当我运行松露测试时,我的测试失败了。请参阅我的测试的以下信息:

uses [...] TypInfo;
[...]
implementation[...]

const
   //  The following consts are to create the table and insert a single row
   //
   //  The difference between them is that scSqlSetUp1 specifies
   //  the size of the NonNullFieldName to 'MAX' whereas scSqlSetUp2 specifies a size of 4096

   scSqlSetUp1 =
  'CREATE TABLE [dbo].[JDTest]('#13#10
   + '  [ID] [int] NOT NULL primary key,'#13#10
   + '  [NonNullFieldName] VarChar(MAX) NOT NULL'#13#10
   + ') ON [PRIMARY]'#13#10
   + ';'#13#10
   + 'Insert JDTest (ID, [NonNullFieldName]) values (1, ''a'')'#13#10
   + ';'#13#10
   + 'SET ANSI_PADDING OFF'#13#10
   + ';';

   scSqlSetUp2 =
  'CREATE TABLE [dbo].[JDTest]('#13#10
   + '  [ID] [int] NOT NULL primary key,'#13#10
   + '  [NonNullFieldName] VarChar(4096) NOT NULL'#13#10
   + ') ON [PRIMARY]'#13#10
   + ';'#13#10
   + 'Insert JDTest (ID, [NonNullFieldName]) values (1, ''a'')'#13#10
   + ';'#13#10
   + 'SET ANSI_PADDING OFF'#13#10
   + ';';

   scSqlDropTable = 'drop table [dbo].[jdtest]';

procedure TForm1.Test1;
var
  AField : TField;
  S : String;
begin

//  Following creates the table.  The define determines the size of the NonNullFieldName

{$define UseMAX}
{$ifdef UseMAX}
  S := scSqlSetUp1;
{$else}
  S := scSqlSetUp2;
{$endif}

  ADOConnection1.Execute(S);
  try
    ADOQuery1.Open;
    try
      ADOQuery1.Edit;

      // Get explicit reference to the NonNullFieldName
      //  field to make working with it and investigating it easier

      AField := ADOQuery1.FieldByName('NonNullFieldName');

      //  The following, which requires the `TypInfo` unit in the `USES` list is to find out which exact type
      //  AField is.  Answer:  ftMemo, or ftString, depending on UseMAX.  
      //  Of course, we could get this info by inspection in the IDE
      //  by creating persistent fields

      S := GetEnumName(TypeInfo(TFieldType), Ord(AField.DataType));
      Caption := S;  // Displays `ftMemo` or `ftString`, of course

      AField.AsString:= '';
      ADOQuery1.Post; //<-- Exception raised while posting
    finally
      ADOQuery1.Close;
    end;
  finally
    //  Tidy up
    ADOConnection1.Execute(scSqlDropTable);
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  Test1;
end;

根据我的理解&#34;地址预期&#34;返回我当前的地址(我在ganache-cli的地址0上运行,在帐户1上使用元掩码(不是零索引))。 通过推动这个元素,我的期望是采用者[0] =预期。

这是我的测试结果:

function testAddPet() public {
  address expected = this;
  address[] storage adopters;
  adopters.push(expected);

  Assert.equal(adopters[0], expected, "Cannot add new pet" );
}

对我来说,测试结果并没有真正帮助我为什么它不起作用。我希望有人在这里了解我没有:)。

谢谢!

1 个答案:

答案 0 :(得分:1)

所以,我的测试工作。请参阅snippit,包括以下说明:

function testAddPet() public {
  address expected = address(this);
  address[] storage adopters;
  adopters.push(expected);

  Assert.equal(adopters[0], expected, "Incorrect adopter");
}

由于Muhammed Altabba指出“this”应该替换为“address(this)”。