Skip to content

Improve python object constructors rendering #2078

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 5, 2023

Conversation

tamarinvs19
Copy link
Collaborator

@tamarinvs19 tamarinvs19 commented Mar 30, 2023

Description

Replaced copyreg._reconstructor by constructor from fuzzer or __new__ method from serializer.

Updated strategy for state before / state after comparison: assertions will generate only if argument have been changed inside a tested function.

Additional changes:

  • group kind and module
  • add field for kwargs
  • improve rendering
  • add comparator for recursive objects

Fixes #2077

How to test

Manual tests

  1. Try to generate tests for a function or method with user object, e.g.
class Node:
    def __init__(self, name: str, children: List[Node]):
        self.name = name
        self.children = children

    def __eq__(self, other):
        if isinstance(other, Node):
            return self.name == other.name
        else:
            return False

    def add_child(self, child: Node):
        self.children.append(child)

Expected constructors: no one copyreg._reconstructor, tests are similar to

class TestNode(unittest.TestCase):
    # region Test suites for executable samples.graph.add_child
    
    # region FUZZER
    
    def test_add_child(self):
        """
        self = samples.graph.Node
        child = samples.graph.Node
        """
        node = samples.graph.Node("foo", [])
        child = samples.graph.Node("abcdefghijklmnopqrst", [])
        child1 = child
        
        actual = node.add_child(child1)
        
        self.assertEqual(None, actual)
        
        child_modified = object.__new__(samples.graph.Node)
        list1 = object.__new__(samples.graph.Node)
        list1.name = 'foo'
        list1.children = [child_modified]
        child_modified.name = 'abcdefghijklmnopqrst'
        child_modified.children = [list1]
        
        self.assertEqual(child_modified, child1)
        
        self.assertEqual(list1, node)
    # endregion
    
    # endregion

Self-check list

Check off the item if the statement is true. Hint: [x] is a marked item.

Please do not delete the list or its items.

  • I've set the proper labels for my PR (at least, for category and component).
  • PR title and description are clear and intelligible.
  • I've added enough comments to my code, particularly in hard-to-understand areas.
  • The functionality I've repaired, changed or added is covered with automated tests.
  • Manual tests have been provided optionally.
  • The documentation for the functionality I've been working on is up-to-date.

* group kind and module
* add field for kwargs
* improve rendering
* add comparator for recursive objects
@tamarinvs19 tamarinvs19 added ctg-enhancement New feature, improvement or change request comp-codegen Issue is related to code generator ctg-refactoring Issue related to refactoring process lang-python Issue is related to python support labels Mar 30, 2023
@tamarinvs19 tamarinvs19 requested a review from tochilinak March 30, 2023 08:43
@tamarinvs19 tamarinvs19 self-assigned this Mar 30, 2023
@tamarinvs19 tamarinvs19 changed the title Replace copyreg._reconstructor by real constructor or __new__ method: Improve python object constructors rendering Mar 30, 2023
@tamarinvs19 tamarinvs19 marked this pull request as draft March 30, 2023 09:24
@tamarinvs19 tamarinvs19 marked this pull request as ready for review March 30, 2023 13:45
@tamarinvs19 tamarinvs19 merged commit 86544b6 into main Apr 5, 2023
@tamarinvs19 tamarinvs19 deleted the tamarinvs19/replace-reconstructor branch April 5, 2023 08:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
comp-codegen Issue is related to code generator ctg-enhancement New feature, improvement or change request ctg-refactoring Issue related to refactoring process lang-python Issue is related to python support
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Improve rendering of python objects
2 participants