Forum Discussion

domik's avatar
domik
Occasional Contributor
15 years ago

How to write Classes/DataObjects generator using Script Extensions

I didn't found any information that would help me do something like this.



I would like
to develop by myself or maybe use any existing extension that would be a
wizard for creating an ODT.Classes

I have code like above and what I would like to do is to create a wizard that would generate code like above right to Script Unit.



The steps I'm thinking of are:


  1. Form for editing values like ClassName, application that this Class applies to (combobox)

  2. Next form would let me point an object in application which will be added to class. I would give this object name according to what this object do in application.

  3. I would like to take properties of this object and put into Class that I'm just creating.

  4. Messagebox that would ask "Do you want to add next property of class ?"

  5. Some empty methods for whole class like DefaultMethod or something


TestedObjectParentPath - FullName of the object parent.

TestedObjectPath  -  FullName of the tested object.



Because I doesn't see any way to store object inside Class or Data Object I want to evaluate this strings to have an object on which I will be executing methods. So question is - is there any easy way to change something like this:



                  Sys.Process('Application').VCLObject('MainForm')



to correct string object which in delphiscript would look like this one



                  'Sys.Process(''''Application'''').VCLObject(''''MainForm'''') '



FindValue - if something would go wrong while evaluating object I can try to find it using parent.FindChildren() - currently I'm using ObjectIdentifier proprerty



ObjClassName - ObjectType property



Does any one have done something similar ? Any tips to creating such code by wizard ?





function CreateClassObject;

var Obj;



begin

  // Declares new classes

  ODT.Data.Clear;

  ODT.Classes.Clear;

  ODT.Classes.Declare('BaseObject');

  Obj := ODT.Classes.TcObject;

    // Adds two properties to the class

  Obj.AddProperty('TestedObjectParentPath', 0);

  Obj.AddProperty('FindProperty', 'ObjectIdentifier');

  Obj.AddProperty('FindValue', '');

 

  Obj.AddProperty('TestedObjectPath', 0);

  Obj.AddProperty('ObjClassName', 0);



  Obj.AddProperty('Value', '');

  Obj.AddMethod('FindObject','Classes.FindObject');

  Obj.AddMethod('GetObjClassName','Classes.GetObjClassName');

  Obj.AddMethod('DefaultOperation','Classes.DefaultOperation');

 



end;



function CreateClassLogin;

var LoginObj, ClassInstance, PropNum;

begin

  DeleteGroupByName('Login');

  DeleteClassByName('Login');

  // Declares new classes

  ODT.Classes.Declare('Login');

  LoginObj := ODT.Classes.Login;

 

  LoginObj.AddPropOfClassType('_wndfrmLogin', 'BaseObject');

  LoginObj.AddPropOfClassType('_edttLogin', 'BaseObject');

  LoginObj.AddPropOfClassType('_edtPassword', 'BaseObject');

  LoginObj.AddPropOfClassType('_btnLogin', 'BaseObject');

end;



function CreateObjectLogin;

  var Group, VarClass;

begin

  DeleteGroupByName('Login');

  // Creates a new data group

  Group := ODT.Data.AddGroup('Login');

  // Adds new variables to the created data group

  VarClass := Group.AddVarOfClassType('LoginTest', 'Login');

end;





procedure FillOdtLoginProperties();

var LoginTestObj;

begin



LoginTestObj:= ODT.Data.Login.LoginTest;

LoginTestObj._wndfrmLogin.TestedObjectParentPath:=ODT.Data.Enviroment.Enviroment._CurrentTestedApplication;

LoginTestObj._wndfrmLogin.FindValue:='wndfrmLogin';



LoginTestObj._edttLogin.TestedObjectParentPath:= LoginTestObj._wndfrmLogin.TestedObjectParentPath;

LoginTestObj._edttLogin.FindValue:='edtUserName';

//LoginTestObj._edtLogin. TestedObjectPath := '';

LoginTestObj._edtLogin.Methods['DefaultOperationMethod'].Enabled := true;



LoginTestObj._edtPassword.TestedObjectParentPath :=LoginTestObj._wndfrmLogin.TestedObjectParentPath;;

LoginTestObj._edtPassword.FindValue:='edtPassword';

//LoginTestObj.edtPassword.TestedObjectPath :='';

LoginTestObj.edtPassword.Methods['DefaultOperationMethod'].Enabled := true;



LoginTestObj._btnLogin.TestedObjectParentPath :=LoginTestObj._wndfrmLogin.TestedObjectParentPath;;

LoginTestObj._btnLogin.FindValue:='btnLogin';

//LoginTestObj.btnLogin.TestedObjectPath:='';

LoginTestObj._btnLogin.Methods['DefaultOperationMethod'].Enabled := true;



end;



procedure FillOdtLoginValues();

var LoginTestObj;

begin

  LoginTestObj:= ODT.Data.Login.LoginTest;

  LoginTestObj._edtLogin.Value:='login';

  LoginTestObj._edtPassword.Value:='password';

end;





procedure OdtLoginTestRun();

begin

ODT.Data.Login.Run;

end;


3 Replies

  • domik's avatar
    domik
    Occasional Contributor
    Hello Alex,



    I think that you misunderstand me. I know that I can't use ODT objects by script extensions. What I would like to do is a code generator.



    So after clicking/writing in some kind of wizard - the code similar to the one posted above would by placed directly in script unit.



    This could work exactly like any other script extension. The only difference would be that there would be generated huge amount of code (text) with procedures etc. just to be able to generate classes and objects programatically.



    Basically instead generating simple



    if something then

    something



    I want to generate



    Procedure generate_classes;

    var

    ..

    begin

    ..

    ..

    ..

    end;



    function do_something;

    begin

    ...

    end;



    Procedure another_generated_procedure;

    var

    ..

    begin

    ..

    ..

    ..

    end;







    Dominik.

  • Hello Dominik,





    If you are going to use your wizard for generating DelphiScript code only, I would recommend that you create a script extension that will make use of the CodeEditor object. This object enables you to add any text to a script unit. Also, in the script extension, you can use the aqString object and invoke its Quote and Replace methods to process strings like Sys.Process('Application').VCLObject('MainForm') and make them valid DelphiScript string constants.





    BTW, you asked whether anyone had ever done something similar. I remember such a case: Several years ago Robert Leahey created the ODT Declaration Generator plug-in for TestComplete 4 that analyzed a class defined in the ODT panel and generated script code recreating that class programmatically. Here is the link to the blog post.