show / test / test_show.py

import pytest
from show import Show
import sys

PY3 = sys.version_info[0] == 3

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

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(1 + 1) == '1 + 1: 2'
    assert nospaces(show(1+1)) == nospaces('1 + 1: 2')
        # may have extra spaces in it, based on codegen.to_source() output
        
    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'
    
    # assert nospaces(show(1+1)) == nospaces('1 + 1: 2')
    
    # NB output may have more or fewer spaces than actual parameter, based on codegen.to_source() 
    # creating its 'idealized' code output

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, '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'"
    
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: name='Joe' namer='joe' _name='JOE'"
    
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.