CFC Undefined with variably defined method name - coldfusion

After reviewing the following topics:
ColdFusion 9 Dynamic Method Call
Dynamic Method Call
I still have a question regarding an error.
I made an illustration to try and make it clear:
I am wanting to use a variably named function. (But this is Not the problem I am having, I can run a variably named function)
I think it's coming back as undefined because service2's method is being called from the context of service1, and cannot call a method in service1 because service1 is "locked", awaiting a return value from service2 first.
I would like to keep the methods where they are, unless there is no workaround. Am I understanding the problem correctly?
Using CF16 and Framework1.

As I could use a quick solution, I will post something I have come up with, which WORKS, but I am still looking for a "best practice" sort of suggestion.
Service1 now calls a new method in service2, say "chooseFunctionToRun".
function chooseFunctionToRun(funcName,param1,param2){
var functionToRun = this[funcName];
return functionToRun(param1,param2);
}
now, whatever functionToRun evaluates to (MethodA for example) can freely call a method from Service1 without Service1 being undefined.
It seems like it's just a "separation of concerns" for the code, and the assets are moved from Service1 to Service2 so Service2 can decide the variable function to be run. Still not sure why it works. any comments welcome.
UPDATE
Alex proposed Using CFInvoke, which is cleaner.
function chooseFunctionToRun(funcName,param1,param2){
invoke("",funcName,{arg1=param1,arg2=param2});
}

Related

Google Mock std::shared_prt Invoke Problems

