コード例 #1
0
	def test_supportsExtraKwargs(self):
		gotKwargs = {}
		def handler(a=None, b=2):
			gotKwargs["a"] = a
			gotKwargs["b"] = b
		extensionPoints.callWithSupportedKwargs(handler, a=1)
		self.assertEqual(gotKwargs, {"a": 1, "b": 2})
コード例 #2
0
	def test_positionalsPassedWhenSupportsNoKwargs(self):
		"""Test that positional arguments are passed untouched.
		"""
		gotArgs = []
		def handler(a, b):
			gotArgs.append(a)
			gotArgs.append(b)
		extensionPoints.callWithSupportedKwargs(handler, 1, 2)
		self.assertEqual(gotArgs, [1, 2])
コード例 #3
0
	def test_handlerTakesParamsWithoutDefaults_positionalArgsGiven_handlerReceivesArgs(self):
		"""Test that positional arguments are passed untouched.
		"""
		gotParams = {}
		def handler(a, b):
			gotParams['a'] = a
			gotParams['b'] = b
		extensionPoints.callWithSupportedKwargs(handler, 'a value', 'b value')
		self.assertEqual(gotParams, {'a': 'a value', 'b': 'b value'})
コード例 #4
0
	def test_unboundInstanceMethodHandler_exceptionRaised(self):
		"""Test to ensure that unhandled callable types (unbound instance methods) are caught and an exception is raised.
		"""
		class handlerClass():
			def handlerMethod(self):
				pass

		unboundInstanceMethod = handlerClass.handlerMethod
		with self.assertRaises(TypeError):
			extensionPoints.callWithSupportedKwargs(unboundInstanceMethod)
コード例 #5
0
	def test_positionalsPassedWhenSupportsAllKwargs(self):
		"""Test that positional arguments are passed untouched when the function has **kwargs,
		since **kwargs is a special case early return in the code.
		"""
		gotArgs = []
		def handler(a, b, **kwargs):
			gotArgs.append(a)
			gotArgs.append(b)
		extensionPoints.callWithSupportedKwargs(handler, 1, 2, c=3)
		self.assertEqual(gotArgs, [1, 2])
コード例 #6
0
	def test_handlerTakesNoParams_noArgsGiven_handlerIsCalled(self):
		"""Essentially another variation of `test_handlerParamsMatchKwargsGiven_valuePassedIn` test
		The handler function expects no params.
		callWithSupportedKwargs is given no arguments.
		Expectation: handler is still called.
		"""
		called = []
		def handler():
			called.append(True)
		extensionPoints.callWithSupportedKwargs(handler)
		self.assertEqual(called, [True])
コード例 #7
0
	def test_handlerTakesParamWithDefault_noKwargsGiven_handlerIsCalled(self):
		""" Tests that when there are default values for the handler param, then it is truly optional.
		The handler function takes a param with a default value set.
		callWithSupportedKwargs is not given any arguments
		Expectation: the handler is expected to be called.
		"""
		gotParams = {}
		def handler(a='default a value'):
			gotParams['a'] = a
		extensionPoints.callWithSupportedKwargs(handler)
		self.assertEqual(gotParams, {'a': 'default a value'})
コード例 #8
0
	def test_handlerTakesParamWithoutDefault_kwargsMatch_handlerIsCalled(self):
		""" Tests that when there are not default values for the handler param, then matching keyword arguments are given.
		The handler function takes a param with no default value set.
		callWithSupportedKwargs is given a keyword arg that match the expected param name.
		Expectation: the handler is expected to be called with the keyword argument value.
		"""
		gotParams = {}
		def handler(a):
			gotParams['a'] = a
		extensionPoints.callWithSupportedKwargs(handler, a='a value')
		self.assertEqual(gotParams, {'a': 'a value'})
コード例 #9
0
	def test_handlerTakesParamWithoutDefault_kwargsDoNotMatch_exceptionRaised(self):
		""" Tests that handlers that when a handler expects params which are not provided, then the function is not called.
		The handler function takes a param with no default value set.
		callWithSupportedKwargs is given keyword arguments that don't match the expected param names.
		Expectation: an exception is raised.
		"""
		gotParams = {}
		def handler(a):
			gotParams['a'] = a
		with self.assertRaises(TypeError):
			extensionPoints.callWithSupportedKwargs(handler, b='b value')
