<commit_before>from nose.tools import eq_
import itertools as it
import collections
from nbdiff.diff import (
    add_results,
    find_candidates,
    find_matches,
    process_col,
    check_match,
    lcs,
    diff_points,
    create_grid,
    diff,
)

def test_diff():
    A = [{u'input':[u'x = [1,3,4]\n', u'z = {1, 2, 3} \n', u'\n', u'm']},{u'input':[u'x = [1,3,3]\n', u'z = {1, 2, 3} \n', u'\n', u'z']}]
    B = [{u'input':[u'x = [1,3,4]\n', u'z = {1, 2, 3} \n', u'\n', u'm']}]
    result = diff(A, B)
    expected = [
        {"state": 'unchanged', 'value': {u'input': [u'x = [1,3,4]\n', u'z = {1, 2, 3} \n', u'\n', u'm']}},
        {"state": 'deleted', 'value': {u'input': [u'x = [1,3,3]\n', u'z = {1, 2, 3} \n', u'\n', u'z']}}
    ]
    eq_(result, expected)
    #diff("aaaaaaaaaaaaaaaaaaaa", "bbbbbbaaaaaaaaaaabbbbbbbbbbb")
    #diff("cabcdef", "abdef")
    #diff("ca", "abdef")
'''
def test_count_similar_lines():
    A = {u'input':[u'x = [1,3,3]\n', u'z = {1, 2, 3} \n', u'\n', u'z']}
    B = {u'input':[u'x = [1,3,4]\n', u'z = {1, 2, 3} \n', u'\n', u'm']}
    result = count_similar_lines(A, B)

    expected = 2
    eq_(result, expected)
'''
def test_create_grid():
    A = "abcabba"
    B = "cbabac"
    expected = [
        # c      b     a     b      a      c
        [False, False, True, False, True, False],  # a
        [False, True, False, True, False, False],  # b
        [True, False, False, False, False, True],  # c
        [False, False, True, False, True, False],  # a
        [False, True, False, True, False, False],  # b
        [False, True, False, True, False, False],  # b
        [False, False, True, False, True, False]   # a
    ]
    grid = create_grid(A, B)
    eq_(grid, expected)

    A, B = ("cabcdef", "abdef")
    grid = create_grid(A, B)
    assert len([True for col in grid if len(col) == 0]) == 0
'''
def test_check_modified():
    A = {u'input':[u'x = [1,3,3]\n', u'z = {1, 2, 3} \n', u'\n', u'z']}
    B = {u'input':[u'x = [1,3,4]\n', u'z = {1, 2, 3} \n', u'\n', u'm']}

    result = check_modified(A, B)
    expected = True
    eq_(result, expected)
'''
def test_diff_points():
    A = [u'x = [1,3,3]\n', u'z = {1, 2, 3} \n', u'\n', u'z']
    B = [u'x = [1,3,4]\n', u'z = {1, 2, 3} \n', u'\n', u'm']

    grid = create_grid(A,B)

    result = diff_points(grid)

    expected = [
        ('deleted', 0, None),
        ('added', None, 0),
        ('unchanged', 1, 1),
        ('unchanged', 2, 2),
        ('deleted', 3, None),
        ('added', None, 3),
    ]
    eq_(result, expected)

def test_find_candidates():
    grid = [
        [False, False, True, False, True, False],
        [False, True, False, True, False, False],
        [True, False, False, False, False, True],
        [False, False, True, False, True, False],
        [False, True, False, True, False, False],
        [False, True, False, True, False, False],
        [False, False, True, False, True, False]
    ]
    result = find_candidates(grid)
    expected = {
        1: [(0, 2), (1, 1), (2, 0)],
        2: [(1, 3), (3, 2), (4, 1)],
        3: [(2, 5), (3, 4), (4, 3), (6, 2)],
        4: [(6, 4)],
    }
    eq_(result, expected)

    grid = [
        [False, True, True],
        [False, True, True],
        [False, True, True],
        [False, True, True],
        [False, True, True],
        [False, True, True],
        [False, True, True]
    ]
    result = find_candidates(grid)
    expected = {1: [(0, 1)], 2: [(1, 2)]}
    eq_(result, expected)

def test_lcs():
    grid = [
        [False, False, True, False, True, False],
        [False, True, False, True, False, False],
        [True, False, False, False, False, True],
        [False, False, True, False, True, False],
        [False, True, False, True, False, False],
        [False, True, False, True, False, False],
        [False, False, True, False, True, False]
    ]
    result = lcs(grid)
    expected = [(1, 1), (3, 2), (4, 3), (6, 4)]
    eq_(result, expected)

    grid = [
        [False, False, True, False, True, False],
        [False, False, False, True, False, False],
        [True, False, False, False, False, True],
        [False, False, True, False, True, False],
        [False, True, False, True, False, False],
        [False, True, False, True, False, False],
        [False, False, True, False, True, False]
    ]
    result = lcs(grid)
    expected = [(2, 0), (3, 2), (4, 3), (6, 4)]
    eq_(result, expected)

    grid = [
        [True, True, True, True, True, True],
        [True, True, True, True, True, True],
        [True, True, True, True, True, True],
        [True, True, True, True, True, True],
        [True, True, True, True, True, True],
        [True, True, True, True, True, True],
        [True, True, True, True, True, True]
    ]
    result = lcs(grid)
    expected = [(0, 0), (1, 1), (2, 2), (3, 3), (4, 4), (5, 5)]
    eq_(result, expected)

    grid = [
        [False, True, True],
        [False, True, True],
        [False, True, True],
        [False, True, True],
        [False, True, True],
        [False, True, True],
        [False, True, True]
    ]
    result = lcs(grid)
    expected = [(0, 1), (1, 2)]
    eq_(result, expected)

def test_add_results():
    k = {1: [(0, 2)]}
    newk = {1: [(1, 1)], 2: [(1, 3)]}
    result = add_results(k, newk)
    expected = {1: [(0, 2), (1, 1)], 2: [(1, 3)]}
    eq_(result, expected)

def test_find_matches():
    A = "abcabba"
    B = "cbabac"
    prod = list(it.product(A, B))
    grid = [
        [
            a == b
            for (a, b) in
            prod
        ][i * len(B):i * len(B) + len(B)]
        for i in range(len(A))
    ]
    colNum = 0
    result = find_matches(grid[colNum], colNum)
    expected = [(0, 2), (0, 4)]
    eq_(result, expected)

def test_process_col():
    d = {1: [(0, 2)]}
    a = [False, True, False, True, False, False]
    col = 1
    expected = {1: [(1, 1)], 2: [(1, 3)]}
    result = process_col(d, a, col)
    eq_(result, expected)

    d = {}
    a = [False, True, False, True, False, False]
    col = 1
    expected = {1: [(1, 1)]}
    result = process_col(d, a, col)
    eq_(result, expected)

    d = {1: [(0, 2)]}
    a = [False, True, False, True, False, True]
    col = 1
    expected = {1: [(1, 1)], 2: [(1, 3)]}
    result = process_col(d, a, col)
    eq_(result, expected)

    d = {1: [(0, 2), (1, 1), (2, 0)], 2: [(1, 3)], 3: [(2, 5)]}
    a = [False, False, False, False, True, False]
    col = 3
    expected = {3: [(3, 4)]}
    result = process_col(d, a, col)
    eq_(result, expected)

    grid = [
        [False, True, True],
        [False, True, True],
        [False, True, True],
    ]
    d = {1: [(0, 1)]}
    a = grid[1]
    col = 1
    expected = {2: [(1, 2)]}
    result = process_col(d, a, col)
    eq_(result, expected)

    d = {1: [(0, 1)], 2: [(1, 2)]}
    a = grid[2]
    col = 2
    expected = {}
    result = process_col(d, a, col)
    eq_(result, expected)

def test_check_match():
    point = (1, 3)
    k = {1: [(0, 2)]}
    expected = 2
    result = check_match(point, k)
    eq_(result, expected)

    point = (1, 1)
    k = {1: [(0, 2)]}
    expected = 1
    result = check_match(point, k)
    eq_(result, expected)

    point = (1, 2)
    k = {1: [(0, 2)]}
    expected = None
    result = check_match(point, k)
    eq_(result, expected)

    point = (3, 4)
    k = {1: [(0, 2), (1, 1), (2, 0)], 2: [(1, 3)], 3: [(2, 5)]}
    expected = 3
    result = check_match(point, k)
    eq_(result, expected)

    point = (2, 0)
    k = {1: [(0, 2), (1, 1)], 2: [(1, 3)]}
    expected = 1
    result = check_match(point, k)
    eq_(result, expected)

    point = (5, 1)
    k = {
        1: [(0, 2), (1, 1), (2, 0)],
        2: [(1, 3), (3, 2), (4, 1)],
        3: [(2, 5), (3, 4), (4, 3)]
    }
    expected = None
    result = check_match(point, k)
    eq_(result, expected)

    #print 'boop boop'
    point = (2, 1)
    k = {1: [(0, 1)], 2: [(1, 2)]}
    expected = None
    result = check_match(point, k)
    eq_(result, expected)

    __author__ = 'Shurouq'

# some test data

cell1 = {
     "cell_type": "code",
     "collapsed": False,
     "input": [
         "x",
         "x",
         "x",
         "x",
         "x",
         "x",
         "y"
     ],
     "language": "python",
     "metadata": {
      "slideshow": {
       "slide_type": "fragment"
      }
     },
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Hello, world!\n",
        "Hello, world!\n"
       ]
      }
     ],
     "prompt_number": 29
    }

cell2 =    {
     "cell_type": "code",
     "collapsed": False,
     "input": [
         "x",
         "x",
         "x",
         "x",
         "x",
         "x"
     ],
     "language": "python",
     "metadata": {
      "slideshow": {
       "slide_type": "fragment"
      }
     },
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Hello, world!\n",
        "Hello, world!\n"
       ]
      }
     ],
     "prompt_number": 29
    }

import nbdiff
import nbdiff.diff as d
import nbdiff.comparable as c

class FakeComparator(object):
    '''Test comparator object. Will compare as modified if it is "close to"
    the specified values'''
    def __init__(self, foo, closeto=[]):
        self.foo = foo
        self.closeto = closeto

    def __eq__(self, other):
        if other.foo in self.closeto:
            return c.BooleanPlus(True, True)
        else:
            return self.foo == other.foo

# this still works...
print d.diff(['a', 'b', 'c'], ['b', 'c'], check_modified=False)

# it doesn't break strings when check_modified is True
print d.diff(['a', 'b', 'c'], ['b', 'c'], check_modified=True)

# CellComparators do actually produce booleanpluses if they are similar enough

c1 = c.CellComparator(cell1)
c2 = c.CellComparator(cell2)

assert type(c1 == c2) == c.BooleanPlus

result = d.diff([c1, c2, c2, c2], [c2, c2, c2, c2], check_modified=True)
print result
assert result[0]['state'] == 'modified'

