# Path: metrics/position.py
# class PosMetric(MetricBase):
#     """Compute the position of functions and methods for the whole source file."""
#     def __init__(self, context):
#         self.name = 'sloc'
#         self._context = context
#         self.reset()
# 
#     def reset(self):
#         """Reset metric counter."""
#         self._positions = []
#         self._line = 1
#         self._curr = None  # current scope we are analyzing
#         self._scope = 0
#         self.language = None
# 
#     def add_scope(self, scope_type, scope_name, scope_start, is_method=False):
#         """we identified a scope and add it to positions."""
#         if self._curr is not None:
#             self._curr['end'] = scope_start - 1  # close last scope
#         self._curr = {
#             'type': scope_type, 'name': scope_name,
#             'start': scope_start, 'end': scope_start
#         }
# 
#         if is_method and self._positions:
#             last = self._positions[-1]
#             if not 'methods' in last:
#                 last['methods'] = []
#             last['methods'].append(self._curr)
#         else:
#             self._positions.append(self._curr)
# 
#     def process_token(self, tok):
#         """count lines and track position of classes and functions"""
#         if tok[0] == Token.Text:
#             count = tok[1].count('\n')
#             if count:
#                 self._line += count  # adjust linecount
# 
#         if self._detector.process(tok):
#             pass  # works been completed in the detector
#         elif tok[0] == Token.Punctuation:
#             if tok[0] == Token.Punctuation and tok[1] == '{':
#                 self._scope += 1
#             if tok[0] == Token.Punctuation and tok[1] == '}':
#                 self._scope += -1
#                 if self._scope == 0 and self._curr is not None:
#                     self._curr['end'] = self._line  # close last scope
#                     self._curr = None
#         elif tok[0] == Token.Name.Class and self._scope == 0:
#             self.add_scope('Class', tok[1], self._line)
#         elif tok[0] == Token.Name.Function and self._scope in [0, 1]:
#             self.add_scope('Function', tok[1], self._line, self._scope == 1)
# 
#     def get_metrics(self):
#         if self._curr is not None:
#             self._curr['end'] = self._line -1
#         for p in self._positions:
#             if 'methods' in p:
#                 for m in p['methods']:
#                     if m['end'] > p['end']:
#                         p['end'] = m['end']
#         return {'block_positions': self._positions}
# 
#     metrics = property(get_metrics)
# 
#     def get_language(self):
#         return self._language
# 
#     def set_language(self, value):
#         self._language = value
#         detector = getattr(sys.modules[__name__], '%sDetector' % value, None)
#         if detector:
#             self._detector = detector(self)
#         else:
#             self._detector = BaseDetector(self)
# 
#     language = property(get_language, set_language)


from pygments.token import Token
from metrics.position import PosMetric
        (Token.Literal.String.Single, u"'"), (Token.Punctuation, u']'),
        (Token.Text, u' '), (Token.Operator, u'+'),
        (Token.Operator, u'='), (Token.Text, u' '),
        (Token.Literal.Number.Integer, u'1'), (Token.Text, u'\n')]

    positions = PosMetric(context={})
    positions.language = 'Python'
    for t in tokens:
        positions.process_token(t)
    assert positions.metrics == {'block_positions': []}


def test_with_python_function():
    # def my_func(p1, p2):
    #     """Work the incredible magic."""
    #     return p1 + p2
    tokens = [
        (Token.Keyword, u'def'), (Token.Text, u' '),
        (Token.Name.Function, u'my_func'), (Token.Punctuation, u'('),
        (Token.Name, u'p1'), (Token.Punctuation, u','), (Token.Text, u' '),
        (Token.Name, u'p2'), (Token.Punctuation, u')'),
        (Token.Punctuation, u':'), (Token.Text, u'\n'),
        (Token.Text, u'    '),
        (Token.Literal.String.Doc, u'"""Work the incredible magic."""'),
        (Token.Text, u'\n'),
        (Token.Text, u'    '), (Token.Keyword, u'return'), (Token.Text, u' '),
        (Token.Name, u'p1'), (Token.Text, u' '), (Token.Operator, u'+'),
        (Token.Text, u' '), (Token.Name, u'p2'), (Token.Text, u'\n')
    ]


