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());
78 auto find(const std::string& param) const
80 auto iter = mParams.begin();
81 for(; iter != mParams.end(); ++iter)
83 if(!iter->parameterName.compare(param))
93 return mParams.begin();
100 std::ostringstream& operator[](std::string name)
102 auto iter = mParams.begin();
103 for(; iter != mParams.end(); ++iter)
105 if(!iter->parameterName.compare(name))
111 if(iter != mParams.end())
117 mParams.push_back(NameValue(name, ""));
118 return mParams.back().value;
122 std::string str() const
124 std::ostringstream out;
126 for(auto& elem : mParams)
128 out << (first ? "" : " ") << elem.parameterName << ": " << elem.value.str();
133 std::vector<NameValue> mParams{};
139 TraceCallStack(bool logging, std::string prefix);
141 TraceCallStack(const TraceCallStack&) = delete;
142 TraceCallStack(TraceCallStack&&) = delete;
150 * Turn on / off tracing
152 void Enable(bool enable);
156 void EnableLogging(bool enable);
159 * Push a call onto the stack if the trace is active
160 * @param[in] method The name of the method
161 * @param[in] params A comma separated list of parameter values
163 void PushCall(std::string method, std::string params);
166 * Push a call onto the stack if the trace is active
167 * @param[in] method The name of the method
168 * @param[in] params A comma separated list of parameter values
169 * @param[in] altParams A map of named parameter values
171 void PushCall(std::string method, std::string params, const NamedParams& altParams);
174 * Search for a method in the stack
175 * @param[in] method The name of the method
176 * @return true if the method was in the stack
178 bool FindMethod(std::string method) const;
181 * Search for a method in the stack and return its parameters if found
182 * @param[in] method The name of the method
183 * @param[out] params of the method
184 * @return true if the method was in the stack
186 bool FindMethodAndGetParameters(std::string method, std::string& params) const;
189 * Count how many times a method was called
190 * @param[in] method The name of the method
191 * @return The number of times it was called
193 int CountMethod(std::string method) const;
196 * Search for a method in the stack with the given parameter list
197 * @param[in] method The name of the method
198 * @param[in] params A comma separated list of parameter values
199 * @return true if the method was in the stack
201 bool FindMethodAndParams(std::string method, std::string params) const;
204 * Search for a method in the stack with the given parameter list
205 * @param[in] method The name of the method
206 * @param[in] params A map of named parameters to test for
207 * @return true if the method was in the stack
209 bool FindMethodAndParams(std::string method, const NamedParams& params) const;
212 * Search for a method in the stack with the given parameter list.
213 * The search is done from a given index.
214 * This allows the order of methods and parameters to be checked.
215 * @param[in] method The name of the method
216 * @param[in] params A comma separated list of parameter values
217 * @param[in/out] startIndex The method index to start looking from.
218 * This is updated if a method is found so subsequent
219 * calls can search for methods occuring after this one.
220 * @return True if the method was in the stack
222 bool FindMethodAndParamsFromStartIndex(std::string method, std::string params, size_t& startIndex) 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 index in the stack where the method was found or -1 otherwise
230 int FindIndexFromMethodAndParams(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 parameter values to match
236 * @return index in the stack where the method was found or -1 otherwise
238 int FindIndexFromMethodAndParams(std::string method, const NamedParams& params) const;
241 * Test if the given method and parameters are at a given index in the stack
242 * @param[in] index Index in the call stack
243 * @param[in] method Name of method to test
244 * @param[in] params A comma separated list of parameter values to test
246 bool TestMethodAndParams(int index, std::string method, std::string params) const;
249 * Reset the call stack
254 * Method to display contents of the TraceCallStack.
255 * @return A string containing a list of function calls and parameters (may contain newline characters)
257 std::string GetTraceString()
259 std::stringstream traceStream;
260 std::size_t functionCount = mCallStack.size();
261 for(std::size_t i = 0; i < functionCount; ++i)
263 Dali::TraceCallStack::FunctionCall functionCall = mCallStack[i];
264 traceStream << "StackTrace: Index:" << i << ", Function:" << functionCall.method << ", ParamList:" << functionCall.paramList << std::endl;
267 return traceStream.str();
271 bool mTraceActive{false}; ///< True if the trace is active
272 bool mLogging{false}; ///< True if the trace is logged to stdout
278 std::string paramList;
279 NamedParams namedParams;
280 FunctionCall(const std::string& aMethod, const std::string& aParamList)
282 paramList(aParamList)
285 FunctionCall(const std::string& aMethod, const std::string& aParamList, const NamedParams& altParams)
287 paramList(aParamList),
288 namedParams(altParams)
293 std::vector<FunctionCall> mCallStack; ///< The call stack
298 #endif // TEST_TRACE_CALL_STACK_H