Skip to content

Commit

Permalink
Import the test fixtures
Browse files Browse the repository at this point in the history
  • Loading branch information
kddnewton committed Aug 21, 2023
1 parent 43c681e commit a77f3ae
Show file tree
Hide file tree
Showing 320 changed files with 1,719 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
test.rb
tmp
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@
source "https://rubygems.org"

gem "parser"
gem "rake"
gem "test-unit"
gem "yarp", "~> 0.8"
6 changes: 6 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,20 @@ GEM
parser (3.2.2.3)
ast (~> 2.4.1)
racc
power_assert (2.0.3)
racc (1.7.1)
rake (13.0.6)
test-unit (3.6.1)
power_assert
yarp (0.8.0)

PLATFORMS
arm64-darwin-22

DEPENDENCIES
parser
rake
test-unit
yarp (~> 0.8)

BUNDLED WITH
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# parser-yarp

An experiment in translating the YARP AST into the whitequark/parser AST.
95 changes: 95 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
# frozen_string_literal: true

require "rake/testtask"

Rake::TestTask.new(:test) do |t|
t.libs << "test"
t.libs << "lib"
t.test_files = FileList["test/**/*_test.rb"]
end

task default: :test

# This namespace's purpose is to extract the examples from the whitequark/parser
# gem and generate a test file that we can use to ensure that our parser
# generates equivalent syntax trees when translating. To do this, it runs the
# parser's test suite but overrides the `assert_parses` method to collect the
# examples into a hash. Then, it writes out the hash to a file that we can use
# to generate our own tests.
#
# To run the test suite, it's important to note that we have to mirror both any
# APIs provided to the test suite (for example the ParseHelper module below).
# This is obviously relatively brittle, but it's effective for now.
namespace :whitequark do
desc "Ensure there's a local copy of whitequark/parser"
file "tmp/whitequark" do
sh "git clone --depth=1 https://github.com/whitequark/parser tmp/whitequark"
end

desc "Ensure we have a fixtures directory for the whitequark/parser tests"
directory "test/fixtures"

desc "Import the whitequark/parser tests"
task import: ["tmp/whitequark", "test/fixtures"] do
cp "tmp/whitequark/LICENSE.txt", "test/fixtures/LICENSE"

mkdir_p "tmp/whitequark/scratch"
touch "tmp/whitequark/scratch/helper.rb"
touch "tmp/whitequark/scratch/parse_helper.rb"
$:.unshift("tmp/whitequark/scratch")

require "ast"
module ParseHelper
include AST::Sexp

# This object is going to collect all of the examples from the parser gem
# into a hash that we can use to generate our own tests.
COLLECTED = Hash.new { |hash, key| hash[key] = [] }
ALL_VERSIONS = %w[3.1 3.2]

private

def assert_context(*)
end

def assert_diagnoses(*)
end

def assert_diagnoses_many(*)
end

def refute_diagnoses(*)
end

def with_versions(*)
end

def assert_parses(_ast, code, _source_maps = "", versions = ALL_VERSIONS)
# We're going to skip any examples that are for older Ruby versions
# that we do not support.
return if (versions & %w[3.1 3.2]).empty?