I am working with google mock to mock out the behavior of a class. The object I am mocking is a std:shared_prt. Somehow I can't redirect the method-call (of a mock method) to another method within the class.
The method I want to call redirectToStartOfBaseClass(), invokes the start()-Method of the base class (NMEADataControler)
The Mock Class:
class NMEADataControler_Mock : public NMEADataControler{
...
// The method I want to redirecto to ...
void redirectToStartOfBaseClass();
...
// ... when this mock method is called
MOCK_METHOD0(start, void());
...
}
The class with the test-fixture I am using
class TestFixtureClass : public ::testing::Test{
...
std::shared_ptr<NMEADataControler_Mock> NEMADummy;
...
}
Test Method:
TEST_F(TestFixtureClass, StupidTest){
...
ON_CALL(*NMEADummy, start())
.WillByDefault(Invoke( ?????? ) //What parameters to I have to put in here?
//To redirect to *NMEADummy->redirectToStartOfBaseClass()
}
I am not that experienced with C++ and quite new to it, so please forgive me if my mistakes are obvious and super stupid.
P.s: I searched for a solution for quite some time now and i can't find anything. That why I am asking you guys, I am quite desperate, I hope you can help me :(
If I'm understanding your question correctly, you just need to pass the NMEADataControler_Mock instance to Invoke, like this:
ON_CALL(*NMEADummy, start())
.WillByDefault(Invoke( NMEADummy.get(), &NMEADataControler_Mock::redirectToStartOfBaseClass));
Here, NMEADummy.get() returns a naked pointer to your dummy.
As an aside: I don't know your use case, so maybe a shared_ptr is a good call in your specific instance, but it doesn't seem like it. I advise you to use shared_ptr only in cases where you need multiple owners of a single resource. If the Fixture is going to be the only owner of your mock, I advise you to use a unique_ptr. shared_ptr are more complex to reason about, they can introduce hard to track ref count loops (they are not garbage collected, there is no mark and sweep happening) and are more heavy-weight, because of additional storage allocated for a control block. They are also slower, because of the atomic operations used to control their refcounts.

Simple definition of stub, spy, fake and mock in unit testing

I'm quite new to unit testing. I've read around on here as well as done some Googling, but am still a bit confused as to the meaning of each of the four meanings. I came across the following definition which help....
Stub - stubs are minimal implementations of interfaces or base classes
Spy - a spy will record which members were invoked
Fake - more complex, a fake may resemble a production implementation
Mock - A mock is usually dynamically created by a mock library and depending on its configuration, a mock can behave like a dummy, a stub, or a spy
However, I'd like to simplify the meaning (if possible) and ask a few questions.
Do all of the above only relate to functions, or can they be objects or any other type?
Stub - Is Stubbing bascially a way to provide dummy info instead of making the calling to the actual database? So for example, if I had an API call, instead of actually making a call to the API, I just instead make a GET request to a JSON file which is in my tests folder which has some dummy data in, and use that instead of making the API call?
Spy - so is this a way of tracking what happens to a function for example. Meaning you follow when it's called, where it gets passed around to?
Fake - Is this for example a function which you create inside the test file to mimic the real function or be a simpified version of the actual function?
Thanks in advance.
There are multiple attempts at definitions. To my knowledge there is no fully consistent definition, probably due to the fact that mocking frameworks defines things slightly differently. Martin Fowler lists the following (https://martinfowler.com/bliki/TestDouble.html):
Dummy objects are passed around but never actually used. Usually they are just used to fill parameter lists.
Fake objects actually have working implementations, but usually take some shortcut which makes them not suitable for production (an InMemoryTestDatabase is a good example).
Stubs provide canned answers to calls made during the test, usually not responding at all to anything outside what's programmed in for the test.
Spies are stubs that also record some information based on how they were called. One form of this might be an email service that records how many messages it was sent.
Mocks are pre-programmed with expectations which form a specification of the calls they are expected to receive. They can throw an exception if they receive a call they don't expect and are checked during verification to ensure they got all the calls they were expecting.
Fowler has them from this page: http://xunitpatterns.com/Test%20Double.html where you can read about them in a bit more detail.
No, they apply to more then just functions
Yes
Kind of, in most cases you will spy on an object
Yes
The implementation and nomenclatur will depend of what framework you use.

Creating a "Publisher->Dispatcher->Subscriber" pattern event system?

Edit: TL;DR
I guess my main problem is I don't know how to store a list of functions that all take one argument, where the argument type is different between each function, but always extends from EventBase, for calling later.
i.e: EventChild extends from EventBase. A function with the signature
<void (EventChild&)>
will not fit into a variable of type
std::function<void(EventBase&)>
How do I store functions like this, knowing that a user shouldn't have to modify the class where they are stored each time they create a new event extending from our EventBase class?
Note: I had previously been told I could use a dynamic_cast to accomplish this. I have been trying to do exactly that, but it hasn't been working. I imagine for that to work I would have to use pointers somehow, but I am new enough to C++ that I'm not sure how to do it. Maybe that should be the starting point?
One of the problems with dynamic casting pointers I have been having is 'I can convert a pointer of type:
(Subbscriber*)(getDemoEvent(EventDemo&)
to type:
void(EventBase&)
or something along those lines. (not at my computer right now to try it)
This is obviously a problem limited to member functions, I assume.
I recently posted a question on here with the intention of solving an issue for a C++ Event system based on a "Publisher->Dispatcher->Subscriber" pattern. I don't know the exact name of this pattern, but I hear that it is a variant on the Observer pattern with an added "middle-man."
I have been trying to get this system to work for a while now and I am completely stuck. It was suggested in the comments of the previous question that for what I was trying to accomplish, my program layout is incorrect. This is very likely the case since I had been researching other event systems that were close to what I am after trying to modify them for use they were unintended for. So I figured I would describe what I am after, and ask the more general question of "How would you go about structuring and creating this?"
So here is my general idea of how the system should be laid out and how it should operate in a basic example:
Starting with the idea of 5 different files (plus headers and maybe some subclasses):
main.cpp
dispatcher.cpp
publisher.cpp
subscriber.cpp
eventbase.cpp
publishers and subscribers could be anything, and they only serve as an example here.
The first order of business would be to create an instance of our Dispatcher class.
Following that, we create instances of our publisher/subscriber classes. These 2 classes could be a part of the same file, different files, multiples of each, or not event be classes at all but simply free functions. For the sake of simplicity and testing, they are 2 separate classes that know nothing about each other.
When these 2 classes are created, they should be passed a reference or pointer to our dispatcher instance.
This is easy enough. Now let's get to how you should use the system.
A user of the system should be able to create a class that inherits from our EventBase class. Ideally, there should be no requirement on variables or functions to override from the base class.
Let's say we have created a new event class called EventDemo, with a public const char* demoString = "I am a Demo Event";.
From our subscriber class, we should be able to tell our dispatcher that we want to listen for and receive some events. The syntax for doing so should be as simple as possible.
Lets create a member function in our subscriber that looks like this:
void Subscriber::getDemoEvent(const EventDemo &ev) {
std::cout << ev.demoString;
}
Now we need a way to bind that member function to our dispatcher. We should probably do that in our constructor. Let's say that the reference to our dispatcher that we passed to our subscriber when we created it is just called 'dispatcher'.
The syntax for subscribing to an event should look something like this:
dispatcher->subscribe("EventToSubTo", &getDemoEvent);
Now since we are in a class trying to pass a member function, this probably isn't possible, but it would work for free functions.
For member functions we will probably need and override that looks like this:
dispatcher->subscribe("EventToSubTo", &Subscriber::getDemoEvent, this);
We use 'this' since we are inside the subscribers constructor. Otherwise, we could use a reference to our subscriber.
Notice that I am simply using a string (or const char* in c++ terms) as my "Event Key". This is on purpose, so that you could use the same event "type" for multiple events. I.E: EventDemo() can be sent to keys "Event1" and "Event2".
Now we need to send an event. This can be done anywhere we have a reference to our dispatcher. In this case, somewhere in our publisher class.
The syntax should look something like this to send our EventDemo:
dispatcher->emit("EventToSubTo", EventDemo());
Super simple. It's worth noting that we should be able to assign data to our event through it's constructor, or even template the event. Both of these cases are only valid if the event created by the user supports it.
In this case, the above code would look something like this:
dispatcher->emit("EventToSubTo", EventDemo(42));
or
dispatcher->emit("EventToSubTo", EventDemo<float>(3.14159f));
It would be up to the user to create a member function to retrieve the data.
OK, so, all of that should seem pretty simple, and in fact, it is, except for one small gotcha. There are already systems out there that store functions in a map with a type of .
And therein lies the problem...
We can store our listener functions, as long as they accept a type of EventBase as their argument. We would then have to type cast that argument to the type of event we are after. That's not overly difficult to do, but that's not really the point. The point is can it be better.
Another solution that was brought up before was the idea of having a separate map, or vector, for each type of event. That's not bad either, but would require the user to either modify the dispatcher class (which would be hard to do when this is a library), or somehow tell the dispatcher to "create this set of maps" at compile time. That would also make event templating a nightmare.
So, the overly generalized question: How do we do that?
That was probably a very long winded explanation for something seemingly simple, but maybe someone will come along not not know about it.
I am very interested to hear thoughts on this. The core idea is that I don't want the 2 communicators (publisher and subscriber) to have to know anything about each other (no pointers or references), but still be able to pass arbitrary data from one to the other. Most implementations I have seen (signals and slots) require that there be some reference to each other. Creating a "middle-man" interface feels much more flexible.
Thank you for your time.
For reference to my last question with code examples of what I have so far:
Store a function with arbitrary arguments and placeholders in a class and call it later
I have more samples I could post, but I think it's highly likely that the structure of the system will have to change. Waiting to hear thoughts!

Where can I find the function definition of route and resource?

Where are the functions route() and resource(), from the following snippet, defined?
I would like read their description.
App.Router.map(function(){
this.route('about');
this.resource('article');
});
These are methods on an internal object called "DSL" used for building routing tables. This is the context object with which Ember.Router calls the function passed to map. You don't need to worry about them. The relevant source code is here. It's not commented, so you're out of luck in terms of reading their descriptions, other than what's in the Ember guides and API docs, which is very little, but then again, they're pretty simple.
The guides are coy about this object:
When calling map, you should pass a function that will be invoked with the value this set to an object which you can use to create routes and resources.
The "object which you can use" being referred to is the DSL.

PHPUnit and seams

So I've decided to investigate using seams in PHPUnit, but I came across a problem
I rearranged my class in a way that I broke the dependencies to database class
db_Class::getMyData($vars);
became
self::getMyData($vars);
and I added functions to my code
protected static function getMyData($vars) {
return db_Class::getMyData($vars);
}
This was done so I can make a class that inherits this class and overloads the getMyData function. To be able to alter it when I run my test cases.
So for example in the seam class that extends the class above and overloads that function:
protected static function getMyData($vars) {
return array('id'=>1, 'name'=>"My Name");
}
This would be very useful, as I can alter the data as I like. However when using PHPUnit you have the possibility to run mocks using $this->getMock and similar. Would I ever be able to achieve this inside the seam class.
I'm trying to look for a solution where I am not using a dependency injector, which would be my other alternative, not so bad at all, just want to evaluate both alternatives.
Michael C. Feathers expressed a seam to be the following:
A seam is a place where you can alter behavior in your program without editing in that place.
So I might not get the full picture, and I've been trying to get it for a while now, and I just cant get my head around it. Please comment if you have any ideas or questions.
What I ask for is a way to work with mocks easy in different scenarios. I dont always want to return the same value in the seam, sometimes I want to return null to get an error, and sometimes an array with correct data, and sometimes something else probably.
Thanks
Because you must reference the class directly when calling static methods, you cannot override them as you can non-static methods. Static methods make testing difficult. I won't bother repeating what's written there, and I highly recommend following the links in the answers.
In any case, why is that method static? Being protected, you can call it only from the same class or its subclasses. Can you post more of the context here? How do you intend to use it, and where will you test it? Can you change it to non-static?
I found an answer to my question here:
http://sebastian-bergmann.de/archives/885-Stubbing-Hard-Coded-Dependencies.html
The idea here is that you can prepare testable version of X where only thing overriden will be getMyData:
protected static function getMyData($vars) {
return $some_dummy_data;
}
You write tests for X indirectly trough TestX.
Now lets assume that you change something in original X that breaks it. TestX does inherit that broken code, thus its tests fail. Exactly what we wanted!