[前][次][番号順一覧][スレッド一覧]

ruby-changes:73068

From: Hiroshi <ko1@a...>
Date: Fri, 26 Aug 2022 12:16:10 +0900 (JST)
Subject: [ruby-changes:73068] 0d9f4ea0d4 (master): Import spec examples from ruby/syntax_suggest

https://git.ruby-lang.org/ruby.git/commit/?id=0d9f4ea0d4

From 0d9f4ea0d45f6577a4a13f898e981958a1f039c6 Mon Sep 17 00:00:00 2001
From: Hiroshi SHIBATA <hsbt@r...>
Date: Fri, 19 Aug 2022 15:37:45 +0900
Subject: Import spec examples from ruby/syntax_suggest

---
 .../fixtures/derailed_require_tree.rb.txt          |   74 +
 spec/syntax_suggest/fixtures/rexe.rb.txt           |  569 ++
 spec/syntax_suggest/fixtures/routes.rb.txt         |  121 +
 spec/syntax_suggest/fixtures/ruby_buildpack.rb.txt | 1344 +++
 spec/syntax_suggest/fixtures/syntax_tree.rb.txt    | 9234 ++++++++++++++++++++
 .../fixtures/this_project_extra_def.rb.txt         |   64 +
 spec/syntax_suggest/fixtures/webmock.rb.txt        |   35 +
 spec/syntax_suggest/integration/exe_cli_spec.rb    |   22 +
 .../integration/ruby_command_line_spec.rb          |  151 +
 .../integration/syntax_suggest_spec.rb             |  211 +
 spec/syntax_suggest/spec_helper.rb                 |   90 +
 spec/syntax_suggest/unit/api_spec.rb               |   83 +
 spec/syntax_suggest/unit/around_block_scan_spec.rb |  165 +
 spec/syntax_suggest/unit/block_expand_spec.rb      |  200 +
 .../unit/capture_code_context_spec.rb              |  202 +
 spec/syntax_suggest/unit/clean_document_spec.rb    |  259 +
 spec/syntax_suggest/unit/cli_spec.rb               |  224 +
 spec/syntax_suggest/unit/code_block_spec.rb        |   77 +
 spec/syntax_suggest/unit/code_frontier_spec.rb     |  135 +
 spec/syntax_suggest/unit/code_line_spec.rb         |  164 +
 spec/syntax_suggest/unit/code_search_spec.rb       |  505 ++
 .../unit/display_invalid_blocks_spec.rb            |  172 +
 spec/syntax_suggest/unit/explain_syntax_spec.rb    |  255 +
 spec/syntax_suggest/unit/lex_all_spec.rb           |   29 +
 .../unit/pathname_from_message_spec.rb             |   56 +
 spec/syntax_suggest/unit/priority_queue_spec.rb    |   95 +
 26 files changed, 14536 insertions(+)
 create mode 100644 spec/syntax_suggest/fixtures/derailed_require_tree.rb.txt
 create mode 100755 spec/syntax_suggest/fixtures/rexe.rb.txt
 create mode 100644 spec/syntax_suggest/fixtures/routes.rb.txt
 create mode 100644 spec/syntax_suggest/fixtures/ruby_buildpack.rb.txt
 create mode 100644 spec/syntax_suggest/fixtures/syntax_tree.rb.txt
 create mode 100644 spec/syntax_suggest/fixtures/this_project_extra_def.rb.txt
 create mode 100644 spec/syntax_suggest/fixtures/webmock.rb.txt
 create mode 100644 spec/syntax_suggest/integration/exe_cli_spec.rb
 create mode 100644 spec/syntax_suggest/integration/ruby_command_line_spec.rb
 create mode 100644 spec/syntax_suggest/integration/syntax_suggest_spec.rb
 create mode 100644 spec/syntax_suggest/spec_helper.rb
 create mode 100644 spec/syntax_suggest/unit/api_spec.rb
 create mode 100644 spec/syntax_suggest/unit/around_block_scan_spec.rb
 create mode 100644 spec/syntax_suggest/unit/block_expand_spec.rb
 create mode 100644 spec/syntax_suggest/unit/capture_code_context_spec.rb
 create mode 100644 spec/syntax_suggest/unit/clean_document_spec.rb
 create mode 100644 spec/syntax_suggest/unit/cli_spec.rb
 create mode 100644 spec/syntax_suggest/unit/code_block_spec.rb
 create mode 100644 spec/syntax_suggest/unit/code_frontier_spec.rb
 create mode 100644 spec/syntax_suggest/unit/code_line_spec.rb
 create mode 100644 spec/syntax_suggest/unit/code_search_spec.rb
 create mode 100644 spec/syntax_suggest/unit/display_invalid_blocks_spec.rb
 create mode 100644 spec/syntax_suggest/unit/explain_syntax_spec.rb
 create mode 100644 spec/syntax_suggest/unit/lex_all_spec.rb
 create mode 100644 spec/syntax_suggest/unit/pathname_from_message_spec.rb
 create mode 100644 spec/syntax_suggest/unit/priority_queue_spec.rb