c1 = FakeComparator(1, [2, 3])
c2 = FakeComparator(2, [1, 3])
c3 = FakeComparator(10, [])

c4 = FakeComparator(2, [])
c5 = FakeComparator(3, [])

# c1 -> c4
# c2 -> c5
# c3 -> deleted
result = d.diff([c1, c2, c3], [c4, c5], check_modified=True)
print result

assert result[0]['state'] == 'modified'
assert result[1]['state'] == 'modified'
assert result[2]['state'] == 'deleted'
<commit_msg>Move tests into a test folder<commit_after>from nose.tools import eq_
import itertools as it
import collections
from nbdiff.diff import (
    add_results,
    find_candidates,
    find_matches,
    process_col,
    check_match,
    lcs,
    diff_points,
    create_grid,
    diff,
)

def test_diff():
    A = [{u'input':[u'x = [1,3,4]\n', u'z = {1, 2, 3} \n', u'\n', u'm']},{u'input':[u'x = [1,3,3]\n', u'z = {1, 2, 3} \n', u'\n', u'z']}]
    B = [{u'input':[u'x = [1,3,4]\n', u'z = {1, 2, 3} \n', u'\n', u'm']}]
    result = diff(A, B)
    expected = [
        {"state": 'unchanged', 'value': {u'input': [u'x = [1,3,4]\n', u'z = {1, 2, 3} \n', u'\n', u'm']}},
        {"state": 'deleted', 'value': {u'input': [u'x = [1,3,3]\n', u'z = {1, 2, 3} \n', u'\n', u'z']}}
    ]
    eq_(result, expected)
    #diff("aaaaaaaaaaaaaaaaaaaa", "bbbbbbaaaaaaaaaaabbbbbbbbbbb")
    #diff("cabcdef", "abdef")
    #diff("ca", "abdef")
'''
def test_count_similar_lines():
    A = {u'input':[u'x = [1,3,3]\n', u'z = {1, 2, 3} \n', u'\n', u'z']}
    B = {u'input':[u'x = [1,3,4]\n', u'z = {1, 2, 3} \n', u'\n', u'm']}
    result = count_similar_lines(A, B)

    expected = 2
    eq_(result, expected)
'''
def test_create_grid():
    A = "abcabba"
    B = "cbabac"
    expected = [
        # c      b     a     b      a      c
        [False, False, True, False, True, False],  # a
        [False, True, False, True, False, False],  # b
        [True, False, False, False, False, True],  # c
        [False, False, True, False, True, False],  # a
        [False, True, False, True, False, False],  # b
        [False, True, False, True, False, False],  # b
        [False, False, True, False, True, False]   # a
    ]
    grid = create_grid(A, B)
    eq_(grid, expected)

    A, B = ("cabcdef", "abdef")
    grid = create_grid(A, B)
    assert len([True for col in grid if len(col) == 0]) == 0
'''
def test_check_modified():
    A = {u'input':[u'x = [1,3,3]\n', u'z = {1, 2, 3} \n', u'\n', u'z']}
    B = {u'input':[u'x = [1,3,4]\n', u'z = {1, 2, 3} \n', u'\n', u'm']}

    result = check_modified(A, B)
    expected = True
    eq_(result, expected)
'''
def test_diff_points():
    A = [u'x = [1,3,3]\n', u'z = {1, 2, 3} \n', u'\n', u'z']
    B = [u'x = [1,3,4]\n', u'z = {1, 2, 3} \n', u'\n', u'm']

    grid = create_grid(A,B)

    result = diff_points(grid)

    expected = [
        ('deleted', 0, None),
        ('added', None, 0),
        ('unchanged', 1, 1),
        ('unchanged', 2, 2),
        ('deleted', 3, None),
        ('added', None, 3),
    ]
    eq_(result, expected)

def test_find_candidates():
    grid = [
        [False, False, True, False, True, False],
        [False, True, False, True, False, False],
        [True, False, False, False, False, True],
        [False, False, True, False, True, False],
        [False, True, False, True, False, False],
        [False, True, False, True, False, False],
        [False, False, True, False, True, False]
    ]
    result = find_candidates(grid)
    expected = {
        1: [(0, 2), (1, 1), (2, 0)],
        2: [(1, 3), (3, 2), (4, 1)],
        3: [(2, 5), (3, 4), (4, 3), (6, 2)],
        4: [(6, 4)],
    }
    eq_(result, expected)

    grid = [
        [False, True, True],
        [False, True, True],
        [False, True, True],
        [False, True, True],
        [False, True, True],
        [False, True, True],
        [False, True, True]
    ]
    result = find_candidates(grid)
    expected = {1: [(0, 1)], 2: [(1, 2)]}
    eq_(result, expected)

def test_lcs():
    grid = [
        [False, False, True, False, True, False],
        [False, True, False, True, False, False],
        [True, False, False, False, False, True],
        [False, False, True, False, True, False],
        [False, True, False, True, False, False],
        [False, True, False, True, False, False],
        [False, False, True, False, True, False]
    ]
    result = lcs(grid)
    expected = [(1, 1), (3, 2), (4, 3), (6, 4)]
    eq_(result, expected)

    grid = [
        [False, False, True, False, True, False],
        [False, False, False, True, False, False],
        [True, False, False, False, False, True],
        [False, False, True, False, True, False],
        [False, True, False, True, False, False],
        [False, True, False, True, False, False],
        [False, False, True, False, True, False]
    ]
    result = lcs(grid)
    expected = [(2, 0), (3, 2), (4, 3), (6, 4)]
    eq_(result, expected)

    grid = [
        [True, True, True, True, True, True],
        [True, True, True, True, True, True],
        [True, True, True, True, True, True],
        [True, True, True, True, True, True],
        [True, True, True, True, True, True],
        [True, True, True, True, True, True],
        [True, True, True, True, True, True]
    ]
    result = lcs(grid)
    expected = [(0, 0), (1, 1), (2, 2), (3, 3), (4, 4), (5, 5)]
    eq_(result, expected)

    grid = [
        [False, True, True],
        [False, True, True],
        [False, True, True],
        [False, True, True],
        [False, True, True],
        [False, True, True],
        [False, True, True]
    ]
    result = lcs(grid)
    expected = [(0, 1), (1, 2)]
    eq_(result, expected)

def test_add_results():
    k = {1: [(0, 2)]}
    newk = {1: [(1, 1)], 2: [(1, 3)]}
    result = add_results(k, newk)
    expected = {1: [(0, 2), (1, 1)], 2: [(1, 3)]}
    eq_(result, expected)

def test_find_matches():
    A = "abcabba"
    B = "cbabac"
    prod = list(it.product(A, B))
    grid = [
        [
            a == b
            for (a, b) in
            prod
        ][i * len(B):i * len(B) + len(B)]
        for i in range(len(A))
    ]
    colNum = 0
    result = find_matches(grid[colNum], colNum)
    expected = [(0, 2), (0, 4)]
    eq_(result, expected)

def test_process_col():
    d = {1: [(0, 2)]}
    a = [False, True, False, True, False, False]
    col = 1
    expected = {1: [(1, 1)], 2: [(1, 3)]}
    result = process_col(d, a, col)
    eq_(result, expected)

    d = {}
    a = [False, True, False, True, False, False]
    col = 1
    expected = {1: [(1, 1)]}
    result = process_col(d, a, col)
    eq_(result, expected)

    d = {1: [(0, 2)]}
    a = [False, True, False, True, False, True]
    col = 1
    expected = {1: [(1, 1)], 2: [(1, 3)]}
    result = process_col(d, a, col)
    eq_(result, expected)

    d = {1: [(0, 2), (1, 1), (2, 0)], 2: [(1, 3)], 3: [(2, 5)]}
    a = [False, False, False, False, True, False]
    col = 3
    expected = {3: [(3, 4)]}
    result = process_col(d, a, col)
    eq_(result, expected)

    grid = [
        [False, True, True],
        [False, True, True],
        [False, True, True],
    ]
    d = {1: [(0, 1)]}
    a = grid[1]
    col = 1
    expected = {2: [(1, 2)]}
    result = process_col(d, a, col)
    eq_(result, expected)

    d = {1: [(0, 1)], 2: [(1, 2)]}
    a = grid[2]
    col = 2
    expected = {}
    result = process_col(d, a, col)
    eq_(result, expected)

def test_check_match():
    point = (1, 3)
    k = {1: [(0, 2)]}
    expected = 2
    result = check_match(point, k)
    eq_(result, expected)

    point = (1, 1)
    k = {1: [(0, 2)]}
    expected = 1
    result = check_match(point, k)
    eq_(result, expected)

    point = (1, 2)
    k = {1: [(0, 2)]}
    expected = None
    result = check_match(point, k)
    eq_(result, expected)

    point = (3, 4)
    k = {1: [(0, 2), (1, 1), (2, 0)], 2: [(1, 3)], 3: [(2, 5)]}
    expected = 3
    result = check_match(point, k)
    eq_(result, expected)

    point = (2, 0)
    k = {1: [(0, 2), (1, 1)], 2: [(1, 3)]}
    expected = 1
    result = check_match(point, k)
    eq_(result, expected)

    point = (5, 1)
    k = {
        1: [(0, 2), (1, 1), (2, 0)],
        2: [(1, 3), (3, 2), (4, 1)],
        3: [(2, 5), (3, 4), (4, 3)]
    }
    expected = None
    result = check_match(point, k)
    eq_(result, expected)

    #print 'boop boop'
    point = (2, 1)
    k = {1: [(0, 1)], 2: [(1, 2)]}
    expected = None
    result = check_match(point, k)
    eq_(result, expected)

