Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 265 lines (218 sloc) 5.864 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
require "jsduck/js_parser"

describe JsDuck::JsParser do

  def parse(input)
    JsDuck::JsParser.new(input).parse
  end

  describe "parsing two comments" do
    before do
      @docs = parse(<<-EOS)
/* Hello world
*/

// Another
EOS
    end

    it "detects 1-based line number of comment on first line" do
      @docs[0][:linenr].should == 1
    end

    it "detects line number of second comment on 4th line" do
      @docs[1][:linenr].should == 4
    end
  end

  describe "parsing line comment" do
    before do
      @docs = parse("// Hello world")
    end

    it "results in plain comment" do
      @docs[0][:type].should == :plain_comment
    end
  end

  describe "parsing block comment" do
    before do
      @docs = parse("/* Hello world */")
    end

    it "results in plain comment" do
      @docs[0][:type].should == :plain_comment
    end

    it "doesn't strip anything from the beginning of comment" do
      @docs[0][:comment].should == " Hello world "
    end
  end

  describe "parsing block comment beginning with /**" do
    before do
      @docs = parse("/** Hello world */")
    end

    it "results in doc comment" do
      @docs[0][:type].should == :doc_comment
    end

    it "strips * at the beginning of comment" do
      @docs[0][:comment].should == " Hello world "
    end
  end

  describe "parsing comment after function" do
    before do
      @docs = parse(<<-EOS)
function a() {
}
// Function A
EOS
    end

    it "detects no code associated with comment" do
      @docs[0][:code].should == nil
    end
  end

  describe "parsing two comments each before function" do
    before do
      @docs = parse(<<-EOS)
// Function A
function a() {
}
// Function B
function b() {
}
EOS
    end

    it "finds two comments" do
      @docs.length.should == 2
    end

    it "detects first comment as belonging to first function" do
      @docs[0][:comment].should == " Function A"
      @docs[0][:code]["type"].should == "FunctionDeclaration"
      @docs[0][:code]["id"]["name"].should == "a"
    end

    it "detects second comment as belonging to second function" do
      @docs[1][:comment].should == " Function B"
      @docs[1][:code]["type"].should == "FunctionDeclaration"
      @docs[1][:code]["id"]["name"].should == "b"
    end
  end

  describe "parsing two block comments before one function" do
    before do
      @docs = parse(<<-EOS)
/* Function A */
/* Function B */
function b() {
}
EOS
    end

    it "finds two comments" do
      @docs.length.should == 2
    end

    it "detects no code associated with first comment" do
      @docs[0][:code].should == nil
    end

    it "detects second comment as belonging to the function" do
      @docs[1][:code]["type"].should == "FunctionDeclaration"
    end
  end

  shared_examples_for "three comments merged" do
    it "finds one comment" do
      @docs.length.should == 1
    end

    it "merges all the line-comments together" do
      @docs[0][:comment].should == " Very\n Long\n Comment"
    end

    it "detects the whole comment as belonging to the function" do
      @docs[0][:code]["type"].should == "FunctionDeclaration"
    end
  end

  describe "parsing three line comments before one function" do
    before do
      @docs = parse(<<-EOS)
// Very
// Long
// Comment
function b() {
}
EOS
    end

    it_should_behave_like "three comments merged"
  end

  describe "parsing three separated line comments before one function" do
    before do
      @docs = parse(<<-EOS)
// Three

// Separate


// Comments
function b() {
}
EOS
    end

    it "gets treated as three separate comments" do
      @docs.length.should == 3
    end
  end

  describe "parsing 2 x two line comments before one function" do
    before do
      @docs = parse(<<-EOS)
// First
// Comment for A
function a() {
}
// Second
// Comment for B
function b() {
}
EOS
    end

    it "finds two comments" do
      @docs.length.should == 2
    end

    it "merges first two line-comments together" do
      @docs[0][:comment].should == " First\n Comment for A"
    end

    it "merges second two line-comments together" do
      @docs[1][:comment].should == " Second\n Comment for B"
    end
  end

  describe "parsing a comment before inner function" do
    before do
      @docs = parse(<<-EOS)
function x() {
// Function A
function a() {
}
}
EOS
    end

    it "detects comment as belonging to the inner function" do
      @docs[0][:code]["type"].should == "FunctionDeclaration"
      @docs[0][:code]["id"]["name"].should == "a"
    end
  end

  describe "parsing heavily nested comment" do
    before do
      @docs = parse(<<-EOS)
(function () {
if (true) {
} else {
var i;
for (i=0; i<10; i++) {
// Function A
function a() {
}
}
}
})();
EOS
    end

    it "detects comment as belonging to the inner function" do
      @docs[0][:code]["type"].should == "FunctionDeclaration"
      @docs[0][:code]["id"]["name"].should == "a"
    end
  end

  describe "parsing comment before object property" do
    before do
      @docs = parse(<<-EOS)
var x = {
foo: 5,
// Some docs
bar: 5
}
EOS
    end

    it "detects comment as belonging to the second property" do
      @docs[0][:code]["type"].should == "Property"
      @docs[0][:code]["key"]["name"].should == "bar"
    end
  end

end

Something went wrong with that request. Please try again.