<commit_before>
import unittest
import fudge
from nose.tools import eq_, raises
from fudge import (
    Fake, Registry, ExpectedCall, ExpectedCallOrder, Call, CallStack, FakeDeclarationError)

def test_decorator_on_def():
    class holder:
        test_called = False
    
    bobby = fudge.Fake()
    bobby.expects("suzie_called")
    
    @raises(AssertionError)
    @fudge.with_fakes
    def some_test():
        holder.test_called = True
    
    eq_(some_test.__name__, 'some_test')
    some_test()
    
    eq_(holder.test_called, True)

# for a test below
_some_fake = fudge.Fake()

class TestFake(unittest.TestCase):
    
    def test_guess_name(self):
        my_obj = fudge.Fake()
        eq_(repr(my_obj), "fake:my_obj")
        
    def test_guess_name_globals(self):
        eq_(repr(_some_fake), "fake:_some_fake")
        
    def test_guess_name_deref(self):
        my_obj = 44
        my_obj = fudge.Fake()
        eq_(repr(my_obj), "fake:my_obj")
    
    def test_attributes(self):
        my_obj = fudge.Fake().has_attr(vice='versa', beach='playa')
        eq_(my_obj.vice, 'versa')
        eq_(my_obj.beach, 'playa')
    
    def test_attributes_can_replace_internals(self):
        my_obj = fudge.Fake().has_attr(provides='hijacked')
        eq_(my_obj.provides, 'hijacked')
    
    def test_repr_shortens_long_values(self):
        fake = Fake("widget").provides("set_bits").with_args(
            "12345678910111213141516171819202122232425262728293031"
        )
        try:
            fake.set_bits()
        except AssertionError, exc:
            eq_(str(exc),
            "fake:widget.set_bits('123456789101112131415161718192021222324252627...') "
            "was called unexpectedly with args ()")
        else:
            raise RuntimeError("expected AssertionError")

class TestLongArgValues(unittest.TestCase):
    
    def test_arg_diffs_are_not_shortened(self):
        fake = Fake("widget").provides("set_bits").with_args(
            "12345678910111213141516171819202122232425262728293031"
        )
        try:
            # this should not be shortened but the above arg spec should:
            fake.set_bits("99999999999999999999999999999999999999999999999999999999")
        except AssertionError, exc:
            eq_(str(exc),
            "fake:widget.set_bits('123456789101112131415161718192021222324252627...') "
            "was called unexpectedly with args "
            "('99999999999999999999999999999999999999999999999999999999')")
        else:
            raise RuntimeError("expected AssertionError")
    
    def test_kwarg_diffs_are_not_shortened(self):
        fake = Fake("widget").provides("set_bits").with_args(
            newbits="12345678910111213141516171819202122232425262728293031"
        )
        try:
            # this should not be shortened but the above arg spec should:
            fake.set_bits(newbits="99999999999999999999999999999999999999999999999999999999")
        except AssertionError, exc:
            eq_(str(exc),
            "fake:widget.set_bits(newbits='123456789101112131415161718192021222324252627...') "
            "was called unexpectedly with args "
            "(newbits='99999999999999999999999999999999999999999999999999999999')")
        else:
            raise RuntimeError("expected AssertionError")

class TestReturnsFake(unittest.TestCase):
    
    def test_returns_fake_has_name(self):
        f = Fake().provides("get_widget").returns_fake()
        eq_(f._name, "get_widget")

