rubysl-strscan 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (65) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +17 -0
  3. data/.travis.yml +7 -0
  4. data/Gemfile +4 -0
  5. data/LICENSE +25 -0
  6. data/README.md +29 -0
  7. data/Rakefile +1 -0
  8. data/lib/rubysl/strscan.rb +2 -0
  9. data/lib/rubysl/strscan/strscan.rb +286 -0
  10. data/lib/rubysl/strscan/version.rb +5 -0
  11. data/lib/strscan.rb +1 -0
  12. data/rubysl-strscan.gemspec +22 -0
  13. data/spec/append_spec.rb +10 -0
  14. data/spec/beginning_of_line_spec.rb +6 -0
  15. data/spec/bol_spec.rb +6 -0
  16. data/spec/check_spec.rb +15 -0
  17. data/spec/check_until_spec.rb +14 -0
  18. data/spec/clear_spec.rb +24 -0
  19. data/spec/concat_spec.rb +10 -0
  20. data/spec/dup_spec.rb +38 -0
  21. data/spec/element_reference_spec.rb +48 -0
  22. data/spec/empty_spec.rb +24 -0
  23. data/spec/eos_spec.rb +6 -0
  24. data/spec/exist_spec.rb +23 -0
  25. data/spec/get_byte_spec.rb +6 -0
  26. data/spec/getbyte_spec.rb +27 -0
  27. data/spec/getch_spec.rb +43 -0
  28. data/spec/initialize_spec.rb +27 -0
  29. data/spec/inspect_spec.rb +19 -0
  30. data/spec/match_spec.rb +27 -0
  31. data/spec/matched_size_spec.rb +6 -0
  32. data/spec/matched_spec.rb +40 -0
  33. data/spec/matchedsize_spec.rb +26 -0
  34. data/spec/must_C_version_spec.rb +7 -0
  35. data/spec/peek_spec.rb +7 -0
  36. data/spec/peep_spec.rb +24 -0
  37. data/spec/pointer_spec.rb +10 -0
  38. data/spec/pos_spec.rb +10 -0
  39. data/spec/post_match_spec.rb +27 -0
  40. data/spec/pre_match_spec.rb +40 -0
  41. data/spec/reset_spec.rb +14 -0
  42. data/spec/rest_size_spec.rb +6 -0
  43. data/spec/rest_spec.rb +47 -0
  44. data/spec/restsize_spec.rb +24 -0
  45. data/spec/scan_full_spec.rb +29 -0
  46. data/spec/scan_spec.rb +81 -0
  47. data/spec/scan_until_spec.rb +22 -0
  48. data/spec/search_full_spec.rb +29 -0
  49. data/spec/shared/bol.rb +25 -0
  50. data/spec/shared/concat.rb +30 -0
  51. data/spec/shared/eos.rb +17 -0
  52. data/spec/shared/extract_range.rb +22 -0
  53. data/spec/shared/extract_range_matched.rb +22 -0
  54. data/spec/shared/get_byte.rb +28 -0
  55. data/spec/shared/matched_size.rb +21 -0
  56. data/spec/shared/peek.rb +44 -0
  57. data/spec/shared/pos.rb +83 -0
  58. data/spec/shared/rest_size.rb +18 -0
  59. data/spec/shared/terminate.rb +17 -0
  60. data/spec/skip_spec.rb +17 -0
  61. data/spec/skip_until_spec.rb +17 -0
  62. data/spec/string_spec.rb +39 -0
  63. data/spec/terminate_spec.rb +6 -0
  64. data/spec/unscan_spec.rb +28 -0
  65. metadata +201 -0
