Commit 838f3ff3 authored by Rene Saarsoo's avatar Rene Saarsoo
Browse files

Initial @throws implementation as meta tag.

parent 2c3ce811
Loading
Loading
Loading
Loading
+46 −0
Original line number Diff line number Diff line
require "jsduck/meta_tag"
require "jsduck/logger"

module JsDuck::Tag
  class Throws < JsDuck::MetaTag
    def initialize
      @name = "throws"
      @key = :throws
      @signature = {:long => "throws", :short => "THR"}
      @multiline = true
    end

    def to_value(tags)
      tags.map do | throws |
        if throws =~ /\A\{([\w\.]+)\}\s+([^ ].*)\Z/m
          {:type => $1, :doc => $2.strip}
        else
          {:type => "Object", :doc => throws}
        end
      end
    end

    def to_html(values)
      return if values.length == 0

      html = values.map do | throws |
        <<-EOHTML
          <li>
            <span class="pre"><a href="#!/api/#{throws[:type]}">#{throws[:type]}</a></span>
            <div class="sub-desc">
              <p>#{throws[:doc]}</p>
            </div>
          </li>
        EOHTML
      end.join

      return <<-EOHTML
        <h3>Throws</h3>
        <ul>
          #{html}
        </ul>
      EOHTML
    end

  end
end
+71 −0
Original line number Diff line number Diff line
require "jsduck/aggregator"
require "jsduck/source_file"

describe JsDuck::Aggregator do
  def parse(string)
    agr = JsDuck::Aggregator.new
    agr.aggregate(JsDuck::SourceFile.new(string))
    agr.result
  end

  describe "@throws with type and description" do
    before do
      @doc = parse(<<-EOS)[0]
        /**
         * Some function
         * @throws {Error} Some text
         * on multiple lines.
         */
        function bar() {}
      EOS
    end

    it "detects one throws tag" do
      @doc[:meta][:throws].length.should == 1
    end

    it "detects type of exception" do
      @doc[:meta][:throws][0][:type].should == "Error"
    end

    it "detects description" do
      @doc[:meta][:throws][0][:doc].should == "Some text\non multiple lines."
    end
  end

  describe "@throws without type" do
    before do
      @doc = parse(<<-EOS)[0]
        /**
         * @throws Some description
         */
        function bar() {}
      EOS
    end

    it "detects type as Object" do
      @doc[:meta][:throws][0][:type].should == "Object"
    end

    it "detects description" do
      @doc[:meta][:throws][0][:doc].should == "Some description"
    end
  end

  describe "multiple @throws" do
    before do
      @doc = parse(<<-EOS)[0]
        /**
         * @throws {Error} first
         * @throws {Error} second
         */
        function bar() {}
      EOS
    end

    it "detects two throws tags" do
      @doc[:meta][:throws].length.should == 2
    end
  end

end