class TestArguments(unittest.TestCase):
    
    def setUp(self):
        self.fake = fudge.Fake()
    
    def tearDown(self):
        fudge.clear_expectations()
    
    @raises(AssertionError)
    def test_wrong_args(self):
        exp = self.fake.expects('theCall').with_args(
                        1, 
                        "ho ho ho ho ho ho ho, it's Santa", 
                        {'ditto':False})
        exp.theCall()
        
    @raises(AssertionError)
    def test_wrong_kwargs(self):
        exp = self.fake.expects('other').with_args(one="twozy", items=[1,2,3,4])
        exp.other(nice="NOT NICE")
        
    @raises(AssertionError)
    def test_arg_count(self):
        exp = self.fake.expects('one').with_arg_count(3)
        exp.one('no', 'maybe')
        
    @raises(AssertionError)
    def test_kwarg_count(self):
        exp = self.fake.expects('__init__').with_kwarg_count(2)
        exp(maybe="yes, maybe")
    
    @raises(FakeDeclarationError)
    def test_with_args_requires_a_method(self):
        self.fake.with_args('something')
    
    @raises(AssertionError)
    def test_with_args_can_operate_on_provision(self):
        self.fake.provides("not_expected").with_args('something')
        self.fake.not_expected() # should still raise arg error
    
    @raises(AssertionError)
    def test_with_args_checks_args(self):
        self.fake.expects('count').with_args('one', two='two')
        self.fake.count(two='two')
        
    @raises(AssertionError)
    def test_with_args_checks_kwargs(self):
        self.fake.expects('count').with_args('one', two='two')
        self.fake.count('one')
    
    @raises(AssertionError)
    def test_too_many_args(self):
        db = Fake("db").expects("execute").with_args(bind={'one':1})
        db.execute("select foozilate()", bind={'one':1}) # unexpected statement arg
    
    def test_zero_keywords_ok(self):
        mail = fudge.Fake('mail').expects('send').with_arg_count(3) 
        mail.send("you", "me", "hi") # no kw args should be ok
    
    def test_zero_args_ok(self):
        mail = fudge.Fake('mail').expects('send').with_kwarg_count(3) 
        mail.send(to="you", from_="me", msg="hi") # no args should be ok

class TestCallArgEquality(unittest.TestCase):
    
    def setUp(self):
        self.fake = Fake("foo")
        self.call = Call(self.fake)
    
    def test_complex_match_yields_no_reason(self):
        one = {'num':1, 'two':2, 'three':3}
        one = {'num':1, 'two':2, 'three':3}
        eq_(self.call._keywords_are_equal(one, one), (True, ""))
    
    def test_simple_mismatch_yields_no_reason(self):
        one = {'num':1}
        two = {'num':2}
        eq_(self.call._keywords_are_equal(one, two), (False, ""))
    
    def test_simple_match_yields_no_reason(self):
        one = {'num':1}
        one = {'num':1}
        eq_(self.call._keywords_are_equal(one, one), (True, ""))
    
    def test_actual_kw_extra_key(self):
        actual = {'one':1, 'two':2}
        expected = {'one':1}
        eq_(self.call._keywords_are_equal(actual, expected), 
            (False, "keyword 'two' was not expected"))
    
    def test_actual_kw_value_inequal(self):
        actual = {'one':1, 'two':2}
        expected = {'one':1, 'two':3}
        eq_(self.call._keywords_are_equal(actual, expected), 
            (False, "two=2 != two=3"))
    
    def test_expected_kw_extra_key(self):
        actual = {'one':1}
        expected = {'one':1, 'two':2}
        eq_(self.call._keywords_are_equal(actual, expected), 
            (False, "these keywords never showed up: 'two'"))
    
    def test_expected_kw_value_inequal(self):
        actual = {'one':1, 'two':'not two'}
        expected = {'one':1, 'two':2}
        eq_(self.call._keywords_are_equal(actual, expected), 
            (False, "two='not two' != two=2"))

class TestCall(unittest.TestCase):
    
    def setUp(self):
        self.fake = fudge.Fake('SMTP')
    <commit_msg>better names for keyword test vars<commit_after>
import unittest
import fudge
from nose.tools import eq_, raises
from fudge import (
    Fake, Registry, ExpectedCall, ExpectedCallOrder, Call, CallStack, FakeDeclarationError)

