ruby 添加context-block jruby-parser示例后。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ruby 添加context-block jruby-parser示例后。相关的知识,希望对你有一定的参考价值。

require_relative '../helpers'

describe Parser do
  VERSIONS.each do |v|
    if v == 2.0
      it "test 2.0" do
        parse("def foo(a:1); p a; end", v)
      end
    end
    if v != 1.8
      it "parses a simple multiple assignment [#{v}]" do
        parse("a,b,c = 1,2,3", v).find_node(:multipleasgn).tap do |masgn|
          masgn.should have_static_assignments([[:a, 1], [:b, 2], [:c, 3]])
        end
      end

      context "left-hand side splat multiple assignment [#{v}]" do
        it "parses a splat as the last assignment" do
          parse("a,*b = 1,2,3", v).find_node(:multipleasgn).tap do |masgn|
            masgn.should have_static_assignments([[:a, 1], [:b, [2, 3]]])
          end
          parse("a,b,*c = 1,2,3,4", v).find_node(:multipleasgn).tap do |masgn|
            masgn.should have_static_assignments([[:a, 1], [:b, 2], [:c, [3,4]]])
          end
        end

        it "parses a splat as the first assignment" do
          parse("*a,b = 1,2,3", v).find_node(:multipleasgn).tap do |masgn|
            masgn.should have_static_assignments([[:a, [1, 2]], [:b, 3]])
          end
          parse("*a,b,c = 1,2,3,4", v).find_node(:multipleasgn).tap do |masgn|
            masgn.should have_static_assignments([[:a, [1, 2]], [:b, 3], [:c, 4]])
          end
        end

        it "parses a splat as the middle assignment" do
          parse("a,*b, c = 1,2,3,4", v).find_node(:multipleasgn).tap do |masgn|
            masgn.should have_static_assignments([[:a, 1], [:b, [2, 3]], [:c, 4]])
          end
          parse("a, b, *c, d = 1,2,3,4,5", v).find_node(:multipleasgn).tap do |masgn|
            masgn.should have_static_assignments([[:a, 1], [:b, 2], [:c, [3, 4]], [:d, 5]])
          end
          parse("a, *b, c, d = 1,2,3,4,5", v).find_node(:multipleasgn).tap do |masgn|
            masgn.should have_static_assignments([[:a, 1], [:b, [2, 3]], [:c, 4], [:d, 5]])
          end
        end

        it "parses a splat that should recieve no assignment" do
          parse("a,*b,c,d = 1,2,3,4", v).find_node(:multipleasgn).tap do |masgn|
            masgn.should have_static_assignments([[:a, 1], [:b, []], [:c, 2], [:d, 3]])
          end
        end
      end

      it "parses a simple rhs splat multiple assignment [#{v}]" do
        ast = parse("a,*b = 1,*foo", v)
        foo = ast.find_node(:vcall)
        ast.find_node(:multipleasgn).tap do |masgn|
          masgn.should have_static_assignments([[:a, 1], [:b, foo]])
        end
      end

      it "parses a simple rhs splat multiple assignment [#{v}]" do
        ast = parse("*a,b = *foo,1", v)
        splatted_foo = ast.find_node(:splat)
        ast.find_node(:multipleasgn).tap do |masgn|
          masgn.should have_static_assignments([[:a, splatted_foo], [:b, 1]])
        end
      end
    end

    it "method: can parse an empty body method with unused param [#{v}]" do
      parse("def foo(a)\nend").find_node(:defn) do |defn|
        defn.args.get_normative_parameter_name_list(true).each do |parameter|
          defn.is_parameter_used(parameter).should == false
        end
      end
    end

    it "method: Can detect simple parameter is used" do
      parse("def foo(a); a; end").find_node(:defn) do |defn|
        defn.args.get_normative_parameter_name_list(true).each do |parameter|
          defn.is_parameter_used(parameter).should == true
        end
      end

      parse("def foo(a,b); a; b; end").find_node(:defn) do |defn|
        defn.args.get_normative_parameter_name_list(true).each do |parameter|
          defn.is_parameter_used(parameter).should == true
        end
      end

      parse("def foo(a,b); a=1; b; end").find_node(:defn) do |defn|
        defn.is_parameter_used("a").should == false
        defn.is_parameter_used("b").should == true
      end
    end

    it "method: Can detect some simple parameters are used" do
      parse("def foo(a,b); b; end").find_node(:defn) do |defn|
        defn.is_parameter_used("a").should == false
        defn.is_parameter_used("b").should == true
      end

      parse("def foo(a,b); b if true; end").find_node(:defn) do |defn|
        defn.is_parameter_used("a").should == false
        defn.is_parameter_used("b").should == true
      end

      parse("def foo(a,b); proc { b if true }; end").find_node(:defn) do |defn|
        defn.is_parameter_used("a").should == false
        defn.is_parameter_used("b").should == true
      end

      parse("def foo a, b, c\nputs a, b, c\nend").find_node(:defn) do |defn|
        defn.is_parameter_used("a").should == true
        defn.is_parameter_used("b").should == true
        defn.is_parameter_used("c").should == true
      end

      parse("def foo(a, b); b.each_answer {|n| data if n == a }; end") do |defn|
        defn.is_parameter_used("a").should == true
        defn.is_parameter_used("b").should == true
      end
    end

    it "method: Can detect some simple optarg params are used" do
      parse("def foo(a,b = {}); b; end").find_node(:defn) do |defn|
        defn.is_parameter_used("a").should == false
        defn.is_parameter_used("b").should == true
      end
    end

    it "method: Can detect zsuper usage" do
      parse("def foo(a,b = {}); super; end").find_node(:defn) do |defn|
        defn.is_parameter_used("a").should == true
        defn.is_parameter_used("b").should == true
      end
    end

    it "block/iter: can parse an empty proc body with unused param [#{v}]" do
      parse("proc do |a|\nend").find_node(:iter) do |iter|
        iter.is_parameter_used("a").should == false
      end
    end

    it "block/iter: Can detect simple parameter is used" do
      parse("proc { |a, b| a; b }").find_node(:iter) do |iter|
        iter.is_parameter_used("a").should == true
        iter.is_parameter_used("b").should == true
      end
    end

    it "block/iter: Can detect simple parameter is used" do
      parse("proc { |a, b| a=1; b }").find_node(:iter) do |iter|
        iter.is_parameter_used("a").should == false
        iter.is_parameter_used("b").should == true
      end
    end

    it "block/iter: Can detect some simple parameters are used" do
      parse("proc {|a,b| b }").find_node(:iter) do |iter|
        iter.is_parameter_used("a").should == false
        iter.is_parameter_used("b").should == true
      end

      parse("proc {|a,b| b if true }").find_node(:iter) do |iter|
        iter.is_parameter_used("a").should == false
        iter.is_parameter_used("b").should == true
      end

      parse("proc {|a,b| proc { b if true } }").find_node(:iter) do |iter|
        iter.is_parameter_used("a").should == false
        iter.is_parameter_used("b").should == true
      end

      parse("proc {|a, b, c| puts a, b, c }").find_node(:iter) do |iter|
        iter.is_parameter_used("a").should == true
        iter.is_parameter_used("b").should == true
        iter.is_parameter_used("c").should == true
      end

      parse("proc {|a, b| b.each_answer {|n| data if n == a } }") do |iter|
        iter.is_parameter_used("a").should == true
        iter.is_parameter_used("b").should == true
      end
    end
  end
end

以上是关于ruby 添加context-block jruby-parser示例后。的主要内容,如果未能解决你的问题,请参考以下文章

ruby 在jruby-parser中添加context-blocks示例之前和之后。

ruby 查找Ruby数组中添加到给定数字的所有数字对

ruby 查找Ruby数组中添加到给定数字的所有数字对

Ruby 之父:Ruby3 有望 3 倍提速,不添加类型推断

使用 Ruby 在文件前添加一行

Ruby:如何自动添加“# encoding: UTF-8”?