Skip to content
/ as3spec Public

A tiny BDD framework for AS3, inspired by Bacon and RSpec

License

Notifications You must be signed in to change notification settings

f1337/as3spec

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

99 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

NEW SYNTAX BELOW!

If running the specs for AS3Spec itself, you should get:

“33 specifications, 1 failures, 2 errors, 1 timeouts, 1 missing” in the summary - this is currently correct.

AS3Spec

AS3Spec is a tiny BDD framework for AS3, inspired by Bacon and RSpec. Built upon sprout project-generation tool for ActionScript/Flash/Flex/AIR.

Quick Start

Install Ruby, RubyGems

Windows

The One-Click Ruby Installer for Windows provides Ruby and RubyGems: rubyinstaller.rubyforge.org/

OSX 10.5 (Leopard)

The developer tools included with Xcode installer provide Ruby and RubyGems: developer.apple.com/technology/xcode.html

OSX 10.4 (Tiger), 10.3 (Panther)

The Ruby One-Click Installer for OSX provides Ruby and RubyGems: rubyosx.rubyforge.org/

Other operating systems

Ruby installation instructions for other platforms are availabe at: www.ruby-lang.org/en/downloads/

RubyGems installation instructions for other platforms are availabe at: docs.rubygems.org/read/chapter/3

Install Sprouts

Open your favorite terminal emulator (PuTTY, Terminal, etc.):

$ sudo gem install sprout

DOS/Windows users do not enter sudo

Sprout a project

From projectsprouts.org/:

$ sprout -n as3 SomeProject
$ cd SomeProject
$ rake

Windows users may have to run:

$ script\generate.rb utils.MathUtil

Why Sprouts?

There is a well written answer to that question at www.projectsprouts.org. I’m not going to copy the entire Sprouts manual here. ;)

Generators

AS3Spec provides some custom generators for Sprouts. Copy the generators directory to your Sprouts project, then use the generators as described below.

$ script/generate world.Hello
      create  src/world
      create  src/world/Hello.as
      create  spec/world
      create  spec/world/HelloSpec.as
   identical  spec/SpecSuite.as
   identical  spec/SpecSuiteXML.as
$ script/generate spec world.Hello
      exists  spec/world
   identical  spec/world/HelloSpec.as
   identical  spec/SpecSuite.as
   identical  spec/SpecSuiteXML.as
$ script/generate suite
   identical  spec/SpecSuite.as
   identical  spec/SpecSuiteXML.as
$ rake spec

Now we can BDD in AS3 (NEW SYNTAX!)

package
{
  import as3spec.*;
  import flash.utils.*;
  import flash.events.*;

  public class AS3Specs extends Spec
  {
    private var nothing:String;
    private var arr:Array;
    private var myTimer1:Timer;
    private var myTimer2:Timer;
    private var myTimer3:Timer;
    private var myTimer4:Timer;
    private var myObject:Object;

    override public function before():void
    {
      arr=new Array;
      myTimer1=new Timer(1000, 1);
      myTimer2=new Timer(1000, 1);
      myTimer3=new Timer(1000, 1);
      myTimer4=new Timer(1000, 1);
      myObject={};
    }

    override public function after():void
    {
      myTimer1.stop();
      myTimer2.stop();
      myTimer3.stop();
    }

    override public function run () :void
    {

      describe ('as3spec', function () :void
      {

          it ('provides should.equal').so(23).should.equal(23);
          it ('provides should.not.equal').so(23).should.not.equal(12);
          it ('provides should.be.same').so(arr).should.be.same_as(arr);
          it ('provides should.not.be.same').so(arr).should.not.be.same_as((new Array));
          it ('provides should.be.nil').so(nothing).should.be.nil;
          it ('provides should.not.be.nil').so(arr).should.not.be.nil;
          it ('provides should.have').so(arr).should.have('length');
          it ('provides should.not.have').so(arr).should.not.have('kittens');
          it ('provides should.match').so('hello').should.match(/ell/);
          it ('provides should.not.match').so('hello').should.not.match(/egg/);
          it ('provides should.be.a.kind_of').so(arr).should.be.a.kind_of(Array);
          it ('provides should.not.be.a.kind_of').so(arr).should.not.be.a.kind_of(Boolean);

          it ('provides should.raise(message)')
            .so(function() :void
            {
              throw('an error');
            })
            .should.raise('an error');

          it ('provides should.not.raise(message)')
            .so(function() :void
            {
              //nothing here
            })
            .should.not.raise('an error');

          it ('provides should.raise(class)')
            .so(function () :void
            {
              throw(new Error('an error'));
            })
            .should.raise(Error);

          it('provides should.not.raise(class)')
            .so(function () :void
            {
              // do nothing
            })
            .should.not.raise(Error);

          it ('provides should.raise()')
            .so(function () :void
            {
              throw(new Error('an error'));
            }).should.raise();

          it ('provides should.not.raise()')
            .so(function () :void
            {
              // do nothing
            }).should.not.raise();

          it ('provides should.not.trigger')
            .so(myTimer1).should.not.trigger('timer');

          it ('provides should.trigger', function() :void
            {
              myTimer1.addEventListener('timer', function(t:*) : void {});
            })
            .so(myTimer1).should.trigger('timer');

          it ('provides after(time).second', function() :void
            {
              setTimeout(function(t:*=null) :void{ myObject.myVal=5234 }, 900);
            })
            .so(myObject, 'myVal')
            .after(1).second
            .should.equal(5234);

          it ('provides after(time).seconds', function() :void
            {
              setTimeout(function(t:*=null) :void{ myObject.myVal=2255 }, 300);
            })
            .so(myObject, 'myVal')
            .after(0.5).seconds
            .should.equal(2255);

          it('provides when.receiving(event)', function() :void
            {
              myTimer2.start();
            })    
            .so(myTimer2)
            .when.receiving(TimerEvent.TIMER)
            .should.be.same_as(myTimer2);

          it('provides when.receiving(event).from(object)', function() :void
            {
              myTimer3.start();
            })    
            .so(123)
            .when.receiving(TimerEvent.TIMER).from(myTimer3)
            .should.be.equal_to(123);

          it('catches a missing spec');

          var arbitraryArg:String = 'dont be this';

          it('takes arbitrary arguments to specify block and can take a method to evaluate(run) in require', function(arbArg:String) :void
            {
              arbitraryArg=arbArg;
            },
            'please be this')
            .so(function() : Boolean {
              return (arbitraryArg == 'please be this');
            })
            .should.give(true);

          it('should generate an error if block passed to require throws')    
            .so(function() : Boolean {
              var someObject:*;
              return someObject.nonExistant();
            })
            .should.give(true);

          it ('catches an error', function () :void
            {
              throw(new Error('catch me if you can!'));
            }).so(arr);

          it ('catches a failure')
            .so(23).should.equal(15);

          timeout = 500;
          it ('can time out', function() :void
            {
              myTimer4.start();
            })
            .so(123)
            .when.receiving(TimerEvent.TIMER).from(myTimer4)
            .should.be.equal_to(123);

      });

    }

  }
}

About

A tiny BDD framework for AS3, inspired by Bacon and RSpec

Resources

License

Stars

Watchers

Forks

Packages

No packages published