def test_decorator_on_def():
    class holder:
        test_called = False
    
    bobby = fudge.Fake()
    bobby.expects("suzie_called")
    
    @raises(AssertionError)
    @fudge.with_fakes
    def some_test():
        holder.test_called = True
    
    eq_(some_test.__name__, 'some_test')
    some_test()
    
    eq_(holder.test_called, True)

# for a test below
_some_fake = fudge.Fake()

class TestFake(unittest.TestCase):
    
    def test_guess_name(self):
        my_obj = fudge.Fake()
        eq_(repr(my_obj), "fake:my_obj")
        
    def test_guess_name_globals(self):
        eq_(repr(_some_fake), "fake:_some_fake")
        
    def test_guess_name_deref(self):
        my_obj = 44
        my_obj = fudge.Fake()
        eq_(repr(my_obj), "fake:my_obj")
    
    def test_attributes(self):
        my_obj = fudge.Fake().has_attr(vice='versa', beach='playa')
        eq_(my_obj.vice, 'versa')
        eq_(my_obj.beach, 'playa')
    
    def test_attributes_can_replace_internals(self):
        my_obj = fudge.Fake().has_attr(provides='hijacked')
        eq_(my_obj.provides, 'hijacked')
    
    def test_repr_shortens_long_values(self):
        fake = Fake("widget").provides("set_bits").with_args(
            "12345678910111213141516171819202122232425262728293031"
        )
        try:
            fake.set_bits()
        except AssertionError, exc:
            eq_(str(exc),
            "fake:widget.set_bits('123456789101112131415161718192021222324252627...') "
            "was called unexpectedly with args ()")
        else:
            raise RuntimeError("expected AssertionError")

class TestLongArgValues(unittest.TestCase):
    
    def test_arg_diffs_are_not_shortened(self):
        fake = Fake("widget").provides("set_bits").with_args(
            "12345678910111213141516171819202122232425262728293031"
        )
        try:
            # this should not be shortened but the above arg spec should:
            fake.set_bits("99999999999999999999999999999999999999999999999999999999")
        except AssertionError, exc:
            eq_(str(exc),
            "fake:widget.set_bits('123456789101112131415161718192021222324252627...') "
            "was called unexpectedly with args "
            "('99999999999999999999999999999999999999999999999999999999')")
        else:
            raise RuntimeError("expected AssertionError")
    
    def test_kwarg_diffs_are_not_shortened(self):
        fake = Fake("widget").provides("set_bits").with_args(
            newbits="12345678910111213141516171819202122232425262728293031"
        )
        try:
            # this should not be shortened but the above arg spec should:
            fake.set_bits(newbits="99999999999999999999999999999999999999999999999999999999")
        except AssertionError, exc:
            eq_(str(exc),
            "fake:widget.set_bits(newbits='123456789101112131415161718192021222324252627...') "
            "was called unexpectedly with args "
            "(newbits='99999999999999999999999999999999999999999999999999999999')")
        else:
            raise RuntimeError("expected AssertionError")

class TestReturnsFake(unittest.TestCase):
    
    def test_returns_fake_has_name(self):
        f = Fake().provides("get_widget").returns_fake()
        eq_(f._name, "get_widget")

