# How to implement this in unit testing

**URL:** <https://www.ruby-forum.com/t/how-to-implement-this-in-unit-testing/138241>\
**Category:** Ruby\
**Created:** [May 21, 2008, 9:35am UTC](https://www.ruby-forum.com/t/how-to-implement-this-in-unit-testing/138241 "2008-05-21T09:35:53Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Muath\_ASKhalaf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c89c15/32.png) [@Muath\_ASKhalaf](https://www.ruby-forum.com/u/Muath_ASKhalaf)\
**Post date:** [May 21, 2008, 9:35am UTC](https://www.ruby-forum.com/t/how-to-implement-this-in-unit-testing/138241/1 "2008-05-21T09:35:53Z")

</div>

Hi,  
If I want to implement something like this in Ruby Unit Testing:  
test\_z\_lessthan\_20  
x = choose(1…5)  
y = choose(0…3)  
z = x \* y  
assert(z \< 20)  
end\_choice  
end\_choice  
end

The idea here is to run the previous test for all x and y values. My  
initial idea is to parse previous code and generate something like  
this:  
test\_z\_lessthan\_20  
[1…5].each do |x|  
[0…3].each do |y|  
z = x \* y  
assert(z \< 20)  
end  
end  
end  
My question is what is the best way to parse previous code i.e. is  
there a parser where I will just change small parts of it to pasre the  
choose part.  
Also is there a way to implement the previous code dynamically during  
runtime or at least do the parsing and change on the fly instead of  
creating some code statically.

Thanks

---

<div class="post-metadata">

**Author:** ![Muath\_ASKhalaf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c89c15/32.png) [@Muath\_ASKhalaf](https://www.ruby-forum.com/u/Muath_ASKhalaf)\
**Post date:** [May 21, 2008, 10:25am UTC](https://www.ruby-forum.com/t/how-to-implement-this-in-unit-testing/138241/2 "2008-05-21T10:25:22Z")

</div>

On May 21, 12:34 am, “Muath A. Khalaf” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> end_choice
> end
> end
> 
> ```
> 
> end  
> My question is what is the best way to parse previous code i.e. is  
> there a parser where I will just change small parts of it to pasre the  
> choose part.  
> Also is there a way to implement the previous code dynamically during  
> runtime or at least do the parsing and change on the fly instead of  
> creating some code statically.
> 
> Thanks

just small change as [1…5].each does not work correctly so I may  
change it to [1,2,3,4,5].each

---

<div class="post-metadata">

**Author:** ![Muath\_ASKhalaf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c89c15/32.png) [@Muath\_ASKhalaf](https://www.ruby-forum.com/u/Muath_ASKhalaf)\
**Post date:** [May 21, 2008, 3:20pm UTC](https://www.ruby-forum.com/t/how-to-implement-this-in-unit-testing/138241/3 "2008-05-21T15:20:06Z")

</div>

On Wed, May 21, 2008 at 4:25 AM, ethica1 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> just small change as [1…5].each does not work correctly so I may  
> change it to [1,2,3,4,5].each

You can use a range instead of an array, e.g., (1…5).each { |n| puts n  
} .

Craig