def test_modified():
    cell1 = {
        "cell_type": "code",
        "collapsed": False,
        "input": [
            "x",
            "x",
            "x",
            "x",
            "x",
            "x",
            "y"
        ],
        "language": "python",
        "metadata": {
        "slideshow": {
        "slide_type": "fragment"
        }
        },
        "outputs": [
        {
        "output_type": "stream",
        "stream": "stdout",
        "text": [
            "Hello, world!\n",
            "Hello, world!\n"
        ]
        }
        ],
        "prompt_number": 29
        }

    cell2 =    {
        "cell_type": "code",
        "collapsed": False,
        "input": [
            "x",
            "x",
            "x",
            "x",
            "x",
            "x"
        ],
        "language": "python",
        "metadata": {
        "slideshow": {
        "slide_type": "fragment"
        }
        },
        "outputs": [
        {
        "output_type": "stream",
        "stream": "stdout",
        "text": [
            "Hello, world!\n",
            "Hello, world!\n"
        ]
        }
        ],
        "prompt_number": 29
        }

    import nbdiff.comparable as c

    class FakeComparator(object):
        '''Test comparator object. Will compare as modified if it is "close to"
        the specified values'''
        def __init__(self, foo, closeto=[]):
            self.foo = foo
            self.closeto = closeto

        def __eq__(self, other):
            if other.foo in self.closeto:
                return c.BooleanPlus(True, True)
            else:
                return self.foo == other.foo

    # ensure this doesn't crash at the least
    result = diff(['a', 'b', 'c'], ['b', 'c'], check_modified=False)
    assert result[0]['state'] == 'deleted'
    assert result[0]['value'] == 'a'

    # it doesn't break strings when check_modified is True
    diff(['a', 'b', 'c'], ['b', 'c'], check_modified=True)

    # CellComparators do actually produce booleanpluses if they are similar enough
    # TODO this should be in its own test in a separate file.
    c1 = c.CellComparator(cell1)
    c2 = c.CellComparator(cell2)

    assert type(c1 == c2) == c.BooleanPlus

    result = diff([c1, c2, c2, c2], [c2, c2, c2, c2], check_modified=True)
    assert result[0]['state'] == 'modified'

    c1 = FakeComparator(1, [2, 3])
    c2 = FakeComparator(2, [1, 3])
    c3 = FakeComparator(10, [])

    c4 = FakeComparator(2, [])
    c5 = FakeComparator(3, [])

    # c1 -> c4
    # c2 -> c5
    # c3 -> deleted
    result = diff([c1, c2, c3], [c4, c5], check_modified=True)

    assert result[0]['state'] == 'modified'
    assert result[1]['state'] == 'modified'
    assert result[2]['state'] == 'deleted'
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
"""Sphinx doctest is just too hard. Manually paste doctest examples here"""
from spacy.en.attrs import IS_LOWER
import pytest

@pytest.mark.models
def test_1():
    import spacy.en
    from spacy.parts_of_speech import ADV
    # Load the pipeline, and call it with some text.
    nlp = spacy.en.English()
    tokens = nlp(u"‘Give it back,’ he pleaded abjectly, ‘it’s mine.’",
                tag=True, parse=False)
    o = u''.join(tok.string.upper() if tok.pos == ADV else tok.string for tok in tokens)
    assert u"‘Give it BACK,’ he pleaded ABJECTLY, ‘it’s mine.’"

    o = nlp.vocab[u'back'].prob
    assert o == -7.403977394104004
    o = nlp.vocab[u'not'].prob
    assert o == -5.407193660736084
    o = nlp.vocab[u'quietly'].prob
    assert o == -11.07155704498291

@pytest.mark.models
def test2():
    import spacy.en
    from spacy.parts_of_speech import ADV
    nlp = spacy.en.English()
    # Find log probability of Nth most frequent word
    probs = [lex.prob for lex in nlp.vocab]
    probs.sort()
    is_adverb = lambda tok: tok.pos == ADV and tok.prob < probs[-1000]
    tokens = nlp(u"‘Give it back,’ he pleaded abjectly, ‘it’s mine.’")
    o = u''.join(tok.string.upper() if is_adverb(tok) else tok.string for tok in tokens)
    o == u'‘Give it back,’ he pleaded ABJECTLY, ‘it’s mine.’'

    nlp.vocab[u'back'].prob
    -7.403977394104004
    nlp.vocab[u'not'].prob
    -5.407193660736084
    nlp.vocab[u'quietly'].prob
    -11.07155704498291

@pytest.mark.models
def test3():
    import spacy.en
    from spacy.parts_of_speech import ADV
    nlp = spacy.en.English()
    # Find log probability of Nth most frequent word
    probs = [lex.prob for lex in nlp.vocab]
    probs.sort()
    is_adverb = lambda tok: tok.pos == ADV and tok.prob < probs[-1000]
    tokens = nlp(u"‘Give it back,’ he pleaded abjectly, ‘it’s mine.’")
    o = u''.join(tok.string.upper() if is_adverb(tok) else tok.string for tok in tokens)
    assert o == u'‘Give it back,’ he pleaded ABJECTLY, ‘it’s mine.’'

    pleaded = tokens[7]
    assert pleaded.repvec.shape == (300,)
    o = pleaded.repvec[:5]
    assert sum(o) != 0
    from numpy import dot
    from numpy.linalg import norm

    cosine = lambda v1, v2: dot(v1, v2) / (norm(v1) * norm(v2))
    words = [w for w in nlp.vocab if w.check(IS_LOWER) and w.has_repvec]
    words.sort(key=lambda w: cosine(w.repvec, pleaded.repvec))
    words.reverse()
    o = [w.orth_ for w in words[0:20]]
    assert o == [u'pleaded', u'pled', u'plead', u'confessed', u'interceded',
                 u'pleads', u'testified', u'conspired', u'motioned', u'demurred',
                 u'countersued', u'remonstrated', u'begged', u'apologised',
                 u'consented', u'acquiesced', u'petitioned', u'quarreled',
                 u'appealed', u'pleading']
    o = [w.orth_ for w in words[50:60]]
    assert o == [u'counselled', u'bragged', u'backtracked', u'caucused', u'refiled',
                 u'dueled', u'mused', u'dissented', u'yearned', u'confesses']
    o = [w.orth_ for w in words[100:110]]
    assert o == [u'cabled', u'ducked', u'sentenced', u'perjured', u'absconded',
                 u'bargained', u'overstayed', u'clerked', u'confided', u'sympathizes']
    #o = [w.orth_ for w in words[1000:1010]]
    #assert o == [u'scorned', u'baled', u'righted', u'requested', u'swindled',
    #             u'posited', u'firebombed', u'slimed', u'deferred', u'sagged']
    #o = [w.orth_ for w in words[50000:50010]]
    #assert o == [u'fb', u'ford', u'systems', u'puck', u'anglers', u'ik', u'tabloid',
    #             u'dirty', u'rims', u'artists']
<commit_msg>* Update doctests<commit_after># -*- coding: utf-8 -*-
"""Sphinx doctest is just too hard. Manually paste doctest examples here"""
from spacy.en.attrs import IS_LOWER
import pytest

@pytest.mark.models
def test_1():
    import spacy.en
    from spacy.parts_of_speech import ADV
    # Load the pipeline, and call it with some text.
    nlp = spacy.en.English()
    tokens = nlp(u"‘Give it back,’ he pleaded abjectly, ‘it’s mine.’",
                tag=True, parse=False)
    o = u''.join(tok.string.upper() if tok.pos == ADV else tok.string for tok in tokens)
    assert u"‘Give it BACK,’ he pleaded ABJECTLY, ‘it’s mine.’"

    o = nlp.vocab[u'back'].prob
    assert o == -7.403977394104004
    o = nlp.vocab[u'not'].prob
    assert o == -5.407193660736084
    o = nlp.vocab[u'quietly'].prob
    assert o == -11.07155704498291

@pytest.mark.models
def test2():
    import spacy.en
    from spacy.parts_of_speech import ADV
    nlp = spacy.en.English()
    # Find log probability of Nth most frequent word
    probs = [lex.prob for lex in nlp.vocab]
    probs.sort()
    is_adverb = lambda tok: tok.pos == ADV and tok.prob < probs[-1000]
    tokens = nlp(u"‘Give it back,’ he pleaded abjectly, ‘it’s mine.’")
    o = u''.join(tok.string.upper() if is_adverb(tok) else tok.string for tok in tokens)
    o == u'‘Give it back,’ he pleaded ABJECTLY, ‘it’s mine.’'

    nlp.vocab[u'back'].prob
    -7.033305644989014
    nlp.vocab[u'not'].prob
    -5.332601070404053
    nlp.vocab[u'quietly'].prob
    -11.994928359985352

@pytest.mark.models
def test3():
    import spacy.en
    from spacy.parts_of_speech import ADV
    nlp = spacy.en.English()
    # Find log probability of Nth most frequent word
    probs = [lex.prob for lex in nlp.vocab]
    probs.sort()
    is_adverb = lambda tok: tok.pos == ADV and tok.prob < probs[-1000]
    tokens = nlp(u"‘Give it back,’ he pleaded abjectly, ‘it’s mine.’")
    o = u''.join(tok.string.upper() if is_adverb(tok) else tok.string for tok in tokens)
    assert o == u'‘Give it back,’ he pleaded ABJECTLY, ‘it’s mine.’'

    pleaded = tokens[7]
    assert pleaded.repvec.shape == (300,)
    o = pleaded.repvec[:5]
    assert sum(o) != 0
    from numpy import dot
    from numpy.linalg import norm

    cosine = lambda v1, v2: dot(v1, v2) / (norm(v1) * norm(v2))
    words = [w for w in nlp.vocab if w.check(IS_LOWER) and w.has_repvec]
    words.sort(key=lambda w: cosine(w.repvec, pleaded.repvec))
    words.reverse()
    o = [w.orth_ for w in words[0:20]]
    assert o == [u'pleaded', u'pled', u'plead', u'confessed', u'interceded',
                 u'pleads', u'testified', u'conspired', u'motioned', u'demurred',
                 u'countersued', u'remonstrated', u'begged', u'apologised',
                 u'consented', u'acquiesced', u'petitioned', u'quarreled',
                 u'appealed', u'pleading']
    o = [w.orth_ for w in words[50:60]]
    assert o == [u'endeavoured', u'martialed', u'counselled', u'bragged',
                 u'backtracked', u'caucused', u'refiled', u'dueled', u'mused',
                 u'dissented']
    o = [w.orth_ for w in words[100:110]]
    assert o == [u'prosecuted', u'acquits', u'cabled', u'ducked', u'sentenced',
                 u'gaoled', u'perjured', u'absconded', u'bargained', u'overstayed']

    #o = [w.orth_ for w in words[1000:1010]]
    #assert o == [u'scorned', u'baled', u'righted', u'requested', u'swindled',
    #             u'posited', u'firebombed', u'slimed', u'deferred', u'sagged']
    #o = [w.orth_ for w in words[50000:50010]]
    #assert o == [u'fb', u'ford', u'systems', u'puck', u'anglers', u'ik', u'tabloid',
    #             u'dirty', u'rims', u'artists']
<|endoftext|>


<commit_before>#!/usr/bin/env python2.6
'''
Test docx module
'''
import os
import lxml
from nose import with_setup
from docx import *

TEST_FILE = 'Short python-docx test.docx'

def setup_func():
    '''Set up test fixtures'''
    testnewdocument()

def teardown_func():
    '''Tear down test fixtures'''
    if TEST_FILE in os.listdir('.'):
        os.remove(TEST_FILE)

def testunsupportedpagebreak():
    '''Ensure unsupported page break types are trapped'''
    document = newdocument()
    docbody = document.xpath('/w:document/w:body', namespaces=nsprefixes)[0]
    try:
        docbody.append(pagebreak(type='unsup'))
    except ValyueError:
        return # passed
    assert False # failed

def testnewdocument():
    '''Test that a new document can be created'''
    relationships = relationshiplist()
    document = newdocument()
    docbody = document.xpath('/w:document/w:body', namespaces=nsprefixes)[0]
    docbody.append(heading('Heading 1',1)  )
    docbody.append(heading('Heading 2',2))
    docbody.append(paragraph('Paragraph 1'))
    for point in ['List Item 1','List Item 2','List Item 3']:
        docbody.append(paragraph(point,style='ListNumber'))
    docbody.append(pagebreak(type='page', orient='portrait'))
    docbody.append(paragraph('Paragraph 2'))
    docbody.append(table([['A1','A2','A3'],['B1','B2','B3'],['C1','C2','C3']]))
    docbody.append(paragraph('Paragraph 3'))
    properties = docproperties('Python docx testnewdocument','A short example of making docx from Python','Alan Brooks',['python','Office Open XML','Word'])
    savedocx(document, properties, contenttypes(), websettings(), wordrelationships(relationships), TEST_FILE)

@with_setup(setup_func, teardown_func)
def testopendocx():
    '''Ensure an etree element is returned'''
    if isinstance(opendocx(TEST_FILE),lxml.etree._Element):
        pass
    else:
        assert False

def testmakeelement():
    '''Ensure custom elements get created'''
    testelement = makeelement('testname',attributes={'testattribute':'testvalue'},tagtext='testtagtext')
    assert testelement.tag == '{http://schemas.openxmlformats.org/wordprocessingml/2006/main}testname'
    assert testelement.attrib == {'{http://schemas.openxmlformats.org/wordprocessingml/2006/main}testattribute': 'testvalue'}
    assert testelement.text == 'testtagtext'

def testparagraph():
    '''Ensure paragraph creates p elements'''
    testpara = paragraph('paratext',style='BodyText')
    assert testpara.tag == '{http://schemas.openxmlformats.org/wordprocessingml/2006/main}p'
    pass

def testtable():
    '''Ensure tables make sense'''
    testtable = table([['A1','A2'],['B1','B2'],['C1','C2']])
    ns = '{http://schemas.openxmlformats.org/wordprocessingml/2006/main}'
    assert testtable.xpath('/ns0:tbl/ns0:tr[2]/ns0:tc[2]/ns0:p/ns0:r/ns0:t',namespaces={'ns0':'http://schemas.openxmlformats.org/wordprocessingml/2006/main'})[0].text == 'B2'<commit_msg>- Code coverage improvements in tests, now covers 97%, but not well in all cases<commit_after>#!/usr/bin/env python2.6
'''
Test docx module
'''
import os
import lxml
from nose import with_setup
from docx import *

TEST_FILE = 'Short python-docx test.docx'

def setup_func():
    '''Set up test fixtures'''
    testnewdocument()

def teardown_func():
    '''Tear down test fixtures'''
    if TEST_FILE in os.listdir('.'):
        os.remove(TEST_FILE)

@with_setup(setup_func, teardown_func)
def testsearchandreplace():
    '''Ensure search and replace functions work'''
    assert False # something is fishy w/search and replace at the moment

@with_setup(setup_func, teardown_func)
def testtextextraction():
    '''Ensure text can be pulled out of a document'''
    document = opendocx(TEST_FILE)
    paratextlist = getdocumenttext(document)
    assert len(paratextlist) > 0

def testunsupportedpagebreak():
    '''Ensure unsupported page break types are trapped'''
    document = newdocument()
    docbody = document.xpath('/w:document/w:body', namespaces=nsprefixes)[0]
    try:
        docbody.append(pagebreak(type='unsup'))
    except ValueError:
        return # passed
    assert False # failed

def testnewdocument():
    '''Test that a new document can be created'''
    relationships = relationshiplist()
    document = newdocument()
    docbody = document.xpath('/w:document/w:body', namespaces=nsprefixes)[0]
    docbody.append(heading('Heading 1',1)  )
    docbody.append(heading('Heading 2',2))
    docbody.append(paragraph('Paragraph 1'))
    for point in ['List Item 1','List Item 2','List Item 3']:
        docbody.append(paragraph(point,style='ListNumber'))
    docbody.append(pagebreak(type='page'))
    docbody.append(paragraph('Paragraph 2'))
    docbody.append(table([['A1','A2','A3'],['B1','B2','B3'],['C1','C2','C3']]))
    docbody.append(pagebreak(type='section', orient='portrait'))
    relationships,picpara = picture(relationships,'image1.png','This is a test description')
    docbody.append(picpara)
    docbody.append(pagebreak(type='section', orient='landscape'))
    docbody.append(paragraph('Paragraph 3'))
    properties = docproperties('Python docx testnewdocument','A short example of making docx from Python','Alan Brooks',['python','Office Open XML','Word'])
    savedocx(document, properties, contenttypes(), websettings(), wordrelationships(relationships), TEST_FILE)

@with_setup(setup_func, teardown_func)
def testopendocx():
    '''Ensure an etree element is returned'''
    if isinstance(opendocx(TEST_FILE),lxml.etree._Element):
        pass
    else:
        assert False

def testmakeelement():
    '''Ensure custom elements get created'''
    testelement = makeelement('testname',attributes={'testattribute':'testvalue'},tagtext='testtagtext')
    assert testelement.tag == '{http://schemas.openxmlformats.org/wordprocessingml/2006/main}testname'
    assert testelement.attrib == {'{http://schemas.openxmlformats.org/wordprocessingml/2006/main}testattribute': 'testvalue'}
    assert testelement.text == 'testtagtext'

def testparagraph():
    '''Ensure paragraph creates p elements'''
    testpara = paragraph('paratext',style='BodyText')
    assert testpara.tag == '{http://schemas.openxmlformats.org/wordprocessingml/2006/main}p'
    pass

def testtable():
    '''Ensure tables make sense'''
    testtable = table([['A1','A2'],['B1','B2'],['C1','C2']])
    ns = '{http://schemas.openxmlformats.org/wordprocessingml/2006/main}'
    assert testtable.xpath('/ns0:tbl/ns0:tr[2]/ns0:tc[2]/ns0:p/ns0:r/ns0:t',namespaces={'ns0':'http://schemas.openxmlformats.org/wordprocessingml/2006/main'})[0].text == 'B2'<|endoftext|>


<commit_before>def test_fail():
    assert False
<commit_msg>Revert "adding a test to check build tests"<commit_after><|endoftext|>


<commit_before>import os

import vtk
import numpy as np
import pytest

import vtki
from vtki import examples
from vtki.plotting import running_xserver

beam = vtki.UnstructuredGrid(examples.hexbeamfile)

# create structured grid
x = np.arange(-10, 10, 2)
y = np.arange(-10, 10, 2)
z = np.arange(-10, 10, 2)
x, y, z = np.meshgrid(x, y, z)
sgrid = vtki.StructuredGrid(x, y, z)

try:
    test_path = os.path.dirname(os.path.abspath(__file__))
    test_data_path = os.path.join(test_path, 'test_data')
except:
    test_path = '/home/alex/afrl/python/source/vtki/tests'

def test_volume():
    assert beam.volume > 0.0

def test_merge():
    beamA = vtki.UnstructuredGrid(examples.hexbeamfile)
    beamB = beamA.copy()
    beamB.points[:, 1] += 1
    beamA.Merge(beamB)

@pytest.mark.skipif(not running_xserver(), reason="Requires X11")
def test_struct_example():
    x = np.arange(-10, 10, 0.25)
    y = np.arange(-10, 10, 0.25)
    x, y = np.meshgrid(x, y)
    r = np.sqrt(x**2 + y**2)
    z = np.sin(r)

    # create and plot structured grid
    grid = vtki.StructuredGrid(x, y, z)
    cpos = grid.plot(off_screen=True)  # basic plot
    assert isinstance(cpos, list)

    # Plot mean curvature
    cpos_curv = grid.plot_curvature(off_screen=True)
    assert isinstance(cpos_curv, list)

def test_init_from_structured():
    unstruct_grid = vtki.UnstructuredGrid(sgrid)
    assert unstruct_grid.points.shape[0] == x.size
    assert np.all(unstruct_grid.celltypes == 12)

def test_init_from_unstructured():
    grid = vtki.UnstructuredGrid(beam, deep=True)
    grid.points += 1
    assert not np.any(grid.points == beam.points)

def test_init_bad_input():
    with pytest.raises(Exception):
        unstruct_grid = vtki.UnstructuredGrid(np.array(1))

    with pytest.raises(Exception):
        unstruct_grid = vtki.UnstructuredGrid(np.array(1),
                                              np.array(1),
                                              np.array(1),
                                              'woa')

def test_init_from_arrays():
    offset = np.array([0, 9], np.int8)
    cells = np.array([8, 0, 1, 2, 3, 4, 5, 6, 7, 8, 8, 9, 10, 11, 12, 13, 14, 15])
    cell_type = np.array([vtk.VTK_HEXAHEDRON, vtk.VTK_HEXAHEDRON], np.int32)

    cell1 = np.array([[0, 0, 0],
                      [1, 0, 0],
                      [1, 1, 0],
                      [0, 1, 0],
                      [0, 0, 1],
                      [1, 0, 1],
                      [1, 1, 1],
                      [0, 1, 1]])

    cell2 = np.array([[0, 0, 2],
                      [1, 0, 2],
                      [1, 1, 2],
                      [0, 1, 2],
                      [0, 0, 3],
                      [1, 0, 3],
                      [1, 1, 3],
                      [0, 1, 3]])

    points = np.vstack((cell1, cell2)).astype(np.int32)
    grid = vtki.UnstructuredGrid(offset, cells, cell_type, points)

    assert grid.number_of_cells == 2
    assert np.allclose(grid.offset, offset)

def test_surface_indices():
    surf = beam.extract_surface()
    surf_ind = surf.point_arrays['vtkOriginalPointIds']
    assert np.allclose(surf_ind, beam.surface_indices())

def test_extract_edges():
    edges = beam.extract_edges(90)
    assert edges.number_of_points

    edges = beam.extract_edges(180)
    assert not edges.number_of_points

@pytest.mark.parametrize('binary', [True, False])
@pytest.mark.parametrize('extension', ['vtu', 'vtk'])
def test_save(extension, binary, tmpdir):
    filename = str(tmpdir.mkdir("tmpdir").join('tmp.%s' % extension))
    beam.save(filename, binary)

    grid = vtki.UnstructuredGrid(filename)
    assert grid.cells.shape == beam.cells.shape
    assert grid.points.shape == beam.points.shape

def test_init_bad_filename():
    filename = os.path.join(test_path, 'test_grid.py')
    with pytest.raises(Exception):
        grid = vtki.UnstructuredGrid(filename)

    with pytest.raises(Exception):
        grid = vtki.UnstructuredGrid('not a file')

def test_save_bad_extension():
    with pytest.raises(Exception):
        grid = vtki.UnstructuredGrid('file.abc')

def test_linear_copy():
    # need a grid with quadratic cells
    lgrid = beam.linear_copy()
    assert np.all(lgrid.celltypes < 20)

def test_extract_cells():
    ind = [1, 2, 3]
    part_beam = beam.extract_cells(ind)
    assert part_beam.number_of_cells == len(ind)
    assert part_beam.number_of_points < beam.number_of_points

    mask = np.zeros(beam.number_of_cells, np.bool)
    mask[:3] = True
    part_beam = beam.extract_cells(mask)
    assert part_beam.number_of_cells == len(ind)
    assert part_beam.number_of_points < beam.number_of_points

def test_merge():
    grid = beam.copy()
    grid.points[:, 0] += 1
    unmerged = grid.merge(beam, inplace=False, merge_points=False)

    grid.merge(beam)
    assert grid.number_of_points > beam.number_of_points
    assert grid.number_of_points < unmerged.number_of_points

def test_merge_not_main():
    grid = beam.copy()
    grid.points[:, 0] += 1
    unmerged = grid.merge(beam, inplace=False, merge_points=False,
                          main_has_priority=False)

    grid.merge(beam)
    assert grid.number_of_points > beam.number_of_points
    assert grid.number_of_points < unmerged.number_of_points

def test_merge_list():
    grid_a = beam.copy()
    grid_a.points[:, 0] += 1

    grid_b = beam.copy()
    grid_b.points[:, 1] += 1

    grid_a.merge([beam, grid_b])
    assert grid_a.number_of_points > beam.number_of_points

def test_init_structured():
    xrng = np.arange(-10, 10, 2)
    yrng = np.arange(-10, 10, 2)
    zrng = np.arange(-10, 10, 2)
    x, y, z = np.meshgrid(xrng, yrng, zrng)
    grid = vtki.StructuredGrid(x, y, z)
    assert np.allclose(sgrid.x, x)
    assert np.allclose(sgrid.y, y)
    assert np.allclose(sgrid.z, z)

    grid_a = vtki.StructuredGrid(grid)
    assert np.allclose(grid_a.points, grid.points)

def test_invalid_init_structured():
    xrng = np.arange(-10, 10, 2)
    yrng = np.arange(-10, 10, 2)
    zrng = np.arange(-10, 10, 2)
    x, y, z = np.meshgrid(xrng, yrng, zrng)
    z = z[:, :, :2]
    with pytest.raises(Exception):
        grid = vtki.StructuredGrid(x, y, z)

@pytest.mark.parametrize('binary', [True, False])
@pytest.mark.parametrize('extension', ['vts', 'vtk'])
def test_save_structured(extension, binary, tmpdir):
    filename = str(tmpdir.mkdir("tmpdir").join('tmp.%s' % extension))
    sgrid.save(filename, binary)

    grid = vtki.StructuredGrid(filename)
    assert grid.x.shape == sgrid.y.shape
    assert grid.number_of_cells
    assert grid.points.shape == sgrid.points.shape

def test_load_structured_bad_filename():
    with pytest.raises(Exception):
        vtki.StructuredGrid('not a file')

    filename = os.path.join(test_path, 'test_grid.py')
    with pytest.raises(Exception):
        grid = vtki.StructuredGrid(filename)
<commit_msg>Add more grid creation tests<commit_after>import os

import vtk
import numpy as np
import pytest

import vtki
from vtki import examples
from vtki.plotting import running_xserver

beam = vtki.UnstructuredGrid(examples.hexbeamfile)

# create structured grid
x = np.arange(-10, 10, 2)
y = np.arange(-10, 10, 2)
z = np.arange(-10, 10, 2)
x, y, z = np.meshgrid(x, y, z)
sgrid = vtki.StructuredGrid(x, y, z)

try:
    test_path = os.path.dirname(os.path.abspath(__file__))
    test_data_path = os.path.join(test_path, 'test_data')
except:
    test_path = '/home/alex/afrl/python/source/vtki/tests'

def test_volume():
    assert beam.volume > 0.0

def test_merge():
    beamA = vtki.UnstructuredGrid(examples.hexbeamfile)
    beamB = beamA.copy()
    beamB.points[:, 1] += 1
    beamA.Merge(beamB)

@pytest.mark.skipif(not running_xserver(), reason="Requires X11")
def test_struct_example():
    x = np.arange(-10, 10, 0.25)
    y = np.arange(-10, 10, 0.25)
    x, y = np.meshgrid(x, y)
    r = np.sqrt(x**2 + y**2)
    z = np.sin(r)

    # create and plot structured grid
    grid = vtki.StructuredGrid(x, y, z)
    cpos = grid.plot(off_screen=True)  # basic plot
    assert isinstance(cpos, list)

    # Plot mean curvature
    cpos_curv = grid.plot_curvature(off_screen=True)
    assert isinstance(cpos_curv, list)

def test_init_from_structured():
    unstruct_grid = vtki.UnstructuredGrid(sgrid)
    assert unstruct_grid.points.shape[0] == x.size
    assert np.all(unstruct_grid.celltypes == 12)

def test_init_from_unstructured():
    grid = vtki.UnstructuredGrid(beam, deep=True)
    grid.points += 1
    assert not np.any(grid.points == beam.points)

def test_init_bad_input():
    with pytest.raises(Exception):
        unstruct_grid = vtki.UnstructuredGrid(np.array(1))

    with pytest.raises(Exception):
        unstruct_grid = vtki.UnstructuredGrid(np.array(1),
                                              np.array(1),
                                              np.array(1),
                                              'woa')

def test_init_from_arrays():
    offset = np.array([0, 9], np.int8)
    cells = np.array([8, 0, 1, 2, 3, 4, 5, 6, 7, 8, 8, 9, 10, 11, 12, 13, 14, 15])
    cell_type = np.array([vtk.VTK_HEXAHEDRON, vtk.VTK_HEXAHEDRON], np.int32)

    cell1 = np.array([[0, 0, 0],
                      [1, 0, 0],
                      [1, 1, 0],
                      [0, 1, 0],
                      [0, 0, 1],
                      [1, 0, 1],
                      [1, 1, 1],
                      [0, 1, 1]])

    cell2 = np.array([[0, 0, 2],
                      [1, 0, 2],
                      [1, 1, 2],
                      [0, 1, 2],
                      [0, 0, 3],
                      [1, 0, 3],
                      [1, 1, 3],
                      [0, 1, 3]])

    points = np.vstack((cell1, cell2)).astype(np.int32)
    grid = vtki.UnstructuredGrid(offset, cells, cell_type, points)

    assert grid.number_of_cells == 2
    assert np.allclose(grid.offset, offset)

def test_surface_indices():
    surf = beam.extract_surface()
    surf_ind = surf.point_arrays['vtkOriginalPointIds']
    assert np.allclose(surf_ind, beam.surface_indices())

def test_extract_edges():
    edges = beam.extract_edges(90)
    assert edges.number_of_points

    edges = beam.extract_edges(180)
    assert not edges.number_of_points

@pytest.mark.parametrize('binary', [True, False])
@pytest.mark.parametrize('extension', ['vtu', 'vtk'])
def test_save(extension, binary, tmpdir):
    filename = str(tmpdir.mkdir("tmpdir").join('tmp.%s' % extension))
    beam.save(filename, binary)

    grid = vtki.UnstructuredGrid(filename)
    assert grid.cells.shape == beam.cells.shape
    assert grid.points.shape == beam.points.shape

def test_init_bad_filename():
    filename = os.path.join(test_path, 'test_grid.py')
    with pytest.raises(Exception):
        grid = vtki.UnstructuredGrid(filename)

    with pytest.raises(Exception):
        grid = vtki.UnstructuredGrid('not a file')

def test_save_bad_extension():
    with pytest.raises(Exception):
        grid = vtki.UnstructuredGrid('file.abc')

def test_linear_copy():
    # need a grid with quadratic cells
    lgrid = beam.linear_copy()
    assert np.all(lgrid.celltypes < 20)

def test_extract_cells():
    ind = [1, 2, 3]
    part_beam = beam.extract_cells(ind)
    assert part_beam.number_of_cells == len(ind)
    assert part_beam.number_of_points < beam.number_of_points

    mask = np.zeros(beam.number_of_cells, np.bool)
    mask[:3] = True
    part_beam = beam.extract_cells(mask)
    assert part_beam.number_of_cells == len(ind)
    assert part_beam.number_of_points < beam.number_of_points

def test_merge():
    grid = beam.copy()
    grid.points[:, 0] += 1
    unmerged = grid.merge(beam, inplace=False, merge_points=False)

    grid.merge(beam)
    assert grid.number_of_points > beam.number_of_points
    assert grid.number_of_points < unmerged.number_of_points

def test_merge_not_main():
    grid = beam.copy()
    grid.points[:, 0] += 1
    unmerged = grid.merge(beam, inplace=False, merge_points=False,
                          main_has_priority=False)

    grid.merge(beam)
    assert grid.number_of_points > beam.number_of_points
    assert grid.number_of_points < unmerged.number_of_points

def test_merge_list():
    grid_a = beam.copy()
    grid_a.points[:, 0] += 1

    grid_b = beam.copy()
    grid_b.points[:, 1] += 1

    grid_a.merge([beam, grid_b])
    assert grid_a.number_of_points > beam.number_of_points

def test_init_structured():
    xrng = np.arange(-10, 10, 2)
    yrng = np.arange(-10, 10, 2)
    zrng = np.arange(-10, 10, 2)
    x, y, z = np.meshgrid(xrng, yrng, zrng)
    grid = vtki.StructuredGrid(x, y, z)
    assert np.allclose(sgrid.x, x)
    assert np.allclose(sgrid.y, y)
    assert np.allclose(sgrid.z, z)

    grid_a = vtki.StructuredGrid(grid)
    assert np.allclose(grid_a.points, grid.points)

def test_invalid_init_structured():
    xrng = np.arange(-10, 10, 2)
    yrng = np.arange(-10, 10, 2)
    zrng = np.arange(-10, 10, 2)
    x, y, z = np.meshgrid(xrng, yrng, zrng)
    z = z[:, :, :2]
    with pytest.raises(Exception):
        grid = vtki.StructuredGrid(x, y, z)

@pytest.mark.parametrize('binary', [True, False])
@pytest.mark.parametrize('extension', ['vts', 'vtk'])
def test_save_structured(extension, binary, tmpdir):
    filename = str(tmpdir.mkdir("tmpdir").join('tmp.%s' % extension))
    sgrid.save(filename, binary)

    grid = vtki.StructuredGrid(filename)
    assert grid.x.shape == sgrid.y.shape
    assert grid.number_of_cells
    assert grid.points.shape == sgrid.points.shape

def test_load_structured_bad_filename():
    with pytest.raises(Exception):
        vtki.StructuredGrid('not a file')

    filename = os.path.join(test_path, 'test_grid.py')
    with pytest.raises(Exception):
        grid = vtki.StructuredGrid(filename)

def test_create_rectilinear_grid_from_specs():
    xrng = np.arange(-10, 10, 2)
    yrng = np.arange(-10, 10, 5)
    zrng = np.arange(-10, 10, 1)
    grid = vtki.RectilinearGrid(xrng, yrng, zrng)
    assert grid.GetNumberOfCells() == 9*3*19
    assert grid.GetNumberOfPoints() == 10*4*20
    assert grid.GetBounds() == (-10.0,8.0, -10.0,5.0, -10.0,9.0)

def test_create_rectilinear_grid_from_file():
    grid = examples.load_rectilinear()
    assert grid.GetNumberOfCells() == 16146
    assert grid.GetNumberOfPoints() == 18144
    assert grid.GetBounds() == (-350.0,1350.0, -400.0,1350.0, -850.0,0.0)
    assert grid.number_of_scalars == 1

def test_create_uniform_grid_from_specs():
    # create UniformGrid
    dims = (10, 10, 10)
    grid = vtki.UniformGrid(dims) # Using default spacing and origin
    assert grid.GetDimensions() == (10, 10, 10)
    assert grid.GetOrigin() == (0.0, 0.0, 0.0)
    assert grid.GetSpacing() == (1.0, 1.0, 1.0)
    spacing = (2, 1, 5)
    grid = vtki.UniformGrid(dims, spacing) # Usign default origin
    assert grid.GetDimensions() == (10, 10, 10)
    assert grid.GetOrigin() == (0.0, 0.0, 0.0)
    assert grid.GetSpacing() == (2.0, 1.0, 5.0)
    origin = (10, 35, 50)
    grid = vtki.UniformGrid(dims, spacing, origin) # Everything is specified
    assert grid.GetDimensions() == (10, 10, 10)
    assert grid.GetOrigin() == (10.0, 35.0, 50.0)
    assert grid.GetSpacing() == (2.0, 1.0, 5.0)

def test_create_uniform_grid_from_file():
    grid = examples.load_uniform()
    assert grid.GetNumberOfCells() == 729
    assert grid.GetNumberOfPoints() == 1000
    assert grid.GetBounds() == (0.0,9.0, 0.0,9.0, 0.0,9.0)
    assert grid.number_of_scalars == 2
<|endoftext|>


<commit_before>#
# This file is part of khmer, http://github.com/ged-lab/khmer/, and is
# Copyright (C) Michigan State University, 2009-2013. It is licensed under
# the three-clause BSD license; see doc/LICENSE.txt.
# Contact: khmer-project@idyll.org
#
# pylint: disable=missing-docstring
import khmer
import screed

import khmer_tst_utils as utils
from nose.plugins.attrib import attr

# Below, 'fakelump.fa' is an artificial data set of 3x1 kb sequences in
# which the last 79 bases are common between the 3 sequences.

@attr('highmem')
def test_fakelump_together():
    fakelump_fa = utils.get_test_data('fakelump.fa')

    ht = khmer.new_hashbits(32, 1e7, 4)
    ht.consume_fasta_and_tag(fakelump_fa)

    subset = ht.do_subset_partition(0, 0)
    ht.merge_subset(subset)

    (n_partitions, n_singletons) = ht.count_partitions()
    assert n_partitions == 1, n_partitions

# try loading stop tags from previously saved

@attr('highmem')
def test_fakelump_stop():
    fakelump_fa = utils.get_test_data('fakelump.fa')
    fakelump_stoptags_txt = utils.get_test_data('fakelump.fa.stoptags.txt')

    ht = khmer.new_hashbits(32, 1e7, 4)
    ht.consume_fasta_and_tag(fakelump_fa)

    for line in open(fakelump_stoptags_txt):
        ht.add_stop_tag(line.strip())

    subset = ht.do_subset_partition(0, 0, True)
    ht.merge_subset(subset)

    (n_partitions, n_singletons) = ht.count_partitions()
    assert n_partitions == 3, n_partitions

# check specific insertion of stop tag

@attr('highmem')
def test_fakelump_stop2():
    fakelump_fa = utils.get_test_data('fakelump.fa')

    ht = khmer.new_hashbits(32, 1e7, 4)
    ht.consume_fasta_and_tag(fakelump_fa)

    ht.add_stop_tag('GGGGAGGGGTGCAGTTGTGACTTGCTCGAGAG')

    subset = ht.do_subset_partition(0, 0, True)
    ht.merge_subset(subset)

    (n_partitions, n_singletons) = ht.count_partitions()
    assert n_partitions == 3, n_partitions

# try repartitioning

@attr('highmem')
def test_fakelump_repartitioning():
    fakelump_fa = utils.get_test_data('fakelump.fa')
    fakelump_fa_foo = utils.get_temp_filename('fakelump.fa.stopfoo')

    ht = khmer.new_hashbits(32, 1e7, 4)
    ht.consume_fasta_and_tag(fakelump_fa)

    subset = ht.do_subset_partition(0, 0)
    ht.merge_subset(subset)

    (n_partitions, n_singletons) = ht.count_partitions()
    assert n_partitions == 1, n_partitions

    # now, break partitions on any k-mer that you see more than once
    # on big excursions, where big excursions are excursions 40 out
    # that encounter more than 82 k-mers.  This should specifically
    # identify our connected sequences in fakelump...

    EXCURSION_DISTANCE = 40
    EXCURSION_KMER_THRESHOLD = 82
    EXCURSION_KMER_COUNT_THRESHOLD = 1
    counting = khmer.new_counting_hash(32, 1e7, 4)

    ht.repartition_largest_partition(None, counting,
                                     EXCURSION_DISTANCE,
                                     EXCURSION_KMER_THRESHOLD,
                                     EXCURSION_KMER_COUNT_THRESHOLD)

    ht.save_stop_tags(fakelump_fa_foo)

    # ok, now re-do everything with these stop tags, specifically.

    ht = khmer.new_hashbits(32, 1e7, 4)
    ht.consume_fasta_and_tag(fakelump_fa)
    ht.load_stop_tags(fakelump_fa_foo)

    subset = ht.do_subset_partition(0, 0, True)
    ht.merge_subset(subset)

    (n_partitions, n_singletons) = ht.count_partitions()
    assert n_partitions == 3, n_partitions

@attr('highmem')
def test_fakelump_load_stop_tags_trunc():
    fakelump_fa = utils.get_test_data('fakelump.fa')
    fakelump_fa_foo = utils.get_temp_filename('fakelump.fa.stopfoo')

    ht = khmer.new_hashbits(32, 1e7, 4)
    ht.consume_fasta_and_tag(fakelump_fa)

    subset = ht.do_subset_partition(0, 0)
    ht.merge_subset(subset)

    (n_partitions, n_singletons) = ht.count_partitions()
    assert n_partitions == 1, n_partitions

    # now, break partitions on any k-mer that you see more than once
    # on big excursions, where big excursions are excursions 40 out
    # that encounter more than 82 k-mers.  This should specifically
    # identify our connected sequences in fakelump...

    EXCURSION_DISTANCE = 40
    EXCURSION_KMER_THRESHOLD = 82
    EXCURSION_KMER_COUNT_THRESHOLD = 1
    counting = khmer.new_counting_hash(32, 1e7, 4)

    ht.repartition_largest_partition(None, counting,
                                     EXCURSION_DISTANCE,
                                     EXCURSION_KMER_THRESHOLD,
                                     EXCURSION_KMER_COUNT_THRESHOLD)

    ht.save_stop_tags(fakelump_fa_foo)
    data = open(fakelump_fa_foo).read()

    fp = open(fakelump_fa_foo, 'wb')
    fp.write(data[:10])
    fp.close()

    # ok, now try loading these stop tags; should fail.
    ht = khmer.new_hashbits(32, 1e7, 4)
    ht.consume_fasta_and_tag(fakelump_fa)

    try:
        ht.load_stop_tags(fakelump_fa_foo)
        assert 0, "this test should fail"
    except IOError:
        pass

@attr('highmem')
def test_fakelump_load_stop_tags_notexist():
    fakelump_fa_foo = utils.get_temp_filename('fakelump.fa.stopfoo')

    # ok, now try loading these stop tags; should fail.
    ht = khmer.new_hashbits(32, 1e7, 4)

    try:
        ht.load_stop_tags(fakelump_fa_foo)
        assert 0, "this test should fail"
    except IOError:
        pass
<commit_msg>Reduced memory requirements in test_lump.py<commit_after>#
# This file is part of khmer, http://github.com/ged-lab/khmer/, and is
# Copyright (C) Michigan State University, 2009-2013. It is licensed under
# the three-clause BSD license; see doc/LICENSE.txt.
# Contact: khmer-project@idyll.org
#
# pylint: disable=missing-docstring
import khmer
import screed

import khmer_tst_utils as utils
from nose.plugins.attrib import attr

# Below, 'fakelump.fa' is an artificial data set of 3x1 kb sequences in
# which the last 79 bases are common between the 3 sequences.

@attr('highmem')
def test_fakelump_together():
    fakelump_fa = utils.get_test_data('fakelump.fa')

    ht = khmer.new_hashbits(32, 1e5, 4)
    ht.consume_fasta_and_tag(fakelump_fa)

    subset = ht.do_subset_partition(0, 0)
    ht.merge_subset(subset)

    (n_partitions, n_singletons) = ht.count_partitions()
    assert n_partitions == 1, n_partitions

# try loading stop tags from previously saved

@attr('highmem')
def test_fakelump_stop():
    fakelump_fa = utils.get_test_data('fakelump.fa')
    fakelump_stoptags_txt = utils.get_test_data('fakelump.fa.stoptags.txt')

    ht = khmer.new_hashbits(32, 1e5, 4)
    ht.consume_fasta_and_tag(fakelump_fa)

    for line in open(fakelump_stoptags_txt):
        ht.add_stop_tag(line.strip())

    subset = ht.do_subset_partition(0, 0, True)
    ht.merge_subset(subset)

    (n_partitions, n_singletons) = ht.count_partitions()
    assert n_partitions == 3, n_partitions

# check specific insertion of stop tag

@attr('highmem')
def test_fakelump_stop2():
    fakelump_fa = utils.get_test_data('fakelump.fa')

    ht = khmer.new_hashbits(32, 1e5, 4)
    ht.consume_fasta_and_tag(fakelump_fa)

    ht.add_stop_tag('GGGGAGGGGTGCAGTTGTGACTTGCTCGAGAG')

    subset = ht.do_subset_partition(0, 0, True)
    ht.merge_subset(subset)

    (n_partitions, n_singletons) = ht.count_partitions()
    assert n_partitions == 3, n_partitions

# try repartitioning

@attr('highmem')
def test_fakelump_repartitioning():
    fakelump_fa = utils.get_test_data('fakelump.fa')
    fakelump_fa_foo = utils.get_temp_filename('fakelump.fa.stopfoo')

    ht = khmer.new_hashbits(32, 1e5, 4)
    ht.consume_fasta_and_tag(fakelump_fa)

    subset = ht.do_subset_partition(0, 0)
    ht.merge_subset(subset)

    (n_partitions, n_singletons) = ht.count_partitions()
    assert n_partitions == 1, n_partitions

    # now, break partitions on any k-mer that you see more than once
    # on big excursions, where big excursions are excursions 40 out
    # that encounter more than 82 k-mers.  This should specifically
    # identify our connected sequences in fakelump...

    EXCURSION_DISTANCE = 40
    EXCURSION_KMER_THRESHOLD = 82
    EXCURSION_KMER_COUNT_THRESHOLD = 1
    counting = khmer.new_counting_hash(32, 1e4, 4)

    ht.repartition_largest_partition(None, counting,
                                     EXCURSION_DISTANCE,
                                     EXCURSION_KMER_THRESHOLD,
                                     EXCURSION_KMER_COUNT_THRESHOLD)

    ht.save_stop_tags(fakelump_fa_foo)

    # ok, now re-do everything with these stop tags, specifically.

    ht = khmer.new_hashbits(32, 1e5, 4)
    ht.consume_fasta_and_tag(fakelump_fa)
    ht.load_stop_tags(fakelump_fa_foo)

    subset = ht.do_subset_partition(0, 0, True)
    ht.merge_subset(subset)

    (n_partitions, n_singletons) = ht.count_partitions()
    assert n_partitions == 3, n_partitions

@attr('highmem')
def test_fakelump_load_stop_tags_trunc():
    fakelump_fa = utils.get_test_data('fakelump.fa')
    fakelump_fa_foo = utils.get_temp_filename('fakelump.fa.stopfoo')

    ht = khmer.new_hashbits(32, 1e5, 4)
    ht.consume_fasta_and_tag(fakelump_fa)

    subset = ht.do_subset_partition(0, 0)
    ht.merge_subset(subset)

    (n_partitions, n_singletons) = ht.count_partitions()
    assert n_partitions == 1, n_partitions

    # now, break partitions on any k-mer that you see more than once
    # on big excursions, where big excursions are excursions 40 out
    # that encounter more than 82 k-mers.  This should specifically
    # identify our connected sequences in fakelump...

    EXCURSION_DISTANCE = 40
    EXCURSION_KMER_THRESHOLD = 82
    EXCURSION_KMER_COUNT_THRESHOLD = 1
    counting = khmer.new_counting_hash(32, 4, 4)

    ht.repartition_largest_partition(None, counting,
                                     EXCURSION_DISTANCE,
                                     EXCURSION_KMER_THRESHOLD,
                                     EXCURSION_KMER_COUNT_THRESHOLD)

    ht.save_stop_tags(fakelump_fa_foo)
    data = open(fakelump_fa_foo).read()

    fp = open(fakelump_fa_foo, 'wb')
    fp.write(data[:10])
    fp.close()

    # ok, now try loading these stop tags; should fail.
    ht = khmer.new_hashbits(32, 4, 4)
    ht.consume_fasta_and_tag(fakelump_fa)

    try:
        ht.load_stop_tags(fakelump_fa_foo)
        assert 0, "this test should fail"
    except IOError:
        pass

@attr('highmem')
def test_fakelump_load_stop_tags_notexist():
    fakelump_fa_foo = utils.get_temp_filename('fakelump.fa.stopfoo')

    # ok, now try loading these stop tags; should fail.
    ht = khmer.new_hashbits(32, 4, 4)

    try:
        ht.load_stop_tags(fakelump_fa_foo)
        assert 0, "this test should fail"
    except IOError:
        pass
<|endoftext|>


<commit_before>import re
import json
import pickle
import unittest
import warnings
import importlib
import importlib_metadata
import pyfakefs.fake_filesystem_unittest as ffs

from . import fixtures
from importlib_metadata import (
    Distribution,
    EntryPoint,
    MetadataPathFinder,
    PackageNotFoundError,
    distributions,
    entry_points,
    metadata,
    packages_distributions,
    version,
)

class BasicTests(fixtures.DistInfoPkg, unittest.TestCase):
    version_pattern = r'\d+\.\d+(\.\d)?'

    def test_retrieves_version_of_self(self):
        dist = Distribution.from_name('distinfo-pkg')
        assert isinstance(dist.version, str)
        assert re.match(self.version_pattern, dist.version)

    def test_for_name_does_not_exist(self):
        with self.assertRaises(PackageNotFoundError):
            Distribution.from_name('does-not-exist')

    def test_package_not_found_mentions_metadata(self):
        """
        When a package is not found, that could indicate that the
        packgae is not installed or that it is installed without
        metadata. Ensure the exception mentions metadata to help
        guide users toward the cause. See #124.
        """
        with self.assertRaises(PackageNotFoundError) as ctx:
            Distribution.from_name('does-not-exist')

        assert "metadata" in str(ctx.exception)

    def test_new_style_classes(self):
        self.assertIsInstance(Distribution, type)
        self.assertIsInstance(MetadataPathFinder, type)

class ImportTests(fixtures.DistInfoPkg, unittest.TestCase):
    def test_import_nonexistent_module(self):
        # Ensure that the MetadataPathFinder does not crash an import of a
        # non-existent module.
        with self.assertRaises(ImportError):
            importlib.import_module('does_not_exist')

    def test_resolve(self):
        ep = entry_points(group='entries')['main']
        self.assertEqual(ep.load().__name__, "main")

    def test_entrypoint_with_colon_in_name(self):
        ep = entry_points(group='entries')['ns:sub']
        self.assertEqual(ep.value, 'mod:main')

    def test_resolve_without_attr(self):
        ep = EntryPoint(
            name='ep',
            value='importlib_metadata',
            group='grp',
        )
        assert ep.load() is importlib_metadata

class NameNormalizationTests(fixtures.OnSysPath, fixtures.SiteDir, unittest.TestCase):
    @staticmethod
    def pkg_with_dashes(site_dir):
        """
        Create minimal metadata for a package with dashes
        in the name (and thus underscores in the filename).
        """
        contents = {
            'my_pkg.dist-info': {
                'METADATA': 'VERSION: 1.0\n',
            },
        }
        fixtures.build_files(contents, site_dir)
        return 'my-pkg'

    def test_dashes_in_dist_name_found_as_underscores(self):
        """
        For a package with a dash in the name, the dist-info metadata
        uses underscores in the name. Ensure the metadata loads.
        """
        pkg_name = self.pkg_with_dashes(self.site_dir)
        assert version(pkg_name) == '1.0'

    @staticmethod
    def pkg_with_mixed_case(site_dir):
        """
        Create minimal metadata for a package with mixed case
        in the name.
        """
        contents = {
            'CherryPy.dist-info': {
                'METADATA': 'VERSION: 1.0\n',
            },
        }
        fixtures.build_files(contents, site_dir)
        return 'CherryPy'

    def test_dist_name_found_as_any_case(self):
        """
        Ensure the metadata loads when queried with any case.
        """
        pkg_name = self.pkg_with_mixed_case(self.site_dir)
        assert version(pkg_name) == '1.0'
        assert version(pkg_name.lower()) == '1.0'
        assert version(pkg_name.upper()) == '1.0'

class NonASCIITests(fixtures.OnSysPath, fixtures.SiteDir, unittest.TestCase):
    @staticmethod
    def pkg_with_non_ascii_description(site_dir):
        """
        Create minimal metadata for a package with non-ASCII in
        the description.
        """
        contents = {
            'portend.dist-info': {
                'METADATA': 'Description: pôrˈtend',
            },
        }
        fixtures.build_files(contents, site_dir)
        return 'portend'

    @staticmethod
    def pkg_with_non_ascii_description_egg_info(site_dir):
        """
        Create minimal metadata for an egg-info package with
        non-ASCII in the description.
        """
        contents = {
            'portend.dist-info': {
                'METADATA': """
                Name: portend

                pôrˈtend""",
            },
        }
        fixtures.build_files(contents, site_dir)
        return 'portend'

    def test_metadata_loads(self):
        pkg_name = self.pkg_with_non_ascii_description(self.site_dir)
        meta = metadata(pkg_name)
        assert meta['Description'] == 'pôrˈtend'

    def test_metadata_loads_egg_info(self):
        pkg_name = self.pkg_with_non_ascii_description_egg_info(self.site_dir)
        meta = metadata(pkg_name)
        assert meta['Description'] == 'pôrˈtend'

class DiscoveryTests(fixtures.EggInfoPkg, fixtures.DistInfoPkg, unittest.TestCase):
    def test_package_discovery(self):
        dists = list(distributions())
        assert all(isinstance(dist, Distribution) for dist in dists)
        assert any(dist.metadata['Name'] == 'egginfo-pkg' for dist in dists)
        assert any(dist.metadata['Name'] == 'distinfo-pkg' for dist in dists)

    def test_invalid_usage(self):
        with self.assertRaises(ValueError):
            list(distributions(context='something', name='else'))

class DirectoryTest(fixtures.OnSysPath, fixtures.SiteDir, unittest.TestCase):
    def test_egg_info(self):
        # make an `EGG-INFO` directory that's unrelated
        self.site_dir.joinpath('EGG-INFO').mkdir()
        # used to crash with `IsADirectoryError`
        with self.assertRaises(PackageNotFoundError):
            version('unknown-package')

    def test_egg(self):
        egg = self.site_dir.joinpath('foo-3.6.egg')
        egg.mkdir()
        with self.add_sys_path(egg):
            with self.assertRaises(PackageNotFoundError):
                version('foo')

class MissingSysPath(fixtures.OnSysPath, unittest.TestCase):
    site_dir = '/does-not-exist'

    def test_discovery(self):
        """
        Discovering distributions should succeed even if
        there is an invalid path on sys.path.
        """
        importlib_metadata.distributions()

class InaccessibleSysPath(fixtures.OnSysPath, ffs.TestCase):
    site_dir = '/access-denied'

    def setUp(self):
        super().setUp()
        self.setUpPyfakefs()
        self.fs.create_dir(self.site_dir, perm_bits=000)

    def test_discovery(self):
        """
        Discovering distributions should succeed even if
        there is an invalid path on sys.path.
        """
        list(importlib_metadata.distributions())

class TestEntryPoints(unittest.TestCase):
    def __init__(self, *args):
        super().__init__(*args)
        self.ep = importlib_metadata.EntryPoint(
            name='name', value='value', group='group'
        )

    def test_entry_point_pickleable(self):
        revived = pickle.loads(pickle.dumps(self.ep))
        assert revived == self.ep

    def test_positional_args(self):
        """
        Capture legacy (namedtuple) construction, discouraged.
        """
        EntryPoint('name', 'value', 'group')

    def test_immutable(self):
        """EntryPoints should be immutable"""
        with self.assertRaises(AttributeError):
            self.ep.name = 'badactor'

    def test_repr(self):
        assert 'EntryPoint' in repr(self.ep)
        assert 'name=' in repr(self.ep)
        assert "'name'" in repr(self.ep)

    def test_hashable(self):
        """EntryPoints should be hashable"""
        hash(self.ep)

    def test_json_dump(self):
        """
        json should not expect to be able to dump an EntryPoint
        """
        with self.assertRaises(Exception):
            with warnings.catch_warnings(record=True):
                json.dumps(self.ep)

    def test_module(self):
        assert self.ep.module == 'value'

    def test_attr(self):
        assert self.ep.attr is None

    def test_sortable(self):
        """
        EntryPoint objects are sortable, but result is undefined.
        """
        sorted(
            [
                EntryPoint(name='b', value='val', group='group'),
                EntryPoint(name='a', value='val', group='group'),
            ]
        )