# Path: metrics/position.py
# class PosMetric(MetricBase):
#     """Compute the position of functions and methods for the whole source file."""
#     def __init__(self, context):
#         self.name = 'sloc'
#         self._context = context
#         self.reset()
# 
#     def reset(self):
#         """Reset metric counter."""
#         self._positions = []
#         self._line = 1
#         self._curr = None  # current scope we are analyzing
#         self._scope = 0
#         self.language = None
# 
#     def add_scope(self, scope_type, scope_name, scope_start, is_method=False):
#         """we identified a scope and add it to positions."""
#         if self._curr is not None:
#             self._curr['end'] = scope_start - 1  # close last scope
#         self._curr = {
#             'type': scope_type, 'name': scope_name,
#             'start': scope_start, 'end': scope_start
#         }
# 
#         if is_method and self._positions:
#             last = self._positions[-1]
#             if not 'methods' in last:
#                 last['methods'] = []
#             last['methods'].append(self._curr)
#         else:
#             self._positions.append(self._curr)
# 
#     def process_token(self, tok):
#         """count lines and track position of classes and functions"""
#         if tok[0] == Token.Text:
#             count = tok[1].count('\n')
#             if count:
#                 self._line += count  # adjust linecount
# 
#         if self._detector.process(tok):
#             pass  # works been completed in the detector
#         elif tok[0] == Token.Punctuation:
#             if tok[0] == Token.Punctuation and tok[1] == '{':
#                 self._scope += 1
#             if tok[0] == Token.Punctuation and tok[1] == '}':
#                 self._scope += -1
#                 if self._scope == 0 and self._curr is not None:
#                     self._curr['end'] = self._line  # close last scope
#                     self._curr = None
#         elif tok[0] == Token.Name.Class and self._scope == 0:
#             self.add_scope('Class', tok[1], self._line)
#         elif tok[0] == Token.Name.Function and self._scope in [0, 1]:
#             self.add_scope('Function', tok[1], self._line, self._scope == 1)
# 
#     def get_metrics(self):
#         if self._curr is not None:
#             self._curr['end'] = self._line -1
#         for p in self._positions:
#             if 'methods' in p:
#                 for m in p['methods']:
#                     if m['end'] > p['end']:
#                         p['end'] = m['end']
#         return {'block_positions': self._positions}
# 
#     metrics = property(get_metrics)
# 
#     def get_language(self):
#         return self._language
# 
#     def set_language(self, value):
#         self._language = value
#         detector = getattr(sys.modules[__name__], '%sDetector' % value, None)
#         if detector:
#             self._detector = detector(self)
#         else:
#             self._detector = BaseDetector(self)
# 
#     language = property(get_language, set_language)


# Path: tests/test_positions_python.py
from pygments.token import Token
from metrics.position import PosMetric

        (Token.Literal.String.Single, u"'"), (Token.Punctuation, u']'),
        (Token.Text, u' '), (Token.Operator, u'+'),
        (Token.Operator, u'='), (Token.Text, u' '),
        (Token.Literal.Number.Integer, u'1'), (Token.Text, u'\n')]

    positions = PosMetric(context={})
    positions.language = 'Python'
    for t in tokens:
        positions.process_token(t)
    assert positions.metrics == {'block_positions': []}


def test_with_python_function():
    # def my_func(p1, p2):
    #     """Work the incredible magic."""
    #     return p1 + p2
    tokens = [
        (Token.Keyword, u'def'), (Token.Text, u' '),
        (Token.Name.Function, u'my_func'), (Token.Punctuation, u'('),
        (Token.Name, u'p1'), (Token.Punctuation, u','), (Token.Text, u' '),
        (Token.Name, u'p2'), (Token.Punctuation, u')'),
        (Token.Punctuation, u':'), (Token.Text, u'\n'),
        (Token.Text, u'    '),
        (Token.Literal.String.Doc, u'"""Work the incredible magic."""'),
        (Token.Text, u'\n'),
        (Token.Text, u'    '), (Token.Keyword, u'return'), (Token.Text, u' '),
        (Token.Name, u'p1'), (Token.Text, u' '), (Token.Operator, u'+'),
        (Token.Text, u' '), (Token.Name, u'p2'), (Token.Text, u'\n')
    ]


    positions = PosMetric(context={})