How can I create a RubyRuleContext for testing purpose

View: New views
6 Messages — Rating Filter:   Alert me  

How can I create a RubyRuleContext for testing purpose

by Philipp Marcus :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,

what is the correct way to create an RubyRuleContext and to fill it with
structure that is given by a certain ruby file? I`d would be very
helpful if someone could give me a little code-snipped showing how to
create a correct RubyRuleContext instance. I need this for testing my
methods in my Ruby module.

Best Regards
    Philipp Marcus

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@...
For additional commands, e-mail: dev-help@...


Re: How can I create a RubyRuleContext for testing purpose

by Tor Norbye :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Sep 1, 2008, at 7:01 AM, Philipp Marcus wrote:

> Hi,
>
> what is the correct way to create an RubyRuleContext and to fill it  
> with structure that is given by a certain ruby file? I`d would be  
> very helpful if someone could give me a little code-snipped showing  
> how to create a correct RubyRuleContext instance. I need this for  
> testing my methods in my Ruby module.

Hi Philipp,
I'm not sure what you're trying to do, but there should already be a  
lot of support for testing quickfixes. You just need to extend  
HintTestBase in the ruby.hints/test/ folder. In a separate module,  
that means adding some <test-dependencies> in your module's layer  
file. Take a look at the ruby.extrahints module which does this  
(though it doesn't currently have any extra hints in it, but it does  
set up all the dependencies, and all you have to do then is the same  
thing as the existing hints in ruby.hints/test -- extend HintTestBase,  
and then call some of the hint-testing methods like findHints,  
applyHint etc.   Basically, there's a lot of stuff going on to create  
all the context you need - locating the test files, parsing them to  
create an AST, opening the document and creating the rule context and  
finally calling your rules.

If you're trying to do something different than what the existing  
hints testing infrastructure sets up for you (ability to check hint  
registration, ability to find all the hints in a file, ability to find  
hint/suggestion for a given caret position, ability to apply a  
specific fix for a specific caret line), then let me know - perhaps we  
can add it rather than you having to add a bunch of generic  
infrastructure on your end.

-- Tor

>
>
> Best Regards
>   Philipp Marcus
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@...
> For additional commands, e-mail: dev-help@...
>


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@...
For additional commands, e-mail: dev-help@...


Re: How can I create a RubyRuleContext for testing purpose

by Philipp Marcus :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi Tor,

I`m developing a module for Ruby on Rails applications that detects if a
ruby on rails model class references a database table that doesn`t exist
in the database. My module returns NodeType.ROOTNODE in the getKinds
method and therefore I have to walk through the AST on my own. I rather
want to do this in an extra method than in the run method - and call my
new method in the unit tests. As you see - the typicall testing
procedure with .rb files and goldenfiles doesn`t help for my case as I
depend on the database content too.
For checking out how to walk through the AST and for testing my methods
I need to call this method with a well defined RubyRuleContext. That`s
the reason I have to know how to create a RubyRuleContext e.g. out of a
given .rb file.
Thanks and Best Regards
    Philipp Marcus



> On Sep 1, 2008, at 7:01 AM, Philipp Marcus wrote:
>
>> Hi,
>>
>> what is the correct way to create an RubyRuleContext and to fill it
>> with structure that is given by a certain ruby file? I`d would be
>> very helpful if someone could give me a little code-snipped showing
>> how to create a correct RubyRuleContext instance. I need this for
>> testing my methods in my Ruby module.
>
> Hi Philipp,
> I'm not sure what you're trying to do, but there should already be a
> lot of support for testing quickfixes. You just need to extend
> HintTestBase in the ruby.hints/test/ folder. In a separate module,
> that means adding some <test-dependencies> in your module's layer
> file. Take a look at the ruby.extrahints module which does this
> (though it doesn't currently have any extra hints in it, but it does
> set up all the dependencies, and all you have to do then is the same
> thing as the existing hints in ruby.hints/test -- extend HintTestBase,
> and then call some of the hint-testing methods like findHints,
> applyHint etc.   Basically, there's a lot of stuff going on to create
> all the context you need - locating the test files, parsing them to
> create an AST, opening the document and creating the rule context and
> finally calling your rules.
>
> If you're trying to do something different than what the existing
> hints testing infrastructure sets up for you (ability to check hint
> registration, ability to find all the hints in a file, ability to find
> hint/suggestion for a given caret position, ability to apply a
> specific fix for a specific caret line), then let me know - perhaps we
> can add it rather than you having to add a bunch of generic
> infrastructure on your end.
>
> -- Tor
>
>>
>>
>> Best Regards
>>   Philipp Marcus
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: dev-unsubscribe@...
>> For additional commands, e-mail: dev-help@...
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@...
> For additional commands, e-mail: dev-help@...
>


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@...
For additional commands, e-mail: dev-help@...


Re: How can I create a RubyRuleContext for testing purpose

by Tor Norbye :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Sep 2, 2008, at 11:16 AM, Philipp Marcus wrote:

> Hi Tor,
>
> I`m developing a module for Ruby on Rails applications that detects  
> if a ruby on rails model class references a database table that  
> doesn`t exist in the database. My module returns NodeType.ROOTNODE  
> in the getKinds method and therefore I have to walk through the AST  
> on my own. I rather want to do this in an extra method than in the  
> run method - and call my new method in the unit tests. As you see -  
> the typicall testing procedure with .rb files and goldenfiles  
> doesn`t help for my case as I depend on the database content too.
> For checking out how to walk through the AST and for testing my  
> methods I need to call this method with a well defined  
> RubyRuleContext. That`s the reason I have to know how to create a  
> RubyRuleContext e.g. out of a given .rb file.

Sounds interesting!

I'm not following exactly what it is you're trying to do from your  
tests, but I would think that from a unit test perspective, you would  
want to mock your database connections in some way?

In any case, a RubyRuleContext is created by the ruby hints manager.  
If you need to, feel free to create testing-only trampoline accessors  
in there (e.g. a public class, with public methods, accessing  
protected/package protected/private methods, and the public class is  
only intended for testing, and is preferably (if possible) in the unit  
testing folder instead of the sources folder).

-- Tor

>
> Thanks and Best Regards
>   Philipp Marcus
>
>
>
>> On Sep 1, 2008, at 7:01 AM, Philipp Marcus wrote:
>>
>>> Hi,
>>>
>>> what is the correct way to create an RubyRuleContext and to fill  
>>> it with structure that is given by a certain ruby file? I`d would  
>>> be very helpful if someone could give me a little code-snipped  
>>> showing how to create a correct RubyRuleContext instance. I need  
>>> this for testing my methods in my Ruby module.
>>
>> Hi Philipp,
>> I'm not sure what you're trying to do, but there should already be  
>> a lot of support for testing quickfixes. You just need to extend  
>> HintTestBase in the ruby.hints/test/ folder. In a separate module,  
>> that means adding some <test-dependencies> in your module's layer  
>> file. Take a look at the ruby.extrahints module which does this  
>> (though it doesn't currently have any extra hints in it, but it  
>> does set up all the dependencies, and all you have to do then is  
>> the same thing as the existing hints in ruby.hints/test -- extend  
>> HintTestBase, and then call some of the hint-testing methods like  
>> findHints, applyHint etc.   Basically, there's a lot of stuff going  
>> on to create all the context you need - locating the test files,  
>> parsing them to create an AST, opening the document and creating  
>> the rule context and finally calling your rules.
>>
>> If you're trying to do something different than what the existing  
>> hints testing infrastructure sets up for you (ability to check hint  
>> registration, ability to find all the hints in a file, ability to  
>> find hint/suggestion for a given caret position, ability to apply a  
>> specific fix for a specific caret line), then let me know - perhaps  
>> we can add it rather than you having to add a bunch of generic  
>> infrastructure on your end.
>>
>> -- Tor
>>
>>>
>>>
>>> Best Regards
>>>  Philipp Marcus
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: dev-unsubscribe@...
>>> For additional commands, e-mail: dev-help@...
>>>
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: dev-unsubscribe@...
>> For additional commands, e-mail: dev-help@...
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@...
> For additional commands, e-mail: dev-help@...
>


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@...
For additional commands, e-mail: dev-help@...


Re: How can I create a RubyRuleContext for testing purpose

by Philipp Marcus :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi Tor,

thanks for your reply! I`ve tried to add those trampoline accessors but
my problem is, that I don`t exactly know which packages and classes you
are referencing in your last post. You`ve written about "ruby hints
manager" but what is the exact class name of the class I need? My goal
is to open a .rb file and create a RubyRuleContext object out of it.

Thanks and Best Regards
    Philipp Marcus


Tor Norbye schrieb:

> On Sep 2, 2008, at 11:16 AM, Philipp Marcus wrote:
>
>> Hi Tor,
>>
>> I`m developing a module for Ruby on Rails applications that detects
>> if a ruby on rails model class references a database table that
>> doesn`t exist in the database. My module returns NodeType.ROOTNODE in
>> the getKinds method and therefore I have to walk through the AST on
>> my own. I rather want to do this in an extra method than in the run
>> method - and call my new method in the unit tests. As you see - the
>> typicall testing procedure with .rb files and goldenfiles doesn`t
>> help for my case as I depend on the database content too.
>> For checking out how to walk through the AST and for testing my
>> methods I need to call this method with a well defined
>> RubyRuleContext. That`s the reason I have to know how to create a
>> RubyRuleContext e.g. out of a given .rb file.
>
> Sounds interesting!
>
> I'm not following exactly what it is you're trying to do from your
> tests, but I would think that from a unit test perspective, you would
> want to mock your database connections in some way?
>
> In any case, a RubyRuleContext is created by the ruby hints manager.  
> If you need to, feel free to create testing-only trampoline accessors
> in there (e.g. a public class, with public methods, accessing
> protected/package protected/private methods, and the public class is
> only intended for testing, and is preferably (if possible) in the unit
> testing folder instead of the sources folder).
>
> -- Tor
>
>>
>> Thanks and Best Regards
>>   Philipp Marcus
>>
>>
>>
>>> On Sep 1, 2008, at 7:01 AM, Philipp Marcus wrote:
>>>
>>>> Hi,
>>>>
>>>> what is the correct way to create an RubyRuleContext and to fill it
>>>> with structure that is given by a certain ruby file? I`d would be
>>>> very helpful if someone could give me a little code-snipped showing
>>>> how to create a correct RubyRuleContext instance. I need this for
>>>> testing my methods in my Ruby module.
>>>
>>> Hi Philipp,
>>> I'm not sure what you're trying to do, but there should already be a
>>> lot of support for testing quickfixes. You just need to extend
>>> HintTestBase in the ruby.hints/test/ folder. In a separate module,
>>> that means adding some <test-dependencies> in your module's layer
>>> file. Take a look at the ruby.extrahints module which does this
>>> (though it doesn't currently have any extra hints in it, but it does
>>> set up all the dependencies, and all you have to do then is the same
>>> thing as the existing hints in ruby.hints/test -- extend
>>> HintTestBase, and then call some of the hint-testing methods like
>>> findHints, applyHint etc.   Basically, there's a lot of stuff going
>>> on to create all the context you need - locating the test files,
>>> parsing them to create an AST, opening the document and creating the
>>> rule context and finally calling your rules.
>>>
>>> If you're trying to do something different than what the existing
>>> hints testing infrastructure sets up for you (ability to check hint
>>> registration, ability to find all the hints in a file, ability to
>>> find hint/suggestion for a given caret position, ability to apply a
>>> specific fix for a specific caret line), then let me know - perhaps
>>> we can add it rather than you having to add a bunch of generic
>>> infrastructure on your end.
>>>
>>> -- Tor
>>>
>>>>
>>>>
>>>> Best Regards
>>>>  Philipp Marcus
>>>>
>>>> ---------------------------------------------------------------------
>>>> To unsubscribe, e-mail: dev-unsubscribe@...
>>>> For additional commands, e-mail: dev-help@...
>>>>
>>>
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: dev-unsubscribe@...
>>> For additional commands, e-mail: dev-help@...
>>>
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: dev-unsubscribe@...
>> For additional commands, e-mail: dev-help@...
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@...
> For additional commands, e-mail: dev-help@...
>


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@...
For additional commands, e-mail: dev-help@...


Re: How can I create a RubyRuleContext for testing purpose

by Tor Norbye :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Sep 4, 2008, at 10:49 AM, Philipp Marcus wrote:

> Hi Tor,
>
> thanks for your reply! I`ve tried to add those trampoline accessors  
> but my problem is, that I don`t exactly know which packages and  
> classes you are referencing in your last post. You`ve written about  
> "ruby hints manager" but what is the exact class name of the class I  
> need? My goal is to open a .rb file and create a RubyRuleContext  
> object out of it.

There is actually a LOT of machinery you'll have to involve to make  
this happen. A RuleContext should for example have a CompilationInfo  
object, which points off to a Document, a ParserResult (and in  
particular a RubyParserResult), a set of parser error or warning  
objects (if any), and so on.  The unit testing infrastructure which is  
there already does this for you.  Make sure your unit test extends  
HintTestBase (in ruby.hints). Then all you need to do is call for  
example "checkHints()" and pass in your source file name (which is a  
file which should reside in your test/unit/data/testfiles/ folder).

You can go into the superclasses (HintTestBase, which leads up to  
RubyTestBase, which leads up to GsfTestBase) to see how it goes about  
invoking a parser, opening documents, creating a rule context etc. The  
RuleContext is created from GsfTestBase. This delegates to the Ruby  
module.

If you look at RubyHintsProvider, there is a createRuleContext()  
method which does a "new RubyRuleContext". That's where you could add  
your own accessor. Note however that you need to also populate the  
fields of the superclass of RubyRuleContext (RuleContext). That's  
normally done by the infrastructure after it calls the language-
specific createRuleContext() method.

In short, it will be far from trivial to try to do all this stuff  
manually in your own unit test, but if you follow the trail from  
HintTestBase and up you'll see all the code that is involved to set up  
everything if you want to do it that way.  But it seems to me that it  
would be better to try to subclass some of this stuff in stead; e.g.  
in your own TestCase class (subclass of HintTestBase) try to set up a  
mock of the database or do whatever else it is you need to do to make  
your database happy in the unit test context.

-- Tor




>
>
> Thanks and Best Regards
>   Philipp Marcus
>
>
> Tor Norbye schrieb:
>> On Sep 2, 2008, at 11:16 AM, Philipp Marcus wrote:
>>
>>> Hi Tor,
>>>
>>> I`m developing a module for Ruby on Rails applications that  
>>> detects if a ruby on rails model class references a database table  
>>> that doesn`t exist in the database. My module returns  
>>> NodeType.ROOTNODE in the getKinds method and therefore I have to  
>>> walk through the AST on my own. I rather want to do this in an  
>>> extra method than in the run method - and call my new method in  
>>> the unit tests. As you see - the typicall testing procedure  
>>> with .rb files and goldenfiles doesn`t help for my case as I  
>>> depend on the database content too.
>>> For checking out how to walk through the AST and for testing my  
>>> methods I need to call this method with a well defined  
>>> RubyRuleContext. That`s the reason I have to know how to create a  
>>> RubyRuleContext e.g. out of a given .rb file.
>>
>> Sounds interesting!
>>
>> I'm not following exactly what it is you're trying to do from your  
>> tests, but I would think that from a unit test perspective, you  
>> would want to mock your database connections in some way?
>>
>> In any case, a RubyRuleContext is created by the ruby hints  
>> manager.  If you need to, feel free to create testing-only  
>> trampoline accessors in there (e.g. a public class, with public  
>> methods, accessing protected/package protected/private methods, and  
>> the public class is only intended for testing, and is preferably  
>> (if possible) in the unit testing folder instead of the sources  
>> folder).
>>
>> -- Tor
>>
>>>
>>> Thanks and Best Regards
>>>  Philipp Marcus
>>>
>>>
>>>
>>>> On Sep 1, 2008, at 7:01 AM, Philipp Marcus wrote:
>>>>
>>>>> Hi,
>>>>>
>>>>> what is the correct way to create an RubyRuleContext and to fill  
>>>>> it with structure that is given by a certain ruby file? I`d  
>>>>> would be very helpful if someone could give me a little code-
>>>>> snipped showing how to create a correct RubyRuleContext  
>>>>> instance. I need this for testing my methods in my Ruby module.
>>>>
>>>> Hi Philipp,
>>>> I'm not sure what you're trying to do, but there should already  
>>>> be a lot of support for testing quickfixes. You just need to  
>>>> extend HintTestBase in the ruby.hints/test/ folder. In a separate  
>>>> module, that means adding some <test-dependencies> in your  
>>>> module's layer file. Take a look at the ruby.extrahints module  
>>>> which does this (though it doesn't currently have any extra hints  
>>>> in it, but it does set up all the dependencies, and all you have  
>>>> to do then is the same thing as the existing hints in ruby.hints/
>>>> test -- extend HintTestBase, and then call some of the hint-
>>>> testing methods like findHints, applyHint etc.   Basically,  
>>>> there's a lot of stuff going on to create all the context you  
>>>> need - locating the test files, parsing them to create an AST,  
>>>> opening the document and creating the rule context and finally  
>>>> calling your rules.
>>>>
>>>> If you're trying to do something different than what the existing  
>>>> hints testing infrastructure sets up for you (ability to check  
>>>> hint registration, ability to find all the hints in a file,  
>>>> ability to find hint/suggestion for a given caret position,  
>>>> ability to apply a specific fix for a specific caret line), then  
>>>> let me know - perhaps we can add it rather than you having to add  
>>>> a bunch of generic infrastructure on your end.
>>>>
>>>> -- Tor
>>>>
>>>>>
>>>>>
>>>>> Best Regards
>>>>> Philipp Marcus
>>>>>
>>>>> ---------------------------------------------------------------------
>>>>> To unsubscribe, e-mail: dev-unsubscribe@...
>>>>> For additional commands, e-mail: dev-help@...
>>>>>
>>>>
>>>>
>>>> ---------------------------------------------------------------------
>>>> To unsubscribe, e-mail: dev-unsubscribe@...
>>>> For additional commands, e-mail: dev-help@...
>>>>
>>>
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: dev-unsubscribe@...
>>> For additional commands, e-mail: dev-help@...
>>>
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: dev-unsubscribe@...
>> For additional commands, e-mail: dev-help@...
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@...
> For additional commands, e-mail: dev-help@...
>


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@...
For additional commands, e-mail: dev-help@...