コード例 #10
0
	def test_handlerTakesParamWithDefault_kwargsGivenMatch_valuePassedIn(self):
		""" Basic test for callWithSupportedKwargs function.
		Handler expects a single parameter, with a default provided.
		callWithSupportedKwargs is called with a single keyword argument that matches the name of the handler param.
		Expectation: handler called with parameter value equal to the keyword args given to callWithSupportedKwargs.
		"""
		gotParams = {}
		def handler(a=None):
			gotParams['a'] = a
		extensionPoints.callWithSupportedKwargs(handler, a='a value')
		self.assertEqual(gotParams, {"a": 'a value'})
コード例 #11
0
	def test_handlerTakesTwoParamsWithoutDefaults_NotEnoughPositionalsGiven_exceptionRaised(self):
		""" Tests that handlers that when a handler expects params which are not provided, then the function is not called.
		The handler function takes a param with no default value set.
		callWithSupportedKwargs is given keyword arguments that don't match the expected param names.
		Expectation: an exception is raised.
		"""
		gotParams = {}
		def handler(a, b):
			gotParams['a'] = a
			gotParams['b'] = b
		with self.assertRaises(TypeError):
			extensionPoints.callWithSupportedKwargs(handler, "a value") # "b value" not provided
コード例 #12
0
	def test_handlerTakesNoParams_kwargsGiven_handlerIsCalled(self):
		""" Tests that if the kwargs given are surplus to requirements the handler is still called. This test ensures
		that the functionality to be able to extend extension points exists. New arguments can be added without breaking
		backwards compatibility.
		The handler function expects no params.
		callWithSupportedKwargs is given some keyword args.
		Expectation: The handler is expected to be called, no args given.
		"""
		called = []
		def handler():
			called.append(True)
		extensionPoints.callWithSupportedKwargs(handler, a='a value')
		self.assertEqual(called, [True])
コード例 #13
0
	def test_handlerTakesParamsWithoutDefaultsAndKwargs_positionalArgsAndKwargsGiven_handlerReceivesArgsAndKwargs(self):
		"""Test that positional arguments are passed untouched when the function has **kwargs,
		since **kwargs is a special case early return in the code.
		"""
		gotParams = {}
		gotKwargs = {}
		def handler(a, b, **kwargs):
			gotParams['a'] = a
			gotParams['b'] = b
			gotKwargs.update(kwargs)
		extensionPoints.callWithSupportedKwargs(handler, 'a value', b='b value', c='c value')
		self.assertEqual(gotParams, {'a': 'a value', 'b': 'b value'})
		self.assertEqual(gotKwargs, {'c': 'c value'})
コード例 #14
0
	def test_handlerTakesParamsWithDefaultAndKwargs_otherKwargsGiven_handlerGetsOtherKwargsAndDefaultValues(self):
		"""Test that extra keyword args is still passed in when params aren't provided.
		Handler has default values for params, and takes **kwargs.
		callWithSupportedKwargs is called only with non-matching keyword arguments.
		Expected: handler is called, non-matching keyword arguments are passed to handler
		"""
		gotParams = {}
		gotKwargs = {}

		def handler(a='a default', **kwargs):
			gotParams['a'] = a
			gotKwargs.update(kwargs)
		extensionPoints.callWithSupportedKwargs(handler, c='c value')
		self.assertEqual(gotParams, {'a': 'a default'})
		self.assertEqual(gotKwargs, {'c': 'c value'})
コード例 #15
0
	def test_instanceMethodHandlerTakesKwargs_givenKwargs(self):
		"""Test to ensure that a instance method handler gets the correct arguments, including implicit "self"
		Handler takes **kwargs.
		callWithSupportedKwargs given key word arguments.
		Handler should get all values.
		"""
		calledKwargs = {}

		class handlerClass():
			def handlerMethod(self, **kwargs):
				calledKwargs.update(kwargs)

		h = handlerClass()
		extensionPoints.callWithSupportedKwargs(h.handlerMethod, a='a value', b='b value')
		self.assertEqual(calledKwargs, {'a': 'a value', 'b': 'b value'})
