    1   package org.codehaus.groovy.syntax.lexer;
    2
    3   import groovy.util.GroovyTestCase;
    4
    5   import org.codehaus.groovy.syntax.ReadException;
    6
    7   public class AbstractCharStreamTest
    8       extends GroovyTestCase
    9   {
    10      public static class MockCharStream
    11          extends AbstractCharStream
    12      {
    13          private int cur;
    14          private String text;
    15
    16          public MockCharStream(String text)
    17          {
    18              this.text = text;
    19              this.cur  = 0;
    20          }
    21
    22          public MockCharStream(String text,
    23                                String description)
    24          {
    25              super( description );
    26              this.text = text;
    27              this.cur  = 0;
    28          }
    29
    30          public char consume()
    31              throws ReadException
    32          {
    33              if ( this.cur >= this.text.length() )
    34              {
    35                  return (char) -1;
    36              }
    37
    38              char c = this.text.charAt( this.cur );
    39
    40              ++this.cur;
    41
    42              return c;
    43          }
    44
    45          public void close()
    46              throws ReadException
    47          {
    48
    49          }
    50      }
    51
    52      public void testConstruct_Default()
    53      {
    54          assertEquals( "<unknown>",
    55                        new MockCharStream( "cheddar" ).getDescription() );
    56      }
    57
    58      public void testConstruct_WithDescription()
    59      {
    60          assertEquals( "/path/to/Cheddar.groovy",
    61                        new MockCharStream( "cheddar",
    62                                            "/path/to/Cheddar.groovy" ).getDescription() );
    63      }
    64
    65      public void testconsume()
    66          throws Exception
    67      {
    68          MockCharStream in = new MockCharStream( "cheddar" );
    69
    70          assertEquals( 'c',
    71                        in.consume() );
    72
    73          assertEquals( 'h',
    74                        in.consume() );
    75
    76          assertEquals( 'e',
    77                        in.consume() );
    78
    79          assertEquals( 'd',
    80                        in.consume() );
    81
    82          assertEquals( 'd',
    83                        in.consume() );
    84
    85          assertEquals( 'a',
    86                        in.consume() );
    87
    88          assertEquals( 'r',
    89                        in.consume() );
    90
    91          assertEquals( (char) -1,
    92                        in.consume() );
    93      }
    94
    95      public void testConsume()
    96          throws Exception
    97      {
    98          MockCharStream in = new MockCharStream( "cheddar" );
    99
    100         assertEquals( 'c',
    101                       in.consume() );
    102
    103         assertEquals( 'h',
    104                       in.consume() );
    105
    106         assertEquals( 'e',
    107                       in.consume() );
    108
    109         assertEquals( 'd',
    110                       in.consume() );
    111
    112         assertEquals( 'd',
    113                       in.consume() );
    114
    115         assertEquals( 'a',
    116                       in.consume() );
    117
    118         assertEquals( 'r',
    119                       in.consume() );
    120
    121         assertEquals( (char) -1,
    122                       in.consume() );
    123
    124     }
    125
    126     public void testConsumeAtEnd()
    127         throws Exception
    128     {
    129         MockCharStream in = new MockCharStream( "" );
    130
    131         assertEquals( (char) -1,
    132                       in.consume() );
    133
    134         assertEquals( (char) -1,
    135                       in.consume() );
    136     }
    137 }