sapling/tests/test-argspans.py
Jun Wu dd1b0586c0 testutil: add a utility to calculate argument positions
Summary:
The utility is used by a later change which provides the "autofix" feature for
Python code.

Reviewed By: xavierd

Differential Revision: D16168823

fbshipit-source-id: feb55a9e6ba5e78ad0f490cadeeafbcc1306e8ca
2019-07-17 21:11:27 -07:00

67 lines
1.4 KiB
Python

# Copyright 2019 Facebook, Inc.
#
# This software may be used and distributed according to the terms of the
# GNU General Public License version 2 or any later version.
# fmt: off
from __future__ import absolute_import
import os
import sys
from testutil import argspans
try:
import parso
parso.parse
except ImportError:
sys.stderr.write("skipped: missing feature: parso\n")
sys.exit(80)
def testfunc():
def foo(x, y):
filepath, lineno, indent, spans = argspans.argspans()
assert os.path.basename(filepath) == "test-argspans.py"
assert lineno == 34
assert indent == 12
assert spans == [((34, 16), (34, 17)), ((34, 19), (34, 24))]
if True:
if True:
foo(1, "abc")
def nested(x, y):
def inner1(x, y):
inner2(x, y)
def inner2(x, y):
filepath, lineno, indent, spans = argspans.argspans(nested=2)
assert lineno == 48
assert indent == 4
assert spans == [((48, 11), (48, 13)), ((48, 15), (48, 20))]
inner1(x, y)
nested(42, "def")
def testoperator():
class A(object):
def __eq__(self, rhs):
filepath, lineno, indent, spans = argspans.argspans()
assert indent == 4
assert spans == [((58, 11), (60, 7))]
A() == """multi
line
"""
testfunc()
testoperator()
# fmt: on