1. Unit-testing in same file

Is it possible to test function in same myfunc.e, not in separate t_myfunc.e? For example, I need Luhn algorithm for verifying credit cards.

http://rosettacode.org/wiki/Luhn_test#Euphoria

When this file will be started, it will check an algorithm correctness. It that is necessary for me. But if I import this file to the program, the constant cc_numbers is imported and the for-cycle will be executed, which are absolutely not necessary to me. Besides it would be desirable not to lose possibility of the automated testing. By the way, Python is able to do such trick by means of checking how the file is started - as the separate program or as the imported function:

if __name__ == "__main__" 
Hope you understand what I mean. Sorry for my ugly machine-translated english.

P.S. It would be absolutely amazing, if Euphoria would have something like Python's doctests. Here is how it could look for Luhn algorithm:

function luhn(sequence cc) 
>>> "49927398716"  --  Output effect of this input value should be... 
1                  --  ...such! 
>>> "49927398717" 
0 
>>> "1234567812345678" 
0 
>>> "1234567812345670" 
1 
-- Other part of function. 

new topic     » topic index » view message » categorize

2. Re: Unit-testing in same file

It is possible to test whether your file is executed directly instead of imported by checking the result of the function command_line().

The second element of the sequence returned by this function is the file that was executed.
If the second element of the result ends with your file name it was executed directly, not imported.

new topic     » goto parent     » topic index » view message » categorize

3. Re: Unit-testing in same file

SnakeCharmer said...

Is it possible to test function in same myfunc.e, not in separate t_myfunc.e? For example, I need Luhn algorithm for verifying credit cards.

http://rosettacode.org/wiki/Luhn_test#Euphoria

When this file will be started, it will check an algorithm correctness. It that is necessary for me. But if I import this file to the program, the constant cc_numbers is imported and the for-cycle will be executed, which are absolutely not necessary to me. Besides it would be desirable not to lose possibility of the automated testing.

The easiest way to do this is to put your code inside of an ifdef block:

-- mylib.e 
ifdef UNITTEST then 
  -- eutest mylib.e 
 
  -- tests go here 
end ifdef 

Then, under normal operation, the tests are skipped (the code isn't even parsed). But it's all there when you want to test it. When you call eutest, it automatically adds a "-d UNITTEST" to the command line when it invokes your program.

Matt

new topic     » goto parent     » topic index » view message » categorize

4. Re: Unit-testing in same file

Thanks, I will be played with it tomorrow. Today I was too tired. One more question, the last for today.

Let's say I want to create big program. How to distribute functions between files? I correctly understand the most ideologically certain way? Each function (or a faithful set of mutually dependent functions) in the separate file. Test for each function in the separate file.

func1.e
func2.e
t_func1.e
t_func2.e

Or my way has the right to existence too? How you do TDD? I don't want to invent a bicycle. I am a beginner both in Euphoria and in testing.

new topic     » goto parent     » topic index » view message » categorize

5. Re: Unit-testing in same file

A monolithic program with multiple includes is quite feasible and executable in Euphoria. Even Matt's own wxEuphoria can be considered monolithic. When I use it with about 20 files of my own 200 or so functions, with all files included at the beginning, there are no problems.

Namespaces were invented at the time when programmers were finding it difficult to give unique names to their functions. With due respect to the efforts of Euphoria programmers who use and are trying to convert everything to namespaces, I feel it is not necessary. Certainly the CPUs and the memory in the computers of today far exceeds what you can normal throw at them.

During development, I use the technique of calling all functions by their full names i.e. nm1:fn7 nm5:fn9 etc. I then use word processors to identify duplicate function names and apply corrections.

Perhaps, having been taught by teachers of the old school, I am old-fashioned, but a monolithic program calling functions in 20 different files in the same sub-folder works with me, using a larger amount of comments within each file and function.

Just my two-pence – no, two-cent's worth!

new topic     » goto parent     » topic index » view message » categorize

6. Re: Unit-testing in same file

I don't protest against monolithic style. My program will have long source code, but not so complicated structure. Code folding should simplify still. Which editor you use? I hesitate between EditPlus and E-TextEditor. Both support Euphoria, but the price differs in 2 times. Certainly, it is possible to try trial versions, but I already lost a lot of time and I want to start to write my code since Monday.

And how you write tests? They are in single file too?

new topic     » goto parent     » topic index » view message » categorize

7. Re: Unit-testing in same file

I would normally rework such code as:

constant cc_numbers = { 
    "49927398716",1, 
    "49927398717",0, 
    "1234567812345678",0, 
    "1234567812345670",1 
} 
  
for i = 1 to length(cc_numbers) by 2 do 
    if luhn(cc_numbers[i])!=cc_numbers[i+1] then ?9/0 end if 
end for 

and just leave it on permanently. If (and only if) I noticed a performance lag, I would add

constant TEST=0 
if TEST then 
  ... 
end if 

Pete

new topic     » goto parent     » topic index » view message » categorize

8. Re: Unit-testing in same file

My first attempt to use unit-testing. What's wrong?

happy.e

include misc.e 
function IsHappy(integer x) 
  integer Happy = 0 
  if x > 10 then 
    sequence Cyphers = sprint(x) 
    integer Capacity = length(Cyphers) 
    if remainder(Capacity, 2) = 0 then 
      Capacity /= 2 
      for i = 1 to Capacity do	 
        Happy += Cyphers[i]	 
        Happy -= Cyphers[Capacity + i] 
      end for	 
      if Happy = 0 then	 
        Happy = 1	 
      else	 
        Happy = 0	 
      end if	 
    end if 
  end if 
  return Happy 
end function 

t_happy.e

include happy.e 
include std/unittest.e 
constant Tickets = {-11, 0, 11, 23, 4444, 0101, 50505, 505050, 173056} 
constant Answers = {0, 0, 1, 0, 1, 0, 0, 0, 1} 
sequence Message 
integer Expected, Outcome 
for i = 1 to length(Tickets) do 
  Message = "Testing Happy Ticket " & i 
  Expected = Answers[i] 
  Outcome = IsHappy(Tickets[i]) 
  test_equal(Message, Expected, Outcome) 
end for 
test_report() 

eutest:

interpreting t_happy.e: 
C:\WORK\t_happy.e:12 
<0074>:: Errors resolving the following references: 
    'IsHappy' (t_happy.e:12) has not been declared. 
 
    Outcome = IsHappy(Tickets[i]) 
                     ^ 
 
FAILURE: t_happy.e EUPHORIA error with status 1 
 
Test results summary: 
    FAIL: t_happy.e 
Files (run: 1) (failed: 1) (0% success) 

new topic     » goto parent     » topic index » view message » categorize

9. Re: Unit-testing in same file

SnakeCharmer said...

My first attempt to use unit-testing. What's wrong?

IsHappy() needs to be declared as either public or export (or global, from pre-4.0) so that it becomes visible outside the scope of happy.e.

happy.e

include misc.e 
public function IsHappy(integer x) 

-Greg

new topic     » goto parent     » topic index » view message » categorize

10. Re: Unit-testing in same file

Thanks, it works!

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu