Ejemplo n.º 1
0
#!/usr/bin/env python3

from utest import utest, utest_exc
from pithy.strings import *


utest(True, string_contains, '', '') # strange, but simply the behavior of string.find.
utest(True, string_contains, 'a', '')
utest(True, string_contains, 'a', 'a')
utest(False, string_contains, '', 'a')

utest('-rest', clip_prefix, 'prefix-rest', 'prefix')
utest('prefix-rest', clip_prefix, 'prefix-rest', 'notpresent', req=False)
utest_exc(ValueError('prefix-rest'), clip_prefix, 'prefix-rest', 'notpresent')

utest('rest-', clip_suffix, 'rest-suffix', 'suffix')
utest('rest-suffix', clip_suffix, 'rest-suffix', 'notpresent', req=False)
utest_exc(ValueError('rest-suffix'), clip_suffix, 'rest-suffix', 'notpresent')
# What is the difference between this and clip definition wise, and why does it work like this

utest('-rest', clip_first_prefix, 'firstprefix-rest', ['firstprefix','first'])
utest('firstprefix-rest', clip_first_prefix, 'firstprefix-rest', ['notpresent','notpresenteither'], req=False)
utest_exc(ValueError('firstprefix-rest'), clip_first_prefix, 'firstprefix-rest', ['notpresent','notpresenteither'])

utest('', plural_s, 1)
utest('s', plural_s, 9)

utest('rest-', clip_suffix, 'rest-suffix', 'suffix')
utest_exc(ValueError('rest-suffix'), clip_suffix, 'rest-suffix', 'notpresent')

format_test_vals = [
Ejemplo n.º 2
0
Archivo: dicts.py Proyecto: em-p/pithy
#!/usr/bin/env python3

from utest import utest, utest_exc
from pithy.dicts import *

def dict_put_test(d, k, v):
  dict_put(d, k, v)
  return d

utest({1:2}, dict_put_test, {}, 1,2)
utest_exc(KeyError(1), dict_put_test, {1:0}, 1,2)

def dict_append_test(d, k, v):
  dict_append(d, k, v)
  return d

utest({'k': [0]}, dict_append_test, {}, 'k', 0)
utest({'k': [0,1]}, dict_append_test, {'k':[0]}, 'k', 1)

def dict_extend_test(d, k, v):
  dict_extend(d, k, v)
  return d

utest({'k': []},dict_extend_test, {}, 'k', [])
utest({'k': [1]},dict_extend_test, {}, 'k', [1])
utest({'k': [0,1,2]}, dict_extend_test,{'k': [0]}, 'k', [1,2])

def dict_set_defaults_test(d, defaults):
  dict_set_defaults(d, defaults)
  return d
Ejemplo n.º 3
0
#!/usr/bin/env python3

from utest import utest, utest_exc
from pithy.dict_utils import *

utest({"k": 0}, dict_put, {}, "k", 0)
utest_exc(KeyError("k"), dict_put, {"k": 0}, "k", 1)

utest({"k": [0]}, dict_list_append, {}, "k", 0)
utest({"k": [0, 1]}, dict_list_append, {"k": [0]}, "k", 1)

utest({"k": []}, dict_list_extend, {}, "k", [])
utest({"k": [0]}, dict_list_extend, {"k": []}, "k", [0])
utest({"k": [0, 1, 2]}, dict_list_extend, {"k": [0]}, "k", [1, 2])

utest({"k": 0, "l": 2}, dict_set_defaults, {"k": 0}, {"k": 1, "l": 2})
utest({"k": 0, "l": 2}, dict_set_defaults, {"k": 0}, [("k", 1), ("l", 2)])


def dict_filter_map_test(d, seq):
    return list(dict_filter_map(d, seq))


utest([-1, -3], dict_filter_map_test, {1: -1, 3: -3}, [0, 1, 2, 3])


def DefaultByKeyDict_test(factory, test_keys):
    d = DefaultByKeyDict(factory)
    for k in test_keys:
        d[k]
    return d