summaryrefslogtreecommitdiff
path: root/src/test/scala/com/google/gimd/query/MessageQueryTestCase.scala
blob: c8c58eb6c4559c3aa3fd510435d41f67a6f968f7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// Copyright (C) 2009 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.gimd.query

import file.{File, FileType}
import org.junit.Test
import org.junit.Assert._
import UserType._

class MessageQueryTestCase {
  case class TreeNode(id: Int, name: String)
  object TreeNodeType extends UserType[TreeNode] {
    val id = FieldSpecOne("id", IntField, _.id)
    val name = FieldSpecOne("name", StringField, _.name)
    def fields = id :: name
    override def children = Seq(new NestedMember("node", TreeNodeType))
    def toUserObject(m: Message) = new TreeNode(id(m), name(m))
  }

  val root = new TreeNode(-1, "a")
  val child_0 = new TreeNode(0, "a")
  val child_1 = new TreeNode(1, "b")
  val child_2 = new TreeNode(2, "a")

  val child_msg0 = TreeNodeType.toMessage(child_0)
  val child_msg1 = TreeNodeType.toMessage(child_1)
  val child_msg2 = TreeNodeType.toMessage(child_2)

  val message = TreeNodeType.toMessageBuffer(root).
    add("node", child_msg0).
    add("node", child_msg1).
    add("node", child_msg2).readOnly

  @Test
  def queryParentWithChildren = {
    val queryResult = query((n: TreeNode) => n.name == "a")

    val expectedResult = Set(
      (PathHandle(Nil), root),
      (handle(("node", child_msg0)), child_0),
      (handle(("node", child_msg2)), child_2)
    )
    assertEquals(expectedResult, Set(queryResult.toList: _*))
  }

  @Test
  def queryChildren = {
    val queryResult = query((n: TreeNode) => n.name == "a" && n.id >= 0)

    val expectedResult = Set(
      (handle(("node", child_msg0)), child_0),
      (handle(("node", child_msg2)), child_2)
    )
    assertEquals(expectedResult, Set(queryResult.toList: _*))
  }

  @Test
  def queryParent = {
    val queryResult = query((n: TreeNode) => n.name == "a" && n.id < 0)

    val expectedResult = Set(
      (PathHandle(Nil), root)
    )
    assertEquals(expectedResult, Set(queryResult.toList: _*))
  }

  private def query(p: (TreeNode) => Boolean) = {
    import Predicate.functionLiteral2Predicate
    MessageQuery.simpleQuery(TreeNodeType, message, p)
  }

  private def handle(p: (String, Message)*) =
    PathHandle(p.map { case (name, msg) => (TreeNodeType, MessageField(name, msg)) } toList)
}