View Javadoc

1   /*
2    * #%L
3    * Native ARchive plugin for Maven
4    * %%
5    * Copyright (C) 2002 - 2014 NAR Maven Plugin developers.
6    * %%
7    * Licensed under the Apache License, Version 2.0 (the "License");
8    * you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   * 
11   * http://www.apache.org/licenses/LICENSE-2.0
12   * 
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   * #L%
19   */
20  package com.github.maven_nar.cpptasks.parser;
21  
22  import java.io.CharArrayReader;
23  import java.io.IOException;
24  
25  import junit.framework.TestCase;
26  
27  /**
28   * Tests for the CParser class.
29   */
30  public final class TestCParser extends TestCase {
31    /**
32     * Constructor.
33     * 
34     * @param name
35     *          String test name
36     */
37    public TestCParser(final String name) {
38      super(name);
39    }
40  
41    /**
42     * Checks parsing of #include <foo.h>.
43     * 
44     * @throws IOException
45     *           test fails on IOException
46     */
47    public void testImmediateImportBracket() throws IOException {
48      final CharArrayReader reader = new CharArrayReader("#import <foo.h> nowhatever  ".toCharArray());
49      final CParser parser = new CParser();
50      parser.parse(reader);
51      final String[] includes = parser.getIncludes();
52      assertEquals(includes.length, 1);
53      assertEquals("foo.h", includes[0]);
54    }
55  
56    /**
57     * Checks parsing of #import "foo.h".
58     * 
59     * @throws IOException
60     *           test fails on IOException
61     */
62    public void testImmediateImportQuote() throws IOException {
63      final CharArrayReader reader = new CharArrayReader("#import \"foo.h\"   ".toCharArray());
64      final CParser parser = new CParser();
65      parser.parse(reader);
66      final String[] includes = parser.getIncludes();
67      assertEquals(includes.length, 1);
68      assertEquals("foo.h", includes[0]);
69    }
70  
71    /**
72     * Checks parsing of #include <foo.h>.
73     * 
74     * @throws IOException
75     *           test fails on IOException
76     */
77    public void testImmediateIncludeBracket() throws IOException {
78      final CharArrayReader reader = new CharArrayReader("#include      <foo.h>   ".toCharArray());
79      final CParser parser = new CParser();
80      parser.parse(reader);
81      final String[] includes = parser.getIncludes();
82      assertEquals(includes.length, 1);
83      assertEquals("foo.h", includes[0]);
84    }
85  
86    /**
87     * Checks parsing of #include "foo.h".
88     * 
89     * @throws IOException
90     *           test fails on IOException.
91     */
92    public void testImmediateIncludeQuote() throws IOException {
93      final CharArrayReader reader = new CharArrayReader("#include     \"foo.h\"   ".toCharArray());
94      final CParser parser = new CParser();
95      parser.parse(reader);
96      final String[] includes = parser.getIncludes();
97      assertEquals(includes.length, 1);
98      assertEquals("foo.h", includes[0]);
99    }
100 
101   /**
102    * Checks parsing of #import <foo.h.
103    * 
104    * @throws IOException
105    *           test fails on IOException
106    */
107   public void testIncompleteImmediateImportBracket() throws IOException {
108     final CharArrayReader reader = new CharArrayReader("#import <foo.h   ".toCharArray());
109     final CParser parser = new CParser();
110     parser.parse(reader);
111     final String[] includes = parser.getIncludes();
112     assertEquals(includes.length, 0);
113   }
114 
115   /**
116    * Checks parsing of #import "foo.h.
117    * 
118    * @throws IOException
119    *           test fails on IOException
120    */
121   public void testIncompleteImmediateImportQuote() throws IOException {
122     final CharArrayReader reader = new CharArrayReader("#import \"foo.h   ".toCharArray());
123     final CParser parser = new CParser();
124     parser.parse(reader);
125     final String[] includes = parser.getIncludes();
126     assertEquals(includes.length, 0);
127   }
128 
129   /**
130    * Checks parsing of #include <foo.h.
131    * 
132    * @throws IOException
133    *           test fails on IOException
134    */
135   public void testIncompleteImmediateIncludeBracket() throws IOException {
136     final CharArrayReader reader = new CharArrayReader("#include <foo.h   ".toCharArray());
137     final CParser parser = new CParser();
138     parser.parse(reader);
139     final String[] includes = parser.getIncludes();
140     assertEquals(includes.length, 0);
141   }
142 
143   /**
144    * Checks parsing of #include "foo.h.
145    * 
146    * @throws IOException
147    *           test fails on IOException
148    */
149   public void testIncompleteImmediateIncludeQuote() throws IOException {
150     final CharArrayReader reader = new CharArrayReader("#include     \"foo.h    ".toCharArray());
151     final CParser parser = new CParser();
152     parser.parse(reader);
153     final String[] includes = parser.getIncludes();
154     assertEquals(includes.length, 0);
155   }
156 
157   /**
158    * Checks parsing when line contains leading whitespace.
159    * 
160    * @throws IOException
161    *           test fails on IOException.
162    */
163   public void testLeadingSpace() throws IOException {
164     final CharArrayReader reader = new CharArrayReader(" #include     \"foo.h\"   ".toCharArray());
165     final CParser parser = new CParser();
166     parser.parse(reader);
167     final String[] includes = parser.getIncludes();
168     assertEquals(includes.length, 1);
169     assertEquals("foo.h", includes[0]);
170   }
171 
172   /**
173    * Checks parsing when line contains a leading tab.
174    * 
175    * @throws IOException
176    *           test fails on IOException.
177    */
178   public void testLeadingTab() throws IOException {
179     final CharArrayReader reader = new CharArrayReader("\t#include     \"foo.h\"   ".toCharArray());
180     final CParser parser = new CParser();
181     parser.parse(reader);
182     final String[] includes = parser.getIncludes();
183     assertEquals(includes.length, 1);
184     assertEquals("foo.h", includes[0]);
185   }
186 
187   /**
188    * Checks parsing of #include foo.h.
189    * 
190    * @throws IOException
191    *           test fails on IOException
192    */
193   public void testNoQuoteOrBracket() throws IOException {
194     final CharArrayReader reader = new CharArrayReader("#include foo.h  ".toCharArray());
195     final CParser parser = new CParser();
196     parser.parse(reader);
197     final String[] includes = parser.getIncludes();
198     assertEquals(includes.length, 0);
199   }
200 
201   /**
202    * Checks parsing of //#include "foo.h".
203    * 
204    * @throws IOException
205    *           test fails on IOException
206    */
207   public void testNotFirstWhitespace() throws IOException {
208     final CharArrayReader reader = new CharArrayReader("//#include \"foo.h\"".toCharArray());
209     final CParser parser = new CParser();
210     parser.parse(reader);
211     final String[] includes = parser.getIncludes();
212     assertEquals(includes.length, 0);
213   }
214 
215 }