Sun May 19, 2019 8:22 pm
Login Register Lost Password? Contact Us


Override a Function of a Module

Questions around writing code and queries

Thu May 02, 2019 10:50 pm Change Time Zone

Hi there,

I would like to know if it is possible to override a function of a non-Virtual Module.
Take the following code as example:

Code: Select all
EXPORT myModuleWithAFunction := MODULE
  EXPORT myFunction(UNSIGNED numA, UNSIGNED numB) := FUNCTION
    RETURN numA + numB
  END;
END;


Now, suppose you need to mock this module and you should override the `myFunction` function. I tried something like this, but since myModuleWithAFunction is not a virtual module, I cannot override the myFunction.

Code: Select all
myMockedFunc(UNSIGNED a) := FUNCTION
  RETURN MODULE($.myModuleWithAFunction)
    EXPORT myFunction := a;
  END;
END;


I tried with `SHARED myFunction := a`, but the error persist. So, the question is:
- Is it possible to override a function inside a non-virtual module? If yes, how to do it.
- If not, how would you test/mock the return of this function?

Obs.: I tried to simplify the problem to focus in the real error, which is overriding a function. The real code is much more complex, with several calls to other functions and services, the returning value is more complex too. The thing is.. All these complex calling functions is not scope of my code, so I just want to mock a return value and go on.


Thanks in advance,

Artur Baruchi
abaruchi
 
Posts: 8
Joined: Thu Apr 18, 2019 4:50 pm

Fri May 03, 2019 11:30 am Change Time Zone

Hi abaruchi,

Depends upon what you want to achieve with a 'mock up'?
I myself and others in my team never try to mock up anything, but exercise unaltered production ready code.

We do this by having different banks of data (logical files/superfiles), which are all accessed via the same 'File' attributes. The MODULE encapsulating said attributes has an ENUM parameter something like:

Code: Select all
RunType := ENUM(UNSIGNED1,UNIT_TEST,QA,CERT,PRODUCTION);
Files(Runtype rt) := MODULE
...


Then the same code gets exercised with UNIT_TEST data as will live production data.

Your 'mock up' should be 'mock up' data, not mocked up functions?

Perhaps I've got the wrong end of the stick.
Yours
Allan
Allan
 
Posts: 363
Joined: Sat Oct 01, 2011 7:26 pm

Fri May 03, 2019 12:12 pm Change Time Zone

Hello Allan,

Thanks for your reply.
We do have mock data. What happens in my situation, is that this function that Im trying to mock, get some data from a service that returns a SOAPCALL and I don't want to perform any calls to a service outside my code. Im able to mock the dataset returned by this function, but when Im trying to override this function to return that mocked dataset, I got an error saying that Im not able to override a non-virtual module.

Thanks!

- Artur Baruchi
abaruchi
 
Posts: 8
Joined: Thu Apr 18, 2019 4:50 pm

Fri May 03, 2019 1:10 pm Change Time Zone

Hi,

Read up VIRTUAL MODULEs in the ECL Ref manual.
Your virtual MODULE can define the interface, then you have two CONCRETE MODULES inheriting from it.

The fact you mention 'non-virtual' means you already know about 'VIRTUAL' in ECL so why can't you use this functionality?
Yours
Allan

Example of Use:
Code: Select all
AbstractMod := MODULE,VIRTUAL
     EXPORT V1 := 42;
     EXPORT STRING V2;
END;

Mod1 := MODULE(AbstractMod)
    EXPORT STRING V2 := 'Mod1';
END;

Mod2 := MODULE(AbstractMod)
    EXPORT STRING V2 := 'Mod2';
END;

{INTEGER V1,STRING V2} F(AbstractMod m) := FUNCTION
     RETURN  ROW({m.V1,m.V2},{INTEGER V1,STRING V2});
END;

F(Mod1);
F(Mod2);
Allan
 
Posts: 363
Joined: Sat Oct 01, 2011 7:26 pm

Fri May 03, 2019 8:05 pm Change Time Zone

Hi Allan,

Thanks for your reply. Actually your message gave me an idea that worked :D

I read again the ECL documentation and it is possible to specify a single definition as VIRTUAL. So using my first code as reference, would be something like this:

Code: Select all
EXPORT myModuleWithAFunction := MODULE
  EXPORT VIRTUAL myFunction(UNSIGNED numA, UNSIGNED numB) := FUNCTION
    RETURN numA + numB
  END;
END;


It was a good solution, since I could keep the code with minimal changes.

Thanks,

Artur Baruchi
abaruchi
 
Posts: 8
Joined: Thu Apr 18, 2019 4:50 pm


Return to Programming

Who is online

Users browsing this forum: No registered users and 2 guests

cron