コード例 #16
0
    def test_handlerTakesNoParams_kwargsGiven_handlerIsCalled(self):
        """ Tests that if the kwargs given are surplus to requirements the handler is still called. This test ensures
		that the functionality to be able to extend extension points exists. New arguments can be added without breaking
		backwards compatibility.
		The handler function expects no params.
		callWithSupportedKwargs is given some keyword args.
		Expectation: The handler is expected to be called, no args given.
		"""
        called = []

        def handler():
            called.append(True)

        extensionPoints.callWithSupportedKwargs(handler, a='a value')
        self.assertEqual(called, [True])
コード例 #17
0
	def test_instanceMethodHandlerTakesParams_givenMatchingNameKwarg(self):
		"""Test to ensure that a instance method handler gets the correct arguments, including implicit "self"
		Handler takes a parameter.
		callWithSupportedKwargs given a keyword arg with a matching name.
		Handler should get kwarg.
		"""
		calledKwargs = {}

		class handlerClass():
			def handlerMethod(self, a):
				calledKwargs['a'] = a

		h = handlerClass()
		extensionPoints.callWithSupportedKwargs(h.handlerMethod, a='a value')
		self.assertEqual(calledKwargs, {'a': 'a value'})
コード例 #18
0
    def test_handlerTakesTwoParamsWithoutDefaults_NotEnoughPositionalsGiven_exceptionRaised(
            self):
        """ Tests that handlers that when a handler expects params which are not provided, then the function is not called.
		The handler function takes a param with no default value set.
		callWithSupportedKwargs is given keyword arguments that don't match the expected param names.
		Expectation: an exception is raised.
		"""
        gotParams = {}

        def handler(a, b):
            gotParams['a'] = a
            gotParams['b'] = b

        with self.assertRaises(TypeError):
            extensionPoints.callWithSupportedKwargs(
                handler, "a value")  # "b value" not provided
コード例 #19
0
    def test_instanceMethodHandlerTakesParamsAndKwargs_givenPositional(self):
        """Test to ensure that a instanceMethod handler gets the correct arguments, including implicit "self"
		Handler takes parameter and key word arguments.
		callWithSupportedKwargs given a positional.
		Handler should get positional.
		"""
        calledKwargs = {}

        class handlerClass():
            def handlerMethod(self, a, **kwargs):
                calledKwargs['a'] = a
                calledKwargs.update(kwargs)

        h = handlerClass()
        extensionPoints.callWithSupportedKwargs(h.handlerMethod, 'a value')
        self.assertEqual(calledKwargs, {'a': 'a value'})
コード例 #20
0
	def test_instanceMethodHandlerTakesParamsAndKwargs_givenPositional(self):
		"""Test to ensure that a instanceMethod handler gets the correct arguments, including implicit "self"
		Handler takes parameter and key word arguments.
		callWithSupportedKwargs given a positional.
		Handler should get positional.
		"""
		calledKwargs = {}

		class handlerClass():
			def handlerMethod(self, a, **kwargs):
				calledKwargs['a'] = a
				calledKwargs.update(kwargs)

		h = handlerClass()
		extensionPoints.callWithSupportedKwargs(h.handlerMethod, 'a value')
		self.assertEqual(calledKwargs, {'a': 'a value'})
コード例 #21
0
    def test_instanceMethodHandlerTakesKwargs_givenKwargs(self):
        """Test to ensure that a instance method handler gets the correct arguments, including implicit "self"
		Handler takes **kwargs.
		callWithSupportedKwargs given key word arguments.
		Handler should get all values.
		"""
        calledKwargs = {}

        class handlerClass():
            def handlerMethod(self, **kwargs):
                calledKwargs.update(kwargs)

        h = handlerClass()
        extensionPoints.callWithSupportedKwargs(h.handlerMethod,
                                                a='a value',
                                                b='b value')
        self.assertEqual(calledKwargs, {'a': 'a value', 'b': 'b value'})
