Commit 947cbe46 authored by Rene Saarsoo's avatar Rene Saarsoo
Browse files

Converted method-tests into rspec.

parent cdd6982e
Loading
Loading
Loading
Loading
+256 −0
Original line number Diff line number Diff line
@@ -8,6 +8,262 @@ describe JsDuck::Aggregator do
    agr.result
  end

  shared_examples_for "method documentation" do
    it "creates method" do
      @doc[:tagname].should == :method
    end

    it "takes documentation from doc-comment" do
      @doc[:doc].should == "Some function"
    end

    it "detects method name" do
      @doc[:name].should == "foo"
    end
  end

  shared_examples_for "no parameters" do
    it "by default methods have no parameters" do
      @doc[:params].length.should == 0
    end
  end

  shared_examples_for "two parameters" do
    it "detects parameter count" do
      @doc[:params].length.should == 2
    end

    it "detects parameter names" do
      @doc[:params][0][:name].should == "x"
      @doc[:params][1][:name].should == "y"
    end
  end

  shared_examples_for "parameter types" do
    it "detects parameter types" do
      @doc[:params][0][:type].should == "String"
      @doc[:params][1][:type].should == "Number"
    end
  end

  shared_examples_for "parameter default types" do
    it "parameter default type is Ibject" do
      @doc[:params][0][:type].should == "Object"
      @doc[:params][1][:type].should == "Object"
    end
  end

  shared_examples_for "parameter docs" do
    it "detects parameter types" do
      @doc[:params][0][:doc].should == "First parameter"
      @doc[:params][1][:doc].should == "Second parameter"
    end
  end

  shared_examples_for "no return" do
    it "default return type is void" do
      @doc[:return][:type].should == "void"
    end
  end

  shared_examples_for "has return" do
    it "detects return type" do
      @doc[:return][:type].should == "String"
    end
    it "detects return value comment" do
      @doc[:return][:doc].should == "return value"
    end
  end

  describe "function declaration" do
    before do
      @doc = parse("/** Some function */ function foo() {}")[0]
    end
    it_should_behave_like "method documentation"
    it_should_behave_like "no parameters"
    it_should_behave_like "no return"
  end

  describe "function declaration with parameters" do
    before do
      @doc = parse("/** Some function */ function foo(x, y) {}")[0]
    end
    it_should_behave_like "method documentation"
    it_should_behave_like "two parameters"
    it_should_behave_like "parameter default types"
    it_should_behave_like "no return"
  end

  describe "function-literal with var" do
    before do
      @doc = parse("/** Some function */ var foo = function() {}")[0]
    end
    it_should_behave_like "method documentation"
  end

  describe "function-literal without var" do
    before do
      @doc = parse("/** Some function */ foo = function() {}")[0]
    end
    it_should_behave_like "method documentation"
  end

  describe "function-literal in object-literal" do
    before do
      @doc = parse("/** Some function */ foo: function() {}")[0]
    end
    it_should_behave_like "method documentation"
  end

  describe "function-literal in object-literal-string" do
    before do
      @doc = parse("/** Some function */ 'foo': function() {}")[0]
    end
    it_should_behave_like "method documentation"
  end

  describe "function-literal in prototype-chain" do
    before do
      @doc = parse("/** Some function */ Some.long.prototype.foo = function() {}")[0]
    end
    it_should_behave_like "method documentation"
  end

  describe "explicit @method without @param-s" do
    before do
      @doc = parse("
/**
 * @method foo
 * Some function
 */")[0]
    end
    it_should_behave_like "method documentation"
    it_should_behave_like "no parameters"
    it_should_behave_like "no return"
  end

  describe "explicit @method with @param-s" do
    before do
      @doc = parse("
/**
 * @method foo
 * Some function
 * @param {String} x First parameter
 * @param {Number} y Second parameter
 */")[0]
    end
    it_should_behave_like "method documentation"
    it_should_behave_like "two parameters"
    it_should_behave_like "parameter types"
    it_should_behave_like "parameter docs"
    it_should_behave_like "no return"
  end

  describe "explicit @method after @params-s" do
    before do
      @doc = parse("
/**
 * Some function
 * @param {String} x First parameter
 * @param {Number} y Second parameter
 * @method foo
 */")[0]
    end
    it_should_behave_like "method documentation"
    it_should_behave_like "two parameters"
    it_should_behave_like "parameter types"
    it_should_behave_like "parameter docs"
    it_should_behave_like "no return"
  end

  describe "explicit @method with @param-s overriding implicit code" do
    before do
      @doc = parse("
/**
 * Some function
 * @param {String} x First parameter
 * @param {Number} y Second parameter
 * @method foo
 */
function bar(q, z) {}
")[0]
    end
    it_should_behave_like "method documentation"
    it_should_behave_like "two parameters"
    it_should_behave_like "parameter types"
    it_should_behave_like "parameter docs"
  end

  describe "@param-s partially overriding implicit params" do
    before do
      @doc = parse("
/**
 * Some function
 * @param {String} x
 */
function foo(q, y) {}
")[0]
    end
    it_should_behave_like "method documentation"
    it_should_behave_like "two parameters"
  end

  describe "@param-s declaring only types" do
    before do
      @doc = parse("
/**
 * Some function
 * @param {String}
 * @param {Number}
 */
function foo(x, y) {}
")[0]
    end
    it_should_behave_like "method documentation"
    it_should_behave_like "two parameters"
    it_should_behave_like "parameter types"
  end

  describe "@return documenting return value" do
    before do
      @doc = parse("
/**
 * Some function
 * @return {String} return value
 */
function foo() {}
")[0]
    end
    it_should_behave_like "method documentation"
    it_should_behave_like "has return"
  end

  describe "@returns being alias for @return" do
    before do
      @doc = parse("
/**
 * Some function
 * @returns {String} return value
 */
function foo() {}
")[0]
    end
    it_should_behave_like "method documentation"
    it_should_behave_like "has return"
  end

  describe "method without doc-comment" do
    before do
      @docs = parse("
// My comment
function foo(x, y) {}
")
    end
    it "remains undocumented" do
      @docs.length.should == 0
    end
  end

  describe "@member" do

    it "defines the class where item belongs" do
+0 −206
Original line number Diff line number Diff line
@@ -3,212 +3,6 @@ require "test/unit"

class TestJsDuck < Test::Unit::TestCase

  def test_method
    docs = JsDuck.parse("
/**
 * Some function
 */
function foo(x) {
}
")
    assert_equal(:method, docs[0][:tagname])
    assert_equal("Some function", docs[0][:doc])
    assert_equal("foo", docs[0][:name])
    assert_equal(1, docs[0][:params].length)
    assert_equal("x", docs[0][:params][0][:name])
  end

  def test_method_with_var
    docs = JsDuck.parse("
/**
 */
var foo = function(x) {
}
")
    assert_equal("foo", docs[0][:name])
    assert_equal("x", docs[0][:params][0][:name])
  end

  def test_method_without_var
    docs = JsDuck.parse("
/**
 */
foo = function(x) {
}
")
    assert_equal("foo", docs[0][:name])
    assert_equal("x", docs[0][:params][0][:name])
  end

  def test_method_in_object_literal
    docs = JsDuck.parse("
/**
 */
foo: function(x) {
}
")
    assert_equal("foo", docs[0][:name])
    assert_equal("x", docs[0][:params][0][:name])
  end

  def test_method_in_object_literal_string
    docs = JsDuck.parse("
/**
 */
'foo': function(x) {
}
")
    assert_equal("foo", docs[0][:name])
    assert_equal("x", docs[0][:params][0][:name])
  end

  def test_method_in_prototype
    docs = JsDuck.parse("
/**
 */
Some.Long.prototype.foo = function(x) {
}
")
    assert_equal("foo", docs[0][:name])
    assert_equal("x", docs[0][:params][0][:name])
  end

  def test_method_private
    docs = JsDuck.parse("
// no doc-comment for this function
function foo() {
}
")
    assert_equal([], docs)
  end

  def test_explicit_method_doc
    docs = JsDuck.parse("
/**
 * @method hello
 * Just some function
 */
eval('hello = new Function();');
")
    assert_equal("hello", docs[0][:name])
    assert_equal("Just some function", docs[0][:doc])
  end

  def test_explicit_method_doc_overrides_implicit_code
    docs = JsDuck.parse("
/**
 * @method hello
 * Just some function
 */
function goodby(){}
")
    assert_equal("hello", docs[0][:name])
    assert_equal("Just some function", docs[0][:doc])
  end

  def test_implicit_method_parameters
    docs = JsDuck.parse("
/**
 */
function f(foo, bar, baz){}
")
    params = docs[0][:params]
    assert_equal("foo", params[0][:name])
    assert_equal("bar", params[1][:name])
    assert_equal("baz", params[2][:name])
  end

  def test_explicit_method_parameters_override_implicit_ones
    docs = JsDuck.parse("
/**
 * @param {String} x
 * @param {String} y
 * @param {String} z
 */
function f(foo, bar, baz){}
")
    params = docs[0][:params]
    assert_equal("x", params[0][:name])
    assert_equal("y", params[1][:name])
    assert_equal("z", params[2][:name])
  end

  def test_some_explicit_and_some_implicit_parameters
    docs = JsDuck.parse("
/**
 * @param {String} x
 */
function f(foo, bar){}
")
    params = docs[0][:params]
    assert_equal("x", params[0][:name])
    assert_equal("bar", params[1][:name])
  end

  def test_explicit_parameter_types_and_implicit_names
    docs = JsDuck.parse("
/**
 * @param {String}
 * @param {Number}
 */
function f(foo, bar){}
")
    params = docs[0][:params]
    assert_equal("foo", params[0][:name])
    assert_equal("String", params[0][:type])
    assert_equal("bar", params[1][:name])
    assert_equal("Number", params[1][:type])
  end

  def test_description_can_precede_method_tag
    docs = JsDuck.parse("/**
 * Method description
 * @param foo
 * @method blah
 */")
    assert_equal("blah", docs[0][:name])
    assert_equal("Method description", docs[0][:doc])
    assert_equal("foo", docs[0][:params][0][:name])
  end

  def test_return
    docs = JsDuck.parse("/**
 * @method foo
 * Method description
 * @return {String} Some really
 * long comment.
 */")
    assert_equal("String", docs[0][:return][:type])
    assert_equal("Some really\nlong comment.", docs[0][:return][:doc])
  end

  def test_default_return_type_is_void
    docs = JsDuck.parse("/**
 * @method foo
 */")
    assert_equal("void", docs[0][:return][:type])
    assert_equal("", docs[0][:return][:doc])
  end

  def test_returns_is_alias_for_return
    docs = JsDuck.parse("/**
 * @method
 * @returns {String} blah
 */")
    assert_equal("String", docs[0][:return][:type])
    assert_equal("blah", docs[0][:return][:doc])
  end

  def test_default_param_type_is_object
    docs = JsDuck.parse("/**
 * @method
 * @param
 */")
    assert_equal("Object", docs[0][:params][0][:type])
    assert_equal("", docs[0][:params][0][:name])
    assert_equal("", docs[0][:params][0][:doc])
  end

  def test_event
    docs = JsDuck.parse("/**
 * @event mousedown