diff --git a/spec/syntax_suggest/fixtures/derailed_require_tree.rb.txt b/spec/syntax_suggest/fixtures/derailed_require_tree.rb.txt
new file mode 100644
index 0000000000..668ac4010b
--- /dev/null
+++ b/spec/syntax_suggest/fixtures/derailed_require_tree.rb.txt
@@ -0,0 +1,74 @@ https://github.com/ruby/ruby/blob/trunk/spec/syntax_suggest/fixtures/derailed_require_tree.rb.txt#L1
+# frozen_string_literal: true
+
+# Tree structure used to store and sort require memory costs
+# RequireTree.new('get_process_mem')
+module DerailedBenchmarks
+  class RequireTree
+    REQUIRED_BY = {}
+
+    attr_reader   :name
+    attr_writer   :cost
+    attr_accessor :parent
+
+    def initialize(name)
+      @name     = name
+      @children = {}
+      @cost = 0
+
+    def self.reset!
+      REQUIRED_BY.clear
+      if defined?(Kernel::REQUIRE_STACK)
+        Kernel::REQUIRE_STACK.clear
+
+        Kernel::REQUIRE_STACK.push(TOP_REQUIRE)
+      end
+    end
+
+    def <<(tree)
+      @children[tree.name.to_s] = tree
+      tree.parent = self
+      (REQUIRED_BY[tree.name.to_s] ||= []) << self.name
+    end
+
+    def [](name)
+      @children[name.to_s]
+    end
+
+    # Returns array of child nodes
+    def children
+      @children.values
+    end
+
+    def cost
+      @cost || 0
+    end
+
+    # Returns sorted array of child nodes from Largest to Smallest
+    def sorted_children
+      children.sort { |c1, c2| c2.cost <=> c1.cost }
+    end
+
+    def to_string
+      str = String.new("#{name}: #{cost.round(4)} MiB")
+      if parent && REQUIRED_BY[self.name.to_s]
+        names = REQUIRED_BY[self.name.to_s].uniq - [parent.name.to_s]
+        if names.any?
+          str << " (Also required by: #{ names.first(2).join(", ") }"
+          str << ", and #{names.count - 2} others" if names.count > 3
+          str << ")"
+        end
+      end
+      str
+    end
+
+    # Recursively prints all child nodes
+    def print_sorted_children(level = 0, out = STDOUT)
+      return if cost < ENV['CUT_OFF'].to_f
+      out.puts "  " * level + self.to_string
+      level += 1
+      sorted_children.each do |child|
+        child.print_sorted_children(level, out)
+      end
+    end
+  end
+end
diff --git a/spec/syntax_suggest/fixtures/rexe.rb.txt b/spec/syntax_suggest/fixtures/rexe.rb.txt
new file mode 100755
index 0000000000..92e44d4d1e
--- /dev/null
+++ b/spec/syntax_suggest/fixtures/rexe.rb.txt
@@ -0,0 +1,569 @@ https://github.com/ruby/ruby/blob/trunk/spec/syntax_suggest/fixtures/rexe.rb.txt#L1
+#!/usr/bin/env ruby
+#
+# rexe - Ruby Command Line Executor Filter
+#
+# Inspired by https://github.com/thisredone/rb
+
+# frozen_string_literal: true
+
+
+require 'bundler'
+require 'date'
+require 'optparse'
+require 'ostruct'
+require 'shellwords'
+
+class Rexe
+
+  VERSION = '1.5.1'
+
+  PROJECT_URL = 'https://github.com/keithrbennett/rexe'
+
+
+  module Helpers
+
+    # Try executing code. If error raised, print message (but not stack trace) & exit -1.
+    def try
+      begin
+        yield
+      rescue Exception => e
+        unless e.class == SystemExit
+          $stderr.puts("rexe: #{e}")
+          $stderr.puts("Use the -h option to get help.")
+          exit(-1)
+        end
+      end
+    end
+  end
+
+
+  class Options < Struct.new(
+      :input_filespec,
+      :input_format,
+      :input_mode,
+      :loads,
+      :output_format,
+      :output_format_tty,
+      :output_format_block,
+      :requires,
+      :log_format,
+      :noop)
+
+
+    def initialize
+      super
+      clear
+    end
+
+
+    def clear
+      self.input_filespec = nil
+      self.input_format        = :none
+      self.input_mode          = :none
+      self.output_format       = :none
+      self.output_format_tty   = :none
+      self.output_format_block = :none
+      self.loads               = []
+      self.requires            = []
+      self.log_format          = :none
+      self.noop                = false
+    end
+  end
+
+
+
+
+
+  class Lookups
+    def input_modes
+      @input_modes ||= {
+          'l' => :line,
+          'e' => :enumerator,
+          'b' => :one_big_string,
+          'n' => :none
+      }
+    end
+
+
+    def input_formats
+      @input_formats ||=  {
+          'j' => :json,
+          'm' => :marshal,
+          'n' => :none,
+          'y' => :yaml,
+      }
+    end
+
+
+    def input_parsers
+      @input_parsers ||= {
+          json:    ->(string)  { JSON.parse(string) },
+          marshal: ->(string)  { Marshal.load(string) },
+          none:    ->(string)  { string },
+          yaml:    ->(string)  { YAML.load(string) },
+      }
+    end
+
+
+    def output_formats
+      @output_formats ||= {
+          'a' => :amazing_print,
+          'i' => :inspect,
+          'j' => :json,
+          'J' => :pretty_json,
+          'm' => :marshal,
+          'n' => :none,
+          'p' => :puts,         # default
+          'P' => :pretty_print,
+          's' => :to_s,
+          'y' => :yaml,
+      }
+    end
+
+
+    def formatters
+      @formatters ||=  {
+          amazing_print: ->(obj)  { obj.ai + "\n" },
+          inspect:       ->(obj)  { obj.inspect + "\n" },
+          json:          ->(obj)  { obj.to_json },
+          marshal:       ->(obj)  { Marshal.dump(obj) },
+          none:          ->(_obj) { nil },
+          pretty_json:   ->(obj)  { JSON.pretty_generate(obj) },
+          pretty_print:  ->(obj)  { obj.pretty_inspect },
+          puts:          ->(obj)  { require 'stringio'; sio = StringIO.new; sio.puts(obj); sio.string },
+          to_s:          ->(obj)  { obj.to_s + "\n" },
+          yaml:          ->(obj)  { obj.to_yaml },
+      }
+    end
+
+
+    def format_requires
+      @format_requires ||= {
+          json:          'json',
+          pretty_json:   'json',
+          amazing_print: 'amazing_print',
+          pretty_print:  'pp',
+          yaml:          'yaml'
+      }
+    end
+  end
+
+
+
+  class CommandLineParser
+
+    include Helpers
+
+    attr_reader :lookups, :options
+
+    def initialize
+      @lookups = Lookups.new
+      @options = Options.new
+    end
+
+
+    # Inserts contents of REXE_OPTIONS environment variable at the beginning of ARGV.
+    private def prepend_environment_options
+      env_opt_string = ENV['REXE_OPTIONS']
+      if env_opt_string
+        args_to_prepend = Shellwords.shells (... truncated)

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]