Skip to content
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

Idle: mock Text class and test thereof #62565

Closed
terryjreedy opened this issue Jul 5, 2013 · 21 comments
Closed

Idle: mock Text class and test thereof #62565

terryjreedy opened this issue Jul 5, 2013 · 21 comments
Assignees
Labels
topic-IDLE type-feature A feature request or enhancement

Comments

@terryjreedy
Copy link
Member

BPO 18365
Nosy @terryjreedy, @ncoghlan, @bitdancer, @rovitotv
Files
  • mock_text.patch
  • mock_text2.diff: (tjr)
  • mock_text5.diff
  • mock_text6.diff
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = 'https://github.com/terryjreedy'
    closed_at = <Date 2013-07-13.22:37:23.730>
    created_at = <Date 2013-07-05.06:49:06.917>
    labels = ['expert-IDLE', 'type-feature']
    title = 'Idle: mock Text class and test thereof'
    updated_at = <Date 2013-07-13.22:37:23.728>
    user = 'https://github.com/terryjreedy'

    bugs.python.org fields:

    activity = <Date 2013-07-13.22:37:23.728>
    actor = 'terry.reedy'
    assignee = 'terry.reedy'
    closed = True
    closed_date = <Date 2013-07-13.22:37:23.730>
    closer = 'terry.reedy'
    components = ['IDLE']
    creation = <Date 2013-07-05.06:49:06.917>
    creator = 'terry.reedy'
    dependencies = []
    files = ['30851', '30865', '30898', '30899']
    hgrepos = []
    issue_num = 18365
    keywords = ['patch']
    message_count = 21.0
    messages = ['192327', '192328', '192610', '192700', '192912', '192915', '192916', '192921', '192926', '192930', '192934', '192982', '192983', '192987', '192995', '192999', '193007', '193008', '193011', '193012', '193016']
    nosy_count = 7.0
    nosy_names = ['terry.reedy', 'ncoghlan', 'r.david.murray', 'Todd.Rovito', 'python-dev', 'JayKrish', 'philwebster']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue18365'
    versions = ['Python 2.7', 'Python 3.3', 'Python 3.4']

    @terryjreedy
    Copy link
    Member Author

    test_rstrip2.patch contain a Text class to be added to mock_tk.py. The purpose of a mock class is to imitate another class with respect to certain behaviors. The way to verify that is does that is to run the same set of tests for those behaviors with both. This is similar in idea and will be the same in execution as running the same set of tests with a Python-coded module and a c-coded accelerator version of at least some functions of the module. The trick is to do this without code duplication is write a mixin class with the tests and derive two testcase classes using the mixin.

    @terryjreedy terryjreedy self-assigned this Jul 5, 2013
    @terryjreedy terryjreedy added topic-IDLE type-feature A feature request or enhancement labels Jul 5, 2013
    @terryjreedy
    Copy link
    Member Author

    The rstrip patch is with bpo-18279. Design discussion occurred on bpo-18226. Both issues and other future issues depend on this one.

    @philwebster
    Copy link
    Mannequin

    philwebster mannequin commented Jul 8, 2013

    Added Text class to mock_tk.py and GUI/non-GUI tests in test_text.py. Running the IDLE tests produced no errors for me.

    @terryjreedy
    Copy link
    Member Author

    Great start! Some revisions:

    • Create root directly (and just once); add root.destroy, which added several warnings.
    • Create Text directly; test should not require Editors. This removed warnings. I suspect that EditorWindow.py does other things that are not properly undone. In any case, tests should only create needed objects.
    • Worked on docstrings; polished _decode.
    • Revised .insert to handle '' and 'a....z\n' and added corresponding tests.
    • Consistently used 'end' instead of 'end' here and 'END' there.
    • Added test_no_delete and edited mock delete to pass.
    • Finished compare methods and added tests.
    • Deleted non-tk .setDate/.getData, which only slightly abbreviate the real .get and .insert methods. I notice that Phil already changed the FormatParagraph test to not use them.

    Here is the result. I think it is about ready to commit after sleeping on it and a final review.

    @terryjreedy
    Copy link
    Member Author

    Upon further review and tests, I notice that ._decode

    • is a partial implementation of Text.index,
    • so it should be renamed index and tested against Text.index,
    • and it currently returns the wrong values for 'end' and high out-of-bounds indexes.

    Tests with 'end' only pass now because the tested methods ignore the buggy decode for 'end' and special-case it instead. When I added tests with out of bounds indexes, they failed. I am working now on fixing all this.

    Spaces are supposed to follow arg-separating commas in function calls. The initial patch lacked them and I have added them. I need them because they make code easier for *me* to read and review, which is an example of why PEP-8 requires them.

    @terryjreedy
    Copy link
    Member Author

    Whoops, I got that partly wrong in that index returns a 'r.c' string after doing what _decode is supposed to do, which is to decode the input according to the current text. The mock index should just return '%s.%s' % _decode(position). There is still the point that index and _decode now give different coordinates for 'end' and that _decode should be tested by testing mock index, which wraps it, against tk index.

    Since tk.Text args can be passed by name, the mock methods should use the same parameter names.

    @philwebster
    Copy link
    Mannequin

    philwebster mannequin commented Jul 11, 2013

    Thanks Terry, I will start PEP-8-checking my code before I submit (as well as testing more thoroughly). I was thinking the same thing about the logic behind _decode and index functions needing to be combined.

    How would you recommend adding functionality to the decode function to handle expressions like "1.2 +5 chars" or "1.0 lineend-5c"? AutoComplete.py (bpo-18409) uses this kind of index, so it will be needed soon. I'm thinking that splitting off the base index then parsing the remaining modifier expressions is the way to go.

    I was also thinking that a _checkIndex method would be good to have especially when using the +/-line and +/-char expressions.

    @terryjreedy
    Copy link
    Member Author

    The real problem with 'end' and 'big.m' is that there are three proper decodings, depending on the method asking. I think this patch mostly solves the problems mentioned before. All tests, including many new ones, pass.

    @terryjreedy
    Copy link
    Member Author

    A little more polishing and the tests pass again. I think I am done with the implemented methods. Before I commit, I want to review the set of 'pass' functions. It seems a bit haphazard. I think display changing methods like .see are fine.I am not sure about including internal operation methods like .mark_set (until actually needed).

    @terryjreedy
    Copy link
    Member Author

    I want to commit this patch more or less as is so we can move forward with the tests that do not need anything more. We can then elaborate mock Text as needed and desired.

    Looking at http://effbot.org/tkinterbook/text.htm, for instance, I see that 'lineend' is an expression modifier in that same category as +/- chars/lines. So _decode should not really handle it until it handles such modifiers more generally.

    As near as I could tell from experiment, 'm.n lineend' is the same as 'm.end', so the former is never needed and could be changed to the latter in Idle code. As far as you know, correct? The use for 'lineend' would seem to be after other modifiers, as in '1.0 +100c lineend', where the line whose end we go to is likely not line 1.

    Remember that for advanced Text use, we have the option of using tk.Text for a test, at least at first. The gain would be not having to reproduce baroque behavior*. The cost would be making the test a gui test and running on fewer buildbots. I felt that the basic mock done so far would be worth the time because a) it would be useful for many tests and b) I would learn the Text widget and how to experiment with it to determine its under-documented behavior.

    *For instance, having optional spaces within modifiers as well as using them to separate modifiers. Do you know if there is a coherent index grammar anywhere? Or is it only defined by the Text.index C code?

    @philwebster
    Copy link
    Mannequin

    philwebster mannequin commented Jul 12, 2013

    Using tk.Text for more involved tests sounds good (at least as a start). The pass functions were used to get the FormatParagraph test (bpo-18226) working.

    I've been using the same effbot.org site as a reference and haven't found anything more detailed.

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Jul 13, 2013

    New changeset 5ac2ec0a34a5 by Terry Jan Reedy in branch '2.7':
    Issue bpo-18365: Add mock Text class and test thereof versus tk.Text.
    http://hg.python.org/cpython/rev/5ac2ec0a34a5

    New changeset 8f13fb4c5826 by Terry Jan Reedy in branch '3.3':
    Issue bpo-18365: Add mock Text class and test thereof versus tk.Text.
    http://hg.python.org/cpython/rev/8f13fb4c5826

    New changeset 5611d3a954f8 by Terry Jan Reedy in branch '3.3':
    Issue bpo-18365: normalize whitespace
    http://hg.python.org/cpython/rev/5611d3a954f8

    New changeset 86cc1983a94d by Terry Jan Reedy in branch '2.7':
    Issue bpo-18365: normalize whitespace
    http://hg.python.org/cpython/rev/86cc1983a94d

    @terryjreedy
    Copy link
    Member Author

    I documented and left most of the 'pass' methods. The bind method is inherited from Misc class and is not specifically a Text method, but I will leave it here for now. These

        def undo_block_start(self, *args):
            pass
    
        def undo_block_stop(self, *args):
            pass

    are not Text methods, so I removed them. We can discuss what you intended as part of the FormatParagraph issue.

    @bitdancer bitdancer reopened this Jul 13, 2013
    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Jul 13, 2013

    New changeset 008d83c4efaf by Terry Jan Reedy in branch '2.7':
    Issue bpo-18365: 2.7 corrections so tests run
    http://hg.python.org/cpython/rev/008d83c4efaf

    @terryjreedy
    Copy link
    Member Author

    Well, that is annoying. I expected 2.7 failures due to mistakes already corrected, but not with 3.x, and these look to be the latter.

    ======================================================================
    ERROR: setUpClass (idlelib.idle_test.test_text.TkTextTest)
    ----------------------------------------------------------------------

    Traceback (most recent call last):
      File "/home/buildbot/buildarea/3.x.coghlan-redhat/build/Lib/idlelib/idle_test/test_text.py", line 219, in setUpClass
        cls.root = Tk()
      File "/home/buildbot/buildarea/3.x.coghlan-redhat/build/Lib/tkinter/__init__.py", line 1789, in __init__
        self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
    _tkinter.TclError: no display name and no $DISPLAY environment variable

    I do not get this on my system, and I just retried with
    F:\Python\dev\py34\PCbuild>python_d -m test -v -ugui test_idle
    and that works.

    I did get a test_idle failure running all tests
    and a crash with test_marshal in test_bad_reader (test.test_marshal.BugsTestCase)
    so I am rerunning with -v. This claims the problem is

    _tkinter.TclError: Can't find a usable init.tcl in the following directories:

    This is followed by a list of non-existent directories that omits the one what it actually is and where it is routinely found and it obviously was on the previous run with only test_idle run.

    What puzzles me is that both buildbots skipped test_tk and test_ttk_guionly, which suggests that they do not have -ugui, (Nick, one of these is yours) which means they should not have ever called tkinter. On my machine,
    requires('gui')
    works to stop the tkinter test case from running when I omit -ugui.

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Jul 13, 2013

    New changeset bc3a34e47923 by Terry Jan Reedy in branch '3.3':
    Issue bpo-18365: convert buildbot errors to skips.
    http://hg.python.org/cpython/rev/bc3a34e47923

    New changeset ba4c826848d5 by Terry Jan Reedy in branch '2.7':
    Issue bpo-18365: convert buildbot errors to skips.
    http://hg.python.org/cpython/rev/ba4c826848d5

    @terryjreedy
    Copy link
    Member Author

    Looking through the 11 stable 3.x buildbots (1 still running), I see 3 outcomes.

    1. Windows: test seemed to run, test_idle listed in 'altered environment'.
    2. Some *nix: test gave no error, test_idle lit in 'tests skipped'. I presume this means that at least one thing was skipped, not the entire idle suite.
    3. Other *nix: no test failed, but the one test case setup gave the tclerror. This also includes the 'murray' machine. Yours, David?

    _tkinter.create just calls Tkapp_New with the same args and the message comes from tcl/tk. I do not know if 'display name' is the same as the 'screenName' parameter or not. The doc string just says "Return a new Toplevel widget on screen SCREENNAME." Is this something to do with X11? If so, why isn't $DISPLAY set? Passing screenName='fake' has no effect on my Win7 machine.

    I think the following patch should quiet things for now, even if not the best patch.

    @bitdancer
    Copy link
    Member

    Yeah, DISPLAY is how unix finds the X11 display. I don't remember how the tkguionly tests deal with that being missing, but since the tests are run "headless" there is indeed no DISPLAY for the tests to talk to.

    @bitdancer
    Copy link
    Member

    Looks like ttk_guionly gets skipped on my buildbot because of the following code at the top of test_ttk_guionly:

    try:
    ttk.Button()
    except TclError as msg:
    # assuming ttk is not available
    raise unittest.SkipTest("ttk not available: %s" % msg)

    So your skip fix may actually be the best solution.

    @terryjreedy
    Copy link
    Member Author

    The change worked, so this issue can be closed. I opened a new issue, bpo-18441, for dealing with the problem in test_idle by moving the TclError check there, so it does not arise in other idle_test/test_*.py files.

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    topic-IDLE type-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants