Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit b0cc299

Browse files
committed
Test out idempotency against shipped Ruby files
1 parent 802d647 commit b0cc299

File tree

6 files changed

+96
-63
lines changed

6 files changed

+96
-63
lines changed

lib/syntax_tree.rb

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,19 @@ def self.format(source)
294294
output.join
295295
end
296296

297+
# Returns the source from the given filepath taking into account any potential
298+
# magic encoding comments.
299+
def self.read(filepath)
300+
encoding =
301+
File.open(filepath, "r") do |file|
302+
header = file.readline
303+
header += file.readline if header.start_with?("#!")
304+
Ripper.new(header).tap(&:parse).encoding
305+
end
306+
307+
File.read(filepath, encoding: encoding)
308+
end
309+
297310
private
298311

299312
# ----------------------------------------------------------------------------
@@ -7357,7 +7370,12 @@ def on_in(pattern, statements, consequent)
73577370
beginning = find_token(Kw, "in")
73587371
ending = consequent || find_token(Kw, "end")
73597372

7360-
statements_start = find_token(Kw, "then", consume: false) || pattern
7373+
statements_start = pattern
7374+
if token = find_token(Kw, "then", consume: false)
7375+
tokens.delete(token)
7376+
statements_start = token
7377+
end
7378+
73617379
statements.bind(
73627380
find_next_statement_start(statements_start.location.end_char),
73637381
ending.location.start_char
@@ -7900,6 +7918,10 @@ def initialize(value:, location:, comments: [])
79007918
@comments = comments
79017919
end
79027920

7921+
def child_nodes
7922+
[]
7923+
end
7924+
79037925
def format(q)
79047926
q.text(value)
79057927
end
@@ -13071,7 +13093,12 @@ def on_when(arguments, statements, consequent)
1307113093
beginning = find_token(Kw, "when")
1307213094
ending = consequent || find_token(Kw, "end")
1307313095

13074-
statements_start = find_token(Kw, "then", consume: false) || arguments
13096+
statements_start = arguments
13097+
if token = find_token(Kw, "then", consume: false)
13098+
tokens.delete(token)
13099+
statements_start = token
13100+
end
13101+
1307513102
statements.bind(
1307613103
find_next_statement_start(statements_start.location.end_char),
1307713104
ending.location.start_char

lib/syntax_tree/cli.rb

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ def run(argv)
174174
patterns.each do |pattern|
175175
Dir.glob(pattern).each do |filepath|
176176
next unless File.file?(filepath)
177-
source = source_for(filepath)
177+
source = SyntaxTree.read(filepath)
178178

179179
begin
180180
action.run(filepath, source)
@@ -210,19 +210,6 @@ def run(argv)
210210

211211
private
212212

213-
# Returns the source from the given filepath taking into account any
214-
# potential magic encoding comments.
215-
def source_for(filepath)
216-
encoding =
217-
File.open(filepath, "r") do |file|
218-
header = file.readline
219-
header += file.readline if header.start_with?("#!")
220-
Ripper.new(header).tap(&:parse).encoding
221-
end
222-
223-
File.read(filepath, encoding: encoding)
224-
end
225-
226213
# Highlights a snippet from a source and parse error.
227214
def highlight_error(error, source)
228215
lines = source.lines

test/behavior_test.rb

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# frozen_string_literal: true
2+
3+
require_relative "test_helper"
4+
5+
class SyntaxTree
6+
class BehaviorTest < Minitest::Test
7+
def test_empty
8+
void_stmt = SyntaxTree.parse("").statements.body.first
9+
assert_kind_of(VoidStmt, void_stmt)
10+
end
11+
12+
def test_multibyte
13+
assign = SyntaxTree.parse("🎉 + 🎉").statements.body.first
14+
assert_equal(5, assign.location.end_char)
15+
end
16+
17+
def test_parse_error
18+
assert_raises(ParseError) { SyntaxTree.parse("<>") }
19+
end
20+
21+
def test_next_statement_start
22+
source = <<~SOURCE
23+
def method # comment
24+
expression
25+
end
26+
SOURCE
27+
28+
bodystmt = SyntaxTree.parse(source).statements.body.first.bodystmt
29+
assert_equal(20, bodystmt.location.start_char)
30+
end
31+
32+
def test_version
33+
refute_nil(VERSION)
34+
end
35+
end
36+
end

test/idempotency_test.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# frozen_string_literal: true
2+
3+
return if ENV["FAST"]
4+
require_relative "test_helper"
5+
6+
class SyntaxTree
7+
class IdempotencyTest < Minitest::Test
8+
Dir[File.join(RbConfig::CONFIG["libdir"], "**/*.rb")].each do |filepath|
9+
define_method(:"test_#{filepath}") do
10+
source = SyntaxTree.read(filepath)
11+
formatted = SyntaxTree.format(source)
12+
13+
assert_equal(formatted, SyntaxTree.format(formatted), "expected #{filepath} to be formatted idempotently")
14+
end
15+
end
16+
end
17+
end

test/syntax_tree_test.rb renamed to test/node_test.rb

Lines changed: 2 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,9 @@
11
# frozen_string_literal: true
22

3-
require "simplecov"
4-
SimpleCov.start { add_filter("prettyprint.rb") }
5-
6-
$LOAD_PATH.unshift(File.expand_path("../lib", __dir__))
7-
require "syntax_tree"
8-
9-
require "json"
10-
require "pp"
11-
require "minitest/autorun"
3+
require_relative "test_helper"
124

135
class SyntaxTree
14-
class SyntaxTreeTest < Minitest::Test
15-
# --------------------------------------------------------------------------
16-
# Tests for behavior
17-
# --------------------------------------------------------------------------
18-
19-
def test_empty
20-
void_stmt = SyntaxTree.parse("").statements.body.first
21-
assert_kind_of(VoidStmt, void_stmt)
22-
end
23-
24-
def test_multibyte
25-
assign = SyntaxTree.parse("🎉 + 🎉").statements.body.first
26-
assert_equal(5, assign.location.end_char)
27-
end
28-
29-
def test_parse_error
30-
assert_raises(ParseError) { SyntaxTree.parse("<>") }
31-
end
32-
33-
def test_next_statement_start
34-
source = <<~SOURCE
35-
def method # comment
36-
expression
37-
end
38-
SOURCE
39-
40-
bodystmt = SyntaxTree.parse(source).statements.body.first.bodystmt
41-
assert_equal(20, bodystmt.location.start_char)
42-
end
43-
44-
def test_version
45-
refute_nil(VERSION)
46-
end
47-
48-
# --------------------------------------------------------------------------
49-
# Tests for nodes
50-
# --------------------------------------------------------------------------
51-
6+
class NodeTest < Minitest::Test
527
def test_BEGIN
538
assert_node(BEGINBlock, "BEGIN", "BEGIN {}")
549
end

test/test_helper.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# frozen_string_literal: true
2+
3+
require "simplecov"
4+
SimpleCov.start { add_filter("prettyprint.rb") }
5+
6+
$LOAD_PATH.unshift(File.expand_path("../lib", __dir__))
7+
require "syntax_tree"
8+
9+
require "json"
10+
require "pp"
11+
require "minitest/autorun"

0 commit comments

Comments
 (0)