[ruby/yarp] Hide debug methods

https://github.com/ruby/yarp/commit/aa0dc2f301
This commit is contained in:
Kevin Newton 2023-08-02 13:17:31 -04:00 коммит произвёл Takashi Kokubun
Родитель 1ea9e444ec
Коммит 820a58c228
6 изменённых файлов: 80 добавлений и 60 удалений

Просмотреть файл

@ -230,9 +230,18 @@ module YARP
Serialize.load(source, serialized)
end
def self.newlines(source)
YARP.parse(source).source.offsets
# This module is used for testing and debugging and is not meant to be used by
# consumers of this library.
module Debug
def self.newlines(source)
YARP.parse(source).source.offsets
end
end
# Marking this as private so that consumers don't see it. It makes it a little
# annoying for testing since you have to const_get it to access the methods,
# but at least this way it's clear it's not meant for consumers.
private_constant :Debug
end
require_relative "yarp/lex_compat"

Просмотреть файл

@ -4,7 +4,7 @@ require "yarp_test_helper"
class MemsizeTest < Test::Unit::TestCase
def test_memsize
result = YARP.memsize("2 + 3")
result = YARP.const_get(:Debug).memsize("2 + 3")
assert_equal 5, result[:length]
assert_kind_of Integer, result[:memsize]

Просмотреть файл

@ -108,7 +108,7 @@ class ParseTest < Test::Unit::TestCase
# Next, assert that the newlines are in the expected places.
expected_newlines = [0]
source.b.scan("\n") { expected_newlines << $~.offset(0)[0] + 1 }
assert_equal expected_newlines, YARP.newlines(source)
assert_equal expected_newlines, YARP.const_get(:Debug).newlines(source)
# Finally, assert that we can lex the source and get the same tokens as
# Ripper.

Просмотреть файл

