Commit 6fa00345 authored by Rene Saarsoo's avatar Rene Saarsoo
Browse files

Converted @class tests to RSpec.

parent 618762f9
Loading
Loading
Loading
Loading
+235 −0
Original line number Diff line number Diff line
@@ -8,6 +8,241 @@ describe JsDuck::Aggregator do
    agr.result
  end

  shared_examples_for "class" do
    it "creates class" do
      @doc[:tagname].should == :class
    end
    it "detects name" do
      @doc[:name].should == "MyClass"
    end
  end

  describe "explicit class" do
    before do
      @doc = parse(<<-EOS)[0]
        /**
         * @class MyClass
         * @extends Your.Class
         * Some documentation.
         * @singleton
         * @xtype nicely
         */
      EOS
    end

    it_should_behave_like "class"
    it "detects extends" do
      @doc[:extends] == "Your.Class"
    end
    it "takes documentation from doc-comment" do
      @doc[:doc].should == "Some documentation."
    end
    it "detects singleton" do
      @doc[:singleton].should == true
    end
    it "detects xtype" do
      @doc[:xtype].should == "nicely"
    end
  end

  describe "function after doc-comment" do
    before do
      @doc = parse("/** */ function MyClass() {}")[0]
    end
    it_should_behave_like "class"
  end

  describe "lambda function after doc-comment" do
    before do
      @doc = parse("/** */ MyClass = function() {}")[0]
    end
    it_should_behave_like "class"
  end

  describe "class name in both code and doc-comment" do
    before do
      @doc = parse("/** @class MyClass */ function YourClass() {}")[0]
    end
    it_should_behave_like "class"
  end

  shared_examples_for "not class" do
    it "does not imply class" do
      @doc[:tagname].should_not == :class
    end
  end

  describe "function beginning with underscore" do
    before do
      @doc = parse("/** */ function _Foo() {}")[0]
    end
    it_should_behave_like "not class"
  end

  describe "lowercase function name" do
    before do
      @doc = parse("/** */ function foo() {}")[0]
    end
    it_should_behave_like "not class"
  end

  describe "Ext.extend() in code" do
    before do
      @doc = parse("/** */ MyClass = Ext.extend(Your.Class, {  });")[0]
    end
    it_should_behave_like "class"
    it "detects implied extends" do
      @doc[:extends] == "Your.Class"
    end
  end

  describe "class with cfgs" do
    before do
      @doc = parse(<<-EOS)[0]
        /**
         * @class MyClass
         * @extends Bar
         * Comment here.
         * @cfg {String} foo Hahaha
         * @private
         * @cfg {Boolean} bar Hihihi
         */
      EOS
    end

    it_should_behave_like "class"
    it "has needed number of configs" do
      @doc[:cfg].length.should == 2
    end
    it "picks up names of all configs" do
      @doc[:cfg][0][:name].should == "foo"
      @doc[:cfg][1][:name].should == "bar"
    end
    it "marks first @cfg as private" do
      @doc[:cfg][0][:private].should == true
    end
  end

  describe "class with constructor" do
    before do
      @doc = parse(<<-EOS)[0]
        /**
         * @class MyClass
         * Comment here.
         * @constructor
         * This constructs the class
         * @param {Number} nr
         */
      EOS
    end

    it_should_behave_like "class"
    it "has one method" do
      @doc[:method].length.should == 1
    end
    it "has method with name 'constructor'" do
      @doc[:method][0][:name].should == "constructor"
    end
    it "has method with needed parameters" do
      @doc[:method][0][:params].length.should == 1
    end
  end

  describe "@xtype after @constructor" do
    before do
      @doc = parse(<<-EOS)[0]
        /**
         * @class MyClass
         * Comment here.
         * @constructor
         * This constructs the class
         * @xtype nicely
         */
      EOS
    end

    it_should_behave_like "class"
    it "detects xtype" do
      @doc[:xtype].should == "nicely"
    end
  end

  describe "member docs after class doc" do
    before do
      @classes = parse(<<-EOS)
        /**
         * @class
         */
        var MyClass = Ext.extend(Ext.Panel, {
          /**
           * @cfg
           */
          fast: false,
          /**
           * @property
           */
          length: 0,
          /**
           */
          doStuff: function() {
            this.addEvents(
              /**
               * @event
               */
              'touch'
            );
          }
        });
      EOS
      @doc = @classes[0]
    end
    it "results in only one item" do
      @classes.length.should == 1
    end
    it_should_behave_like "class"
    it "should have configs" do
      @doc[:cfg].length.should == 1
    end
    it "should have properties" do
      @doc[:property].length.should == 1
    end
    it "should have method" do
      @doc[:method].length.should == 1
    end
    it "should have events" do
      @doc[:event].length.should == 1
    end
  end

  describe "multiple classes" do
    before do
      @classes = parse(<<-EOS)
        /**
         * @class
         */
        function Foo(){}
        /**
         * @class
         */
        function Bar(){}
      EOS
    end

    it "results in multiple classes" do
      @classes.length.should == 2
    end

    it "both are class tags" do
      @classes[0][:tagname] == :class
      @classes[1][:tagname] == :class
    end

    it "names come in order" do
      @classes[0][:name] == "Foo"
      @classes[1][:name] == "Bar"
    end
  end

  describe "one class many times" do
    before do
      @classes = parse(<<-EOS)
+0 −204
Original line number Diff line number Diff line
@@ -3,142 +3,6 @@ require "test/unit"

