• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc.  All rights reserved.
3 //
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file or at
6 // https://developers.google.com/open-source/licenses/bsd
7 
8 package com.google.protobuf;
9 
10 import static com.google.common.truth.Truth.assertThat;
11 import static com.google.common.truth.Truth.assertWithMessage;
12 
13 import org.junit.Test;
14 import org.junit.runner.RunWith;
15 import org.junit.runners.JUnit4;
16 
17 /** Test {@link TextFormatParseLocation}. */
18 @RunWith(JUnit4.class)
19 public class TextFormatParseLocationTest {
20 
21   @Test
testCreateEmpty()22   public void testCreateEmpty() {
23     TextFormatParseLocation location = TextFormatParseLocation.create(-1, -1);
24     assertThat(location).isEqualTo(TextFormatParseLocation.EMPTY);
25   }
26 
27   @Test
testCreate()28   public void testCreate() {
29     TextFormatParseLocation location = TextFormatParseLocation.create(2, 1);
30     assertThat(location.getLine()).isEqualTo(2);
31     assertThat(location.getColumn()).isEqualTo(1);
32   }
33 
34   @Test
testCreateThrowsIllegalArgumentExceptionForInvalidIndex()35   public void testCreateThrowsIllegalArgumentExceptionForInvalidIndex() {
36     try {
37       TextFormatParseLocation.create(-1, 0);
38       assertWithMessage("Should throw IllegalArgumentException if line is less than 0").fail();
39     } catch (IllegalArgumentException unused) {
40       // pass
41     }
42     try {
43       TextFormatParseLocation.create(0, -1);
44       assertWithMessage("Should throw, column < 0").fail();
45     } catch (IllegalArgumentException unused) {
46       // pass
47     }
48   }
49 
50   @Test
testHashCode()51   public void testHashCode() {
52     TextFormatParseLocation loc0 = TextFormatParseLocation.create(2, 1);
53     TextFormatParseLocation loc1 = TextFormatParseLocation.create(2, 1);
54 
55     assertThat(loc0.hashCode()).isEqualTo(loc1.hashCode());
56     assertThat(TextFormatParseLocation.EMPTY.hashCode())
57         .isEqualTo(TextFormatParseLocation.EMPTY.hashCode());
58   }
59 
60   @Test
testEquals()61   public void testEquals() {
62     TextFormatParseLocation loc0 = TextFormatParseLocation.create(2, 1);
63     TextFormatParseLocation loc1 = TextFormatParseLocation.create(1, 2);
64     TextFormatParseLocation loc2 = TextFormatParseLocation.create(2, 2);
65     TextFormatParseLocation loc3 = TextFormatParseLocation.create(2, 1);
66 
67     assertThat(loc0).isEqualTo(loc3);
68     assertThat(loc0).isNotSameInstanceAs(loc1);
69     assertThat(loc0).isNotSameInstanceAs(loc2);
70     assertThat(loc1).isNotSameInstanceAs(loc2);
71   }
72 }
73