Skip to content
This repository
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 277 lines (249 sloc) 7.958 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 265 266 267 268 269 270 271 272 273 274 275 276
require "helper"

require 'stringio'
class ParserTest < Test::Unit::TestCase
  include Johnson::Nodes
  def setup
    @parser = Johnson::Parser
  end

  def test_parser_init
    assert @parser
    tree = @parser.parse(StringIO.new(""))
    assert_kind_of SourceElements, tree
    assert_equal 0, tree.line
    assert_equal 0, tree.column
    assert_sexp([], tree)
  end

  def test_exception_raised
    assert_raises(Johnson::Parser::SyntaxError) {
      @parser.parse('if(')
    }
  end

  def test_exception_has_info
    begin
      @parser.parse('if(')
    rescue Johnson::Parser::SyntaxError => ex
      assert_equal(1, ex.line_number)
      assert_equal('', ex.file_name)
      assert_equal('syntax error', ex.message)
    end
  end

  def test_exception_has_filename
    begin
      @parser.parse('if(', 'test.js')
    rescue Johnson::Parser::SyntaxError => ex
      assert_equal(1, ex.line_number)
      assert_equal('test.js', ex.file_name)
      assert_equal('syntax error', ex.message)
    end
  end

  def test_exception_has_linenum
    begin
      @parser.parse('if(', 'test.js', 10)
    rescue Johnson::Parser::SyntaxError => ex
      assert_equal(10, ex.line_number)
      assert_equal('test.js', ex.file_name)
      assert_equal('syntax error', ex.message)
    end
  end

  def test_variable_declaration_no_init
    assert_sexp(
      [[:var, [[:name, 'foo']]]],
      @parser.parse('var foo;')
    )
    assert_ecma('var foo;', @parser.parse('var foo;'))
  end

  def test_new_foo
    assert_sexp([[:var, [[:assign, [:name, "a"], [:new, [[:name, "foo"]]]]]]],
                      @parser.parse('var a = new foo;'))
    assert_sexp([[:new, [[:name, "foo"]]]],
                      @parser.parse('new foo;'))
  end

  def test_function_call
    assert_sexp([[:bracket_access,
                [:function_call, [[:name, "bar"]]],
                [:lit, 1]]],
                      @parser.parse('bar()[1];'))
    assert_ecma('bar()[1];', @parser.parse('bar()[1]'))
  end

  def test_postfix_inc
    assert_sexp([[:postfix_inc, [:name, "x"]]],
                  @parser.parse('x++;'))
    assert_ecma('x++;', @parser.parse('x++;'))
    assert_sexp([[:postfix_inc, [:dot_accessor, [:name, "x"], [:name, "foo"]]]],
                  @parser.parse('foo.x++;'))
    assert_ecma('foo.x++;', @parser.parse('foo.x++;'))
    assert_sexp([[:postfix_inc, [:bracket_access, [:name, "x"], [:lit, 1]]]],
                  @parser.parse('x[1]++;'))
    assert_ecma('x[1]++;', @parser.parse('x[1]++;'))
  end

  def test_prefix_inc
    assert_sexp([[:prefix_inc, [:name, "x"]]],
                  @parser.parse('++x;'))
    assert_ecma('++x;', @parser.parse('++x;'))
    assert_sexp([[:prefix_inc, [:dot_accessor, [:name, "x"], [:name, "foo"]]]],
                  @parser.parse('++foo.x;'))
    assert_ecma('++foo.x;', @parser.parse('++foo.x;'))
    assert_sexp([[:prefix_inc, [:bracket_access, [:name, "x"], [:lit, 1]]]],
                  @parser.parse('++x[1];'))
    assert_ecma('++x[1];', @parser.parse('++x[1];'))
  end

  def test_postfix_dec
    assert_sexp([[:postfix_dec, [:name, "x"]]],
                  @parser.parse('x--;'))
    assert_ecma('x--;', @parser.parse('x--;'))
    assert_sexp([[:postfix_dec, [:dot_accessor, [:name, "x"], [:name, "foo"]]]],
                  @parser.parse('foo.x--;'))
    assert_ecma('foo.x--;', @parser.parse('foo.x--;'))
    assert_sexp([[:postfix_dec, [:bracket_access, [:name, "x"], [:lit, 1]]]],
                  @parser.parse('x[1]--;'))
    assert_ecma('x[1]--;', @parser.parse('x[1]--;'))
  end

  def test_prefix_dec
    assert_sexp([[:prefix_dec, [:name, "x"]]],
                  @parser.parse('--x;'))
    assert_ecma('--x;', @parser.parse('--x;'))
    assert_sexp([[:prefix_dec, [:dot_accessor, [:name, "x"], [:name, "foo"]]]],
                  @parser.parse('--foo.x;'))
    assert_ecma('--foo.x;', @parser.parse('--foo.x;'))
    assert_sexp([[:prefix_dec, [:bracket_access, [:name, "x"], [:lit, 1]]]],
                  @parser.parse('--x[1];'))
    assert_ecma('--x[1];', @parser.parse('--x[1];'))
  end

  def test_expr_comma
    assert_sexp([[:comma,
                [[:op_equal, [:name, 'i'], [:lit, 10]],
                [:op_equal, [:name, 'j'], [:lit, 11]]]]],
                @parser.parse('i = 10, j = 11;')
               )
    assert_ecma('i = 10, j = 11;', @parser.parse('i = 10, j = 11'))
  end

  def test_primary_expr_paren
    assert_sexp(
      [[:var,
        [[:assign, [:name, "a"], [:paren, [:lit, 10]]]]
      ]],
      @parser.parse('var a = (10);'))
    assert_ecma('var a = (10);', @parser.parse('var a = (10);'))
  end

  def test_parser_var_ints
    assert @parser
    tree = @parser.parse(StringIO.new("var foo = 10, bar = 1;"))
    assert_kind_of SourceElements, tree
    assert_kind_of VarStatement, tree.value.first
    assert_sexp(
      [[:var,
        [
          [:assign, [:name, "foo"], [:lit, 10]],
          [:assign, [:name, "bar"], [:lit, 1]],
      ]]],
      tree
    )
    assert_ecma('var foo = 10, bar = 1;',
                @parser.parse('var foo = 10, bar = 1'))
  end

  def test_parser_var_string_lit
    assert @parser
    tree = @parser.parse(StringIO.new("var foo = 'hello world';"))
    assert_sexp(
      [[:var,
        [
          [:assign, [:name, "foo"], [:str, 'hello world']],
      ]]],
      tree
    )
    assert_ecma('var foo = "hello world";',
                @parser.parse('var foo = "hello world"'))
  end

  def test_accessor_as_square_brace
    assert_ecma("foo['hello-world'];",
                @parser.parse("foo['hello-world']"))
  end

  def test_paren_around_function
    assert_ecma("(function() { }).foo();",
                @parser.parse('(function() { }).foo()'))
    assert_ecma("(function() { })();",
                @parser.parse('(function() { })()'))
  end

  def test_parser_var_nil_lit
    assert @parser
    tree = @parser.parse(StringIO.new("var foo = null;"))
    assert_sexp(
      [[:var,
        [
          [:assign, [:name, "foo"], [:nil]],
      ]]],
      tree
    )
    assert_ecma('var foo = null;', @parser.parse('var foo = null'))
  end

  def test_parser_var_true_lit
    assert @parser
    tree = @parser.parse(StringIO.new("var foo = true;"))
    assert_sexp(
      [[:var,
        [
          [:assign, [:name, "foo"], [:true]],
      ]]],
      tree
    )
    assert_ecma('var foo = true;', @parser.parse('var foo = true'))
  end

  def test_parser_var_false_lit
    assert @parser
    tree = @parser.parse(StringIO.new("var foo = false;"))
    assert_sexp(
      [[:var,
        [
          [:assign, [:name, "foo"], [:false]],
      ]]],
      tree
    )
    assert_ecma('var foo = false;', @parser.parse('var foo = false'))
  end

  def test_parser_var_this_lit
    assert @parser
    tree = @parser.parse(StringIO.new("var foo = this;"))
    assert_sexp(
      [[:var,
        [
          [:assign, [:name, "foo"], [:this]],
      ]]],
      tree
    )
    assert_ecma('var foo = this;', @parser.parse('var foo = this'))
  end

  def test_parser_var_regex_lit
    assert @parser
    tree = @parser.parse(StringIO.new("var foo = /abc/;"))
    assert_sexp(
      [[:var,
        [
          [:assign, [:name, "foo"], [:lit, "/abc/"]],
      ]]],
      tree
    )
    assert_ecma('var foo = /abc/;', @parser.parse('var foo = /abc/'))
  end

  def test_parser_var_float
    assert @parser
    tree = @parser.parse(StringIO.new("var foo = 10, bar = 1.1;"))
    assert_kind_of SourceElements, tree
    assert_kind_of VarStatement, tree.value.first
    assert_sexp(
      [[:var,
        [
          [:assign, [:name, "foo"], [:lit, 10]],
          [:assign, [:name, "bar"], [:lit, 1.1]],
      ]]],
      tree
    )
    assert_ecma('var foo = 10, bar = 1.1;',
                @parser.parse('var foo = 10, bar = 1.1'))
  end

  def assert_sexp(expected, node)
    assert_equal(expected, node.to_sexp)
  end

  def assert_ecma(expected, node)
    assert_equal(expected, node.to_ecma)
  end
end
Something went wrong with that request. Please try again.