|
7 | 7 |
|
8 | 8 | package com.nexthink.utils.parsing.combinator.completion
|
9 | 9 |
|
| 10 | +import com.nexthink.utils.parsing.combinator.completion.CompletionTestDefinitions.Tagged |
10 | 11 | import org.junit.{Assert, Test}
|
11 | 12 |
|
12 | 13 | import scala.util.parsing.combinator.Parsers
|
13 | 14 |
|
14 | 15 | class ExpandedCompletionsTest {
|
15 | 16 |
|
16 | 17 | object FiniteArithmeticParser extends Parsers with RegexCompletionSupportWithExpansion {
|
17 |
| - val number = "[0-9]+".r %> "99" |
| 18 | + val number = "[0-9]+".r %> "99" |
18 | 19 | def expr(maxNesting: Int) = term(maxNesting) ~ ("+" | "-") ~! term(maxNesting)
|
19 |
| - def term(maxNesting: Int) = factor(maxNesting) ~ ("*" | "/") ~! factor(maxNesting) |
| 20 | + def term(maxNesting: Int) = factor(maxNesting) ~ "*" ~! factor(maxNesting) |
20 | 21 | def factor(maxNesting: Int): Parser[Any] = if (maxNesting == 0) number else number | "(" ~> expr(maxNesting - 1) <~ ")"
|
21 | 22 |
|
22 | 23 | def exprWithExpandedCompletions() = expandedCompletions(expr(1))
|
23 | 24 | }
|
24 | 25 |
|
25 | 26 | @Test
|
26 | 27 | def finiteExpressionParses(): Unit = {
|
27 |
| - val result = FiniteArithmeticParser.parse(FiniteArithmeticParser.exprWithExpandedCompletions(), "( 99 / 99 - 99 / 99 ) / ( 99 / 99 + 99 / 99 ) - ( 99 * 99 - 99 * 99 ) / ( 99 * 99 - 99 / 99 )") |
28 |
| - println(result) |
| 28 | + val result = FiniteArithmeticParser.parse(FiniteArithmeticParser.exprWithExpandedCompletions(), |
| 29 | + "( 99 * 99 + 99 * 99 ) * ( 99 * 99 + 99 * 99 ) + ( 99 * 99 + 99 * 99 ) * ( 99 * 99 + 99 * 99 )") |
29 | 30 | assert(result.successful)
|
30 | 31 | }
|
31 | 32 |
|
32 | 33 | @Test
|
33 | 34 | def finiteExpressionCompletesToAllAlternatives(): Unit = {
|
34 | 35 | val completions = FiniteArithmeticParser.completeString(FiniteArithmeticParser.exprWithExpandedCompletions(), "")
|
35 |
| - println(completions) |
| 36 | + Assert.assertEquals(completions.length, 162) |
36 | 37 | }
|
37 | 38 |
|
38 |
| - object InfiniteExpressionParser extends Parsers with RegexCompletionSupportWithExpansion { |
| 39 | + object InfiniteExpressionParser extends Parsers with RegexCompletionSupportWithExpansion with CompletionTestParser { |
39 | 40 | val fox = "the quick brown fox"
|
40 |
| - val jumpsOver = "which jumps over the lazy" |
41 |
| - val jumpsOverDogOrCat = jumpsOver ~ ("dog" | "cat") |
| 41 | + val jumpsOver = "which jumps over the lazy" % "action" |
| 42 | + val jumpsOverDogOrCat = jumpsOver ~ ("dog" | "cat") % "animal" %? "dogs and cats" % 10 |
42 | 43 | lazy val parser = jumpsOverDogOrCat | jumpsOverDogOrCat ~ which()
|
43 | 44 | def which(): Parser[Any] = expandedCompletions(parser, stop = jumpsOverDogOrCat ~ jumpsOverDogOrCat)
|
44 | 45 | lazy val infiniteDogsAndCats = fox ~ which
|
45 | 46 | }
|
46 | 47 |
|
47 | 48 | @Test
|
48 | 49 | def infiniteExpressionParses(): Unit = {
|
49 |
| - val result = InfiniteExpressionParser.parse(InfiniteExpressionParser.infiniteDogsAndCats, |
50 |
| - "the quick brown fox which jumps over the lazy cat which jumps over the lazy dog which jumps over the lazy cat") |
| 50 | + val result = InfiniteExpressionParser.parse( |
| 51 | + InfiniteExpressionParser.infiniteDogsAndCats, |
| 52 | + "the quick brown fox which jumps over the lazy cat which jumps over the lazy dog which jumps over the lazy cat" |
| 53 | + ) |
51 | 54 | assert(result.successful)
|
52 | 55 | }
|
53 | 56 |
|
54 | 57 | @Test
|
55 | 58 | def infiniteExpressionCompletesToAlternativesUpToStop(): Unit = {
|
56 |
| - val completions = InfiniteExpressionParser.completeString(InfiniteExpressionParser.infiniteDogsAndCats, "the quick brown fox ") |
57 |
| - Assert.assertEquals( |
58 |
| - Seq( |
59 |
| - " which jumps over the lazy cat which jumps over the lazy cat", |
60 |
| - " which jumps over the lazy cat which jumps over the lazy dog", |
61 |
| - " which jumps over the lazy dog which jumps over the lazy cat", |
62 |
| - " which jumps over the lazy dog which jumps over the lazy dog" |
63 |
| - ), |
| 59 | + val completions = InfiniteExpressionParser.complete(InfiniteExpressionParser.infiniteDogsAndCats, "the quick brown fox ") |
| 60 | + InfiniteExpressionParser.assertHasCompletions( |
| 61 | + Set( |
| 62 | + Tagged( |
| 63 | + "animal", |
| 64 | + Some("dogs and cats"), |
| 65 | + 10, |
| 66 | + " which jumps over the lazy cat which jumps over the lazy cat", |
| 67 | + " which jumps over the lazy cat which jumps over the lazy dog", |
| 68 | + " which jumps over the lazy dog which jumps over the lazy cat", |
| 69 | + " which jumps over the lazy dog which jumps over the lazy dog" |
| 70 | + )), |
64 | 71 | completions
|
65 | 72 | )
|
66 | 73 | }
|
|
0 commit comments