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
45 typedef std::map<std::string, std::string> NamedParams;
50 TraceCallStack(std::string prefix = "");
58 * Turn on / off tracing
60 void Enable(bool enable);
64 void EnableLogging(bool enable);
67 * Push a call onto the stack if the trace is active
68 * @param[in] method The name of the method
69 * @param[in] params A comma separated list of parameter values
71 void PushCall(std::string method, std::string params);
74 * Push a call onto the stack if the trace is active
75 * @param[in] method The name of the method
76 * @param[in] params A comma separated list of parameter values
77 * @param[in] altParams A map of named parameter values
79 void PushCall(std::string method, std::string params, const NamedParams& altParams);
82 * Search for a method in the stack
83 * @param[in] method The name of the method
84 * @return true if the method was in the stack
86 bool FindMethod(std::string method) const;
89 * Search for a method in the stack and return its parameters if found
90 * @param[in] method The name of the method
91 * @param[out] params of the method
92 * @return true if the method was in the stack
94 bool FindMethodAndGetParameters(std::string method, std::string& params) const;
97 * Count how many times a method was called
98 * @param[in] method The name of the method
99 * @return The number of times it was called
101 int CountMethod(std::string method) const;
104 * Search for a method in the stack with the given parameter list
105 * @param[in] method The name of the method
106 * @param[in] params A comma separated list of parameter values
107 * @return true if the method was in the stack
109 bool FindMethodAndParams(std::string method, std::string params) const;
112 * Search for a method in the stack with the given parameter list
113 * @param[in] method The name of the method
114 * @param[in] params A map of named parameters to test for
115 * @return true if the method was in the stack
117 bool FindMethodAndParams(std::string method, const NamedParams& params) const;
120 * Search for a method in the stack with the given parameter list.
121 * The search is done from a given index.
122 * This allows the order of methods and parameters to be checked.
123 * @param[in] method The name of the method
124 * @param[in] params A comma separated list of parameter values
125 * @param[in/out] startIndex The method index to start looking from.
126 * This is updated if a method is found so subsequent
127 * calls can search for methods occuring after this one.
128 * @return True if the method was in the stack
130 bool FindMethodAndParamsFromStartIndex(std::string method, std::string params, size_t& startIndex) const;
133 * Search for a method in the stack with the given parameter list
134 * @param[in] method The name of the method
135 * @param[in] params A comma separated list of parameter values
136 * @return index in the stack where the method was found or -1 otherwise
138 int FindIndexFromMethodAndParams(std::string method, std::string params) const;
141 * Search for a method in the stack with the given parameter list
142 * @param[in] method The name of the method
143 * @param[in] params A map of named parameter values to match
144 * @return index in the stack where the method was found or -1 otherwise
146 int FindIndexFromMethodAndParams(std::string method, const NamedParams& params) const;
149 * Test if the given method and parameters are at a given index in the stack
150 * @param[in] index Index in the call stack
151 * @param[in] method Name of method to test
152 * @param[in] params A comma separated list of parameter values to test
154 bool TestMethodAndParams(int index, std::string method, std::string params) const;
157 * Reset the call stack
162 * Method to display contents of the TraceCallStack.
163 * @return A string containing a list of function calls and parameters (may contain newline characters)
165 std::string GetTraceString()
167 std::stringstream traceStream;
168 std::size_t functionCount = mCallStack.size();
169 for(std::size_t i = 0; i < functionCount; ++i)
171 Dali::TraceCallStack::FunctionCall functionCall = mCallStack[i];
172 traceStream << "StackTrace: Index:" << i << ", Function:" << functionCall.method << ", ParamList:" << functionCall.paramList << std::endl;
175 return traceStream.str();
179 bool mTraceActive{false}; ///< True if the trace is active
180 bool mLogging{false}; ///< True if the trace is logged to stdout
186 std::string paramList;
187 NamedParams namedParams;
188 FunctionCall(const std::string& aMethod, const std::string& aParamList)
190 paramList(aParamList)
193 FunctionCall(const std::string& aMethod, const std::string& aParamList, const NamedParams& altParams)
195 paramList(aParamList),
196 namedParams(altParams)
201 std::vector<FunctionCall> mCallStack; ///< The call stack
206 #endif // TEST_TRACE_CALL_STACK_H