Class: RegexpGsubFormatTest
- Defined in:
- lib/regexp.rb
Instance Method Summary collapse
- #test_basic_replacement ⇒ Object
- #test_different_named_captures ⇒ Object
- #test_multiple_matches ⇒ Object
- #test_no_match ⇒ Object
- #test_with_context ⇒ Object
Instance Method Details
#test_basic_replacement ⇒ Object
48 49 50 51 52 53 54 55 56 |
# File 'lib/regexp.rb', line 48 def test_basic_replacement input_str = '123 example' re = /(?<foo>\d+) (?<bar>\w+)/ fmt = '%<foo>d : %<bar>s' result = re.gsub_format(input_str, fmt) assert_equal '123 : example', result end |
#test_different_named_captures ⇒ Object
78 79 80 81 82 83 84 85 86 |
# File 'lib/regexp.rb', line 78 def test_different_named_captures input_str = 'Jane is 25 years old.' re = /^(?<name>\w+) is (?<age>\d+).*$/ fmt = "%<name>s's age is %<age>d" result = re.gsub_format(input_str, fmt) assert_equal "Jane's age is 25", result end |
#test_multiple_matches ⇒ Object
68 69 70 71 72 73 74 75 76 |
# File 'lib/regexp.rb', line 68 def test_multiple_matches input_str = '123 example, 456 test' re = /(?<foo>\d+) (?<bar>\w+)/ fmt = '[%<foo>d %<bar>s]' result = re.gsub_format(input_str, fmt) assert_equal '[123 example], [456 test]', result end |
#test_no_match ⇒ Object
58 59 60 61 62 63 64 65 66 |
# File 'lib/regexp.rb', line 58 def test_no_match input_str = 'This is a test.' re = /(?<foo>\d+) (?<bar>\w+)/ fmt = '%<foo>d : %<bar>s' result = re.gsub_format(input_str, fmt) assert_equal 'This is a test.', result end |
#test_with_context ⇒ Object
88 89 90 91 92 93 94 95 96 |
# File 'lib/regexp.rb', line 88 def test_with_context input_str = 'Jane is 25 years old.' re = /^(?<name>\w+) is (?<age>\d+).*$/ fmt = "%<name>s's age is %<age>d and she lives in %<city>s" result = re.gsub_format(input_str, re, fmt, context: { city: 'New York' }) assert_equal "Jane's age is 25 and she lives in New York", result end |