Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1441 ariadna 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 core;
18
 
19
/**
20
 * Unit tests for partial() in moodlelib.php.
21
 *
22
 * @package   core
23
 * @category  test
24
 * @copyright 2024 Andrew Lyons <andrew@nicols.co.uk>
25
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26
 * @covers ::partial
27
 */
28
final class moodlelib_partial_test extends \advanced_testcase {
29
    /**
30
     * Test that arguments to partial can be passed as anticipated.
31
     *
32
     * @param callable $callable The callable to partially apply.
33
     * @param array $initialargs The initial arguments to pass to the callable.
34
     * @param array $calledargs The arguments to pass to the partially applied callable.
35
     * @param mixed $expected The expected return value.
36
     * @dataProvider partial_args_provider
37
     */
38
    public function test_partial_args(
39
        callable $callable,
40
        array $initialargs,
41
        array $calledargs,
42
        mixed $expected,
43
    ): void {
44
        $this->assertEquals($expected, partial($callable, ...$initialargs)(...$calledargs));
45
    }
46
 
47
    public function test_partial_args_intance_methods(): void {
48
        // Using named args on initial args only - instance method.
49
        $this->assertEquals(
50
            'foo/bar/baz/bum',
51
            partial(
52
                [$this, 'example_instance_method'],
53
                ...['foo' => 'foo']
54
            )(...['bar', 'baz', 'bum']),
55
        );
56
 
57
        // Using named args on called args only - instance method.
58
        $this->assertEquals(
59
            'foo/bar/baz/bum',
60
            partial(
61
                [$this, 'example_instance_method'],
62
                ...['foo' => 'foo']
63
            )(...['bar' => 'bar', 'baz' => 'baz', 'bum' => 'bum']),
64
        );
65
    }
66
 
67
    /**
68
     * An example static method as part of the testcase.
69
     *
70
     * @param string $foo The first argument.
71
     * @param string $bar The second argument.
72
     * @param string $baz The third argument.
73
     * @param string $bum The fourth argument.
74
     */
75
    public static function example_static_method(
76
        string $foo,
77
        string $bar,
78
        string $baz,
79
        string $bum
80
    ): string {
81
        return implode('/', [$foo, $bar, $baz, $bum]);
82
    }
83
 
84
    /**
85
     * An example method as part of the testcase.
86
     *
87
     * @param string $foo The first argument.
88
     * @param string $bar The second argument.
89
     * @param string $baz The third argument.
90
     * @param string $bum The fourth argument.
91
     */
92
    public function example_instance_method(
93
        string $foo,
94
        string $bar,
95
        string $baz,
96
        string $bum
97
    ): string {
98
        return implode('/', [$foo, $bar, $baz, $bum]);
99
    }
100
 
101
    /**
102
     * Data provider for test_partial_args.
103
     *
104
     * @return array
105
     */
106
    public static function partial_args_provider(): array {
107
        return [
108
            'Using positional args' => [
109
                'str_contains',
110
                ['foobar'],
111
                ['foo'],
112
                true,
113
            ],
114
            'Using positional args swapped' => [
115
                'str_contains',
116
                ['foo'],
117
                ['foobar'],
118
                false,
119
            ],
120
            'Using named args' => [
121
                'str_contains',
122
                ['needle' => 'foo'],
123
                ['haystack' => 'foobar'],
124
                true,
125
            ],
126
            'Using named args on callable args only' => [
127
                'str_contains',
128
                ['foobar'],
129
                ['needle' => 'foo'],
130
                true,
131
            ],
132
            'Using named args on initial args only - static method' => [
133
                [self::class, 'example_static_method'],
134
                ['foo' => 'foo'],
135
                ['bar', 'baz', 'bum'],
136
                'foo/bar/baz/bum',
137
            ],
138
            'Using named args on called args only - static method' => [
139
                [self::class, 'example_static_method'],
140
                ['foo'],
141
                ['bar' => 'bar', 'baz' => 'baz', 'bum' => 'bum'],
142
                'foo/bar/baz/bum',
143
            ],
144
        ];
145
    }
146
}