data/spec/bol_spec.rb ADDED
@@ -0,0 +1,6 @@
1
+ require File.expand_path('../shared/bol.rb', __FILE__)
2
+ require 'strscan'
3
+
4
+ describe "StringScanner#bol?" do
5
+ it_behaves_like(:strscan_bol, :bol?)
6
+ end
@@ -0,0 +1,15 @@
1
+ require 'strscan'
2
+
3
+ describe "StringScanner#check" do
4
+ before :each do
5
+ @s = StringScanner.new("This is a test")
6
+ end
7
+
8
+ it "returns the value that scan would return, without advancing the scan pointer" do
9
+ @s.check(/This/).should == "This"
10
+ @s.matched.should == "This"
11
+ @s.pos.should == 0
12
+ @s.check(/is/).should == nil
13
+ @s.matched.should == nil
14
+ end
15
+ end
@@ -0,0 +1,14 @@
1
+ require 'strscan'
2
+
3
+ describe "StringScanner#check_until" do
4
+ before :each do
5
+ @s = StringScanner.new("This is a test")
6
+ end
7
+
8
+ it "returns the same value of scan_until, but don't advances the scan pointer" do
9
+ @s.check_until(/a/).should == "This is a"
10
+ @s.pos.should == 0
11
+ @s.matched.should == "a"
12
+ @s.check_until(/test/).should == "This is a test"
13
+ end
14
+ end
@@ -0,0 +1,24 @@
1
+ require File.expand_path('../shared/terminate.rb', __FILE__)
2
+ require 'strscan'
3
+
4
+ describe "StringScanner#clear" do
5
+ it_behaves_like(:strscan_terminate, :clear)
6
+
7
+ it "warns in verbose mode that the method is obsolete" do
8
+ s = StringScanner.new("abc")
9
+ begin
10
+ old = $VERBOSE
11
+ lambda {
12
+ $VERBOSE = true
13
+ s.clear
14
+ }.should complain(/clear.*obsolete.*terminate/)
15
+
16
+ lambda {
17
+ $VERBOSE = false
18
+ s.clear
19
+ }.should_not complain
20
+ ensure
21
+ $VERBOSE = old
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,10 @@
1
+ require File.expand_path('../shared/concat.rb', __FILE__)
2
+ require 'strscan'
3
+
4
+ describe "StringScanner#concat" do
5
+ it_behaves_like(:strscan_concat, :concat)
6
+ end
7
+
8
+ describe "StringScanner#concat when passed a Fixnum" do
9
+ it_behaves_like(:strscan_concat_fixnum, :concat)
10
+ end
data/spec/dup_spec.rb ADDED
@@ -0,0 +1,38 @@
1
+ require 'strscan'
2
+
3
+ describe "StringScanner#dup" do
4
+ before(:each) do
5
+ @string = "this is a test"
6
+ @orig_s = StringScanner.new(@string)
7
+ end
8
+
9
+ it "copies the passed StringScanner's content to self" do
10
+ s = @orig_s.dup
11
+ s.string.should == @string
12
+ end
13
+
14
+ it "copies the passed StringSCanner's position to self" do
15
+ @orig_s.pos = 5
16
+ s = @orig_s.dup
17
+ s.pos.should eql(5)
18
+ end
19
+
20
+ it "copies previous match state" do
21
+ @orig_s.scan(/\w+/)
22
+ @orig_s.scan(/\s/)
23
+
24
+ @orig_s.pre_match.should == "this"
25
+
26
+ s = @orig_s.dup
27
+ s.pre_match.should == "this"
28
+
29
+ s.unscan
30
+ s.scan(/\s/).should == " "
31
+ end
32
+
33
+ it "copies the passed StringScanner scan pointer to self" do
34
+ @orig_s.terminate
35
+ s = @orig_s.dup
36
+ s.eos?.should be_true
37
+ end
38
+ end
@@ -0,0 +1,48 @@
1
+ require 'strscan'
2
+
3
+ describe "StringScanner#[]" do
4
+ before :each do
5
+ @s = StringScanner.new("Fri Jun 13 2008 22:43")
6
+ end
7
+
8
+ it "returns nil if there is no current match" do
9
+ @s[0].should be_nil
10
+ end
11
+
12
+ it "returns the n-th subgroup in the most recent match" do
13
+ @s.scan(/(\w+) (\w+) (\d+) /)
14
+ @s[0].should == "Fri Jun 13 "
15
+ @s[1].should == "Fri"
16
+ @s[2].should == "Jun"
17
+ @s[3].should == "13"
18
+ @s[-3].should == "Fri"
19
+ @s[-2].should == "Jun"
20
+ @s[-1].should == "13"
21
+ end
22
+
23
+ it "returns nil if index is outside of self" do
24
+ @s.scan(/(\w+) (\w+) (\d+) /)
25
+ @s[5].should == nil
26
+ @s[-5].should == nil
27
+ end
28
+
29
+ it "calls to_int on the given index" do
30
+ @s.scan(/(\w+) (\w+) (\d+) /)
31
+ @s[0.5].should == "Fri Jun 13 "
32
+ end
33
+
34
+ it "raises a TypeError if the given index is nil" do
35
+ @s.scan(/(\w+) (\w+) (\d+) /)
36
+ lambda { @s[nil]}.should raise_error(TypeError)
37
+ end
38
+
39
+ it "raises a TypeError when a String is as argument" do
40
+ @s.scan(/(\w+) (\w+) (\d+) /)
41
+ lambda { @s["Fri"]}.should raise_error(TypeError)
42
+ end
43
+
44
+ it "raises a TypeError when a Range is as argument" do
45
+ @s.scan(/(\w+) (\w+) (\d+) /)
46
+ lambda { @s[0..2]}.should raise_error(TypeError)
47
+ end
48
+ end
@@ -0,0 +1,24 @@
1
+ require File.expand_path('../shared/eos.rb', __FILE__)
2
+ require 'strscan'
3
+
4
+ describe "StringScanner#empty?" do
5
+ it_behaves_like(:strscan_eos, :empty?)
6
+
7
+ it "warns in verbose mode that the method is obsolete" do
8
+ s = StringScanner.new("abc")
9
+ begin
10
+ old = $VERBOSE
11
+ lambda {
12
+ $VERBOSE = true
13
+ s.empty?
14
+ }.should complain(/empty?.*obsolete.*eos?/)
15
+
16
+ lambda {
17
+ $VERBOSE = false
18
+ s.empty?
19
+ }.should_not complain
20
+ ensure
21
+ $VERBOSE = old
22
+ end
23
+ end
24
+ end
data/spec/eos_spec.rb ADDED
@@ -0,0 +1,6 @@
1
+ require File.expand_path('../shared/eos.rb', __FILE__)
2
+ require 'strscan'
3
+
4
+ describe "StringScanner#eos?" do
5
+ it_behaves_like(:strscan_eos, :eos?)
6
+ end
@@ -0,0 +1,23 @@
1
+ require 'strscan'
2
+
3
+ describe "StringScanner#exist?" do
4
+ before :each do
5
+ @s = StringScanner.new("This is a test")
6
+ end
7
+
8
+ it "returns the index of the first occurrence of the given pattern" do
9
+ @s.exist?(/s/).should == 4
10
+ @s.scan(/This is/)
11
+ @s.exist?(/s/).should == 6
12
+ end
13
+
14
+ it "returns 0 if the pattern is empty" do
15
+ @s.exist?(//).should == 0
16
+ end
17
+
18
+ it "returns nil if the pattern isn't found in the string" do
19
+ @s.exist?(/S/).should == nil
20
+ @s.scan(/This is/)
21
+ @s.exist?(/i/).should == nil
22
+ end
23
+ end
@@ -0,0 +1,6 @@
1
+ require File.expand_path('../shared/get_byte', __FILE__)
2
+ require 'strscan'
3
+
4
+ describe "StringScanner#get_byte" do
5
+ it_behaves_like :strscan_get_byte, :get_byte
6
+ end
@@ -0,0 +1,27 @@
1
+ require File.expand_path('../shared/get_byte', __FILE__)
2
+ require File.expand_path('../shared/extract_range', __FILE__)
3
+ require 'strscan'
4
+
5
+ describe "StringScanner#getbyte" do
6
+ it_behaves_like :strscan_get_byte, :getbyte
7
+
8
+ it "warns in verbose mode that the method is obsolete" do
9
+ s = StringScanner.new("abc")
10
+ begin
11
+ old = $VERBOSE
12
+ lambda {
13
+ $VERBOSE = true
14
+ s.getbyte
15
+ }.should complain(/getbyte.*obsolete.*get_byte/)
16
+
17
+ lambda {
18
+ $VERBOSE = false
19
+ s.getbyte
20
+ }.should_not complain
21
+ ensure
22
+ $VERBOSE = old
23
+ end
24
+ end
25
+
26
+ it_behaves_like :extract_range, :getbyte
27
+ end
@@ -0,0 +1,43 @@
1
+ require File.expand_path('../shared/extract_range', __FILE__)
2
+ require 'strscan'
3
+
4
+ describe "StringScanner#getch" do
5
+ before :each do
6
+ @kcode = $KCODE
7
+ end
8
+
9
+ after :each do
10
+ $KCODE = @kcode
11
+ end
12
+
13
+ it "scans one character and returns it" do
14
+ s = StringScanner.new('abc')
15
+ s.getch.should == "a"
16
+ s.getch.should == "b"
17
+ s.getch.should == "c"
18
+ end
19
+
20
+ it "is multi-byte character sensitive" do
21
+ $KCODE = 'EUC'
22
+
23
+ # Japanese hiragana "A" in EUC-JP
24
+ src = encode("\244\242", "euc-jp")
25
+
26
+ s = StringScanner.new(src)
27
+ s.getch.should == src
28
+ end
29
+
30
+ it "returns nil at the end of the string" do
31
+ # empty string case
32
+ s = StringScanner.new('')
33
+ s.getch.should == nil
34
+ s.getch.should == nil
35
+
36
+ # non-empty string case
37
+ s = StringScanner.new('a')
38
+ s.getch # skip one
39
+ s.getch.should == nil
40
+ end
41
+
42
+ it_behaves_like :extract_range, :getch
43
+ end
@@ -0,0 +1,27 @@
1
+ require 'strscan'
2
+
3
+ describe "StringScanner#initialize" do
4
+ before :each do
5
+ @s = StringScanner.new("This is a test")
6
+ end
7
+
8
+ it "is a private method" do
9
+ StringScanner.should have_private_instance_method(:initialize)
10
+ end
11
+
12
+ it "returns an instance of StringScanner" do
13
+ @s.should be_kind_of(StringScanner)
14
+ @s.tainted?.should be_false
15
+ @s.eos?.should be_false
16
+ end
17
+
18
+ it "converts the argument into a string using #to_str" do
19
+ m = mock(:str)
20
+
21
+ s = "test"
22
+ m.should_receive(:to_str).and_return(s)
23
+
24
+ scan = StringScanner.new(m)
25
+ scan.string.should == s
26
+ end
27
+ end
@@ -0,0 +1,19 @@
1
+ require 'strscan'
2
+
3
+ describe "StringScanner#inspect" do
4
+ before :each do
5
+ @s = StringScanner.new("This is a test")
6
+ end
7
+
8
+ it "returns a String object" do
9
+ @s.inspect.should be_kind_of(String)
10
+ end
11
+
12
+ it "returns a string that represents the StringScanner object" do
13
+ @s.inspect.should == "#<StringScanner 0/14 @ \"This ...\">"
14
+ @s.scan_until /is/
15
+ @s.inspect.should == "#<StringScanner 4/14 \"This\" @ \" is a...\">"
16
+ @s.terminate
17
+ @s.inspect.should == "#<StringScanner fin>"
18
+ end
19
+ end
@@ -0,0 +1,27 @@
1
+ require 'strscan'
2
+
3
+ describe "StringScanner#match?" do
4
+ before :each do
5
+ @s = StringScanner.new("This is a test")
6
+ end
7
+
8
+ it "returns the length of the match and the scan pointer is not advanced" do
9
+ @s.match?(/\w+/).should == 4
10
+ @s.match?(/\w+/).should == 4
11
+ @s.pos.should == 0
12
+ end
13
+
14
+ it "returns nil if there's no match" do
15
+ @s.match?(/\d+/).should == nil
16
+ @s.match?(/\s+/).should == nil
17
+ end
18
+
19
+ it "effects pre_match" do
20
+ @s.scan(/\w+/)
21
+ @s.scan(/\s/)
22
+
23
+ @s.pre_match.should == "This"
24
+ @s.match?(/\w+/)
25
+ @s.pre_match.should == "This "
26
+ end
27
+ end
@@ -0,0 +1,6 @@
1
+ require File.expand_path('../shared/matched_size.rb', __FILE__)
2
+ require 'strscan'
3
+
4
+ describe "StringScanner#matched_size" do
5
+ it_behaves_like(:strscan_matched_size, :matched_size)
6
+ end
@@ -0,0 +1,40 @@
1
+ require File.expand_path('../shared/extract_range_matched', __FILE__)
2
+ require 'strscan'
3
+
4
+ describe "StringScanner#matched" do
5
+ before :each do
6
+ @s = StringScanner.new("This is a test")
7
+ end
8
+
9
+ it "returns the last matched string" do
10
+ @s.match?(/\w+/)
11
+ @s.matched.should == "This"
12
+ @s.getch
13
+ @s.matched.should == "T"
14
+ @s.get_byte
15
+ @s.matched.should == "h"
16
+ end
17
+
18
+ it "returns nil if there's no match" do
19
+ @s.match?(/\d+/)
20
+ @s.matched.should == nil
21
+ end
22
+
23
+ it_behaves_like :extract_range_matched, :matched
24
+ end
25
+
26
+ describe "StringScanner#matched?" do
27
+ before :each do
28
+ @s = StringScanner.new("This is a test")
29
+ end
30
+
31
+ it "returns true if the last match was successful" do
32
+ @s.match?(/\w+/)
33
+ @s.matched?.should be_true
34
+ end
35
+
36
+ it "returns false if there's no match" do
37
+ @s.match?(/\d+/)
38
+ @s.matched?.should be_false
39
+ end
40
+ end
@@ -0,0 +1,26 @@
1
+ require File.expand_path('../shared/matched_size.rb', __FILE__)
2
+ require 'strscan'
3
+
4
+ ruby_version_is "" ... "1.9" do
5
+ describe "StringScanner#matchedsize" do
6
+ it_behaves_like(:strscan_matched_size, :matchedsize)
7
+
8
+ it "warns in verbose mode that the method is obsolete" do
9
+ s = StringScanner.new("abc")
10
+ begin
11
+ old = $VERBOSE
12
+ lambda {
13
+ $VERBOSE = true
14
+ s.matchedsize
15
+ }.should complain(/matchedsize.*obsolete.*matched_size/)
16
+
17
+ lambda {
18
+ $VERBOSE = false
19
+ s.matchedsize
20
+ }.should_not complain
21
+ ensure
22
+ $VERBOSE = old
23
+ end
24
+ end
25
+ end
26
+ end