summaryrefslogtreecommitdiff
path: root/yarnlib/scenario_step_connector_tests.py
blob: e93d904fb8cd705c6a01f0642289fe8cc23532e5 (plain)
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
# Copyright 2014  Codethink Limited
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# =*= License: GPL-3+ =*=


import unittest

import yarnlib


class ImplementsMatchesTests(unittest.TestCase):

    def test_match(self):
        i = yarnlib.Implementation('THEN', r'foo', 'echo foo\n')
        step = yarnlib.ScenarioStep('THEN', 'foo')
        self.assertTrue(yarnlib.implements_matches_step(i, step))

    def test_no_match(self):
        i = yarnlib.Implementation('THEN', r'foo', 'echo foo\n')
        step = yarnlib.ScenarioStep('THEN', 'bar')
        self.assertIs(yarnlib.implements_matches_step(i, step), None)

    def test_not_match_all(self):
        i = yarnlib.Implementation('THEN', r'foo', 'echo foo\n')
        step = yarnlib.ScenarioStep('THEN', 'foo bar')
        self.assertIs(yarnlib.implements_matches_step(i, step), None)


class ScenarioStepConnectorTests(unittest.TestCase):

    def setUp(self):
        pass

    def test_all_implemented(self):
        impl = yarnlib.Implementation('THEN', r'foo', 'echo foo\n')
        scenario = yarnlib.Scenario('foo')
        scenario.steps = [
            yarnlib.ScenarioStep('THEN', 'foo')
        ]
        ssc = yarnlib.ScenarioStepConnector([impl])
        scenarios = ssc.connect_implementations([scenario])
        self.assertIs(scenarios[0].steps[0].implementation, impl)

    def test_multiple_impls(self):
        ssc = yarnlib.ScenarioStepConnector([
            yarnlib.Implementation('THEN', r'foo \S+', 'echo foo\n'),
            yarnlib.Implementation('THEN', r'\S+ bar', 'echo bar\n'),
        ])
        scenario = yarnlib.Scenario('foo bar')
        scenario.steps = [
            yarnlib.ScenarioStep('THEN', 'foo bar')
        ]
        with self.assertRaises(yarnlib.StepMultipleImplementationsError):
            scenarios = ssc.connect_implementations([scenario])

    def test_noimpl_fail(self):
        ssc = yarnlib.ScenarioStepConnector([
            yarnlib.Implementation('THEN', r'foo', 'echo foo\n'),
        ])
        scenarios = []
        for scenario_name, step_text in [('foo', 'foo'), ('bar', 'bar')]:
            scenario = yarnlib.Scenario(scenario_name)
            scenario.steps = [
                yarnlib.ScenarioStep('THEN', step_text),
            ]
            scenarios.append(scenario)
        with self.assertRaises(yarnlib.StepNotImplementedError):
            scenarios = ssc.connect_implementations(scenarios)

    def test_noimpl_ignore(self):
        ssc = yarnlib.ScenarioStepConnector([
            yarnlib.Implementation('THEN', r'foo', 'echo foo\n'),
        ], lambda *args: True)
        all_scenarios = []
        for scenario_name, step_text in [('foo', 'foo'), ('bar', 'bar')]:
            scenario = yarnlib.Scenario(scenario_name)
            scenario.steps = [
                yarnlib.ScenarioStep('THEN', step_text),
            ]
            all_scenarios.append(scenario)
        implemented_scenarios = ssc.connect_implementations(all_scenarios)
        self.assertTrue(implemented_scenarios)