MediaWiki REL1_34
LuaEnvironmentComparisonTest.php
Go to the documentation of this file.
1<?php
2
9class Scribunto_LuaEnvironmentComparisonTest extends PHPUnit\Framework\TestCase {
10 use MediaWikiCoversValidator;
11 use PHPUnit4And6Compat;
12
13 public $sandboxOpts = [
14 'memoryLimit' => 50000000,
15 'cpuLimit' => 30,
16 'allowEnvFuncs' => true,
17 ];
18 public $standaloneOpts = [
19 'errorFile' => null,
20 'luaPath' => null,
21 'memoryLimit' => 50000000,
22 'cpuLimit' => 30,
23 'allowEnvFuncs' => true,
24 ];
25
26 protected $engines = [];
27
28 private function makeEngine( $class, $opts ) {
29 $parser = new Parser;
30 $options = new ParserOptions;
31 $options->setTemplateCallback( [ $this, 'templateCallback' ] );
32 $parser->startExternalParse( Title::newMainPage(), $options, Parser::OT_HTML, true );
33 $engine = new $class ( [ 'parser' => $parser ] + $opts );
34 $parser->scribunto_engine = $engine;
35 $engine->setTitle( $parser->getTitle() );
36 $engine->getInterpreter();
37 return $engine;
38 }
39
40 protected function setUp() {
41 parent::setUp();
42
43 try {
44 $this->engines['LuaSandbox'] = $this->makeEngine(
45 'Scribunto_LuaSandboxEngine', $this->sandboxOpts
46 );
48 $this->markTestSkipped( "LuaSandbox interpreter not available" );
49 return;
50 }
51
52 try {
53 $this->engines['LuaStandalone'] = $this->makeEngine(
54 'Scribunto_LuaStandaloneEngine', $this->standaloneOpts
55 );
57 $this->markTestSkipped( "LuaStandalone interpreter not available" );
58 return;
59 }
60 }
61
62 protected function tearDown() {
63 foreach ( $this->engines as $engine ) {
64 $engine->destroy();
65 }
66 $this->engines = [];
67 parent::tearDown();
68 }
69
70 private function getGlobalEnvironment( $engine ) {
71 static $script = <<<LUA
72 xxxseen = {}
73 function xxxGetTable( t )
74 if xxxseen[t] then
75 return 'table'
76 end
77 local ret = {}
78 xxxseen[t] = ret
79 for k, v in pairs( t ) do
80 if k ~= '_G' and string.sub( k, 1, 3 ) ~= 'xxx' then
81 if type( v ) == 'table' then
82 ret[k] = xxxGetTable( v )
83 elseif type( v ) == 'string'
84 or type( v ) == 'number'
85 or type( v ) == 'boolean'
86 or type( v ) == 'nil'
87 then
88 ret[k] = v
89 else
90 ret[k] = type( v )
91 end
92 end
93 end
94 return ret
95 end
96 return xxxGetTable( _G )
97LUA;
98 $func = $engine->getInterpreter()->loadString( $script, 'script' );
99 return $engine->getInterpreter()->callFunction( $func );
100 }
101
102 public function testGlobalEnvironment() {
103 // Grab the first engine as the "standard"
104 $firstEngine = reset( $this->engines );
105 $firstName = key( $this->engines );
106 $firstEnv = $this->getGlobalEnvironment( $firstEngine );
107
108 // Test all others against it
109 foreach ( $this->engines as $secondName => $secondEngine ) {
110 if ( $secondName !== $firstName ) {
111 $secondEnv = $this->getGlobalEnvironment( $secondEngine );
112 $this->assertEquals( $firstEnv, $secondEnv,
113 "Environments for $firstName and $secondName are not equivalent" );
114 }
115 }
116 }
117}
Set options of the Parser.
setTemplateCallback( $x)
Callback for template fetching; first argument to call_user_func().
PHP Parser - Processes wiki markup (which uses a more user-friendly syntax, such as "[[link]]" for ma...
Definition Parser.php:74
@group Lua @group LuaSandbox @group LuaStandalone @coversNothing