@ -8,27 +8,27 @@ class RegexpTest < Test::Unit::TestCase
##############################################################################
def test_named_captures_with_arrows
assert_equal(["foo"], YARP.named_captures("(?<foo>bar)"))
assert_equal(["foo"], named_captures("(?<foo>bar)"))
end
def test_named_captures_with_single_quotes
assert_equal(["foo"], YARP.named_captures("(?'foo'bar)"))
assert_equal(["foo"], named_captures("(?'foo'bar)"))
end
def test_nested_named_captures_with_arrows
assert_equal(["foo", "bar"], YARP.named_captures("(?<foo>(?<bar>baz))"))
assert_equal(["foo", "bar"], named_captures("(?<foo>(?<bar>baz))"))
end
def test_nested_named_captures_with_single_quotes
assert_equal(["foo", "bar"], YARP.named_captures("(?'foo'(?'bar'baz))"))
assert_equal(["foo", "bar"], named_captures("(?'foo'(?'bar'baz))"))
end
def test_allows_duplicate_named_captures
assert_equal(["foo", "foo"], YARP.named_captures("(?<foo>bar)(?<foo>baz)"))
assert_equal(["foo", "foo"], named_captures("(?<foo>bar)(?<foo>baz)"))
end
def test_named_capture_inside_fake_range_quantifier
assert_equal(["foo"], YARP.named_captures("foo{1, (?<foo>2)}"))
assert_equal(["foo"], named_captures("foo{1, (?<foo>2)}"))
end
##############################################################################
@ -38,154 +38,160 @@ class RegexpTest < Test::Unit::TestCase
##############################################################################
def test_alternation
refute_nil(YARP.named_captures("foo|bar"))
refute_nil(named_captures("foo|bar"))
end
def test_anchors
refute_nil(YARP.named_captures("^foo$"))
refute_nil(named_captures("^foo$"))
end
def test_any
refute_nil(YARP.named_captures("."))
refute_nil(named_captures("."))
end
def test_posix_character_classes
refute_nil(YARP.named_captures("[[:digit:]]"))
refute_nil(named_captures("[[:digit:]]"))
end
def test_negated_posix_character_classes
refute_nil(YARP.named_captures("[[:^digit:]]"))
refute_nil(named_captures("[[:^digit:]]"))
end
def test_invalid_posix_character_classes_should_fall_back_to_regular_classes
refute_nil(YARP.named_captures("[[:foo]]"))
refute_nil(named_captures("[[:foo]]"))
end
def test_character_sets
refute_nil(YARP.named_captures("[abc]"))
refute_nil(named_captures("[abc]"))
end
def test_nested_character_sets
refute_nil(YARP.named_captures("[[abc]]"))
refute_nil(named_captures("[[abc]]"))
end
def test_nested_character_sets_with_operators
refute_nil(YARP.named_captures("[[abc] && [def]]"))
refute_nil(named_captures("[[abc] && [def]]"))
end
def test_named_capture_inside_nested_character_set
assert_equal([], YARP.named_captures("[foo (?<foo>bar)]"))
assert_equal([], named_captures("[foo (?<foo>bar)]"))
end
def test_negated_character_sets
refute_nil(YARP.named_captures("[^abc]"))
refute_nil(named_captures("[^abc]"))
end
def test_character_ranges
refute_nil(YARP.named_captures("[a-z]"))
refute_nil(named_captures("[a-z]"))
end
def test_negated_character_ranges
refute_nil(YARP.named_captures("[^a-z]"))
refute_nil(named_captures("[^a-z]"))
end
def test_fake_named_captures_inside_character_sets
assert_equal([], YARP.named_captures("[a-z(?<foo>)]"))
assert_equal([], named_captures("[a-z(?<foo>)]"))
end
def test_fake_named_capture_inside_character_set_with_escaped_ending
assert_equal([], YARP.named_captures("[a-z\\](?<foo>)]"))
assert_equal([], named_captures("[a-z\\](?<foo>)]"))
end
def test_comments
refute_nil(YARP.named_captures("(?#foo)"))
refute_nil(named_captures("(?#foo)"))
end
def test_comments_with_escaped_parentheses
refute_nil(YARP.named_captures("(?#foo\\)\\))"))
refute_nil(named_captures("(?#foo\\)\\))"))
end
def test_non_capturing_groups
refute_nil(YARP.named_captures("(?:foo)"))
refute_nil(named_captures("(?:foo)"))
end
def test_positive_lookaheads
refute_nil(YARP.named_captures("(?=foo)"))
refute_nil(named_captures("(?=foo)"))
end
def test_negative_lookaheads
refute_nil(YARP.named_captures("(?!foo)"))
refute_nil(named_captures("(?!foo)"))
end
def test_positive_lookbehinds
refute_nil(YARP.named_captures("(?<=foo)"))
refute_nil(named_captures("(?<=foo)"))
end
def test_negative_lookbehinds
refute_nil(YARP.named_captures("(?<!foo)"))
refute_nil(named_captures("(?<!foo)"))
end
def test_atomic_groups
refute_nil(YARP.named_captures("(?>foo)"))
refute_nil(named_captures("(?>foo)"))
end
def test_absence_operator
refute_nil(YARP.named_captures("(?~foo)"))
refute_nil(named_captures("(?~foo)"))
end
def test_conditional_expression_with_index
refute_nil(YARP.named_captures("(?(1)foo)"))
refute_nil(named_captures("(?(1)foo)"))
end
def test_conditional_expression_with_name
refute_nil(YARP.named_captures("(?(foo)bar)"))
refute_nil(named_captures("(?(foo)bar)"))
end
def test_conditional_expression_with_group
refute_nil(YARP.named_captures("(?(<foo>)bar)"))
refute_nil(named_captures("(?(<foo>)bar)"))
end
def test_options_on_groups
refute_nil(YARP.named_captures("(?imxdau:foo)"))
refute_nil(named_captures("(?imxdau:foo)"))
end
def test_options_on_groups_with_invalid_options
assert_nil(YARP.named_captures("(?z:bar)"))
assert_nil(named_captures("(?z:bar)"))
end
def test_options_on_groups_getting_turned_off
refute_nil(YARP.named_captures("(?-imx:foo)"))
refute_nil(named_captures("(?-imx:foo)"))
end
def test_options_on_groups_some_getting_turned_on_some_getting_turned_off
refute_nil(YARP.named_captures("(?im-x:foo)"))
refute_nil(named_captures("(?im-x:foo)"))
end
def test_star_quantifier
refute_nil(YARP.named_captures("foo*"))
refute_nil(named_captures("foo*"))
end
def test_plus_quantifier
refute_nil(YARP.named_captures("foo+"))
refute_nil(named_captures("foo+"))
end
def test_question_mark_quantifier
refute_nil(YARP.named_captures("foo?"))
refute_nil(named_captures("foo?"))
end
def test_endless_range_quantifier
refute_nil(YARP.named_captures("foo{1,}"))
refute_nil(named_captures("foo{1,}"))
end
def test_beginless_range_quantifier
refute_nil(YARP.named_captures("foo{,1}"))
refute_nil(named_captures("foo{,1}"))
end
def test_range_quantifier
refute_nil(YARP.named_captures("foo{1,2}"))
refute_nil(named_captures("foo{1,2}"))
end
def test_fake_range_quantifier_because_of_spaces
refute_nil(YARP.named_captures("foo{1, 2}"))
refute_nil(named_captures("foo{1, 2}"))
end
private
def named_captures(source)
YARP.const_get(:Debug).named_captures(source)
end
end