class TestJsDuck < Test::Unit::TestCase

  def test_explicit_class
    docs = JsDuck.parse("/**
 * @class My.nice.Class
 * @extends Your.Class
 * A good class indeed.
 * @singleton
 * @xtype nicely
 */")
    assert_equal(:class, docs[0][:tagname])
    assert_equal("My.nice.Class", docs[0][:name])
    assert_equal("Your.Class", docs[0][:extends])
    assert_equal("A good class indeed.", docs[0][:doc])
    assert_equal(true, docs[0][:singleton])
    assert_equal("nicely", docs[0][:xtype])
  end

  def test_implicit_class_name_from_function
    docs = JsDuck.parse("/**
 */
function MyClass() {}
")
    assert_equal(:class, docs[0][:tagname])
    assert_equal("MyClass", docs[0][:name])
  end

  def test_implicit_class_name_from_function
    docs = JsDuck.parse("/**
 */
function MyClass() {}
")
    assert_equal(:class, docs[0][:tagname])
    assert_equal("MyClass", docs[0][:name])
  end

  def test_implicit_class_name_from_lambda
    docs = JsDuck.parse("/**
 */
My.Class = function(a,b,c) {}
")
    assert_equal(:class, docs[0][:tagname])
    assert_equal("My.Class", docs[0][:name])
  end

  def test_explicit_class_name_overrides_implicit
    docs = JsDuck.parse("
/**
 * @class Foo
 */
function Bar(){}
")
    assert_equal("Foo", docs[0][:name])
  end

  def test_underscore_does_not_imply_class_name
    # name beginning with uppercase letter is assumed to be class name,
    # but we have to ensure that "_" is not treated as uppercase letter.
    docs = JsDuck.parse("
/** */
_blah = {}
")
    assert_equal(:property, docs[0][:tagname])
  end

  def test_implicit_extends
    docs = JsDuck.parse("
/**
 */
MyClass = Ext.extend(Ext.util.Observable, {
});
")
    assert_equal("MyClass", docs[0][:name])
    assert_equal("Ext.util.Observable", docs[0][:extends])
  end

  def test_class_with_cfgs
    docs = JsDuck.parse("/**
 * @class Foo
 * @extends Bar
 * Comment here.
 * @cfg {String} foo Hahaha
 * @private
 * @cfg {Boolean} bar Hihihi
 */")
    assert_equal(:class, docs[0][:tagname])
    assert_equal("Foo", docs[0][:name])
    assert_equal("Bar", docs[0][:extends])
    assert_equal("Comment here.", docs[0][:doc])

    cfgs = docs[0][:cfg]
    assert_equal(2, cfgs.length)

    assert_equal(:cfg, cfgs[0][:tagname])
    assert_equal("String", cfgs[0][:type])
    assert_equal("foo", cfgs[0][:name])
    assert_equal("Hahaha", cfgs[0][:doc])
    assert_equal(true, cfgs[0][:private])

    assert_equal(:cfg, cfgs[1][:tagname])
    assert_equal("Boolean", cfgs[1][:type])
    assert_equal("bar", cfgs[1][:name])
    assert_equal("Hihihi", cfgs[1][:doc])
  end

  def test_class_with_constructor
    docs = JsDuck.parse("/**
 * @class Foo
 * Comment here.
 * @constructor
 * This constructs the class
 * @param {Number} nr
 */")
    assert_equal(:class, docs[0][:tagname])
    assert_equal("Foo", docs[0][:name])

    methods = docs[0][:method]
    assert_equal(1, methods.length)
    assert_equal(:method, methods[0][:tagname])
    assert_equal("constructor", methods[0][:name])

    params = methods[0][:params]
    assert_equal("nr", params[0][:name])
    assert_equal("Number", params[0][:type])
  end

  def test_xtype_after_constructor
    docs = JsDuck.parse("/**
 * @class Foo
 * Comment here.
 * @constructor
 * Often in ExtJS the xtype tag follows constructor tag.
 * @xtype blah
 */")
    assert_equal(:class, docs[0][:tagname])
    assert_equal("blah", docs[0][:xtype])
  end

  def test_implicit_property_type
    comment = "
/**
@@ -184,73 +48,5 @@ MyClass = Ext.extend(Ext.util.Observable, {
    assert_equal(true, docs[0][:static])
  end

  def test_member_docs_following_class
    docs = JsDuck.parse("
/**
 * @class
 */
var MyPanel = Ext.extend(Ext.Panel, {
  /**
   * @cfg
   */
  fast: false,
  /**
   * @property
   */
  length: 0,
  /**
   */
  doStuff: function() {
    this.addEvents(
      /**
       * @event
       */
      'touch'
    );
  }
});
")
    assert_equal(:class, docs[0][:tagname])
    assert_equal("MyPanel", docs[0][:name])

    cfgs = docs[0][:cfg]
    assert_equal(1, cfgs.length)
    assert_equal(:cfg, cfgs[0][:tagname])
    assert_equal("fast", cfgs[0][:name])

    props = docs[0][:property]
    assert_equal(1, props.length)
    assert_equal(:property, props[0][:tagname])
    assert_equal("length", props[0][:name])

    methods = docs[0][:method]
    assert_equal(1, methods.length)
    assert_equal(:method, methods[0][:tagname])
    assert_equal("doStuff", methods[0][:name])

    events = docs[0][:event]
    assert_equal(1, events.length)
    assert_equal(:event, events[0][:tagname])
    assert_equal("touch", events[0][:name])
  end

  def test_multiple_classes
    docs = JsDuck.parse("
/**
 * @class
 */
function Foo(){}
/**
 * @class
 */
function Bar(){}
")
    assert_equal(2, docs.length)
    assert_equal(:class, docs[0][:tagname])
    assert_equal("Foo", docs[0][:name])
    assert_equal(:class, docs[1][:tagname])
    assert_equal("Bar", docs[1][:name])
  end

end