Bartosz Dziewoński has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/193114

Change subject: tests: Run JS/PHP tests for widgets with required parameters, 
too
......................................................................

tests: Run JS/PHP tests for widgets with required parameters, too

Also, extend the test code to support passing other widgets as
parameters.

Now we can run the tests for layouts!

Change-Id: Ife218d8978910bc5ac1b12db4cf79c2fdb9477c6
---
M bin/generate-JSPHP-for-karma.php
M bin/testsuitegenerator.rb
M tests/JSPHP.test.karma.js
M tests/JSPHP.test.standalone.js
M tests/index.php
5 files changed, 95 insertions(+), 25 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/oojs/ui refs/changes/14/193114/1

diff --git a/bin/generate-JSPHP-for-karma.php b/bin/generate-JSPHP-for-karma.php
index 08f3065..b4b9738 100644
--- a/bin/generate-JSPHP-for-karma.php
+++ b/bin/generate-JSPHP-for-karma.php
@@ -16,15 +16,27 @@
 $testSuite = json_decode( $testSuiteJSON, true );
 $testSuiteOutput = array();
 
+function new_OOUI( $class, $config = array() ) {
+       $class = "OOUI\\" . $class;
+       return new $class( $config );
+}
+function unstub( &$value ) {
+       if ( is_string( $value ) && substr( $value, 0, 13 ) === '_placeholder_' 
) {
+               $value = json_decode( substr( $value, 13 ), true );
+               array_walk_recursive( $value['config'], 'unstub' );
+               $value = new_OOUI( $value['class'], $value['config'] );
+       }
+}
 // Keep synchronized with tests/index.php
 $themes = array( 'ApexTheme', 'MediaWikiTheme' );
 foreach ( $themes as $theme ) {
-       $class = "OOUI\\" . $theme;
-       OOUI\Theme::setSingleton( new $class() );
+       OOUI\Theme::setSingleton( new_OOUI( $theme ) );
        foreach ( $testSuite as $className => $tests ) {
                foreach ( $tests as $test ) {
-                       $class = "OOUI\\" . $test['class'];
-                       $instance = new $class( $test['config'] );
+                       // Unstub placeholders
+                       $config = $test['config'];
+                       array_walk_recursive( $config, 'unstub' );
+                       $instance = new_OOUI( $test['class'], $config );
                        $testSuiteOutput[$theme][$className][] = "$instance";
                }
        }
diff --git a/bin/testsuitegenerator.rb b/bin/testsuitegenerator.rb
index 9029ecb..a096813 100644
--- a/bin/testsuitegenerator.rb
+++ b/bin/testsuitegenerator.rb
@@ -19,7 +19,7 @@
                .reject{|c| !c[:parent] || c[:parent] == 'ElementMixin' || 
c[:parent] == 'Theme' } # can't test abstract
                .reject{|c| %w[Element Widget Layout Theme].include? c[:name] } 
# no toplevel
                .reject{|c| c[:name] == 'DropdownInputWidget' } # different PHP 
and JS implementations
-               .select{|c| c[:methods][0][:params].empty? } # only without 
params :(
+               .reject{|c| c[:name] == 'GridLayout' } # has strictly required 
parameters in config
 
        # values to test for each type
        expandos = {
@@ -34,6 +34,8 @@
                'href' => ['http://example.com/'],
                ['TextInputWidget', 'type'] => %w[text password],
                ['ButtonInputWidget', 'type'] => %w[button input],
+               ['FieldLayout', 'help'] => true, # different PHP and JS 
implementations
+               ['FieldsetLayout', 'help'] => true, # different PHP and JS 
implementations
                'type' => %w[text button],
                'method' => %w[GET POST],
                'action' => [],
@@ -54,16 +56,36 @@
                'text' => true,
        }
 
+       find_class = lambda do |klass|
+               return classes.find{|c| c[:name] == klass }
+       end
+
        expand_types_to_values = lambda do |types|
                return types.map{|t|
                        as_array = true if t.sub! '[]', ''
-                       vals = expandos[ t ] || [] # the empty value will 
result in no tests being generated
+                       t = 'ButtonWidget' if t == 'Widget' # Widget is not 
"testable", use a subclass
+                       if expandos[t]
+                               # Primitive. Run tests with the provided values.
+                               vals = expandos[t]
+                       elsif testable_classes.find{|c| c[:name] == t }
+                               # OOUI object. Test suite will instantiate one 
and run the test with it.
+                               params = 
find_class.call(t)[:methods][0][:params] || []
+                               config = params.map{|config_option|
+                                       types = config_option[:type].split '|'
+                                       values = 
expand_types_to_values.call(types)
+                                       { config_option[:name] => values[0] }
+                               }
+                               vals = [ '_placeholder_' + {
+                                       class: t,
+                                       config: config.inject({}, :merge)
+                               }.to_json ]
+                       else
+                               # We don't know how to test this. The empty 
value will result in no
+                               # tests being generated for this combination of 
config values.
+                               vals = []
+                       end
                        as_array ? vals.map{|v| [v] } : vals
                }.inject(:+)
-       end
-
-       find_class = lambda do |klass|
-               return classes.find{|c| c[:name] == klass }
        end
 
        find_config_sources = lambda do |klass_name|
@@ -81,17 +103,17 @@
        end
 
        testable_classes.each do |klass|
-               # constructor = klass[:methods][0]
-               # params = constructor[:params]
-
-               config_sources = find_config_sources.call klass[:name]
-               config = config_sources.map{|c| 
find_class.call(c)[:methods][0][:config] }.compact.inject(:+)
+               config_sources = find_config_sources.call(klass[:name])
+                       .map{|c| find_class.call(c)[:methods][0] }
+               config = config_sources.map{|c| c[:config] }.compact.inject(:+)
+               required_config = klass[:methods][0][:params] || []
 
                # generate every possible configuration of configuration option 
sets
                maxlength = [config.length, 2].min
                config_combinations = (0..maxlength).map{|l| 
config.combination(l).to_a }.inject(:+)
                # for each set, generate all possible values to use based on 
option's type
                config_combinations = config_combinations.map{|config_comb|
+                       config_comb += required_config
                        expanded = config_comb.map{|config_option|
                                types = config_option[:type].split '|'
                                sensible = sensible_values[ [ klass[:name], 
config_option[:name] ] ] ||
@@ -106,12 +128,14 @@
                        expanded.length > 0 ? 
expanded[0].product(*expanded[1..-1]) : []
                }.inject(:concat).map(&:compact).uniq
 
-               # param_types = params.map{|p| { placeholder_for: p[:type] } }
+               # really require the required ones
+               config_combinations = config_combinations.select{|config_comb|
+                       required_config.all?{|r| config_comb.find{|c| c[:name] 
== r[:name] } }
+               }
 
                config_combinations.each do |config_comb|
                        tests << {
                                class: klass[:name],
-                               # params: param_types,
                                config: Hash[ config_comb.map{|c| [ c[:name], 
c[:value] ] } ]
                        }
                end
diff --git a/tests/JSPHP.test.karma.js b/tests/JSPHP.test.karma.js
index 561e534..9dbd032 100644
--- a/tests/JSPHP.test.karma.js
+++ b/tests/JSPHP.test.karma.js
@@ -8,14 +8,25 @@
                        MediaWikiTheme: new OO.ui.MediaWikiTheme()
                };
 
+       function unstub( value ) {
+               var config;
+               if ( typeof value === 'string' && value.substr( 0, 13 ) === 
'_placeholder_' ) {
+                       value = JSON.parse( value.substr( 13 ) );
+                       config = OO.copy( value.config, null, unstub );
+                       return new OO.ui[ value.class ]( config );
+               }
+       }
+
        function makeTest( theme, klassName, tests, output ) {
                QUnit.test( theme + ': ' + klassName, tests.length, function ( 
assert ) {
-                       var test, instance, $fromPhp, i, testName;
+                       var test, config, instance, $fromPhp, i, testName;
                        OO.ui.theme = themes[ theme ];
                        for ( i = 0; i < tests.length; i++ ) {
                                test = tests[ i ];
+                               // Unstub placeholders
+                               config = OO.copy( test.config, null, unstub );
 
-                               instance = new OO.ui[ test.class ]( test.config 
);
+                               instance = new OO.ui[ test.class ]( config );
                                $fromPhp = $( $.parseHTML( output[ i ] ) );
 
                                $( '#qunit-fixture' ).append( 
instance.$element, $fromPhp );
diff --git a/tests/JSPHP.test.standalone.js b/tests/JSPHP.test.standalone.js
index 79fefd5..13535e3 100644
--- a/tests/JSPHP.test.standalone.js
+++ b/tests/JSPHP.test.standalone.js
@@ -8,14 +8,25 @@
                        MediaWikiTheme: new OO.ui.MediaWikiTheme()
                };
 
+       function unstub( value ) {
+               var config;
+               if ( typeof value === 'string' && value.substr( 0, 13 ) === 
'_placeholder_' ) {
+                       value = JSON.parse( value.substr( 13 ) );
+                       config = OO.copy( value.config, null, unstub );
+                       return new OO.ui[ value.class ]( config );
+               }
+       }
+
        function makeTest( theme, klassName, tests ) {
                QUnit.test( theme + ': ' + klassName, tests.length, function ( 
assert ) {
-                       var test, instance, id, fromPhp, i, testName;
+                       var test, config, instance, id, fromPhp, i, testName;
                        OO.ui.theme = themes[ theme ];
                        for ( i = 0; i < tests.length; i++ ) {
                                test = tests[ i ];
+                               // Unstub placeholders
+                               config = OO.copy( test.config, null, unstub );
 
-                               instance = new OO.ui[ test.class ]( test.config 
);
+                               instance = new OO.ui[ test.class ]( config );
 
                                id = 'JSPHPTestSuite_' + theme + klassName + i;
                                fromPhp = document.getElementById( id 
).firstChild;
diff --git a/tests/index.php b/tests/index.php
index 4acbba2..d187ccf 100644
--- a/tests/index.php
+++ b/tests/index.php
@@ -43,15 +43,27 @@
 <body>
        <div id="JSPHPTestSuite" style="display: none;">
                <?php
+                       function new_OOUI( $class, $config = array() ) {
+                               $class = "OOUI\\" . $class;
+                               return new $class( $config );
+                       }
+                       function unstub( &$value ) {
+                               if ( is_string( $value ) && substr( $value, 0, 
13 ) === '_placeholder_' ) {
+                                       $value = json_decode( substr( $value, 
13 ), true );
+                                       array_walk_recursive( $value['config'], 
'unstub' );
+                                       $value = new_OOUI( $value['class'], 
$value['config'] );
+                               }
+                       }
                        // Keep synchronized with 
bin/generate-JSPHP-for-karma.php
                        $themes = array( 'ApexTheme', 'MediaWikiTheme' );
                        foreach ( $themes as $theme ) {
-                               $class = "OOUI\\" . $theme;
-                               OOUI\Theme::setSingleton( new $class() );
+                               OOUI\Theme::setSingleton( new_OOUI( $theme ) );
                                foreach ( $testSuite as $className => $tests ) {
                                        foreach ( $tests as $index => $test ) {
-                                               $class = "OOUI\\" . 
$test['class'];
-                                               $instance = new $class( 
$test['config'] );
+                                               // Unstub placeholders
+                                               $config = $test['config'];
+                                               array_walk_recursive( $config, 
'unstub' );
+                                               $instance = new_OOUI( 
$test['class'], $config );
                                                echo "<div 
id='JSPHPTestSuite_$theme$className$index'>$instance</div>\n";
                                        }
                                }

-- 
To view, visit https://gerrit.wikimedia.org/r/193114
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ife218d8978910bc5ac1b12db4cf79c2fdb9477c6
Gerrit-PatchSet: 1
Gerrit-Project: oojs/ui
Gerrit-Branch: master
Gerrit-Owner: Bartosz Dziewoński <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to