class TestArguments(unittest.TestCase):
    
    def setUp(self):
        self.fake = fudge.Fake()
    
    def tearDown(self):
        fudge.clear_expectations()
    
    @raises(AssertionError)
    def test_wrong_args(self):
        exp = self.fake.expects('theCall').with_args(
                        1, 
                        "ho ho ho ho ho ho ho, it's Santa", 
                        {'ditto':False})
        exp.theCall()
        
    @raises(AssertionError)
    def test_wrong_kwargs(self):
        exp = self.fake.expects('other').with_args(one="twozy", items=[1,2,3,4])
        exp.other(nice="NOT NICE")
        
    @raises(AssertionError)
    def test_arg_count(self):
        exp = self.fake.expects('one').with_arg_count(3)
        exp.one('no', 'maybe')
        
    @raises(AssertionError)
    def test_kwarg_count(self):
        exp = self.fake.expects('__init__').with_kwarg_count(2)
        exp(maybe="yes, maybe")
    
    @raises(FakeDeclarationError)
    def test_with_args_requires_a_method(self):
        self.fake.with_args('something')
    
    @raises(AssertionError)
    def test_with_args_can_operate_on_provision(self):
        self.fake.provides("not_expected").with_args('something')
        self.fake.not_expected() # should still raise arg error
    
    @raises(AssertionError)
    def test_with_args_checks_args(self):
        self.fake.expects('count').with_args('one', two='two')
        self.fake.count(two='two')
        
    @raises(AssertionError)
    def test_with_args_checks_kwargs(self):
        self.fake.expects('count').with_args('one', two='two')
        self.fake.count('one')
    
    @raises(AssertionError)
    def test_too_many_args(self):
        db = Fake("db").expects("execute").with_args(bind={'one':1})
        db.execute("select foozilate()", bind={'one':1}) # unexpected statement arg
    
    def test_zero_keywords_ok(self):
        mail = fudge.Fake('mail').expects('send').with_arg_count(3) 
        mail.send("you", "me", "hi") # no kw args should be ok
    
    def test_zero_args_ok(self):
        mail = fudge.Fake('mail').expects('send').with_kwarg_count(3) 
        mail.send(to="you", from_="me", msg="hi") # no args should be ok

class TestArgEquality(unittest.TestCase):
    
    def setUp(self):
        self.fake = Fake("foo")
        self.call = Call(self.fake)
    # 
    # def test_complex_match_yields_no_reason(self):
    #     actual = ('one','two','three')
    #     expected = ('one','two','three')
    #     eq_(self.call._args_are_equal(actual, expected), (True, ""))

class TestKeywordArgEquality(unittest.TestCase):
    
    def setUp(self):
        self.fake = Fake("foo")
        self.call = Call(self.fake)
    
    def test_complex_match_yields_no_reason(self):
        actual = {'num':1, 'two':2, 'three':3}
        expected = {'num':1, 'two':2, 'three':3}
        eq_(self.call._keywords_are_equal(actual, expected), (True, ""))
    
    def test_simple_mismatch_yields_no_reason(self):
        actual = {'num':1}
        expected = {'num':2}
        eq_(self.call._keywords_are_equal(actual, expected), (False, ""))
    
    def test_simple_match_yields_no_reason(self):
        actual = {'num':1}
        expected = {'num':1}
        eq_(self.call._keywords_are_equal(actual, expected), (True, ""))
    
    def test_actual_kw_extra_key(self):
        actual = {'one':1, 'two':2}
        expected = {'one':1}
        eq_(self.call._keywords_are_equal(actual, expected), 
            (False, "keyword 'two' was not expected"))
    
    def test_actual_kw_value_inequal(self):
        actual = {'one':1, 'two':2}
        expected = {'one':1, 'two':3}
        eq_(self.call._keywords_are_equal(actual, expected), 
            (False, "two=2 != two=3"))
    
    def test_expected_kw_extra_key(self):
        actual = {'one':1}
        expected = {'one':1, 'two':2}
        eq_(self.call._keywords_are_equal(actual, expected), 
            (False, "these keywords never showed up: 'two'"))
    
    def test_expected_kw_value_inequal(self):
        actual = {'one':1, 'two':'not two'}
        expected = {'one':1, 'two':2}
        eq_(self.call._keywords_are_equal(actual, expected), 
            (False, "two='not two' != two=2"))

class TestCall(unittest.TestCase):
    
    def setUp(self):
        self.fake = fudge.Fake('SMTP')
    
{'fraction_non_alphanumeric': 0.12740227480716435, 'fraction_numerical': 0.0728199764675121, 'mean_word_length': 2.764517716535433, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 8, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7556397', 'n_tokens_mistral': 5764, 'n_tokens_neox': 4931, 'n_words': 806}