MessageMatchers.scala 5.85 KB
Newer Older
1
package eu.nomad_lab.integrated_pipeline_tests
2
3
4

import java.nio.file.{ Path, Paths }

5
6
import eu.nomad_lab.integrated_pipeline.messages._
import eu.nomad_lab.parsers.ParseResult.ParseResult
7
import eu.nomad_lab.parsers.{ FinishedParsingSession, ParseEvent, StartedParsingSession }
8
9
import org.scalatest.matchers.{ HavePropertyMatchResult, HavePropertyMatcher }

10
11
12
trait MessageMatchers
  extends FileTreeTaskMatchers
  with FileParsingTaskMatchers
13
14
15

object MessageMatchers extends MessageMatchers

16
17
18
19
20
21
22
23
private object Helpers {
  def propertyMatcher[T, E](propertyName: String, expected: E, test: T => E): HavePropertyMatcher[T, E] = {
    new HavePropertyMatcher[T, E] {
      def apply(element: T): HavePropertyMatchResult[E] = {
        val actual = test(element)
        HavePropertyMatchResult(actual == expected, propertyName, expected, actual)
      }
      override def toString(): String = s"$propertyName '$expected'"
24
    }
25
  }
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43

  def subStringPropertyMatcher[T](propertyName: String, expected: Seq[String],
    test: T => String, display: T => String, ignoreCase: Boolean = true): HavePropertyMatcher[T, String] = {
    new HavePropertyMatcher[T, String] {
      def apply(element: T): HavePropertyMatchResult[String] = {
        val message = if (ignoreCase) test(element).toLowerCase else test(element)
        HavePropertyMatchResult(
          matches = expected.forall(test => message.contains(
            if (ignoreCase) test.toLowerCase else test
          )),
          propertyName = "error message content",
          expectedValue = expected.mkString("all of '", "', '", "'"),
          actualValue = display(element)
        )
      }
      override def toString(): String = s"$propertyName '${expected.mkString("all of '", "', '", "'")}'"
    }
  }
44
45
46
47
48
49
50
51
52
}

trait FileTreeTaskMatchers {
  def basePath(expectedValue: Path): HavePropertyMatcher[FileTreeScanTask, Path] =
    Helpers.propertyMatcher(
      propertyName = "tree base path",
      expected = expectedValue,
      test = (x: FileTreeScanTask) => x.treeBasePath
    )
53
54
55
}

trait FileParsingTaskMatchers {
56
57
58
59
60
61
  def treeTask(expectedValue: FileTreeScanTask): HavePropertyMatcher[FileParsingTaskSignal, FileTreeScanTask] =
    Helpers.propertyMatcher(
      propertyName = "parent tree task",
      expected = expectedValue,
      test = (x: FileParsingTaskSignal) => x.treeTask
    )
62

63
64
65
66
67
  def relativePath(expectedValue: Path): HavePropertyMatcher[FileParsingTask, Path] =
    Helpers.propertyMatcher(
      propertyName = "relative file path",
      expected = expectedValue,
      test = (x: FileParsingTask) => x.relativePath
68
    )
69

70
  def relativePath(expectedValue: String): HavePropertyMatcher[FileParsingTask, Path] = {
71
72
73
    relativePath(Paths.get(expectedValue))
  }

74
75
76
77
78
  def extractedPath(expectedValue: Option[Path]): HavePropertyMatcher[FileParsingTask, Option[Path]] =
    Helpers.propertyMatcher(
      propertyName = "temporary extracted file path",
      expected = expectedValue,
      test = (x: FileParsingTask) => x.extractedPath
79
    )
80

81
  def extractedPathString(expectedValue: Option[String]): HavePropertyMatcher[FileParsingTask, Option[Path]] = {
82
83
    extractedPath(expectedValue.map(Paths.get(_)))
  }
84

85
86
87
88
89
90
91
  def numParsingTasks(expectedValue: Long): HavePropertyMatcher[FileParsingSignalEndTree, Long] =
    Helpers.propertyMatcher(
      propertyName = "number of identified candidate calculations",
      expected = expectedValue,
      test = (x: FileParsingSignalEndTree) => x.numParsingTasks
    )

92
93
94
95
}

trait FileParsingResultMatchers {

96
97
98
99
100
101
  def treeTask(expectedValue: FileTreeScanTask): HavePropertyMatcher[FileParsingResultSignal, FileTreeScanTask] =
    Helpers.propertyMatcher(
      propertyName = "parent tree task",
      expected = expectedValue,
      test = (x: FileParsingResultSignal) => x.treeTask
    )
102

103
104
105
106
107
108
  def status(expectedValue: ParseResult): HavePropertyMatcher[FileParsingResult, ParseResult] =
    Helpers.propertyMatcher(
      propertyName = "parsing result status",
      expected = expectedValue,
      test = (x: FileParsingResult) => x.result
    )
109

110
111
112
113
114
115
116
117
118
  def relativePath(expectedValue: Path): HavePropertyMatcher[FileParsingResult, Path] =
    Helpers.propertyMatcher(
      propertyName = "relative file path",
      expected = expectedValue,
      test = (x: FileParsingResult) => x.task.relativePath
    )

  def relativePath(expectedValue: String): HavePropertyMatcher[FileParsingResult, Path] =
    relativePath(Paths.get(expectedValue))
119
120
121
122
123
124
125

  def numParsingTasks(expectedValue: Long): HavePropertyMatcher[FileParsingSignalEndTree, Long] =
    Helpers.propertyMatcher(
      propertyName = "number of identified candidate calculations",
      expected = expectedValue,
      test = (x: FileParsingSignalEndTree) => x.numParsingTasks
    )
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149

  def startSignal(expectedValue: Option[StartedParsingSession]): HavePropertyMatcher[FileParsingResult, Option[StartedParsingSession]] =
    Helpers.propertyMatcher(
      propertyName = "session start signal",
      expected = expectedValue,
      test = (x: FileParsingResult) => x.start
    )

  def endSignal(expectedValue: Option[FinishedParsingSession]): HavePropertyMatcher[FileParsingResult, Option[FinishedParsingSession]] =
    Helpers.propertyMatcher(
      propertyName = "session end signal",
      expected = expectedValue,
      test = (x: FileParsingResult) => x.end
    )

  def events(expectedValue: Seq[ParseEvent]): HavePropertyMatcher[FileParsingResult, Seq[ParseEvent]] =
    Helpers.propertyMatcher(
      propertyName = "emitted parsing events",
      expected = expectedValue,
      test = (x: FileParsingResult) => x match {
      case x: InMemoryResult => x.events
    }
    )

150
151
152
153
154
155
156
157
  def errorMessage(first: String, other: String*): HavePropertyMatcher[FileParsingResult, String] =
    Helpers.subStringPropertyMatcher(
      propertyName = "error message content",
      expected = first +: other,
      test = (x: FileParsingResult) => x.error.getOrElse(""),
      display = (x: FileParsingResult) => x.error.toString
    )

158
}