Просмотреть файл

@ -15,7 +15,7 @@ module UnescapeTest
private
def assert_unescape_none(source)
assert_equal(source, YARP.unescape_none(source))
assert_equal(source, YARP.const_get(:Debug).unescape_none(source))
end
end
@ -35,7 +35,7 @@ module UnescapeTest
private
def assert_unescape_minimal(expected, source)
assert_equal(expected, YARP.unescape_minimal(source))
assert_equal(expected, YARP.const_get(:Debug).unescape_minimal(source))
end
end
@ -83,7 +83,7 @@ module UnescapeTest
assert_unescape_all("က", "\\u1000", "UTF-8")
assert_unescape_all("", "\\u1010", "UTF-8")
assert_nil(YARP.unescape_all("\\uxxxx"))
assert_nil(unescape_all("\\uxxxx"))
end
def test_unicode_codepoints
@ -95,8 +95,8 @@ module UnescapeTest
assert_unescape_all("𐀐", "\\u{10010}", "UTF-8")
assert_unescape_all("aĀကတ𐀀𐀐", "\\u{ 61\s100\n1000\t1010\r10000\v10010 }", "UTF-8")
assert_nil(YARP.unescape_all("\\u{110000}"))
assert_nil(YARP.unescape_all("\\u{110000 110001 110002}"))
assert_nil(unescape_all("\\u{110000}"))
assert_nil(unescape_all("\\u{110000 110001 110002}"))
end
def test_control_characters
@ -136,8 +136,12 @@ module UnescapeTest
private
def unescape_all(source)
YARP.const_get(:Debug).unescape_all(source)
end
def assert_unescape_all(expected, source, forced_encoding = nil)
result = YARP.unescape_all(source)
result = unescape_all(source)
result.force_encoding(forced_encoding) if forced_encoding
assert_equal(expected, result)
end

Просмотреть файл

@ -512,12 +512,13 @@ Init_yarp(void) {
// Next, the functions that will be called by the parser to perform various
// internal tasks. We expose these to make them easier to test.
rb_define_singleton_method(rb_cYARP, "named_captures", named_captures, 1);
rb_define_singleton_method(rb_cYARP, "unescape_none", unescape_none, 1);
rb_define_singleton_method(rb_cYARP, "unescape_minimal", unescape_minimal, 1);
rb_define_singleton_method(rb_cYARP, "unescape_all", unescape_all, 1);
rb_define_singleton_method(rb_cYARP, "memsize", memsize, 1);
rb_define_singleton_method(rb_cYARP, "profile_file", profile_file, 1);
VALUE rb_cYARPDebug = rb_define_module_under(rb_cYARP, "Debug");
rb_define_singleton_method(rb_cYARPDebug, "named_captures", named_captures, 1);
rb_define_singleton_method(rb_cYARPDebug, "unescape_none", unescape_none, 1);
rb_define_singleton_method(rb_cYARPDebug, "unescape_minimal", unescape_minimal, 1);
rb_define_singleton_method(rb_cYARPDebug, "unescape_all", unescape_all, 1);
rb_define_singleton_method(rb_cYARPDebug, "memsize", memsize, 1);
rb_define_singleton_method(rb_cYARPDebug, "profile_file", profile_file, 1);
// Next, initialize the pack API.
Init_yarp_pack();