[ruby/yarp] Hide debug methods
https://github.com/ruby/yarp/commit/aa0dc2f301
This commit is contained in:
parent
1ea9e444ec
commit
820a58c228
Notes:
git
2023-08-17 00:48:08 +00:00
13
lib/yarp.rb
13
lib/yarp.rb
@ -230,9 +230,18 @@ module YARP
|
|||||||
Serialize.load(source, serialized)
|
Serialize.load(source, serialized)
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.newlines(source)
|
# This module is used for testing and debugging and is not meant to be used by
|
||||||
YARP.parse(source).source.offsets
|
# consumers of this library.
|
||||||
|
module Debug
|
||||||
|
def self.newlines(source)
|
||||||
|
YARP.parse(source).source.offsets
|
||||||
|
end
|
||||||
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
|
end
|
||||||
|
|
||||||
require_relative "yarp/lex_compat"
|
require_relative "yarp/lex_compat"
|
||||||
|
@ -4,7 +4,7 @@ require "yarp_test_helper"
|
|||||||
|
|
||||||
class MemsizeTest < Test::Unit::TestCase
|
class MemsizeTest < Test::Unit::TestCase
|
||||||
def test_memsize
|
def test_memsize
|
||||||
result = YARP.memsize("2 + 3")
|
result = YARP.const_get(:Debug).memsize("2 + 3")
|
||||||
|
|
||||||
assert_equal 5, result[:length]
|
assert_equal 5, result[:length]
|
||||||
assert_kind_of Integer, result[:memsize]
|
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.
|
# Next, assert that the newlines are in the expected places.
|
||||||
expected_newlines = [0]
|
expected_newlines = [0]
|
||||||
source.b.scan("\n") { expected_newlines << $~.offset(0)[0] + 1 }
|
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
|
# Finally, assert that we can lex the source and get the same tokens as
|
||||||
# Ripper.
|
# Ripper.
|
||||||
|
@ -8,27 +8,27 @@ class RegexpTest < Test::Unit::TestCase
|
|||||||
##############################################################################
|
##############################################################################
|
||||||
|
|
||||||
def test_named_captures_with_arrows
|
def test_named_captures_with_arrows
|
||||||
assert_equal(["foo"], YARP.named_captures("(?<foo>bar)"))
|
assert_equal(["foo"], named_captures("(?<foo>bar)"))
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_named_captures_with_single_quotes
|
def test_named_captures_with_single_quotes
|
||||||
assert_equal(["foo"], YARP.named_captures("(?'foo'bar)"))
|
assert_equal(["foo"], named_captures("(?'foo'bar)"))
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_nested_named_captures_with_arrows
|
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
|
end
|
||||||
|
|
||||||
def test_nested_named_captures_with_single_quotes
|
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
|
end
|
||||||
|
|
||||||
def test_allows_duplicate_named_captures
|
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
|
end
|
||||||
|
|
||||||
def test_named_capture_inside_fake_range_quantifier
|
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
|
end
|
||||||
|
|
||||||
##############################################################################
|
##############################################################################
|
||||||
@ -38,154 +38,160 @@ class RegexpTest < Test::Unit::TestCase
|
|||||||
##############################################################################
|
##############################################################################
|
||||||
|
|
||||||
def test_alternation
|
def test_alternation
|
||||||
refute_nil(YARP.named_captures("foo|bar"))
|
refute_nil(named_captures("foo|bar"))
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_anchors
|
def test_anchors
|
||||||
refute_nil(YARP.named_captures("^foo$"))
|
refute_nil(named_captures("^foo$"))
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_any
|
def test_any
|
||||||
refute_nil(YARP.named_captures("."))
|
refute_nil(named_captures("."))
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_posix_character_classes
|
def test_posix_character_classes
|
||||||
refute_nil(YARP.named_captures("[[:digit:]]"))
|
refute_nil(named_captures("[[:digit:]]"))
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_negated_posix_character_classes
|
def test_negated_posix_character_classes
|
||||||
refute_nil(YARP.named_captures("[[:^digit:]]"))
|
refute_nil(named_captures("[[:^digit:]]"))
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_invalid_posix_character_classes_should_fall_back_to_regular_classes
|
def test_invalid_posix_character_classes_should_fall_back_to_regular_classes
|
||||||
refute_nil(YARP.named_captures("[[:foo]]"))
|
refute_nil(named_captures("[[:foo]]"))
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_character_sets
|
def test_character_sets
|
||||||
refute_nil(YARP.named_captures("[abc]"))
|
refute_nil(named_captures("[abc]"))
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_nested_character_sets
|
def test_nested_character_sets
|
||||||
refute_nil(YARP.named_captures("[[abc]]"))
|
refute_nil(named_captures("[[abc]]"))
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_nested_character_sets_with_operators
|
def test_nested_character_sets_with_operators
|
||||||
refute_nil(YARP.named_captures("[[abc] && [def]]"))
|
refute_nil(named_captures("[[abc] && [def]]"))
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_named_capture_inside_nested_character_set
|
def test_named_capture_inside_nested_character_set
|
||||||
assert_equal([], YARP.named_captures("[foo (?<foo>bar)]"))
|
assert_equal([], named_captures("[foo (?<foo>bar)]"))
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_negated_character_sets
|
def test_negated_character_sets
|
||||||
refute_nil(YARP.named_captures("[^abc]"))
|
refute_nil(named_captures("[^abc]"))
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_character_ranges
|
def test_character_ranges
|
||||||
refute_nil(YARP.named_captures("[a-z]"))
|
refute_nil(named_captures("[a-z]"))
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_negated_character_ranges
|
def test_negated_character_ranges
|
||||||
refute_nil(YARP.named_captures("[^a-z]"))
|
refute_nil(named_captures("[^a-z]"))
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_fake_named_captures_inside_character_sets
|
def test_fake_named_captures_inside_character_sets
|
||||||
assert_equal([], YARP.named_captures("[a-z(?<foo>)]"))
|
assert_equal([], named_captures("[a-z(?<foo>)]"))
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_fake_named_capture_inside_character_set_with_escaped_ending
|
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
|
end
|
||||||
|
|
||||||
def test_comments
|
def test_comments
|
||||||
refute_nil(YARP.named_captures("(?#foo)"))
|
refute_nil(named_captures("(?#foo)"))
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_comments_with_escaped_parentheses
|
def test_comments_with_escaped_parentheses
|
||||||
refute_nil(YARP.named_captures("(?#foo\\)\\))"))
|
refute_nil(named_captures("(?#foo\\)\\))"))
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_non_capturing_groups
|
def test_non_capturing_groups
|
||||||
refute_nil(YARP.named_captures("(?:foo)"))
|
refute_nil(named_captures("(?:foo)"))
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_positive_lookaheads
|
def test_positive_lookaheads
|
||||||
refute_nil(YARP.named_captures("(?=foo)"))
|
refute_nil(named_captures("(?=foo)"))
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_negative_lookaheads
|
def test_negative_lookaheads
|
||||||
refute_nil(YARP.named_captures("(?!foo)"))
|
refute_nil(named_captures("(?!foo)"))
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_positive_lookbehinds
|
def test_positive_lookbehinds
|
||||||
refute_nil(YARP.named_captures("(?<=foo)"))
|
refute_nil(named_captures("(?<=foo)"))
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_negative_lookbehinds
|
def test_negative_lookbehinds
|
||||||
refute_nil(YARP.named_captures("(?<!foo)"))
|
refute_nil(named_captures("(?<!foo)"))
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_atomic_groups
|
def test_atomic_groups
|
||||||
refute_nil(YARP.named_captures("(?>foo)"))
|
refute_nil(named_captures("(?>foo)"))
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_absence_operator
|
def test_absence_operator
|
||||||
refute_nil(YARP.named_captures("(?~foo)"))
|
refute_nil(named_captures("(?~foo)"))
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_conditional_expression_with_index
|
def test_conditional_expression_with_index
|
||||||
refute_nil(YARP.named_captures("(?(1)foo)"))
|
refute_nil(named_captures("(?(1)foo)"))
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_conditional_expression_with_name
|
def test_conditional_expression_with_name
|
||||||
refute_nil(YARP.named_captures("(?(foo)bar)"))
|
refute_nil(named_captures("(?(foo)bar)"))
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_conditional_expression_with_group
|
def test_conditional_expression_with_group
|
||||||
refute_nil(YARP.named_captures("(?(<foo>)bar)"))
|
refute_nil(named_captures("(?(<foo>)bar)"))
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_options_on_groups
|
def test_options_on_groups
|
||||||
refute_nil(YARP.named_captures("(?imxdau:foo)"))
|
refute_nil(named_captures("(?imxdau:foo)"))
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_options_on_groups_with_invalid_options
|
def test_options_on_groups_with_invalid_options
|
||||||
assert_nil(YARP.named_captures("(?z:bar)"))
|
assert_nil(named_captures("(?z:bar)"))
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_options_on_groups_getting_turned_off
|
def test_options_on_groups_getting_turned_off
|
||||||
refute_nil(YARP.named_captures("(?-imx:foo)"))
|
refute_nil(named_captures("(?-imx:foo)"))
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_options_on_groups_some_getting_turned_on_some_getting_turned_off
|
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
|
end
|
||||||
|
|
||||||
def test_star_quantifier
|
def test_star_quantifier
|
||||||
refute_nil(YARP.named_captures("foo*"))
|
refute_nil(named_captures("foo*"))
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_plus_quantifier
|
def test_plus_quantifier
|
||||||
refute_nil(YARP.named_captures("foo+"))
|
refute_nil(named_captures("foo+"))
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_question_mark_quantifier
|
def test_question_mark_quantifier
|
||||||
refute_nil(YARP.named_captures("foo?"))
|
refute_nil(named_captures("foo?"))
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_endless_range_quantifier
|
def test_endless_range_quantifier
|
||||||
refute_nil(YARP.named_captures("foo{1,}"))
|
refute_nil(named_captures("foo{1,}"))
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_beginless_range_quantifier
|
def test_beginless_range_quantifier
|
||||||
refute_nil(YARP.named_captures("foo{,1}"))
|
refute_nil(named_captures("foo{,1}"))
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_range_quantifier
|
def test_range_quantifier
|
||||||
refute_nil(YARP.named_captures("foo{1,2}"))
|
refute_nil(named_captures("foo{1,2}"))
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_fake_range_quantifier_because_of_spaces
|
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
|
||||||
end
|
end
|
||||||
|
@ -15,7 +15,7 @@ module UnescapeTest
|
|||||||
private
|
private
|
||||||
|
|
||||||
def assert_unescape_none(source)
|
def assert_unescape_none(source)
|
||||||
assert_equal(source, YARP.unescape_none(source))
|
assert_equal(source, YARP.const_get(:Debug).unescape_none(source))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -35,7 +35,7 @@ module UnescapeTest
|
|||||||
private
|
private
|
||||||
|
|
||||||
def assert_unescape_minimal(expected, source)
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -83,7 +83,7 @@ module UnescapeTest
|
|||||||
assert_unescape_all("က", "\\u1000", "UTF-8")
|
assert_unescape_all("က", "\\u1000", "UTF-8")
|
||||||
assert_unescape_all("တ", "\\u1010", "UTF-8")
|
assert_unescape_all("တ", "\\u1010", "UTF-8")
|
||||||
|
|
||||||
assert_nil(YARP.unescape_all("\\uxxxx"))
|
assert_nil(unescape_all("\\uxxxx"))
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_unicode_codepoints
|
def test_unicode_codepoints
|
||||||
@ -95,8 +95,8 @@ module UnescapeTest
|
|||||||
assert_unescape_all("𐀐", "\\u{10010}", "UTF-8")
|
assert_unescape_all("𐀐", "\\u{10010}", "UTF-8")
|
||||||
assert_unescape_all("aĀကတ𐀀𐀐", "\\u{ 61\s100\n1000\t1010\r10000\v10010 }", "UTF-8")
|
assert_unescape_all("aĀကတ𐀀𐀐", "\\u{ 61\s100\n1000\t1010\r10000\v10010 }", "UTF-8")
|
||||||
|
|
||||||
assert_nil(YARP.unescape_all("\\u{110000}"))
|
assert_nil(unescape_all("\\u{110000}"))
|
||||||
assert_nil(YARP.unescape_all("\\u{110000 110001 110002}"))
|
assert_nil(unescape_all("\\u{110000 110001 110002}"))
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_control_characters
|
def test_control_characters
|
||||||
@ -136,8 +136,12 @@ module UnescapeTest
|
|||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
|
def unescape_all(source)
|
||||||
|
YARP.const_get(:Debug).unescape_all(source)
|
||||||
|
end
|
||||||
|
|
||||||
def assert_unescape_all(expected, source, forced_encoding = nil)
|
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
|
result.force_encoding(forced_encoding) if forced_encoding
|
||||||
assert_equal(expected, result)
|
assert_equal(expected, result)
|
||||||
end
|
end
|
||||||
|
@ -512,12 +512,13 @@ Init_yarp(void) {
|
|||||||
|
|
||||||
// Next, the functions that will be called by the parser to perform various
|
// Next, the functions that will be called by the parser to perform various
|
||||||
// internal tasks. We expose these to make them easier to test.
|
// internal tasks. We expose these to make them easier to test.
|
||||||
rb_define_singleton_method(rb_cYARP, "named_captures", named_captures, 1);
|
VALUE rb_cYARPDebug = rb_define_module_under(rb_cYARP, "Debug");
|
||||||
rb_define_singleton_method(rb_cYARP, "unescape_none", unescape_none, 1);
|
rb_define_singleton_method(rb_cYARPDebug, "named_captures", named_captures, 1);
|
||||||
rb_define_singleton_method(rb_cYARP, "unescape_minimal", unescape_minimal, 1);
|
rb_define_singleton_method(rb_cYARPDebug, "unescape_none", unescape_none, 1);
|
||||||
rb_define_singleton_method(rb_cYARP, "unescape_all", unescape_all, 1);
|
rb_define_singleton_method(rb_cYARPDebug, "unescape_minimal", unescape_minimal, 1);
|
||||||
rb_define_singleton_method(rb_cYARP, "memsize", memsize, 1);
|
rb_define_singleton_method(rb_cYARPDebug, "unescape_all", unescape_all, 1);
|
||||||
rb_define_singleton_method(rb_cYARP, "profile_file", profile_file, 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.
|
// Next, initialize the pack API.
|
||||||
Init_yarp_pack();
|
Init_yarp_pack();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user