1 #ifndef TEST_TRACE_CALL_STACK_H
2 #define TEST_TRACE_CALL_STACK_H
5 * Copyright (c) 2021 Samsung Electronics Co., Ltd.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
29 std::string ToString(const T& x)
34 std::string ToString(int x);
35 std::string ToString(unsigned int x);
36 std::string ToString(float x);
39 * Helper class to track method calls in the abstraction and search for them in test cases
44 /// Typedef for passing and storing named parameters
50 std::string parameterName;
51 std::ostringstream value;
52 NameValue(std::string pname, std::string aValue)
53 : parameterName(pname),
57 NameValue(const NameValue& rhs)
58 : parameterName(rhs.parameterName),
59 value(rhs.value.str())
62 NameValue& operator=(const NameValue& rhs)
66 this->parameterName = rhs.parameterName;
67 this->value.str(rhs.value.str());
72 bool operator==(const NameValue& rhs)
74 return !parameterName.compare(rhs.parameterName) && !value.str().compare(rhs.value.str());
77 bool operator==(int match) const;
80 auto find(const std::string& param) const
82 auto iter = mParams.begin();
83 for(; iter != mParams.end(); ++iter)
85 if(!iter->parameterName.compare(param))
95 return mParams.begin();
102 std::ostringstream& operator[](std::string name)
104 auto iter = mParams.begin();
105 for(; iter != mParams.end(); ++iter)
107 if(!iter->parameterName.compare(name))
113 if(iter != mParams.end())
119 mParams.push_back(NameValue(name, ""));
120 return mParams.back().value;
124 std::string str() const
126 std::ostringstream out;
128 for(auto& elem : mParams)
130 out << (first ? "" : " ") << elem.parameterName << ": " << elem.value.str();
135 std::vector<NameValue> mParams{};
141 TraceCallStack(bool logging, std::string prefix);
143 TraceCallStack(const TraceCallStack&) = delete;
144 TraceCallStack(TraceCallStack&&) = delete;
152 * Turn on / off tracing
154 void Enable(bool enable);
158 void EnableLogging(bool enable);
161 * Push a call onto the stack if the trace is active
162 * @param[in] method The name of the method
163 * @param[in] params A comma separated list of parameter values
165 void PushCall(std::string method, std::string params);
168 * Push a call onto the stack if the trace is active
169 * @param[in] method The name of the method
170 * @param[in] params A comma separated list of parameter values
171 * @param[in] altParams A map of named parameter values
173 void PushCall(std::string method, std::string params, const NamedParams& altParams);
176 * Search for a method in the stack
177 * @param[in] method The name of the method
178 * @return true if the method was in the stack
180 bool FindMethod(std::string method) const;
183 * Search for a method in the stack and return its parameters if found
184 * @param[in] method The name of the method
185 * @param[out] params of the method
186 * @return true if the method was in the stack
188 bool FindMethodAndGetParameters(std::string method, std::string& params) const;
191 * Count how many times a method was called
192 * @param[in] method The name of the method
193 * @return The number of times it was called
195 int CountMethod(std::string method) const;
198 * Search for a method in the stack with the given parameter list
199 * @param[in] method The name of the method
200 * @param[in] params A comma separated list of parameter values
201 * @return true if the method was in the stack
203 bool FindMethodAndParams(std::string method, std::string params) const;
206 * Search for a method in the stack with the given parameter list
207 * @param[in] method The name of the method
208 * @param[in] params A map of named parameters to test for
209 * @return true if the method was in the stack
211 bool FindMethodAndParams(std::string method, const NamedParams& params) const;
214 * Search for a method in the stack with the given parameter list.
215 * The search is done from a given index.
216 * This allows the order of methods and parameters to be checked.
217 * @param[in] method The name of the method
218 * @param[in] params A comma separated list of parameter values
219 * @param[in/out] startIndex The method index to start looking from.
220 * This is updated if a method is found so subsequent
221 * calls can search for methods occuring after this one.
222 * @return True if the method was in the stack
224 bool FindMethodAndParamsFromStartIndex(std::string method, std::string params, size_t& startIndex) const;
227 * Search for a method in the stack with the given parameter list
228 * @param[in] method The name of the method
229 * @param[in] params A comma separated list of parameter values
230 * @return index in the stack where the method was found or -1 otherwise
232 int FindIndexFromMethodAndParams(std::string method, std::string params) const;
235 * Search for a method in the stack with the given parameter list
236 * @param[in] method The name of the method
237 * @param[in] params A map of named parameter values to match
238 * @return index in the stack where the method was found or -1 otherwise
240 int FindIndexFromMethodAndParams(std::string method, const NamedParams& params) const;
243 * Search for the most recent occurrence of the method with the given (partial) parameters.
244 * @param[in] method The name of the method
245 * @param[in] params A map of named parameter values to match
246 * @return The full named parameters of the matching call.
248 const NamedParams* FindLastMatch(std::string method, const TraceCallStack::NamedParams& params) const;
251 * Test if the given method and parameters are at a given index in the stack
252 * @param[in] index Index in the call stack
253 * @param[in] method Name of method to test
254 * @param[in] params A comma separated list of parameter values to test
256 bool TestMethodAndParams(int index, std::string method, std::string params) const;
259 * Reset the call stack
264 * Method to display contents of the TraceCallStack.
265 * @return A string containing a list of function calls and parameters (may contain newline characters)
267 std::string GetTraceString()
269 std::stringstream traceStream;
270 std::size_t functionCount = mCallStack.size();
271 for(std::size_t i = 0; i < functionCount; ++i)
273 Dali::TraceCallStack::FunctionCall functionCall = mCallStack[i];
274 traceStream << "StackTrace: Index:" << i << ", Function:" << functionCall.method << ", ParamList:" << functionCall.paramList << std::endl;
277 return traceStream.str();
281 bool mTraceActive{false}; ///< True if the trace is active
282 bool mLogging{false}; ///< True if the trace is logged to stdout
288 std::string paramList;
289 NamedParams namedParams;
290 FunctionCall(const std::string& aMethod, const std::string& aParamList)
292 paramList(aParamList)
295 FunctionCall(const std::string& aMethod, const std::string& aParamList, const NamedParams& altParams)
297 paramList(aParamList),
298 namedParams(altParams)
303 std::vector<FunctionCall> mCallStack; ///< The call stack
308 #endif // TEST_TRACE_CALL_STACK_H