wibble  1.1
parse.test.h
Go to the documentation of this file.
1 // -*- C++ -*-
2 
3 #include <wibble/parse.h>
4 #include <wibble/test.h>
5 
6 using namespace wibble;
7 
8 
9 struct TestParse {
10  enum TokenId { Invalid, Number };
12 
13  struct IOStream {
14  std::istream &i;
15 
16  std::string remove() {
17  char block[1024];
18  i.read( block, 1023 );
19  block[i.gcount()] = 0;
20  return block;
21  }
22 
23  bool eof() {
24  return i.eof();
25  }
26 
27  IOStream( std::istream &i ) : i( i ) {}
28  };
29 
30  struct Lexer : wibble::Lexer< Token, IOStream >
31  {
32  Token remove() {
33  this->skipWhitespace();
34  this->match( isdigit, isdigit, Number );
35  return this->decide();
36  }
37 
39  : wibble::Lexer< Token, IOStream >( s )
40  {}
41  };
42 
43  Test lexer() {
44  std::stringstream s;
45  IOStream is( s );
46  Token t;
47  Lexer l( is );
48 
49  s << "1 2";
50 
51  t = l.remove();
52  assert_eq( t.id, Number );
53  assert_eq( t.data, "1" );
54 
55  t = l.remove();
56  assert_eq( t.id, Number );
57  assert_eq( t.data, "2" );
58 
59  t = l.remove();
60  assert_eq( t.id, Invalid );
61  }
62 };