How to use _find_matcher method in pyshould

Best Python code snippet using pyshould_python

expectation.py

Source:expectation.py Github

copy

Full Screen

...151 # If a description has been given include it in the matcher152 if self.description:153 matcher = hc.described_as(self.description, matcher)154 return matcher155 def _find_matcher(self, alias):156 """ Finds a matcher based on the given alias or raises an error if no157 matcher could be found.158 """159 matcher = lookup(alias)160 if not matcher:161 msg = 'Matcher "%s" not found' % alias162 # Try to find similarly named matchers to help the user163 similar = suggest(alias, max=3, cutoff=0.5)164 if len(similar) > 1:165 last = similar.pop()166 msg += '. Perhaps you meant to use %s or %s?' % (', '.join(similar), last)167 elif len(similar) > 0:168 msg += '. Perhaps you meant to use %s?' % similar.pop()169 raise KeyError(msg)170 return matcher171 def _init_matcher(self, *args, **kwargs):172 """ Executes the current matcher appending it to the expression """173 # If subject-less expectation are provided as arguments convert them174 # to plain Hamcrest matchers in order to allow complex compositions175 fn = lambda x: x.evaluate() if isinstance(x, Expectation) else x176 args = [fn(x) for x in args]177 kwargs = dict((k, fn(v)) for k, v in kwargs.items())178 matcher = self.matcher(*args, **kwargs)179 self.expr.append(matcher)180 self.matcher = None181 return matcher182 def described_as(self, description, *args):183 """ Specify a custom message for the matcher """184 if len(args):185 description = description.format(*args)186 self.description = description187 return self188 def desc(self, description, *args):189 """ Just an alias to described_as """190 return self.described_as(description, *args)191 def __getattribute__(self, name):192 """ Hijack property access to handle some special cases.193 Since we might have patched the root object to include194 the `should` properties, we have to capture their use195 here.196 """197 # Ignore .should. style properties198 lowname = name.lower()199 if lowname in ('should', 'to'):200 return self201 if lowname == 'should_not':202 return ExpectationNot(203 self.value,204 self.deferred,205 self.description,206 self.factory,207 self.def_op,208 self.def_matcher209 )210 return object.__getattribute__(self, name)211 def __getattr__(self, name):212 """ Overload property access to interpret them as matchers. """213 # Ignore private (protocol) methods214 if name[0:2] == '__':215 raise AttributeError216 # In factory mode we always create a new instance. This avoids217 # problems when defining multiple expectations using the `should`218 # keyword without resolving every expectation in order.219 obj = self.clone() if self.factory else self220 # If we still have an uninitialized matcher then init it now221 if obj.matcher:222 obj._init_matcher()223 # In deferred mode we will resolve in the __ror__ overload224 if not obj.deferred:225 obj.resolve(obj.value)226 # Normalize the name227 name = re.sub(r'([a-z])([A-Z])', r'\1_\2', name)228 parts = name.lower().split('_')229 # Check if we have a coordinator as first item230 expr = []231 if parts[0] == 'and':232 expr.append(OPERATOR.AND)233 parts.pop(0)234 elif parts[0] == 'or':235 expr.append(OPERATOR.OR)236 parts.pop(0)237 elif parts[0] == 'but':238 expr.append(OPERATOR.BUT)239 parts.pop(0)240 # If no coordinator is given assume a default one241 elif len(obj.expr):242 expr.append(obj.def_op)243 # Negation can come just after a combinator (ie: .and_not_be_equal)244 if 'not' in parts:245 expr.append(OPERATOR.NOT)246 parts.pop(parts.index('not'))247 if len(parts):248 name = '_'.join(parts)249 else:250 name = obj.last_matcher or obj.def_matcher251 # Find a matcher for the computed name252 try:253 obj.matcher = obj._find_matcher(name)254 obj.last_matcher = name255 obj.expr.extend(expr)256 except KeyError as ex:257 # Signal correctly for `hasattr`258 raise AttributeError(str(ex))259 return obj260 def __call__(self, *args, **kwargs):261 """ Execute the matcher just registered by __getattr__ passing any given262 arguments. If we're in deferred mode we don't resolve the matcher yet,263 it'll be done in the __ror__ overload.264 """265 # When called directly (ie: should(foo).xxx) register the param as a transform266 if (len(args) == 1 and hasattr(args[0], '__call__')267 and not self.expr and not self.matcher):...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run pyshould automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful