View Javadoc
1   package net.sourceforge.jenesis4java.util;
2   
3   /*
4    * #%L
5    * Jenesis 4 Java Code Generator
6    * %%
7    * Copyright (C) 2000 - 2016 jenesis4java
8    * %%
9    * This program is free software: you can redistribute it and/or modify
10   * it under the terms of the GNU Lesser General Public License as
11   * published by the Free Software Foundation, either version 3 of the
12   * License, or (at your option) any later version.
13   * 
14   * This program is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU General Lesser Public License for more details.
18   * 
19   * You should have received a copy of the GNU General Lesser Public
20   * License along with this program.  If not, see
21   * <http://www.gnu.org/licenses/lgpl-3.0.html>.
22   * #L%
23   */
24  
25  import net.sourceforge.jenesis4java.*;
26  import net.sourceforge.jenesis4java.impl.MCodeWriter;
27  import org.junit.Test;
28  
29  import java.io.PrintWriter;
30  import java.io.StringWriter;
31  import java.util.ArrayList;
32  import java.util.List;
33  
34  import static net.sourceforge.jenesis4java.impl.CommentsTest.MultiLineComment.DEFAULT_MULTILINE_COMMENT;
35  import static net.sourceforge.jenesis4java.util.CodeGenerationAssertions.assertThatTrailingCommentIsOnFirstLine;
36  import static org.junit.Assert.assertEquals;
37  import static org.junit.Assert.assertSame;
38  
39  public abstract class StatementTestBase {
40  
41      public abstract Statement getInstance();
42  
43      @Test
44      public void testSingleLineComment() {
45          Statement statement = getInstance();
46  
47          String withoutComment = statement.toString();
48          statement.setComment(Comment.SINGLE_LINE, "comment");
49          String withComment = statement.toString();
50  
51          String message = "Applying single-line comment to " + statement.getClass() //
52                  + " but got unexpected result.";
53          assertEquals(message, "// comment\n" + withoutComment, withComment);
54      }
55  
56      @Test
57      public void testMultiLineComment() {
58          Statement statement = getInstance();
59  
60          String withoutComment = statement.toString();
61          statement.setComment(Comment.MULTI_LINE, "comment");
62          String withComment = statement.toString();
63  
64          String message = "Applying multi-line comment to " + statement.getClass() //
65                  + " but got unexpected result.";
66          assertEquals(message, DEFAULT_MULTILINE_COMMENT + withoutComment, withComment);
67      }
68  
69      @Test
70      public void testTrailingCommentIsOnFirstLine() {
71          assertThatTrailingCommentIsOnFirstLine(getInstance());
72      }
73  
74      @Test
75      public void testDefaultIsNoComment() {
76          Statement statement = getInstance();
77          assertEquals("Expecting no comment.", null, statement.getComment());
78      }
79  
80      @Test
81      public void testCommentIsTrailingComment() {
82          Statement instance = getInstance();
83          String withoutComment = instance.toString();
84  
85          Comment comment = instance.comment("comment");
86          assertEquals(Comment.SINGLE_LINE, comment.getType());
87          assertEquals("// comment\n" + withoutComment, instance.toString());
88      }
89  
90      @Test
91      public void testSetAndGetComment() {
92          Statement statement = getInstance();
93          statement.setComment(Comment.MULTI_LINE, "comment");
94  
95          Comment comment = statement.getComment();
96          assertEquals(Comment.MULTI_LINE, comment.getType());
97          assertEquals("comment", comment.getText());
98      }
99  
100     @Test
101     public void testToCodeReturnsGivenCodeWriter() {
102         Statement statement = getInstance();
103 
104         CodeWriter cout = new MCodeWriter(new PrintWriter(new StringWriter()));
105         assertSame(cout, statement.toCode(cout));
106     }
107 
108     @Test
109     public void testVisitComment() {
110         final String commentMsg = "specific comment";
111         Statement statement = getInstance();
112         statement.setComment(Comment.SINGLE_LINE, commentMsg);
113 
114         final List<Comment> comments = new ArrayList<>();
115 
116         statement.visit(new ConditionalVisitor() {
117 
118             @Override
119             public boolean visit(Codeable current, Codeable parent) {
120                 if (current instanceof Comment) {
121                     comments.add((Comment) current);
122                 }
123                 return true;
124             }
125         });
126 
127         String message = "Expected to visit 1 comment in \n" + statement + "\n, instead got: " + comments.size();
128         assertEquals(message,  1, comments.size());
129     }
130 
131     @Test
132     public void testSetLabelIsChainable() {
133         Statement statement = getInstance();
134         Statement returnedStatement = statement.setLabel("label");
135         assertSame(statement, returnedStatement);
136     }
137 }