1   /****************************************************************
2    * Licensed to the Apache Software Foundation (ASF) under one   *
3    * or more contributor license agreements.  See the NOTICE file *
4    * distributed with this work for additional information        *
5    * regarding copyright ownership.  The ASF licenses this file   *
6    * to you under the Apache License, Version 2.0 (the            *
7    * "License"); you may not use this file except in compliance   *
8    * with the License.  You may obtain a copy of the License at   *
9    *                                                              *
10   *   http://www.apache.org/licenses/LICENSE-2.0                 *
11   *                                                              *
12   * Unless required by applicable law or agreed to in writing,   *
13   * software distributed under the License is distributed on an  *
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
15   * KIND, either express or implied.  See the License for the    *
16   * specific language governing permissions and limitations      *
17   * under the License.                                           *
18   ****************************************************************/
19  package org.apache.jsieve.parser;
20  
21  import java.util.List;
22  
23  import junit.framework.TestCase;
24  
25  import org.apache.jsieve.utils.JUnitUtils;
26  
27  public class SieveNodeCommentTest extends TestCase {
28  
29      protected void setUp() throws Exception {
30          super.setUp();
31      }
32  
33      protected void tearDown() throws Exception {
34          super.tearDown();
35      }
36  
37      public void testGetNoCommentsBefore() throws Exception {
38          SieveNode node = (SieveNode) JUnitUtils.parse("if address :contains [\"To\", \"From\"] \"Fish!\"{ }");
39          List comments = node.getPrecedingComments();
40          assertNotNull(comments);
41          assertEquals(0, comments.size());
42      }
43      
44      public void testGetBracketCommentsBefore() throws Exception {
45          SieveNode node = (SieveNode) JUnitUtils.parse("/* A Comment *//* Another comment */if address :contains [\"To\", \"From\"] \"Fish!\"{ }");
46          List comments = node.getPrecedingComments();
47          assertNotNull(comments);
48          assertEquals(2, comments.size());
49          assertEquals(" A Comment ", comments.get(0));
50          assertEquals(" Another comment ", comments.get(1));
51      }
52      
53      public void testGetHashCommentsBefore() throws Exception {
54          SieveNode node = (SieveNode) JUnitUtils.parse("/* A Comment */#A Line Comment\nif address :contains [\"To\", \"From\"] \"Fish!\"{ }");
55          List comments = node.getPrecedingComments();
56          assertNotNull(comments);
57          assertEquals(2, comments.size());
58          assertEquals(" A Comment ", comments.get(0));
59          assertEquals("A Line Comment", comments.get(1));
60      }
61      
62      public void testGetHashCommentsBeforeCRLF() throws Exception {
63          SieveNode node = (SieveNode) JUnitUtils.parse("/* A Comment */#A Line Comment\r\nif address :contains [\"To\", \"From\"] \"Fish!\"{ }");
64          List comments = node.getPrecedingComments();
65          assertNotNull(comments);
66          assertEquals(2, comments.size());
67          assertEquals(" A Comment ", comments.get(0));
68          assertEquals("A Line Comment", comments.get(1));
69      }
70      
71  
72      public void testGetLastCommentNoneBefore() throws Exception {
73          SieveNode node = (SieveNode) JUnitUtils.parse("if address :contains [\"To\", \"From\"] \"Fish!\"{ }");
74          assertNull(node.getLastComment());
75      }
76      
77      public void testGetBracketLastCommentBefore() throws Exception {
78          SieveNode node = (SieveNode) JUnitUtils.parse("/* A Comment *//* Another comment */if address :contains [\"To\", \"From\"] \"Fish!\"{ }");
79          assertEquals(" Another comment ", node.getLastComment());
80      }
81      
82      public void testGetHashLastCommentBefore() throws Exception {
83          SieveNode node = (SieveNode) JUnitUtils.parse("/* A Comment */#A Line Comment\nif address :contains [\"To\", \"From\"] \"Fish!\"{ }");
84          assertEquals("A Line Comment", node.getLastComment());
85      }
86      
87      public void testGetHashLastCommentBeforeCRLF() throws Exception {
88          SieveNode node = (SieveNode) JUnitUtils.parse("/* A Comment */#A Line Comment\r\nif address :contains [\"To\", \"From\"] \"Fish!\"{ }");;
89          assertEquals("A Line Comment", node.getLastComment());
90      }
91  }