You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_core.py 2.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. from .._core import Automaton, NoTransition
  2. from unittest import TestCase
  3. class CoreTests(TestCase):
  4. """
  5. Tests for Automat's (currently private, implementation detail) core.
  6. """
  7. def test_NoTransition(self):
  8. """
  9. A L{NoTransition} exception describes the state and input symbol
  10. that caused it.
  11. """
  12. # NoTransition requires two arguments
  13. with self.assertRaises(TypeError):
  14. NoTransition()
  15. state = "current-state"
  16. symbol = "transitionless-symbol"
  17. noTransitionException = NoTransition(state=state, symbol=symbol)
  18. self.assertIs(noTransitionException.symbol, symbol)
  19. self.assertIn(state, str(noTransitionException))
  20. self.assertIn(symbol, str(noTransitionException))
  21. def test_noOutputForInput(self):
  22. """
  23. L{Automaton.outputForInput} raises L{NoTransition} if no
  24. transition for that input is defined.
  25. """
  26. a = Automaton()
  27. self.assertRaises(NoTransition, a.outputForInput,
  28. "no-state", "no-symbol")
  29. def test_oneTransition(self):
  30. """
  31. L{Automaton.addTransition} adds its input symbol to
  32. L{Automaton.inputAlphabet}, all its outputs to
  33. L{Automaton.outputAlphabet}, and causes L{Automaton.outputForInput} to
  34. start returning the new state and output symbols.
  35. """
  36. a = Automaton()
  37. a.addTransition("beginning", "begin", "ending", ["end"])
  38. self.assertEqual(a.inputAlphabet(), {"begin"})
  39. self.assertEqual(a.outputAlphabet(), {"end"})
  40. self.assertEqual(a.outputForInput("beginning", "begin"),
  41. ("ending", ["end"]))
  42. self.assertEqual(a.states(), {"beginning", "ending"})
  43. def test_oneTransition_nonIterableOutputs(self):
  44. """
  45. L{Automaton.addTransition} raises a TypeError when given outputs
  46. that aren't iterable and doesn't add any transitions.
  47. """
  48. a = Automaton()
  49. nonIterableOutputs = 1
  50. self.assertRaises(
  51. TypeError,
  52. a.addTransition,
  53. "fromState", "viaSymbol", "toState", nonIterableOutputs)
  54. self.assertFalse(a.inputAlphabet())
  55. self.assertFalse(a.outputAlphabet())
  56. self.assertFalse(a.states())
  57. self.assertFalse(a.allTransitions())
  58. def test_initialState(self):
  59. """
  60. L{Automaton.initialState} is a descriptor that sets the initial
  61. state if it's not yet set, and raises L{ValueError} if it is.
  62. """
  63. a = Automaton()
  64. a.initialState = "a state"
  65. self.assertEqual(a.initialState, "a state")
  66. with self.assertRaises(ValueError):
  67. a.initialState = "another state"
  68. # FIXME: addTransition for transition that's been added before