This file is indexed.

/usr/lib/python2.7/dist-packages/automat/_test/test_methodical.py is in python-automat 0.6.0-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
"""
Tests for the public interface of Automat.
"""

from functools import reduce
from unittest import TestCase

from .. import MethodicalMachine, NoTransition
from .. import _methodical

class MethodicalTests(TestCase):
    """
    Tests for L{MethodicalMachine}.
    """

    def test_oneTransition(self):
        """
        L{MethodicalMachine} provides a way for you to declare a state machine
        with inputs, outputs, and states as methods.  When you have declared an
        input, an output, and a state, calling the input method in that state
        will produce the specified output.
        """

        class Machination(object):
            machine = MethodicalMachine()
            @machine.input()
            def anInput(self):
                "an input"

            @machine.output()
            def anOutput(self):
                "an output"
                return "an-output-value"

            @machine.output()
            def anotherOutput(self):
                "another output"
                return "another-output-value"

            @machine.state(initial=True)
            def anState(self):
                "a state"

            @machine.state()
            def anotherState(self):
                "another state"

            anState.upon(anInput, enter=anotherState, outputs=[anOutput])
            anotherState.upon(anInput, enter=anotherState,
                              outputs=[anotherOutput])

        m = Machination()
        self.assertEqual(m.anInput(), ["an-output-value"])
        self.assertEqual(m.anInput(), ["another-output-value"])


    def test_machineItselfIsPrivate(self):
        """
        L{MethodicalMachine} is an implementation detail.  If you attempt to
        access it on an instance of your class, you will get an exception.
        However, since tools may need to access it for the purposes of, for
        example, visualization, you may access it on the class itself.
        """
        expectedMachine = MethodicalMachine()
        class Machination(object):
            machine = expectedMachine
        machination = Machination()
        with self.assertRaises(AttributeError) as cm:
            machination.machine
        self.assertIn("MethodicalMachine is an implementation detail",
                      str(cm.exception))
        self.assertIs(Machination.machine, expectedMachine)


    def test_outputsArePrivate(self):
        """
        One of the benefits of using a state machine is that your output method
        implementations don't need to take invalid state transitions into
        account - the methods simply won't be called.  This property would be
        broken if client code called output methods directly, so output methods
        are not directly visible under their names.
        """
        class Machination(object):
            machine = MethodicalMachine()
            counter = 0
            @machine.input()
            def anInput(self):
                "an input"
            @machine.output()
            def anOutput(self):
                self.counter += 1
            @machine.state(initial=True)
            def state(self):
                "a machine state"
            state.upon(anInput, enter=state, outputs=[anOutput])
        mach1 = Machination()
        mach1.anInput()
        self.assertEqual(mach1.counter, 1)
        mach2 = Machination()
        with self.assertRaises(AttributeError) as cm:
            mach2.anOutput
        self.assertEqual(mach2.counter, 0)

        self.assertIn(
            "Machination.anOutput is a state-machine output method; to "
            "produce this output, call an input method instead.",
            str(cm.exception)
        )


    def test_multipleMachines(self):
        """
        Two machines may co-exist happily on the same instance; they don't
        interfere with each other.
        """
        class MultiMach(object):
            a = MethodicalMachine()
            b = MethodicalMachine()

            @a.input()
            def inputA(self):
                "input A"
            @b.input()
            def inputB(self):
                "input B"
            @a.state(initial=True)
            def initialA(self):
                "initial A"
            @b.state(initial=True)
            def initialB(self):
                "initial B"
            @a.output()
            def outputA(self):
                return "A"
            @b.output()
            def outputB(self):
                return "B"
            initialA.upon(inputA, initialA, [outputA])
            initialB.upon(inputB, initialB, [outputB])

        mm = MultiMach()
        self.assertEqual(mm.inputA(), ["A"])
        self.assertEqual(mm.inputB(), ["B"])


    def test_collectOutputs(self):
        """
        Outputs can be combined with the "collector" argument to "upon".
        """
        import operator
        class Machine(object):
            m = MethodicalMachine()
            @m.input()
            def input(self):
                "an input"
            @m.output()
            def outputA(self):
                return "A"
            @m.output()
            def outputB(self):
                return "B"
            @m.state(initial=True)
            def state(self):
                "a state"
            state.upon(input, state, [outputA, outputB],
                       collector=lambda x: reduce(operator.add, x))
        m = Machine()
        self.assertEqual(m.input(), "AB")


    def test_methodName(self):
        """
        Input methods preserve their declared names.
        """
        class Mech(object):
            m = MethodicalMachine()
            @m.input()
            def declaredInputName(self):
                "an input"
            @m.state(initial=True)
            def aState(self):
                "state"
        m = Mech()
        with self.assertRaises(TypeError) as cm:
            m.declaredInputName("too", "many", "arguments")
        self.assertIn("declaredInputName", str(cm.exception))


    def test_inputWithArguments(self):
        """
        If an input takes an argument, it will pass that along to its output.
        """
        class Mechanism(object):
            m = MethodicalMachine()
            @m.input()
            def input(self, x, y=1):
                "an input"
            @m.state(initial=True)
            def state(self):
                "a state"
            @m.output()
            def output(self, x, y=1):
                self._x = x
                return x + y
            state.upon(input, state, [output])

        m = Mechanism()
        self.assertEqual(m.input(3), [4])
        self.assertEqual(m._x, 3)


    def test_inputFunctionsMustBeEmpty(self):
        """
        The wrapped input function must have an empty body.
        """
        # input functions are executed to assert that the signature matches,
        # but their body must be empty

        _methodical._empty() # chase coverage
        _methodical._docstring()

        class Mechanism(object):
            m = MethodicalMachine()
            with self.assertRaises(ValueError) as cm:
                @m.input()
                def input(self):
                    "an input"
                    list() # pragma: no cover
            self.assertEqual(str(cm.exception), "function body must be empty")

        # all three of these cases should be valid. Functions/methods with
        # docstrings produce slightly different bytecode than ones without.

        class MechanismWithDocstring(object):
            m = MethodicalMachine()
            @m.input()
            def input(self):
                "an input"
            @m.state(initial=True)
            def start(self):
                "starting state"
            start.upon(input, enter=start, outputs=[])
        MechanismWithDocstring().input()

        class MechanismWithPass(object):
            m = MethodicalMachine()
            @m.input()
            def input(self):
                pass
            @m.state(initial=True)
            def start(self):
                "starting state"
            start.upon(input, enter=start, outputs=[])
        MechanismWithPass().input()

        class MechanismWithDocstringAndPass(object):
            m = MethodicalMachine()
            @m.input()
            def input(self):
                "an input"
                pass
            @m.state(initial=True)
            def start(self):
                "starting state"
            start.upon(input, enter=start, outputs=[])
        MechanismWithDocstringAndPass().input()

        class MechanismReturnsNone(object):
            m = MethodicalMachine()
            @m.input()
            def input(self):
                return None
            @m.state(initial=True)
            def start(self):
                "starting state"
            start.upon(input, enter=start, outputs=[])
        MechanismReturnsNone().input()

        class MechanismWithDocstringAndReturnsNone(object):
            m = MethodicalMachine()
            @m.input()
            def input(self):
                "an input"
                return None
            @m.state(initial=True)
            def start(self):
                "starting state"
            start.upon(input, enter=start, outputs=[])
        MechanismWithDocstringAndReturnsNone().input()



    def test_inputOutputMismatch(self):
        """
        All the argument lists of the outputs for a given input must match; if
        one does not the call to C{upon} will raise a C{TypeError}.
        """
        class Mechanism(object):
            m = MethodicalMachine()
            @m.input()
            def nameOfInput(self, a):
                "an input"
            @m.output()
            def outputThatMatches(self, a):
                "an output that matches"
            @m.output()
            def outputThatDoesntMatch(self, b):
                "an output that doesn't match"
            @m.state()
            def state(self):
                "a state"
            with self.assertRaises(TypeError) as cm:
                state.upon(nameOfInput, state, [outputThatMatches,
                                                outputThatDoesntMatch])
            self.assertIn("nameOfInput", str(cm.exception))
            self.assertIn("outputThatDoesntMatch", str(cm.exception))


    def test_multipleInitialStatesFailure(self):
        """
        A L{MethodicalMachine} can only have one initial state.
        """

        class WillFail(object):
            m = MethodicalMachine()

            @m.state(initial=True)
            def firstInitialState(self):
                "The first initial state -- this is OK."

            with self.assertRaises(ValueError):
                @m.state(initial=True)
                def secondInitialState(self):
                    "The second initial state -- results in a ValueError."


    def test_multipleTransitionsFailure(self):
        """
        A L{MethodicalMachine} can only have one transition per start/event
        pair.
        """

        class WillFail(object):
            m = MethodicalMachine()

            @m.state(initial=True)
            def start(self):
                "We start here."
            @m.state()
            def end(self):
                "Rainbows end."

            @m.input()
            def event(self):
                "An event."
            start.upon(event, enter=end, outputs=[])
            with self.assertRaises(ValueError):
                start.upon(event, enter=end, outputs=[])

    def test_badTransitionForCurrentState(self):
        """
        Calling any input method that lacks a transition for the machine's
        current state raises an informative L{NoTransition}.
        """

        class OnlyOnePath(object):
            m = MethodicalMachine()
            @m.state(initial=True)
            def start(self):
                "Start state."
            @m.state()
            def end(self):
                "End state."
            @m.input()
            def advance(self):
                "Move from start to end."
            @m.input()
            def deadEnd(self):
                "A transition from nowhere to nowhere."
            start.upon(advance, end, [])

        machine = OnlyOnePath()
        with self.assertRaises(NoTransition) as cm:
            machine.deadEnd()
        self.assertIn("deadEnd", str(cm.exception))
        self.assertIn("start", str(cm.exception))
        machine.advance()
        with self.assertRaises(NoTransition) as cm:
            machine.deadEnd()
        self.assertIn("deadEnd", str(cm.exception))
        self.assertIn("end", str(cm.exception))


    def test_saveState(self):
        """
        L{MethodicalMachine.serializer} is a decorator that modifies its
        decoratee's signature to take a "state" object as its first argument,
        which is the "serialized" argument to the L{MethodicalMachine.state}
        decorator.
        """

        class Mechanism(object):
            m = MethodicalMachine()
            def __init__(self):
                self.value = 1
            @m.state(serialized="first-state", initial=True)
            def first(self):
                "First state."
            @m.state(serialized="second-state")
            def second(self):
                "Second state."
            @m.serializer()
            def save(self, state):
                return {
                    'machine-state': state,
                    'some-value': self.value,
                }

        self.assertEqual(
            Mechanism().save(),
            {
                "machine-state": "first-state",
                "some-value": 1,
            }
        )

    def test_restoreState(self):
        """
        L{MethodicalMachine.unserializer} decorates a function that becomes a
        machine-state unserializer; its return value is mapped to the
        C{serialized} parameter to C{state}, and the L{MethodicalMachine}
        associated with that instance's state is updated to that state.
        """

        class Mechanism(object):
            m = MethodicalMachine()
            def __init__(self):
                self.value = 1
                self.ranOutput = False
            @m.state(serialized="first-state", initial=True)
            def first(self):
                "First state."
            @m.state(serialized="second-state")
            def second(self):
                "Second state."
            @m.input()
            def input(self):
                "an input"
            @m.output()
            def output(self):
                self.value = 2
                self.ranOutput = True
                return 1
            @m.output()
            def output2(self):
                return 2
            first.upon(input, second, [output],
                       collector=lambda x: list(x)[0])
            second.upon(input, second, [output2],
                        collector=lambda x: list(x)[0])
            @m.serializer()
            def save(self, state):
                return {
                    'machine-state': state,
                    'some-value': self.value,
                }

            @m.unserializer()
            def _restore(self, blob):
                self.value = blob['some-value']
                return blob['machine-state']

            @classmethod
            def fromBlob(cls, blob):
                self = cls()
                self._restore(blob)
                return self

        m1 = Mechanism()
        m1.input()
        blob = m1.save()
        m2 = Mechanism.fromBlob(blob)
        self.assertEqual(m2.ranOutput, False)
        self.assertEqual(m2.input(), 2)
        self.assertEqual(
            m2.save(),
            {
                'machine-state': 'second-state',
                'some-value': 2,
            }
        )



# FIXME: error for wrong types on any call to _oneTransition
# FIXME: better public API for .upon; maybe a context manager?
# FIXME: when transitions are defined, validate that we can always get to
# terminal? do we care about this?
# FIXME: implementation (and use-case/example) for passing args from in to out

# FIXME: possibly these need some kind of support from core
# FIXME: wildcard state (in all states, when input X, emit Y and go to Z)
# FIXME: wildcard input (in state X, when any input, emit Y and go to Z)
# FIXME: combined wildcards (in any state for any input, emit Y go to Z)