Skip to content

Commit f61d9a0

Browse files
committed
added a test and changed from == to equals
1 parent c6cf911 commit f61d9a0

File tree

2 files changed

+59
-1
lines changed

2 files changed

+59
-1
lines changed

shared/src/main/scala/scala/util/parsing/input/Position.scala

+6-1
Original file line numberDiff line numberDiff line change
@@ -66,5 +66,10 @@ trait Position {
6666
* @param `that` a `Position` to compare to this `Position`
6767
* @return true if the line numbers and column numbers are equal.
6868
*/
69-
def ==(that: Position) = this.line == that.line && this.column == that.column
69+
override def equals(other: Any) = {
70+
other match {
71+
case that: Position => this.line == that.line && this.column == that.column
72+
case _ => false
73+
}
74+
}
7075
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package scala.util.parsing.combinator
2+
3+
import java.io.StringReader
4+
5+
import scala.language.implicitConversions
6+
import scala.util.parsing.combinator.Parsers
7+
import scala.util.parsing.input.StreamReader
8+
9+
import org.junit.Test
10+
import org.junit.Assert.{ assertEquals, assertTrue, fail }
11+
12+
class LongestMatchTest {
13+
class TestParsers extends Parsers {
14+
type Elem = Char
15+
16+
def ab: Parser[String] = 'a' ~ 'b' ^^^ "ab"
17+
def a: Parser[String] = 'a' ^^^ "a"
18+
def ab_alt: Parser[String] = 'a' ~ 'b' ^^^ "alt"
19+
}
20+
21+
@Test
22+
def longestMatchFirst: Unit = {
23+
val tParsers = new TestParsers
24+
val reader = StreamReader(new StringReader("ab"))
25+
val p = tParsers.ab ||| tParsers.a
26+
p(reader) match {
27+
case tParsers.Success(result, _) => assertEquals("ab", result)
28+
case _ => fail()
29+
}
30+
}
31+
32+
@Test
33+
def longestMatchSecond: Unit = {
34+
val tParsers = new TestParsers
35+
val reader = StreamReader(new StringReader("ab"))
36+
val p = tParsers.a ||| tParsers.ab
37+
p(reader) match {
38+
case tParsers.Success(result, _) => assertEquals("ab", result)
39+
case _ => fail()
40+
}
41+
}
42+
43+
@Test
44+
def tieGoesToFirst: Unit = {
45+
val tParsers = new TestParsers
46+
val reader = StreamReader(new StringReader("ab"))
47+
val p = tParsers.ab ||| tParsers.ab_alt
48+
p(reader) match {
49+
case tParsers.Success(result, _) => assertEquals("ab", result)
50+
case _ => fail()
51+
}
52+
}
53+
}

0 commit comments

Comments
 (0)