1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc.  All rights reserved.
3 // https://developers.google.com/protocol-buffers/
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 //     * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 //     * Redistributions in binary form must reproduce the above
12 // copyright notice, this list of conditions and the following disclaimer
13 // in the documentation and/or other materials provided with the
14 // distribution.
15 //     * Neither the name of Google Inc. nor the names of its
16 // contributors may be used to endorse or promote products derived from
17 // this software without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 
31 package com.google.protobuf;
32 
33 import junit.framework.TestCase;
34 
35 /**
36  * Test @{link TextFormatParseLocation}.
37  */
38 public class TextFormatParseLocationTest extends TestCase {
39 
testCreateEmpty()40   public void testCreateEmpty() {
41     TextFormatParseLocation location = TextFormatParseLocation.create(-1, -1);
42     assertEquals(TextFormatParseLocation.EMPTY, location);
43   }
44 
testCreate()45   public void testCreate() {
46     TextFormatParseLocation location = TextFormatParseLocation.create(2, 1);
47     assertEquals(2, location.getLine());
48     assertEquals(1, location.getColumn());
49   }
50 
testCreateThrowsIllegalArgumentExceptionForInvalidIndex()51   public void testCreateThrowsIllegalArgumentExceptionForInvalidIndex() {
52     try {
53       TextFormatParseLocation.create(-1, 0);
54       fail("Should throw IllegalArgumentException if line is less than 0");
55     } catch (IllegalArgumentException unused) {
56       // pass
57     }
58     try {
59       TextFormatParseLocation.create(0, -1);
60       fail("Should throw, column < 0");
61     } catch (IllegalArgumentException unused) {
62       // pass
63     }
64   }
65 
testHashCode()66   public void testHashCode() {
67     TextFormatParseLocation loc0 = TextFormatParseLocation.create(2, 1);
68     TextFormatParseLocation loc1 = TextFormatParseLocation.create(2, 1);
69 
70     assertEquals(loc0.hashCode(), loc1.hashCode());
71     assertEquals(
72         TextFormatParseLocation.EMPTY.hashCode(), TextFormatParseLocation.EMPTY.hashCode());
73   }
74 
testEquals()75   public void testEquals() {
76     TextFormatParseLocation loc0 = TextFormatParseLocation.create(2, 1);
77     TextFormatParseLocation loc1 = TextFormatParseLocation.create(1, 2);
78     TextFormatParseLocation loc2 = TextFormatParseLocation.create(2, 2);
79     TextFormatParseLocation loc3 = TextFormatParseLocation.create(2, 1);
80 
81     assertEquals(loc0, loc3);
82     assertNotSame(loc0, loc1);
83     assertNotSame(loc0, loc2);
84     assertNotSame(loc1, loc2);
85   }
86 }
87