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
87
|
// 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._
class MessageQueryTestCase {
case class TreeNode(id: Int, name: String)
object TreeNodeType extends UserType[TreeNode] {
def toUserObject(m: Message): TreeNode =
new TreeNode(
m.one("id").intField.value,
m.one("name").stringField.value
)
override def children = Seq(new NestedMember("node", TreeNodeType))
def fields = List(FieldSpec("id", IntField, _.id), FieldSpec("name", StringField, _.name))
}
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)
}
|