entry = caller.find { _1.include?("test_parser.rb") }
_, name = *entry.match(/\d+:in `(?:block in )?(?:test_|assert_parses_)?(.+)'/)

COLLECTED[name] << code
end
end

require "parser/current"
require "minitest/autorun"
require_relative "tmp/whitequark/test/test_parser"

Minitest.after_run do
ParseHelper::COLLECTED.each do |(name, codes)|
File.write("test/fixtures/#{name}.rb", "#{codes.sort.join("\n\n")}\n")
end
end
end

desc "Clean up tmp files related to whitequark/parser"
task :clean do
rm_rf "tmp/whitequark"
rm_rf "test/fixtures"
end
end
File renamed without changes.
25 changes: 25 additions & 0 deletions test/fixtures/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
Copyright (c) 2013-2016 whitequark <[email protected]>

Parts of the source are derived from ruby_parser:
Copyright (c) Ryan Davis, seattle.rb

MIT License

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
1 change: 1 addition & 0 deletions test/fixtures/__ENCODING__.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__ENCODING__
1 change: 1 addition & 0 deletions test/fixtures/__ENCODING___legacy_.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__ENCODING__
1 change: 1 addition & 0 deletions test/fixtures/alias.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
alias :foo bar
3 changes: 3 additions & 0 deletions test/fixtures/alias_gvar.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
alias $a $+

alias $a $b
1 change: 1 addition & 0 deletions test/fixtures/ambiuous_quoted_label_in_ternary_operator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a ? b & '': nil
3 changes: 3 additions & 0 deletions test/fixtures/and.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
foo && bar

foo and bar
3 changes: 3 additions & 0 deletions test/fixtures/and_asgn.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
foo.a &&= 1

foo[0, 1] &&= 2
3 changes: 3 additions & 0 deletions test/fixtures/and_or_masgn.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
foo && (a, b = bar)

foo || (a, b = bar)
1 change: 1 addition & 0 deletions test/fixtures/anonymous_blockarg.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
def foo(&); bar(&); end
3 changes: 3 additions & 0 deletions test/fixtures/arg.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
def f(foo); end

def f(foo, bar); end
3 changes: 3 additions & 0 deletions test/fixtures/arg_duplicate_ignored.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
def foo(_, _); end

def foo(_a, _a); end
6 changes: 6 additions & 0 deletions test/fixtures/arg_label.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
def foo
a:b end

def foo() a:b end

f { || a:b }
1 change: 1 addition & 0 deletions test/fixtures/arg_scope.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
lambda{|;a|a}
63 changes: 63 additions & 0 deletions test/fixtures/args.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
def f &b; end

def f (((a))); end

def f ((*)); end

def f ((*, p)); end

def f ((*r)); end

def f ((*r, p)); end

def f ((a, *)); end

def f ((a, *, p)); end

def f ((a, *r)); end

def f ((a, *r, p)); end

def f ((a, a1)); end

def f (foo: 1, &b); end

def f (foo: 1, bar: 2, **baz, &b); end

def f **baz, &b; end

def f *, **; end

def f *r, &b; end

def f *r, p, &b; end

def f ; end

def f a, &b; end

def f a, *r, &b; end

def f a, *r, p, &b; end

def f a, o=1, &b; end

def f a, o=1, *r, &b; end

def f a, o=1, *r, p, &b; end

def f a, o=1, p, &b; end

def f foo:
; end

def f foo: -1
; end

def f o=1, &b; end

def f o=1, *r, &b; end

def f o=1, *r, p, &b; end

def f o=1, p, &b; end
3 changes: 3 additions & 0 deletions test/fixtures/args_args_assocs.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fun(foo, :foo => 1)

fun(foo, :foo => 1, &baz)
1 change: 1 addition & 0 deletions test/fixtures/args_args_assocs_comma.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
foo[bar, :baz => 1,]
1 change: 1 addition & 0 deletions test/fixtures/args_args_comma.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
foo[bar,]
3 changes: 3 additions & 0 deletions test/fixtures/args_args_star.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fun(foo, *bar)

fun(foo, *bar, &baz)
11 changes: 11 additions & 0 deletions test/fixtures/args_assocs.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
fun(:foo => 1)

fun(:foo => 1, &baz)

self.[]= foo, :a => 1

self[:bar => 1]

super(:foo => 42)

yield(:foo => 42)
1 change: 1 addition & 0 deletions test/fixtures/args_assocs_comma.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
foo[:baz => 1,]
11 changes: 11 additions & 0 deletions test/fixtures/args_assocs_legacy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
fun(:foo => 1)

fun(:foo => 1, &baz)

self.[]= foo, :a => 1

self[:bar => 1]

super(:foo => 42)

yield(:foo => 42)
1 change: 1 addition & 0 deletions test/fixtures/args_block_pass.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fun(&bar)
1 change: 1 addition & 0 deletions test/fixtures/args_cmd.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fun(f bar)
3 changes: 3 additions & 0 deletions test/fixtures/args_star.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fun(*bar)

fun(*bar, &baz)
3 changes: 3 additions & 0 deletions test/fixtures/array_assocs.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[ 1 => 2 ]

[ 1, 2 => 3 ]
1 change: 1 addition & 0 deletions test/fixtures/array_plain.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[1, 2]
5 changes: 5 additions & 0 deletions test/fixtures/array_splat.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[*foo]

[1, *foo, 2]

[1, *foo]
1 change: 1 addition & 0 deletions test/fixtures/array_symbols.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
%i[foo bar]
3 changes: 3 additions & 0 deletions test/fixtures/array_symbols_empty.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
%I()

%i[]
3 changes: 3 additions & 0 deletions test/fixtures/array_symbols_interp.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
%I[foo #{bar}]

%I[foo#{bar}]
1 change: 1 addition & 0 deletions test/fixtures/array_words.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
%w[foo bar]
3 changes: 3 additions & 0 deletions test/fixtures/array_words_empty.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
%W()

%w[]
3 changes: 3 additions & 0 deletions test/fixtures/array_words_interp.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
%W[foo #{bar}]

%W[foo #{bar}foo#@baz]
3 changes: 3 additions & 0 deletions test/fixtures/asgn_cmd.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
foo = bar = m foo

foo = m foo
5 changes: 5 additions & 0 deletions test/fixtures/asgn_mrhs.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
foo = *bar

foo = bar, 1

foo = baz, *bar
1 change: 1 addition & 0 deletions test/fixtures/back_ref.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
$+
1 change: 1 addition & 0 deletions test/fixtures/bang.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
!foo
1 change: 1 addition & 0 deletions test/fixtures/bang_cmd.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
!m foo
1 change: 1 addition & 0 deletions test/fixtures/begin_cmdarg.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
p begin 1.times do 1 end end
2 changes: 2 additions & 0 deletions test/fixtures/beginless_erange_after_newline.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
foo
...100
2 changes: 2 additions & 0 deletions test/fixtures/beginless_irange_after_newline.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
foo
..100
3 changes: 3 additions & 0 deletions test/fixtures/beginless_range.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
...100

..100
1 change: 1 addition & 0 deletions test/fixtures/blockarg.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
def f(&block); end
Loading

0 comments on commit a77f3ae

Please sign in to comment.