1 #ifndef TEST_TRACE_CALL_STACK_H
2 #define TEST_TRACE_CALL_STACK_H
5 * Copyright (c) 2022 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 const std::ostringstream& operator[](std::string name) const
126 static std::ostringstream empty;
127 auto iter = mParams.begin();
128 for(; iter != mParams.end(); ++iter)
130 if(!iter->parameterName.compare(name))
136 if(iter != mParams.end())
143 std::string str() const
145 std::ostringstream out;
147 for(auto& elem : mParams)
149 out << (first ? "" : " ") << elem.parameterName << ": " << elem.value.str();
154 std::vector<NameValue> mParams{};
160 TraceCallStack(bool logging, std::string prefix);
162 TraceCallStack(const TraceCallStack&) = delete;
163 TraceCallStack(TraceCallStack&&) = delete;
171 * Turn on / off tracing
173 void Enable(bool enable);
177 void EnableLogging(bool enable);
180 * Push a call onto the stack if the trace is active
181 * @param[in] method The name of the method
182 * @param[in] params A comma separated list of parameter values
184 void PushCall(std::string method, std::string params);
187 * Push a call onto the stack if the trace is active
188 * @param[in] method The name of the method
189 * @param[in] params A comma separated list of parameter values
190 * @param[in] altParams A map of named parameter values
192 void PushCall(std::string method, std::string params, const NamedParams& altParams);
195 * Search for a method in the stack
196 * @param[in] method The name of the method
197 * @return true if the method was in the stack
199 bool FindMethod(std::string method) const;
202 * Search for a method in the stack and return its parameters if found
203 * @param[in] method The name of the method
204 * @param[out] params of the method
205 * @return true if the method was in the stack
207 bool FindMethodAndGetParameters(std::string method, std::string& params) const;
210 * Search for a method in the stack and return its parameters if found
211 * @param[in] method The name of the method
212 * @param[out] params of the method
213 * @return true if the method was in the stack
215 bool FindMethodAndGetParameters(std::string method, NamedParams& params) const;
218 * Count how many times a method was called
219 * @param[in] method The name of the method
220 * @return The number of times it was called
222 int CountMethod(std::string method) const;
225 * Search for a method in the stack with the given parameter list
226 * @param[in] method The name of the method
227 * @param[in] params A comma separated list of parameter values
228 * @return true if the method was in the stack
230 bool FindMethodAndParams(std::string method, std::string params) const;
233 * Search for a method in the stack with the given parameter list
234 * @param[in] method The name of the method
235 * @param[in] params A map of named parameters to test for
236 * @return true if the method was in the stack
238 bool FindMethodAndParams(std::string method, const NamedParams& params) const;
241 * Search for a method in the stack with the given parameter list.
242 * The search is done from a given index.
243 * This allows the order of methods and parameters to be checked.
244 * @param[in] method The name of the method
245 * @param[in] params A comma separated list of parameter values
246 * @param[in/out] startIndex The method index to start looking from.
247 * This is updated if a method is found so subsequent
248 * calls can search for methods occuring after this one.
249 * @return True if the method was in the stack
251 bool FindMethodAndParamsFromStartIndex(std::string method, std::string params, size_t& startIndex) const;
254 * Search for a method in the stack with the given parameter list
255 * @param[in] method The name of the method
256 * @param[in] params A comma separated list of parameter values
257 * @return index in the stack where the method was found or -1 otherwise
259 int FindIndexFromMethodAndParams(std::string method, std::string params) const;
262 * Search for a method in the stack with the given parameter list
263 * @param[in] method The name of the method
264 * @param[in] params A map of named parameter values to match
265 * @return index in the stack where the method was found or -1 otherwise
267 int FindIndexFromMethodAndParams(std::string method, const NamedParams& params) const;
270 * Search for the most recent occurrence of the method with the given (partial) parameters.
271 * @param[in] method The name of the method
272 * @param[in] params A map of named parameter values to match
273 * @return The full named parameters of the matching call.
275 const NamedParams* FindLastMatch(std::string method, const TraceCallStack::NamedParams& params) const;
278 * Test if the given method and parameters are at a given index in the stack
279 * @param[in] index Index in the call stack
280 * @param[in] method Name of method to test
281 * @param[in] params A comma separated list of parameter values to test
283 bool TestMethodAndParams(int index, std::string method, std::string params) const;
286 * Reset the call stack
291 * Method to display contents of the TraceCallStack.
292 * @return A string containing a list of function calls and parameters (may contain newline characters)
294 std::string GetTraceString()
296 std::stringstream traceStream;
297 std::size_t functionCount = mCallStack.size();
298 for(std::size_t i = 0; i < functionCount; ++i)
300 Dali::TraceCallStack::FunctionCall functionCall = mCallStack[i];
301 traceStream << "StackTrace: Index:" << i << ", Function:" << functionCall.method << ", ParamList:" << functionCall.paramList << std::endl;
304 return traceStream.str();
308 bool mTraceActive{false}; ///< True if the trace is active
309 bool mLogging{false}; ///< True if the trace is logged to stdout
315 std::string paramList;
316 NamedParams namedParams;
317 FunctionCall(const std::string& aMethod, const std::string& aParamList)
319 paramList(aParamList)
322 FunctionCall(const std::string& aMethod, const std::string& aParamList, const NamedParams& altParams)
324 paramList(aParamList),
325 namedParams(altParams)
330 std::vector<FunctionCall> mCallStack; ///< The call stack
335 #endif // TEST_TRACE_CALL_STACK_H