Skip to content

Commit

Permalink
Merge pull request #5 from andrykonchin/ak/fix-regexp-parsing
Browse files Browse the repository at this point in the history
Add -r/--require option
  • Loading branch information
andrykonchin authored Dec 7, 2023
2 parents bcfe2d4 + 2e4ee06 commit e08852e
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lib/marshal-parser/cli/commands.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,15 @@ class Tokens < Dry::CLI::Command
desc "Parse a dump and print tokens. By default reads dump from the stdin."
option :file, type: :string, aliases: ["-f"], desc: "Read a dump from file with provided name"
option :evaluate, type: :string, aliases: ["-e"], desc: "Ruby expression to dump"
option :require, type: :string, aliases: ["-r"], desc: "Load the library using require. It is useful when -e is specified"
option :annotate, type: :boolean, aliases: ["-a"], desc: "Print a table with annonated tokens"
option :hex, type: :boolean, aliases: ["-x"], desc: "Print tokens in a hexadecimal encoding"

def call(**options)
if options[:require]
require options[:require]
end

dump = \
if options[:file]
File.read(options[:file])
Expand Down Expand Up @@ -42,6 +47,7 @@ class AST < Dry::CLI::Command
desc "Parse a dump and print AST. By default reads dump from the stdin and uses S-expressions format."
option :file, type: :string, aliases: ["-f"], desc: "Read a dump from file with provided name"
option :evaluate, type: :string, aliases: ["-e"], desc: "Ruby expression to dump"
option :require, type: :string, aliases: ["-r"], desc: "Load the library using require. It is useful when -e is specified"
option :"only-tokens", type: :boolean, aliases: ["-o"], desc: "Print only tokens"
option :annotate, type: :boolean, aliases: ["-a"], desc: "Print annotations"
option :width, type: :string, aliases: ["-w"],
Expand All @@ -50,6 +56,10 @@ class AST < Dry::CLI::Command
option :compact, type: :boolean, aliases: ["-c"], desc: "Don't print node attributes"

def call(**options)
if options[:require]
require options[:require]
end

dump = \
if options[:file]
File.read(options[:file])
Expand Down
48 changes: 48 additions & 0 deletions spec/integration/cli_options_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,27 @@
end
end

context "--require option" do
it "requires a specified source file" do
path = "./spec/integration/fixtures/a.rb"

command = "ruby -Ilib bin/marshal-cli tokens --require #{path} --evaluate ':symbol'"
expect(`#{command}`).to eql(<<~'EOF'.b)
Hello from a.rb
"\x04\b" : "\v" symbol
EOF
end

it "requires a specified file before evaluating code snippet when --evaluate option provided" do
path = "./spec/integration/fixtures/b.rb"

command = "ruby -Ilib bin/marshal-cli tokens --require #{path} --evaluate SYMBOL"
expect(`#{command}`).to eql(<<~'EOF'.b)
"\x04\b" : "\v" symbol
EOF
end
end

context "--annotate option" do
it "prints tokens as a table and provides description for every token" do
command = 'ruby -e "puts Marshal.dump(:symbol)" | ruby -Ilib bin/marshal-cli tokens --annotate'
Expand Down Expand Up @@ -59,6 +80,7 @@
Options:
--file=VALUE, -f VALUE # Read a dump from file with provided name
--evaluate=VALUE, -e VALUE # Ruby expression to dump
--require=VALUE, -r VALUE # Load the library using require. It is useful when -e is specified
--[no-]annotate, -a # Print a table with annonated tokens
--[no-]hex, -x # Print tokens in a hexadecimal encoding
--help, -h # Print this help
Expand Down Expand Up @@ -107,6 +129,31 @@
end
end

context "--require option" do
it "requires a specified source file" do
path = "./spec/integration/fixtures/a.rb"

command = "ruby -Ilib bin/marshal-cli ast --require #{path} --evaluate ':symbol'"
expect(`#{command}`).to eql(<<~EOF)
Hello from a.rb
(symbol
(length 6)
(content "symbol"))
EOF
end

it "requires a specified file before evaluating code snippet when --evaluate option provided" do
path = "./spec/integration/fixtures/b.rb"

command = "ruby -Ilib bin/marshal-cli ast --require #{path} --evaluate SYMBOL"
expect(`#{command}`).to eql(<<~EOF)
(symbol
(length 6)
(content "symbol"))
EOF
end
end

context "--only-tokens" do
it "prints tokens instead of S-expression" do
command = 'ruby -e "puts Marshal.dump([1, true, :symbol])" | ruby -Ilib bin/marshal-cli ast --only-tokens'
Expand Down Expand Up @@ -220,6 +267,7 @@
Options:
--file=VALUE, -f VALUE # Read a dump from file with provided name
--evaluate=VALUE, -e VALUE # Ruby expression to dump
--require=VALUE, -r VALUE # Load the library using require. It is useful when -e is specified
--[no-]only-tokens, -o # Print only tokens
--[no-]annotate, -a # Print annotations
--width=VALUE, -w VALUE # Width of the column with AST, used with --annotate
Expand Down
1 change: 1 addition & 0 deletions spec/integration/fixtures/a.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
puts "Hello from a.rb"
1 change: 1 addition & 0 deletions spec/integration/fixtures/b.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SYMBOL = :symbol

0 comments on commit e08852e

Please sign in to comment.