コード例 #22
0
    def test_handlerTakesParamsWithoutDefaultsAndKwargs_positionalArgsAndKwargsGiven_handlerReceivesArgsAndKwargs(
            self):
        """Test that positional arguments are passed untouched when the function has **kwargs,
		since **kwargs is a special case early return in the code.
		"""
        gotParams = {}
        gotKwargs = {}

        def handler(a, b, **kwargs):
            gotParams['a'] = a
            gotParams['b'] = b
            gotKwargs.update(kwargs)

        extensionPoints.callWithSupportedKwargs(handler,
                                                'a value',
                                                b='b value',
                                                c='c value')
        self.assertEqual(gotParams, {'a': 'a value', 'b': 'b value'})
        self.assertEqual(gotKwargs, {'c': 'c value'})
コード例 #23
0
ファイル: eventHandler.py プロジェクト: bramd/nvda
	def next(self):
		func, args = next(self._gen)
		try:
			return func(*args, **self.kwargs)
		except TypeError:
			log.warning("Could not execute function {func} defined in {module} module due to unsupported kwargs: {kwargs}".format(
				func=func.__name__,
				module=func.__module__ or "unknown",
				kwargs=self.kwargs
			), exc_info=True)
			return extensionPoints.callWithSupportedKwargs(func, *args, **self.kwargs)
コード例 #24
0
	def next(self):
		func, args = next(self._gen)
		try:
			return func(*args, **self.kwargs)
		except TypeError:
			log.warning("Could not execute function {func} defined in {module} module; kwargs: {kwargs}".format(
				func=func.__name__,
				module=func.__module__ or "unknown",
				kwargs=self.kwargs
			), exc_info=True)
			return extensionPoints.callWithSupportedKwargs(func, *args, **self.kwargs)
コード例 #25
0
	def test_handlerParamsChangeOrder_KwargsGiven_correctArgValuesReceived(self):
		""" Test that the order of params for handlers does not matter if keyword arguments are used with
		`callWithSupportedKwargs`
		Note: Positionals passed to `callWithSupportedKwargs` will be position dependent, thus handlers with differing order
		may be called with incorrect argument order, it is recommended to use keyword arguments when calling
		`callWithSupportedKwargs`
		"""
		calledKwargsAB = {}
		def handlerAB(a, b):
			calledKwargsAB.update({'a': a, 'b': b})

		calledKwargsBA = {}
		def handlerBA(b, a):
			calledKwargsBA.update({'a': a, 'b': b})

		extensionPoints.callWithSupportedKwargs(handlerAB, a='a-value', b='b-value')
		extensionPoints.callWithSupportedKwargs(handlerBA, a='a-value', b='b-value')

		expected = {'a': 'a-value', 'b': 'b-value'}
		self.assertEqual(calledKwargsAB, expected)
		self.assertEqual(calledKwargsBA, expected)
コード例 #26
0
	def test_supportsNoKwargs(self):
		called = []
		def handler():
			called.append(True)
		extensionPoints.callWithSupportedKwargs(handler, a=1)
		self.assertEqual(called, [True])
コード例 #27
0
	def test_supportsAllKwargs(self):
		gotKwargs = {}
		def handler(**kwargs):
			gotKwargs.update(kwargs)
		extensionPoints.callWithSupportedKwargs(handler, a=1)
		self.assertEqual(gotKwargs, {"a": 1})
コード例 #28
0
	def test_supportsLessKwargs(self):
		gotKwargs = {}
		def handler(a=None):
			gotKwargs["a"] = a
		extensionPoints.callWithSupportedKwargs(handler, a=1, b=2)
		self.assertEqual(gotKwargs, {"a": 1})
コード例 #29
0
	def test_handlerTakesOnlyKwargs_kwargsGiven_handlerReceivesKwargs(self):
		gotParams = {}
		def handler(**kwargs):
			gotParams.update(kwargs)
		extensionPoints.callWithSupportedKwargs(handler, a='a value')
		self.assertEqual(gotParams, {'a': 'a value'})