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(std::string prefix = "gl");
140 TraceCallStack(bool logging, std::string prefix = "gl");
148 * Turn on / off tracing
150 void Enable(bool enable);
154 void EnableLogging(bool enable);
157 * Push a call onto the stack if the trace is active
158 * @param[in] method The name of the method
159 * @param[in] params A comma separated list of parameter values
161 void PushCall(std::string method, std::string params);
164 * Push a call onto the stack if the trace is active
165 * @param[in] method The name of the method
166 * @param[in] params A comma separated list of parameter values
167 * @param[in] altParams A map of named parameter values
169 void PushCall(std::string method, std::string params, const NamedParams& altParams);
172 * Search for a method in the stack
173 * @param[in] method The name of the method
174 * @return true if the method was in the stack
176 bool FindMethod(std::string method) const;
179 * Search for a method in the stack and return its parameters if found
180 * @param[in] method The name of the method
181 * @param[out] params of the method
182 * @return true if the method was in the stack
184 bool FindMethodAndGetParameters(std::string method, std::string& params) const;
187 * Count how many times a method was called
188 * @param[in] method The name of the method
189 * @return The number of times it was called
191 int CountMethod(std::string method) const;
194 * Search for a method in the stack with the given parameter list
195 * @param[in] method The name of the method
196 * @param[in] params A comma separated list of parameter values
197 * @return true if the method was in the stack
199 bool FindMethodAndParams(std::string method, std::string params) const;
202 * Search for a method in the stack with the given parameter list
203 * @param[in] method The name of the method
204 * @param[in] params A map of named parameters to test for
205 * @return true if the method was in the stack
207 bool FindMethodAndParams(std::string method, const NamedParams& params) const;
210 * Search for a method in the stack with the given parameter list.
211 * The search is done from a given index.
212 * This allows the order of methods and parameters to be checked.
213 * @param[in] method The name of the method
214 * @param[in] params A comma separated list of parameter values
215 * @param[in/out] startIndex The method index to start looking from.
216 * This is updated if a method is found so subsequent
217 * calls can search for methods occuring after this one.
218 * @return True if the method was in the stack
220 bool FindMethodAndParamsFromStartIndex(std::string method, std::string params, size_t& startIndex) const;
223 * Search for a method in the stack with the given parameter list
224 * @param[in] method The name of the method
225 * @param[in] params A comma separated list of parameter values
226 * @return index in the stack where the method was found or -1 otherwise
228 int FindIndexFromMethodAndParams(std::string method, std::string params) const;
231 * Search for a method in the stack with the given parameter list
232 * @param[in] method The name of the method
233 * @param[in] params A map of named parameter values to match
234 * @return index in the stack where the method was found or -1 otherwise
236 int FindIndexFromMethodAndParams(std::string method, const NamedParams& params) const;
239 * Test if the given method and parameters are at a given index in the stack
240 * @param[in] index Index in the call stack
241 * @param[in] method Name of method to test
242 * @param[in] params A comma separated list of parameter values to test
244 bool TestMethodAndParams(int index, std::string method, std::string params) const;
247 * Reset the call stack
252 * Method to display contents of the TraceCallStack.
253 * @return A string containing a list of function calls and parameters (may contain newline characters)
255 std::string GetTraceString()
257 std::stringstream traceStream;
258 std::size_t functionCount = mCallStack.size();
259 for(std::size_t i = 0; i < functionCount; ++i)
261 Dali::TraceCallStack::FunctionCall functionCall = mCallStack[i];
262 traceStream << "StackTrace: Index:" << i << ", Function:" << functionCall.method << ", ParamList:" << functionCall.paramList << std::endl;
265 return traceStream.str();
269 bool mTraceActive{false}; ///< True if the trace is active
270 bool mLogging{false}; ///< True if the trace is logged to stdout
276 std::string paramList;
277 NamedParams namedParams;
278 FunctionCall(const std::string& aMethod, const std::string& aParamList)
280 paramList(aParamList)
283 FunctionCall(const std::string& aMethod, const std::string& aParamList, const NamedParams& altParams)
285 paramList(aParamList),
286 namedParams(altParams)
291 std::vector<FunctionCall> mCallStack; ///< The call stack
296 #endif // TEST_TRACE_CALL_STACK_H