Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
// This file is part of Moodle - http://moodle.org/
3
//
4
// Moodle is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8
//
9
// Moodle is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU General Public License
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
 
17
namespace tool_generator\local\testscenario;
18
 
19
/**
20
 * Tests for parsedfeature class.
21
 *
22
 * @package tool_generator
23
 * @copyright 2023 Ferran Recio <ferran@moodel.com>
24
 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25
 * @coversDefaultClass \tool_generator\local\testscenario\parsedfeature
26
 */
27
class parsedfeature_test extends \advanced_testcase {
28
    /**
29
     * Get a parsed feature from a content.
30
     * @param string $content the feature content.
31
     * @return parsedfeature the parsed feature.
32
     */
33
    private function get_feature_from_content(string $content): parsedfeature {
34
        $runner = new runner();
35
        $runner->init();
36
        return $runner->parse_feature($content);
37
    }
38
 
39
    /**
40
     * Test for parse_feature.
41
     * @covers ::get_general_error
42
     * @covers ::add_scenario
43
     * @covers ::add_step
44
     */
45
    public function test_general_error(): void {
46
        $nosteps = get_string('testscenario_nosteps', 'tool_generator');
47
        $invalidfile = get_string('testscenario_invalidfile', 'tool_generator');
48
 
49
        $parsedfeature = new parsedfeature();
50
        $this->assertEquals($nosteps, $parsedfeature->get_general_error());
51
 
52
        $parsedfeature->add_scenario('Scenario', 'Test scenario');
53
        $this->assertEquals($nosteps, $parsedfeature->get_general_error());
54
 
55
        // Add some valid step.
56
        $extrafeature = $this->get_feature_from_content('Feature: Test feature
57
            Scenario: Create users
58
                Given the following "users" exist:
59
                    | username | firstname  | lastname | email              |
60
                    | teacher1 | Teacher    | Test1    | sample@example.com |
61
        ');
62
        $step = $extrafeature->get_all_steps()[0];
63
        $parsedfeature->add_step($step);
64
        $this->assertEquals('', $parsedfeature->get_general_error());
65
 
66
        // Only generator methods are allowed.
67
        $extrafeature = $this->get_feature_from_content('Feature: Test feature
68
            Scenario: Not generator
69
                Given I am in a course
70
        ');
71
        $step = $extrafeature->get_all_steps()[0];
72
        $parsedfeature->add_step($step);
73
        $this->assertEquals($invalidfile, $parsedfeature->get_general_error());
74
    }
75
 
76
    /**
77
     * Test for parse_feature.
78
     * @covers ::is_valid
79
     * @covers ::add_scenario
80
     * @covers ::add_step
81
     */
82
    public function test_is_valid(): void {
83
        $parsedfeature = new parsedfeature();
84
        $this->assertFalse($parsedfeature->is_valid());
85
 
86
        $parsedfeature->add_scenario('Scenario', 'Test scenario');
87
        $this->assertFalse($parsedfeature->is_valid());
88
 
89
        // Add some valid step.
90
        $extrafeature = $this->get_feature_from_content('Feature: Test feature
91
            Scenario: Create users
92
                Given the following "users" exist:
93
                    | username | firstname  | lastname | email              |
94
                    | teacher1 | Teacher    | Test1    | sample@example.com |
95
        ');
96
        $step = $extrafeature->get_all_steps()[0];
97
        $parsedfeature->add_step($step);
98
        $this->assertTrue($parsedfeature->is_valid());
99
 
100
        // Only generator methods are allowed.
101
        $extrafeature = $this->get_feature_from_content('Feature: Test feature
102
            Scenario: Not generator
103
                Given I am in a course
104
        ');
105
        $step = $extrafeature->get_all_steps()[0];
106
        $parsedfeature->add_step($step);
107
        $this->assertFalse($parsedfeature->is_valid());
108
    }
109
 
110
    /**
111
     * Test for ading steps into scenarios.
112
     * @covers ::add_step
113
     * @covers ::add_scenario
114
     * @covers ::get_all_steps
115
     * @covers ::get_scenarios
116
     */
117
    public function test_add_step(): void {
118
        $parsedfeature = new parsedfeature();
119
        $this->assertEquals(0, count($parsedfeature->get_all_steps()));
120
 
121
        $extrafeature = $this->get_feature_from_content('Feature: Test feature
122
            Scenario: Create users
123
                Given the following "users" exist:
124
                    | username | firstname  | lastname | email              |
125
                    | teacher1 | Teacher    | Test1    | sample@example.com |
126
        ');
127
        $step = $extrafeature->get_all_steps()[0];
128
        $parsedfeature->add_step($step);
129
        $this->assertEquals(1, count($parsedfeature->get_all_steps()));
130
 
131
        // Should create a default scenario.
132
        $scenarios = $parsedfeature->get_scenarios();
133
        $this->assertEquals(1, count($scenarios));
134
        $this->assertEquals('scenario', $scenarios[0]->type);
135
        $this->assertEquals('', $scenarios[0]->name);
136
        $this->assertEquals(1, count($scenarios[0]->steps));
137
        $this->assertEquals($step, $scenarios[0]->steps[0]);
138
        $this->assertEquals('', $scenarios[0]->error);
139
 
140
        // Add a second step.
141
        $extrafeature = $this->get_feature_from_content('Feature: Test feature
142
            Scenario: Not generator
143
                Given I am in a course
144
        ');
145
        $step2 = $extrafeature->get_all_steps()[0];
146
        $parsedfeature->add_step($step2);
147
        $scenarios = $parsedfeature->get_scenarios();
148
        $this->assertEquals(1, count($scenarios));
149
        $this->assertEquals('scenario', $scenarios[0]->type);
150
        $this->assertEquals('', $scenarios[0]->name);
151
        $this->assertEquals(2, count($scenarios[0]->steps));
152
        $this->assertEquals($step, $scenarios[0]->steps[0]);
153
        $this->assertEquals($step2, $scenarios[0]->steps[1]);
154
        $this->assertEquals('', $scenarios[0]->error);
155
 
156
        // Create a new scenario.
157
        $parsedfeature->add_scenario('scenario', 'Test scenario 2');
158
        $parsedfeature->add_step($step2);
159
        $scenarios = $parsedfeature->get_scenarios();
160
        $this->assertEquals(2, count($scenarios));
161
        // Scenario 1.
162
        $this->assertEquals('scenario', $scenarios[0]->type);
163
        $this->assertEquals('', $scenarios[0]->name);
164
        $this->assertEquals(2, count($scenarios[0]->steps));
165
        $this->assertEquals($step, $scenarios[0]->steps[0]);
166
        $this->assertEquals($step2, $scenarios[0]->steps[1]);
167
        $this->assertEquals('', $scenarios[0]->error);
168
        // Scenario 2.
169
        $this->assertEquals('scenario', $scenarios[1]->type);
170
        $this->assertEquals('Test scenario 2', $scenarios[1]->name);
171
        $this->assertEquals(1, count($scenarios[1]->steps));
172
        $this->assertEquals($step2, $scenarios[1]->steps[0]);
173
        $this->assertEquals('', $scenarios[1]->error);
174
    }
175
 
176
    /**
177
     * Test for ading errors into scenarios.
178
     * @covers ::add_error
179
     * @covers ::add_scenario
180
     * @covers ::add_step
181
     * @covers ::get_scenarios
182
     */
183
    public function test_add_error(): void {
184
        $parsedfeature = new parsedfeature();
185
 
186
        // Add some valid step.
187
        $extrafeature = $this->get_feature_from_content('Feature: Test feature
188
            Scenario: Create users
189
                Given the following "users" exist:
190
                    | username | firstname  | lastname | email              |
191
                    | teacher1 | Teacher    | Test1    | sample@example.com |
192
        ');
193
        $step = $extrafeature->get_all_steps()[0];
194
 
195
        $parsedfeature->add_scenario('scenario', 'Test scenario 1');
196
        $parsedfeature->add_step($step);
197
        $parsedfeature->add_error('Error message');
198
        $parsedfeature->add_scenario('scenario', 'Test scenario 2');
199
        $parsedfeature->add_step($step);
200
 
201
        $scenarios = $parsedfeature->get_scenarios();
202
        $this->assertEquals(2, count($scenarios));
203
        // Scenario 1.
204
        $this->assertEquals('scenario', $scenarios[0]->type);
205
        $this->assertEquals('Test scenario 1', $scenarios[0]->name);
206
        $this->assertEquals(1, count($scenarios[0]->steps));
207
        $this->assertEquals($step, $scenarios[0]->steps[0]);
208
        $this->assertEquals('Error message', $scenarios[0]->error);
209
        // Scenario 2.
210
        $this->assertEquals('scenario', $scenarios[1]->type);
211
        $this->assertEquals('Test scenario 2', $scenarios[1]->name);
212
        $this->assertEquals(1, count($scenarios[1]->steps));
213
        $this->assertEquals($step, $scenarios[1]->steps[0]);
214
        $this->assertEquals('', $scenarios[1]->error);
215
    }
216
}