#!/usr/bin/env python
#-*- coding: utf-8 -*-

#This software is distributed under the Creative Commons license (CC0) version 1.0. A copy of this license should have been distributed with this software.
#The license can also be read online: <https://creativecommons.org/publicdomain/zero/1.0/>. If this online license differs from the license provided with this software, the license provided with this software should be applied.

"""
Tests the behaviour of the validated dictionary.
"""

import configurationtype.validated_dictionary #The module we're testing.
import luna.tests #For parametrised tests.

class TestValidatedDictionary(luna.tests.TestCase):
	"""
	Collection of tests for the ``ValidatedDictionary``.
	"""

	@classmethod
	def setUp(self):
		"""
		Creates a fixture dictionary to test on.
		"""
		self.empty = configurationtype.validated_dictionary.ValidatedDictionary()

	def test_add_no_validation(self):
		"""
		Tests the simple case where the value is not validated.
		"""
		self.empty.add("apples", 4)
		#Test setting the item to a few various sorts of values to make sure it isn't validated.
		self.empty["apples"] = 4
		self.empty["apples"] = "3"
		self.empty["apples"] = None
		self.empty["apples"] = lambda x: x ** 2

	def test_add_validate_positive(self):
		"""
		Tests adding an item with a simple validation function.
		"""
		self.empty.add("pears", 4, lambda n: n > 0)
		#Test setting the item to a few good values.
		self.empty["pears"] = 2
		self.empty["pears"] = 1
		#Test setting the item to a few bad values.
		with self.assertRaises(ValueError):
			self.empty["pears"] = 0
		with self.assertRaises(ValueError):
			self.empty["pears"] = -5
#!/usr/bin/env python
#-*- coding: utf-8 -*-

#This software is distributed under the Creative Commons license (CC0) version 1.0. A copy of this license should have been distributed with this software.
#The license can also be read online: <https://creativecommons.org/publicdomain/zero/1.0/>. If this online license differs from the license provided with this software, the license provided with this software should be applied.

"""
Tests the behaviour of the validated dictionary.
"""

import configurationtype.validated_dictionary #The module we're testing.
import luna.tests #For parametrised tests.

class TestValidatedDictionary(luna.tests.TestCase):
	"""
	Collection of tests for the ``ValidatedDictionary``.
	"""

	@classmethod
	def setUp(self):
		"""
		Creates a fixture dictionary to test on.
		"""
		self.empty = configurationtype.validated_dictionary.ValidatedDictionary()

	def test_add_no_validation(self):
		"""
		Tests the simple case where the value is not validated.
		"""
		self.empty.add("apples", 4)
		#Test setting the item to a few various sorts of values to make sure it isn't validated.
		self.empty["apples"] = 4
		self.empty["apples"] = "3"
		self.empty["apples"] = None
		self.empty["apples"] = lambda x: x ** 2

	def test_add_not_a_predicate(self):
		"""
		Tests what happens when you provide something that is not a predicate.
		"""
		with self.assertRaises(ValueError):
			self.empty.add("oranges", 2, lambda x, y: x * y)

	def test_add_validate_positive(self):
		"""
		Tests adding an item with a simple validation function.
		"""
		self.empty.add("pears", 4, lambda n: n > 0)
		#Test setting the item to a few good values.
		self.empty["pears"] = 2
		self.empty["pears"] = 1
		#Test setting the item to a few bad values.
		with self.assertRaises(ValueError):
			self.empty["pears"] = 0
		with self.assertRaises(ValueError):
			self.empty["pears"] = -5
Add test for adding items with non-predicate functions