class FileSystem(
    fixtures.OnSysPath, fixtures.SiteDir, fixtures.FileBuilder, unittest.TestCase
):
    def test_unicode_dir_on_sys_path(self):
        """
        Ensure a Unicode subdirectory of a directory on sys.path
        does not crash.
        """
        fixtures.build_files(
            {self.unicode_filename(): {}},
            prefix=self.site_dir,
        )
        list(distributions())

class PackagesDistributionsPrebuiltTest(fixtures.ZipFixtures, unittest.TestCase):
    def test_packages_distributions_example(self):
        self._fixture_on_path('example-21.12-py3-none-any.whl')
        assert packages_distributions()['example'] == ['example']

    def test_packages_distributions_example2(self):
        """
        Test packages_distributions on a wheel built
        by trampolim.
        """
        self._fixture_on_path('example2-1.0.0-py3-none-any.whl')
        assert packages_distributions()['example2'] == ['example2']

class PackagesDistributionsTest(
    fixtures.OnSysPath, fixtures.SiteDir, unittest.TestCase
):
    def test_packages_distributions_neither_toplevel_nor_files(self):
        """
        Test a package built without 'top-level.txt' or a file list.
        """
        fixtures.build_files(
            {
                'trim_example-1.0.0.dist-info': {
                    'METADATA': """
                Name: trim_example
                Version: 1.0.0
                """,
                }
            },
            prefix=self.site_dir,
        )
        packages_distributions()
<commit_msg>Consolidate logic for generating minimal metadata.<commit_after>import re
import json
import pickle
import unittest
import warnings
import importlib
import importlib_metadata
import pyfakefs.fake_filesystem_unittest as ffs

from . import fixtures
from importlib_metadata import (
    Distribution,
    EntryPoint,
    MetadataPathFinder,
    PackageNotFoundError,
    distributions,
    entry_points,
    metadata,
    packages_distributions,
    version,
)

class BasicTests(fixtures.DistInfoPkg, unittest.TestCase):
    version_pattern = r'\d+\.\d+(\.\d)?'

    def test_retrieves_version_of_self(self):
        dist = Distribution.from_name('distinfo-pkg')
        assert isinstance(dist.version, str)
        assert re.match(self.version_pattern, dist.version)

    def test_for_name_does_not_exist(self):
        with self.assertRaises(PackageNotFoundError):
            Distribution.from_name('does-not-exist')

    def test_package_not_found_mentions_metadata(self):
        """
        When a package is not found, that could indicate that the
        packgae is not installed or that it is installed without
        metadata. Ensure the exception mentions metadata to help
        guide users toward the cause. See #124.
        """
        with self.assertRaises(PackageNotFoundError) as ctx:
            Distribution.from_name('does-not-exist')

        assert "metadata" in str(ctx.exception)

    def test_new_style_classes(self):
        self.assertIsInstance(Distribution, type)
        self.assertIsInstance(MetadataPathFinder, type)

class ImportTests(fixtures.DistInfoPkg, unittest.TestCase):
    def test_import_nonexistent_module(self):
        # Ensure that the MetadataPathFinder does not crash an import of a
        # non-existent module.
        with self.assertRaises(ImportError):
            importlib.import_module('does_not_exist')

    def test_resolve(self):
        ep = entry_points(group='entries')['main']
        self.assertEqual(ep.load().__name__, "main")

    def test_entrypoint_with_colon_in_name(self):
        ep = entry_points(group='entries')['ns:sub']
        self.assertEqual(ep.value, 'mod:main')

    def test_resolve_without_attr(self):
        ep = EntryPoint(
            name='ep',
            value='importlib_metadata',
            group='grp',
        )
        assert ep.load() is importlib_metadata

class NameNormalizationTests(fixtures.OnSysPath, fixtures.SiteDir, unittest.TestCase):
    @staticmethod
    def make_pkg(name):
        """
        Create minimal metadata for a dist-info package with
        the indicated name on the file system.
        """
        return {
            f'{name}.dist-info': {
                'METADATA': 'VERSION: 1.0\n',
            },
        }

    def test_dashes_in_dist_name_found_as_underscores(self):
        """
        For a package with a dash in the name, the dist-info metadata
        uses underscores in the name. Ensure the metadata loads.
        """
        fixtures.build_files(self.make_pkg('my_pkg'), self.site_dir)
        assert version('my-pkg') == '1.0'

    def test_dist_name_found_as_any_case(self):
        """
        Ensure the metadata loads when queried with any case.
        """
        pkg_name = 'CherryPy'
        fixtures.build_files(self.make_pkg(pkg_name), self.site_dir)
        assert version(pkg_name) == '1.0'
        assert version(pkg_name.lower()) == '1.0'
        assert version(pkg_name.upper()) == '1.0'

class NonASCIITests(fixtures.OnSysPath, fixtures.SiteDir, unittest.TestCase):
    @staticmethod
    def pkg_with_non_ascii_description(site_dir):
        """
        Create minimal metadata for a package with non-ASCII in
        the description.
        """
        contents = {
            'portend.dist-info': {
                'METADATA': 'Description: pôrˈtend',
            },
        }
        fixtures.build_files(contents, site_dir)
        return 'portend'

    @staticmethod
    def pkg_with_non_ascii_description_egg_info(site_dir):
        """
        Create minimal metadata for an egg-info package with
        non-ASCII in the description.
        """
        contents = {
            'portend.dist-info': {
                'METADATA': """
                Name: portend

                pôrˈtend""",
            },
        }
        fixtures.build_files(contents, site_dir)
        return 'portend'

    def test_metadata_loads(self):
        pkg_name = self.pkg_with_non_ascii_description(self.site_dir)
        meta = metadata(pkg_name)
        assert meta['Description'] == 'pôrˈtend'

    def test_metadata_loads_egg_info(self):
        pkg_name = self.pkg_with_non_ascii_description_egg_info(self.site_dir)
        meta = metadata(pkg_name)
        assert meta['Description'] == 'pôrˈtend'

class DiscoveryTests(fixtures.EggInfoPkg, fixtures.DistInfoPkg, unittest.TestCase):
    def test_package_discovery(self):
        dists = list(distributions())
        assert all(isinstance(dist, Distribution) for dist in dists)
        assert any(dist.metadata['Name'] == 'egginfo-pkg' for dist in dists)
        assert any(dist.metadata['Name'] == 'distinfo-pkg' for dist in dists)

    def test_invalid_usage(self):
        with self.assertRaises(ValueError):
            list(distributions(context='something', name='else'))

class DirectoryTest(fixtures.OnSysPath, fixtures.SiteDir, unittest.TestCase):
    def test_egg_info(self):
        # make an `EGG-INFO` directory that's unrelated
        self.site_dir.joinpath('EGG-INFO').mkdir()
        # used to crash with `IsADirectoryError`
        with self.assertRaises(PackageNotFoundError):
            version('unknown-package')

    def test_egg(self):
        egg = self.site_dir.joinpath('foo-3.6.egg')
        egg.mkdir()
        with self.add_sys_path(egg):
            with self.assertRaises(PackageNotFoundError):
                version('foo')

class MissingSysPath(fixtures.OnSysPath, unittest.TestCase):
    site_dir = '/does-not-exist'

    def test_discovery(self):
        """
        Discovering distributions should succeed even if
        there is an invalid path on sys.path.
        """
        importlib_metadata.distributions()

class InaccessibleSysPath(fixtures.OnSysPath, ffs.TestCase):
    site_dir = '/access-denied'

    def setUp(self):
        super().setUp()
        self.setUpPyfakefs()
        self.fs.create_dir(self.site_dir, perm_bits=000)

    def test_discovery(self):
        """
        Discovering distributions should succeed even if
        there is an invalid path on sys.path.
        """
        list(importlib_metadata.distributions())

class TestEntryPoints(unittest.TestCase):
    def __init__(self, *args):
        super().__init__(*args)
        self.ep = importlib_metadata.EntryPoint(
            name='name', value='value', group='group'
        )

    def test_entry_point_pickleable(self):
        revived = pickle.loads(pickle.dumps(self.ep))
        assert revived == self.ep

    def test_positional_args(self):
        """
        Capture legacy (namedtuple) construction, discouraged.
        """
        EntryPoint('name', 'value', 'group')

    def test_immutable(self):
        """EntryPoints should be immutable"""
        with self.assertRaises(AttributeError):
            self.ep.name = 'badactor'

    def test_repr(self):
        assert 'EntryPoint' in repr(self.ep)
        assert 'name=' in repr(self.ep)
        assert "'name'" in repr(self.ep)

    def test_hashable(self):
        """EntryPoints should be hashable"""
        hash(self.ep)

    def test_json_dump(self):
        """
        json should not expect to be able to dump an EntryPoint
        """
        with self.assertRaises(Exception):
            with warnings.catch_warnings(record=True):
                json.dumps(self.ep)

    def test_module(self):
        assert self.ep.module == 'value'

    def test_attr(self):
        assert self.ep.attr is None

    def test_sortable(self):
        """
        EntryPoint objects are sortable, but result is undefined.
        """
        sorted(
            [
                EntryPoint(name='b', value='val', group='group'),
                EntryPoint(name='a', value='val', group='group'),
            ]
        )

class FileSystem(
    fixtures.OnSysPath, fixtures.SiteDir, fixtures.FileBuilder, unittest.TestCase
):
    def test_unicode_dir_on_sys_path(self):
        """
        Ensure a Unicode subdirectory of a directory on sys.path
        does not crash.
        """
        fixtures.build_files(
            {self.unicode_filename(): {}},
            prefix=self.site_dir,
        )
        list(distributions())

class PackagesDistributionsPrebuiltTest(fixtures.ZipFixtures, unittest.TestCase):
    def test_packages_distributions_example(self):
        self._fixture_on_path('example-21.12-py3-none-any.whl')
        assert packages_distributions()['example'] == ['example']

    def test_packages_distributions_example2(self):
        """
        Test packages_distributions on a wheel built
        by trampolim.
        """
        self._fixture_on_path('example2-1.0.0-py3-none-any.whl')
        assert packages_distributions()['example2'] == ['example2']

class PackagesDistributionsTest(
    fixtures.OnSysPath, fixtures.SiteDir, unittest.TestCase
):
    def test_packages_distributions_neither_toplevel_nor_files(self):
        """
        Test a package built without 'top-level.txt' or a file list.
        """
        fixtures.build_files(
            {
                'trim_example-1.0.0.dist-info': {
                    'METADATA': """
                Name: trim_example
                Version: 1.0.0
                """,
                }
            },
            prefix=self.site_dir,
        )
        packages_distributions()
<|endoftext|>