show / test / test_show.py

import pytest
from show import Show
import sys, six

_PY3 = sys.version_info[0] == 3

show = Show(where=False, retvalue=True)

global_var = 'hey!'

def nospaces(s):
    return s.replace(' ', '')
    
def test_basic(param = 'Yo'):

    a = 1
    b = 3.141
    c = "something else!"
    
    assert show(a) == 'a: 1'
    assert show(a, b) == 'a: 1  b: 3.141'
    assert show(a, b, c) == "a: 1  b: 3.141  c: 'something else!'"
        
    assert show(len(c), c) == \
                "len(c): 15  c: 'something else!'"
    
            # anything with paraens in it must be on separate line, given weak parser
            # of show parameters
            
def test_literals():
    assert show(1 + 1) == '1 + 1: 2'
    assert show(1+1) == '1 + 1: 2'
    
    # for when using astor as codegen replacement
    # assert show(1 + 1) == '(1 + 1): 2'
    # assert show(1+1) == '(1 + 1): 2'
    
    # NB output may have more or fewer spaces than actual parameter, based on codegen.to_source() 
    # creating its 'idealized' code output
    
    # when we were using codegen to recreate source, it did so without parenthesis
    # astor uses parens -- which seem superflous here...but printing literals isn't
    # the main use case, so whatever...

def test_say_params():
    a = 1
    b = 3.141
    c = "something else!"
    
    assert show(a, indent='+1') == '    a: 1'
    assert show(a, b, sep='\n') == 'a: 1\nb: 3.141'

def test_strings():
    assert show('this') == "this"
    x = 44
    assert show("x = {x}") == 'x = 44'
    
def test_string_example():
    
    n = 14312
    assert show("{n} iterations, still running") == "14312 iterations, still running"

    s = '{n} iterations'
    assert show(s) == "s: '{n} iterations'"
        
def test_set():
    
    s = set([1,2,99])
    
    # native literals for sets different in py2 and py3
    if _PY3:
        assert show(s) == "s: {1, 2, 99}"
    else:
        assert show(s) == "s: set([1, 2, 99])"
    
def test_show_example():
    
    x = 12
    nums = list(range(4))

    assert show(x) == 'x: 12'
    assert show(nums) == 'nums: [0, 1, 2, 3]'
    assert show(x, nums, len(nums)) == \
        'x: 12  nums: [0, 1, 2, 3]  len(nums): 4'
    assert show(x, nums, len(nums), indent='+1') == \
        '    x: 12  nums: [0, 1, 2, 3]  len(nums): 4'
    assert show(x, nums, len(nums), sep='\n') == \
        'x: 12\nnums: [0, 1, 2, 3]\nlen(nums): 4'
    assert show(x, nums, len(nums), sep='\n', indent=1) == \
        '    x: 12\n    nums: [0, 1, 2, 3]\n    len(nums): 4'

def test_show_items():
    nums = list(range(4))
    assert show.items(nums) == \
        'nums (4 items): [0, 1, 2, 3]'
    
    astring = 'this'
    assert show.items(astring) == \
        "astring (4 chars): 'this'"
    
    d = {'a': 1, 'b': 2}
    fstr =  show.items(d)
    assert fstr == "d (2 items): {'a': 1, 'b': 2}" or fstr == "d (2 items): {'b': 2, 'a': 1}" 
    
def test_show_props():
    
    class O(object):
        name    = None
        age     = None
        address = None
    o = O()
    o.name = 'An Object'
    o.age  = 2
    o.address = 'RAM'
    #assert show.props(o) == "o: address='RAM' age=2 name='An Object'"
    #assert show.props(o, 'name,age,address') == "o: name='An Object' age=2 address='RAM'"
    #assert show.props(o, 'name,age') == "o: name='An Object' age=2"
    assert show.props(o) == "o:\n    address='RAM'\n    age=2\n    name='An Object'"
    assert show.props(o, 'name,age,address') == "o:\n    name='An Object'\n    age=2\n    address='RAM'"
    assert show.props(o, 'name,age') == "o:\n    name='An Object'\n    age=2"
    
    assert show.props(o, 'name,age') == show(o, props='name,age')
        
    # Other, not (yet, at any rate) API options
    
    # v0 = show.props(o)
    # assert show(props(o)) == v0
    
    # v1 = show.props(o, 'name,age')
    # assert show(props(o, 'name,age')) == v1
    
def test_show_props2():
    
    class OO(object):
        def __init__(self, name, age, address):
            self.name = name
            self._name = name.upper()
            self.age  = age
            self.address = address
        def test(self):
            return self.age > 17
        def best(self):
            return self.name.capitalize()
    
    oo = OO('Joe', 36, 'Waverly Place')

    # assert show.props(oo) == "oo: address='Waverly Place' age=36 name='Joe' _name='JOE'"
    assert show.props(oo) == "oo:\n    address='Waverly Place'\n    age=36\n    name='Joe'\n    _name='JOE'"
    
def test_show_real_properties():
        
    class P(object):
        def __init__(self, name):
            self._name = name.upper()
            self.name = name
        @property
        def namer(self):
            return self._name.lower()
        def named(self):
            return self.name
    
    p = P('Joe')
    assert show.props(p) == "p:\n    name='Joe'\n    namer='joe'\n    _name='JOE'"
    
def test_show_locals():
    x = 1
    y = 2
    assert show.locals() == 'x: 1  y: 2'
    
    a = 22
    b = 23
    assert show.locals(omit='x') == 'a: 22  b: 23  y: 2'
    # assert show.locals(global_var) == "global_var: 'hey!'  x: 1  y: 2"
    
def test_show_watch():
    x = 1
    assert show.watch() == 'x: 1'
    y = 2
    assert show.watch() == 'y: 2'
    x = 4
    assert show.watch() == 'x: 4'
    x = 5
    y = 3
    _z = 99
    assert show.watch() == 'x: 5  y: 3'
    assert show.watch() == six.u('\u2205')
    assert show.watch(_z) == '_z: 99'
    assert show.watch() == six.u('\u2205')
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.