def test_called_once(self): calls = 0 def get_value(): nonlocal calls calls += 1 return 2013 o = tk.set_gfyear(get_value) self.assertEqual(calls, 0) with o: self.assertEqual(calls, 1) self.assertEqual(tk.get_gfyear(), 2013) self.assertEqual(calls, 1) self.assertEqual(tk.get_gfyear(), 2013) self.assertEqual(calls, 1)
def test_lambda(self): with tk.set_gfyear(lambda: 2013): self.assertEqual(tk.get_gfyear(), 2013)
def test_invalid_gfyear(self): with self.assertRaisesRegex(ValueError, "'12345' is not a valid gfyear"): tk.get_gfyear(12345)
def test_notset(self): with self.assertRaisesRegex( ValueError, "No context gfyear set. Use the gfyear argument or " "set_gfyear."): tk.get_gfyear()
def test_reentrant(self): with tk.set_gfyear(2011): self.assertEqual(tk.get_gfyear(), 2011) with tk.set_gfyear(2012): self.assertEqual(tk.get_gfyear(), 2012) self.assertEqual(tk.get_gfyear(), 2011)
def test_context_manager(self): with tk.set_gfyear(2012): self.assertEqual(tk.get_gfyear(), 2012)