def parse_tolerance(source, want): """ Returns a version of ``want`` marked up with the tolerance tags specified in ``source``. INPUT: - ``source`` -- a string, the source of a doctest - ``want`` -- a string, the desired output of the doctest OUTPUT: - ``want`` if there are no tolerance tags specified; a :class:`MarkedOutput` version otherwise. EXAMPLES:: sage: from sage.doctest.parsing import parse_tolerance sage: marked = parse_tolerance("sage: s.update(abs_tol = .0000001)", "") sage: type(marked) <type 'str'> sage: marked = parse_tolerance("sage: s.update(tol = 0.1); s.rel_tol # abs tol 0.01", "") sage: marked.tol 0 sage: marked.rel_tol 0 sage: marked.abs_tol 0.01 """ safe, literals, state = strip_string_literals(source) first_line = safe.split('\n', 1)[0] if '#' not in first_line: return want comment = first_line[first_line.find('#')+1:] comment = comment[comment.index('(')+1 : comment.rindex(')')] # strip_string_literals replaces comments comment = literals[comment] if random_marker.search(comment): want = MarkedOutput(want).update(random=True) else: m = tolerance_pattern.search(comment) if m: rel_or_abs, epsilon = m.groups() if epsilon is None: epsilon = 1e-15 else: epsilon = float(epsilon.strip()) if rel_or_abs is None: want = MarkedOutput(want).update(tol=epsilon) elif rel_or_abs.startswith('rel'): want = MarkedOutput(want).update(rel_tol=epsilon) elif rel_or_abs.startswith('abs'): want = MarkedOutput(want).update(abs_tol=epsilon) else: raise RuntimeError return want
def parse_tolerance(source, want): """ Returns a version of ``want`` marked up with the tolerance tags specified in ``source``. INPUT: - ``source`` -- a string, the source of a doctest - ``want`` -- a string, the desired output of the doctest OUTPUT: - ``want`` if there are no tolerance tags specified; a :class:`MarkedOutput` version otherwise. EXAMPLES:: sage: from sage.doctest.parsing import parse_tolerance sage: marked = parse_tolerance("sage: s.update(abs_tol = .0000001)", "") sage: type(marked) <type 'str'> sage: marked = parse_tolerance("sage: s.update(tol = 0.1); s.rel_tol # abs tol 0.01 ", "") sage: marked.tol 0 sage: marked.rel_tol 0 sage: marked.abs_tol 0.010000000000000000000? """ safe, literals, state = strip_string_literals(source) first_line = safe.split('\n', 1)[0] if '#' not in first_line: return want comment = first_line[first_line.find('#') + 1:] comment = comment[comment.index('(') + 1:comment.rindex(')')] # strip_string_literals replaces comments comment = literals[comment] if random_marker.search(comment): want = MarkedOutput(want).update(random=True) else: m = tolerance_pattern.search(comment) if m: rel_or_abs, epsilon = m.groups() if epsilon is None: epsilon = RIFtol("1e-15") else: epsilon = RIFtol(epsilon) if rel_or_abs is None: want = MarkedOutput(want).update(tol=epsilon) elif rel_or_abs.startswith('rel'): want = MarkedOutput(want).update(rel_tol=epsilon) elif rel_or_abs.startswith('abs'): want = MarkedOutput(want).update(abs_tol=epsilon) else: raise RuntimeError return want
def parse_optional_tags(string): """ Returns a set consisting of the optional tags from the following set that occur in a comment on the first line of the input string. - 'long time' - 'not implemented' - 'not tested' - 'known bug' - 'optional: PKG_NAME' -- the set will just contain 'PKG_NAME' EXAMPLES:: sage: from sage.doctest.parsing import parse_optional_tags sage: parse_optional_tags("sage: magma('2 + 2')# optional: magma") set(['magma']) sage: parse_optional_tags("sage: #optional -- mypkg") set(['mypkg']) sage: parse_optional_tags("sage: print(1) # parentheses are optional here") set([]) sage: parse_optional_tags("sage: print(1) # optional") set(['']) sage: sorted(list(parse_optional_tags("sage: #optional -- foo bar, baz"))) ['bar', 'foo'] sage: sorted(list(parse_optional_tags(" sage: factor(10^(10^10) + 1) # LoNg TiME, NoT TeSTED; OptioNAL -- P4cka9e"))) ['long time', 'not tested', 'p4cka9e'] sage: parse_optional_tags(" sage: raise RuntimeError # known bug") set(['bug']) sage: sorted(list(parse_optional_tags(" sage: determine_meaning_of_life() # long time, not implemented"))) ['long time', 'not implemented'] We don't parse inside strings:: sage: parse_optional_tags(" sage: print ' # long time'") set([]) sage: parse_optional_tags(" sage: print ' # long time' # not tested") set(['not tested']) UTF-8 works:: sage: parse_optional_tags("'ěščřžýáíéďĎ'") set([]) """ safe, literals, state = strip_string_literals(string) first_line = safe.split('\n', 1)[0] if '#' not in first_line: return set() comment = first_line[first_line.find('#')+1:] comment = comment[comment.index('(')+1 : comment.rindex(')')] # strip_string_literals replaces comments comment = "#" + (literals[comment]).lower() tags = [] for m in optional_regex.finditer(comment): cmd = m.group(1) if cmd == 'known bug': tags.append('bug') # so that such tests will be run by sage -t ... -only-optional=bug elif cmd: tags.append(cmd) else: tags.extend(m.group(3).split() or [""]) return set(tags)
def parse_optional_tags(string): """ Returns a set consisting of the optional tags from the following set that occur in a comment on the first line of the input string. - 'long time' - 'not implemented' - 'not tested' - 'known bug' - 'optional: PKG_NAME' -- the set will just contain 'PKG_NAME' EXAMPLES:: sage: from sage.doctest.parsing import parse_optional_tags sage: parse_optional_tags("sage: magma('2 + 2')# optional: magma") {'magma'} sage: parse_optional_tags("sage: #optional -- mypkg") {'mypkg'} sage: parse_optional_tags("sage: print(1) # parentheses are optional here") set() sage: parse_optional_tags("sage: print(1) # optional") {''} sage: sorted(list(parse_optional_tags("sage: #optional -- foo bar, baz"))) ['bar', 'foo'] sage: sorted(list(parse_optional_tags(" sage: factor(10^(10^10) + 1) # LoNg TiME, NoT TeSTED; OptioNAL -- P4cka9e"))) ['long time', 'not tested', 'p4cka9e'] sage: parse_optional_tags(" sage: raise RuntimeError # known bug") {'bug'} sage: sorted(list(parse_optional_tags(" sage: determine_meaning_of_life() # long time, not implemented"))) ['long time', 'not implemented'] We don't parse inside strings:: sage: parse_optional_tags(" sage: print ' # long time'") set() sage: parse_optional_tags(" sage: print ' # long time' # not tested") {'not tested'} UTF-8 works:: sage: parse_optional_tags("'ěščřžýáíéďĎ'") set() """ safe, literals, state = strip_string_literals(string) first_line = safe.split('\n', 1)[0] if '#' not in first_line: return set() comment = first_line[first_line.find('#') + 1:] comment = comment[comment.index('(') + 1:comment.rindex(')')] # strip_string_literals replaces comments comment = "#" + (literals[comment]).lower() tags = [] for m in optional_regex.finditer(comment): cmd = m.group(1) if cmd == 'known bug': tags.append( 'bug' ) # so that such tests will be run by sage -t ... -only-optional=bug elif cmd: tags.append(cmd) else: tags.extend(m.group(3).split() or [""]) return set(tags)