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 * Count how many times a method was called
211 * @param[in] method The name of the method
212 * @return The number of times it was called
214 int CountMethod(std::string method) const;
217 * Search for a method in the stack with the given parameter list
218 * @param[in] method The name of the method
219 * @param[in] params A comma separated list of parameter values
220 * @return true if the method was in the stack
222 bool FindMethodAndParams(std::string method, std::string params) 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 map of named parameters to test for
228 * @return true if the method was in the stack
230 bool FindMethodAndParams(std::string method, const NamedParams& params) const;
233 * Search for a method in the stack with the given parameter list.
234 * The search is done from a given index.
235 * This allows the order of methods and parameters to be checked.
236 * @param[in] method The name of the method
237 * @param[in] params A comma separated list of parameter values
238 * @param[in/out] startIndex The method index to start looking from.
239 * This is updated if a method is found so subsequent
240 * calls can search for methods occuring after this one.
241 * @return True if the method was in the stack
243 bool FindMethodAndParamsFromStartIndex(std::string method, std::string params, size_t& startIndex) const;
246 * Search for a method in the stack with the given parameter list
247 * @param[in] method The name of the method
248 * @param[in] params A comma separated list of parameter values
249 * @return index in the stack where the method was found or -1 otherwise
251 int FindIndexFromMethodAndParams(std::string method, std::string params) 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 map of named parameter values to match
257 * @return index in the stack where the method was found or -1 otherwise
259 int FindIndexFromMethodAndParams(std::string method, const NamedParams& params) const;
262 * Search for the most recent occurrence of the method with the given (partial) parameters.
263 * @param[in] method The name of the method
264 * @param[in] params A map of named parameter values to match
265 * @return The full named parameters of the matching call.
267 const NamedParams* FindLastMatch(std::string method, const TraceCallStack::NamedParams& params) const;
270 * Test if the given method and parameters are at a given index in the stack
271 * @param[in] index Index in the call stack
272 * @param[in] method Name of method to test
273 * @param[in] params A comma separated list of parameter values to test
275 bool TestMethodAndParams(int index, std::string method, std::string params) const;
278 * Reset the call stack
283 * Method to display contents of the TraceCallStack.
284 * @return A string containing a list of function calls and parameters (may contain newline characters)
286 std::string GetTraceString()
288 std::stringstream traceStream;
289 std::size_t functionCount = mCallStack.size();
290 for(std::size_t i = 0; i < functionCount; ++i)
292 Dali::TraceCallStack::FunctionCall functionCall = mCallStack[i];
293 traceStream << "StackTrace: Index:" << i << ", Function:" << functionCall.method << ", ParamList:" << functionCall.paramList << std::endl;
296 return traceStream.str();
300 bool mTraceActive{false}; ///< True if the trace is active
301 bool mLogging{false}; ///< True if the trace is logged to stdout
307 std::string paramList;
308 NamedParams namedParams;
309 FunctionCall(const std::string& aMethod, const std::string& aParamList)
311 paramList(aParamList)
314 FunctionCall(const std::string& aMethod, const std::string& aParamList, const NamedParams& altParams)
316 paramList(aParamList),
317 namedParams(altParams)
322 std::vector<FunctionCall> mCallStack; ///< The call stack
327 #endif // TEST_TRACE_CALL_STACK_H