2 # Copyright 2007-2010 Wincent Colaiuta. All rights reserved.
3 # Redistribution and use in source and binary forms, with or without
4 # modification, are permitted provided that the following conditions are met:
6 # 1. Redistributions of source code must retain the above copyright notice,
7 # this list of conditions and the following disclaimer.
8 # 2. Redistributions in binary form must reproduce the above copyright notice,
9 # this list of conditions and the following disclaimer in the documentation
10 # and/or other materials provided with the distribution.
12 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
13 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
14 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
15 # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
16 # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
17 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
18 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
19 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
20 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
21 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
22 # POSSIBILITY OF SUCH DAMAGE.
24 require File.expand_path('../../spec_helper', File.dirname(__FILE__))
27 describe '#to_class_name' do
28 it 'works with require names' do
29 'foo_bar'.to_class_name.should == 'FooBar'
32 it 'works with a single-letter' do
33 'f'.to_class_name.should == 'F'
36 it 'works with double-underscores' do
37 'foo__bar'.to_class_name.should == 'FooBar'
40 it 'works with terminating double-underscores' do
41 'foo__'.to_class_name.should == 'Foo'
46 describe 'iterating over a string' do
47 # formerly a bug: the StringScanner used under the covers was returning nil
48 # (stopping) on hitting a newline
49 it 'should be able to iterate over strings containing newlines' do
51 "hello\nworld".each_char { |c| chars << c }
52 chars.should == ['h', 'e', 'l', 'l', 'o', "\n",
53 'w', 'o', 'r', 'l', 'd']
57 describe 'working with Unicode strings' do
59 # € (Euro) is a three-byte UTF-8 glyph: "\342\202\254"
60 @string = 'Unicode €!'
63 it 'the "each_char" method should work with multibyte characters' do
65 @string.each_char { |c| chars << c }
66 chars.should == ['U', 'n', 'i', 'c', 'o', 'd', 'e', ' ', '€', '!']
69 it 'the "chars" method should work with multibyte characters' do
70 @string.chars.to_a.should == ['U', 'n', 'i', 'c', 'o', 'd', 'e', ' ', '€', '!']
73 it 'should be able to use "enumerator" convenience method to get a string enumerator' do
74 enumerator = 'hello€'.enumerator
75 enumerator.next.should == 'h'
76 enumerator.next.should == 'e'
77 enumerator.next.should == 'l'
78 enumerator.next.should == 'l'
79 enumerator.next.should == 'o'
80 enumerator.next.should == '€'
81 enumerator.next.should be_nil
84 it 'the "jlength" method should correctly report the number of characters in a string' do
85 @string.jlength.should == 10
86 "€".jlength.should == 1 # three bytes long, but one character
90 # For more detailed specification of the StringParslet behaviour see
91 # string_parslet_spec.rb.
92 describe 'using shorthand to get StringParslets from String instances' do
93 it 'chaining two Strings with the "&" operator should yield a two-element sequence' do
94 sequence = 'foo' & 'bar'
95 sequence.parse('foobar').should == ['foo', 'bar']
96 lambda { sequence.parse('no match') }.should raise_error(Walrat::ParseError)
99 it 'chaining three Strings with the "&" operator should yield a three-element sequence' do
100 sequence = 'foo' & 'bar' & '...'
101 sequence.parse('foobar...').should == ['foo', 'bar', '...']
102 lambda { sequence.parse('no match') }.should raise_error(Walrat::ParseError)
105 it 'alternating two Strings with the "|" operator should yield a single string' do
106 sequence = 'foo' | 'bar'
107 sequence.parse('foo').should == 'foo'
108 sequence.parse('foobar').should == 'foo'
109 sequence.parse('bar').should == 'bar'
110 lambda { sequence.parse('no match') }.should raise_error(Walrat::ParseError)