Best Python code snippet using lemoncheesecake
composites.py
Source:composites.py
...44 return description45 else:46 return _build_multi_line_description(matchers, transformation, relationship)47def _serialize_sub_matcher_result(matcher, result):48 content = "%s => %s" % (matcher.build_short_description(MatcherDescriptionTransformer()), "OK" if result else "KO")49 if result.description is not None:50 content += ", %s" % result.description51 return content52class AllOf(Matcher):53 def __init__(self, matchers):54 # type: (List[Matcher]) -> None55 self.matchers = matchers56 def build_short_description(self, transformation):57 return ":"58 def build_description(self, transformation):59 return _build_composite_description(self.matchers, transformation, "and")60 def matches(self, actual):61 results = []62 is_success = True63 for matcher in self.matchers:64 result = matcher.matches(actual)65 results.append((matcher, result))66 if not result:67 is_success = False68 break69 match_details = "\n".join(70 ["got:"] +71 _make_items([72 _serialize_sub_matcher_result(matcher, result) for matcher, result in results73 ], relationship="and")74 )75 return MatchResult(is_success, match_details)76def all_of(*matchers):77 # type: (Any) -> AllOf78 """Test if all matchers match (logical AND between matchers)."""79 return AllOf(list(map(is_, matchers)))80class AnyOf(Matcher):81 def __init__(self, matchers):82 # type: (List[Matcher]) -> None83 self.matchers = matchers84 def build_short_description(self, transformation):85 return ":"86 def build_description(self, transformation):87 return _build_composite_description(self.matchers, transformation, "or")88 def matches(self, actual):89 results = []90 for matcher in self.matchers:91 match = matcher.matches(actual)92 if match:93 return match94 results.append(match)95 return MatchResult.failure(96 ", ".join(97 OrderedSet(result.description for result in results if result.description)98 )...
list_.py
Source:list_.py
...25 def build_description(self, transformation):26 return transformation(27 "to have an item whose value %s" % self.expected.build_description(MatcherDescriptionTransformer(conjugate=True))28 )29 def build_short_description(self, transformation):30 return transformation(31 "to have an item whose value %s" % self.expected.build_short_description(MatcherDescriptionTransformer(conjugate=True))32 )33 def matches(self, actual):34 for index, item in enumerate(actual):35 result = self.expected.matches(item)36 if result:37 return HasItemMatchResult.found(index, item)38 return HasItemMatchResult.not_found()39def has_item(expected):40 # type: (Any) -> HasItem41 """Test if the sequence has item matching expected"""42 return HasItem(is_(expected))43class HasItems(Matcher):44 def __init__(self, expected):45 self.expected = expected46 def build_description(self, transformation):47 return transformation("to have items %s" % _jsonify_items(self.expected))48 def matches(self, actual):49 missing = []50 for expected in self.expected:51 if expected not in actual:52 missing.append(expected)53 if missing:54 return MatchResult.failure("Missing items: %s" % _jsonify_items(missing))55 else:56 return MatchResult.success("got %s" % jsonify(actual))57def has_items(values):58 # type: (Iterable) -> HasItems59 """Test if the sequence contains at least the given values"""60 return HasItems(values)61class HasOnlyItems(Matcher):62 def __init__(self, expected):63 self.expected = expected64 def build_description(self, transformation):65 return transformation("to have only items %s" % _jsonify_items(self.expected))66 def matches(self, actual):67 expected = list(self.expected)68 extra = []69 for value in actual:70 if value in expected:71 expected.remove(value)72 else:73 extra.append(value)74 if len(expected) == 0 and len(extra) == 0:75 return MatchResult.success("got %s" % jsonify(actual))76 else:77 details = []78 if len(expected) > 0:79 details.append("Missing items: %s" % _jsonify_items(expected))80 if len(extra) > 0:81 details.append("Extra items: %s" % _jsonify_items(extra))82 return MatchResult.failure("; ".join(details))83def has_only_items(expected):84 # type: (Iterable) -> HasOnlyItems85 """Test if the sequence only contains the given values"""86 return HasOnlyItems(expected)87class HasAllItems(Matcher):88 def __init__(self, expected):89 self.expected = expected90 def build_description(self, transformation):91 return transformation(92 "to have all items whose value %s" %93 self.expected.build_description(MatcherDescriptionTransformer(conjugate=True))94 )95 def build_short_description(self, transformation):96 return transformation(97 "to have all items whose value %s" %98 self.expected.build_short_description(MatcherDescriptionTransformer(conjugate=True))99 )100 def matches(self, actual):101 failures = {}102 for idx, item in enumerate(actual):103 result = self.expected.matches(item)104 if not result:105 failures[idx] = result106 if failures:107 return MatchResult.failure(108 "\n".join(109 ["Non-matching items:"] +110 ["- at index %d: %s" % (idx, failures[idx].description) for idx in sorted(failures)]111 )112 )...
dict_.py
Source:dict_.py
...40 def __init__(self, key_matcher, value_matcher):41 # type: (EntryMatcher, Matcher) -> None42 self.key_matcher = key_matcher43 self.value_matcher = value_matcher44 def build_short_description(self, transformation):45 ret = transformation('to have entry %s' % self.key_matcher.build_description())46 if self.value_matcher:47 ret += " that " + self.value_matcher.build_short_description(MatcherDescriptionTransformer(conjugate=True))48 return ret49 def build_description(self, transformation):50 ret = transformation('to have entry %s' % self.key_matcher.build_description())51 if self.value_matcher:52 ret += " that " + self.value_matcher.build_description(MatcherDescriptionTransformer(conjugate=True))53 return ret54 def matches(self, actual):55 try:56 value = self.key_matcher.get_entry(actual)57 except KeyError:58 return MatchResult.failure('No entry %s' % self.key_matcher.build_description())59 if self.value_matcher:60 return self.value_matcher.matches(value)61 else:...
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!