MV Plugin Test Code

Hi, scripter.

I wrote MV Plugin's test code, and run test.

Requirements:

Step1: Create "NewPlugin" directory, and "NewPlugin/NewPlugin.js" file.

Step2: Write "NewPlugin/gulpfile.js".

Spoiler
var gulp = require('gulp');

var mocha = require('gulp-mocha');

 

gulp.task('default', ['test']);

 

gulp.task('test', function() {

    return gulp.src('test/NewPlugin.spec.js', {read: false})

    .pipe(mocha({reporter: 'nyan'}));

});
Step3: Create "NewPlugin/test" directory, and "NewPlugin/test/NewPlugin.spec.js".

Step4: Write "NewPlugin/test/NewPlugin.spec.js" test code.

Spoiler
isTest = true;

 

should = require('chai').should();

sinon = require('sinon');

 

describe('NewPlugin', function() {

    // initialize

    before(function() {

        // define mock PluginManager

        PluginManager = {};

        PluginManager.parameters = sinon.stub()

        .withArgs('MyTemplate').returns({

            // PluginParam: 'default'

        });

        // define mock Game_Interpreter

        Game_Interpreter = function() {};

        Game_Interpreter.prototype.pluginCommand = sinon.stub();

 

        // import Test functions.

        NewPlugin = require('../NewPlugin.js');

        // localFunction = NewPlugin.localFunction;

    });

 

    describe('call from PluginCommand', function() {

        beforeEach(function() {

            gameInterpreter = new Game_Interpreter();

        });

 

        describe('command name match', function() {

            it('valid argments', function() {

                gameInterpreter.pluginCommand('CommandName', ['validArguments']);

            });

 

            it('invalid argments', function() {

                gameInterpreter.pluginCommand('isAnyItemHave', ['invalidArguments']);

            });

 

        });

 

        it('command name not match', function() {

            gameInterpreter.pluginCommand('otherCommand', ['1', 'foo', 'bar']);

        });

    });

 

});
 

Step5: Write "NewPlugin/NewPlugin.js" plugin code.

Spoiler
var Imported = Imported || {};

Imported.NewPlugin = {};

 

(function() {

 

    'use strict';

 

    var parameters = PluginManager.parameters('NewPlugin');

    // var PluginParam = String(parameters['PluginParam'] || 'default');

 

    var _Game_Interpreter_pluginCommand =

            Game_Interpreter.prototype.pluginCommand;

    Game_Interpreter.prototype.pluginCommand = function(command, args) {

        _Game_Interpreter_pluginCommand.call(this, command, args);

        if (command === 'CommandName') {

            // write code.

        }

    };

 

    try {

        if (isTest) {

            // exports.NewPlugin = localFunction;

        }

    }

    catch(e) {

    }

})();
Step6: Run test.

Spoiler
$ cd NewPlugin

$ gulp
Sample testable plugin is here, with test code is here.
Hmm, unit testing framework to make it easier to test code. I like it.
Great initiative!

I think automated tests will be very userful, maybe we shall be able to detect conflicts between scripts. 🙂

Congrats!

Original thread: https://forums.rpgmakerweb.com/threads/mv-plugin-test-code.50564/