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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
|
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.document.predicate;
import org.junit.Test;
import static com.yahoo.document.predicate.Predicates.and;
import static com.yahoo.document.predicate.Predicates.feature;
import static com.yahoo.document.predicate.Predicates.not;
import static com.yahoo.document.predicate.Predicates.or;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
/**
* @author <a href="mailto:simon@yahoo-inc.com">Simon Thoresen Hult</a>
*/
public class PredicateTest {
@Test
public void requireThatPredicateIsCloneable() {
assertTrue(Cloneable.class.isAssignableFrom(Predicate.class));
}
@Test
public void requireThatANDConstructsAConjunction() {
Predicate foo = SimplePredicates.newString("foo");
Predicate bar = SimplePredicates.newString("bar");
Predicate predicate = and(foo, bar);
assertEquals(Conjunction.class, predicate.getClass());
assertEquals(new Conjunction(foo, bar), predicate);
}
@Test
public void requireThatORConstructsADisjunction() {
Predicate foo = SimplePredicates.newString("foo");
Predicate bar = SimplePredicates.newString("bar");
Predicate predicate = or(foo, bar);
assertEquals(Disjunction.class, predicate.getClass());
assertEquals(new Disjunction(foo, bar), predicate);
}
@Test
public void requireThatNOTConstructsANegation() {
Predicate foo = SimplePredicates.newString("foo");
Predicate predicate = not(foo);
assertEquals(Negation.class, predicate.getClass());
assertEquals(new Negation(foo), predicate);
}
@Test
public void requireThatFeatureBuilderCanConstructFeatureRange() {
assertEquals(new FeatureRange("key", 6L, 9L),
feature("key").inRange(6, 9));
assertEquals(new Negation(new FeatureRange("key", 6L, 9L)),
feature("key").notInRange(6, 9));
assertEquals(new FeatureRange("key", 7L, null),
feature("key").greaterThan(6));
assertEquals(new FeatureRange("key", 6L, null),
feature("key").greaterThanOrEqualTo(6));
assertEquals(new FeatureRange("key", null, 5L),
feature("key").lessThan(6));
assertEquals(new FeatureRange("key", null, 9L),
feature("key").lessThanOrEqualTo(9));
}
@Test
public void requireThatFeatureBuilderCanConstructFeatureSet() {
assertEquals(new FeatureSet("key", "valueA", "valueB"),
feature("key").inSet("valueA", "valueB"));
assertEquals(new Negation(new FeatureSet("key", "valueA", "valueB")),
feature("key").notInSet("valueA", "valueB"));
}
@Test
public void requireThatPredicatesCanBeConstructedUsingConstructors() {
assertEquals("country in [no, se] and age in [20..30]",
new Conjunction(new FeatureSet("country", "no", "se"),
new FeatureRange("age", 20L, 30L)).toString());
assertEquals("country not in [no, se] or age in [20..] or height in [..160]",
new Disjunction(new Negation(new FeatureSet("country", "no", "se")),
new FeatureRange("age", 20L, null),
new FeatureRange("height", null, 160L)).toString());
}
@Test
public void requireThatPredicatesCanBeBuiltUsingChainedMethodCalls() {
assertEquals("country not in [no, se] or age in [20..] or height in [..160]",
new Disjunction()
.addOperand(new Negation(new FeatureSet("country").addValue("no").addValue("se")))
.addOperand(new FeatureRange("age").setFromInclusive(20L))
.addOperand(new FeatureRange("height").setToInclusive(160L))
.toString());
}
@Test
public void requireThatPredicatesCanBeBuiltUsingSeparateMethodCalls() {
Conjunction conjunction = new Conjunction();
FeatureSet countrySet = new FeatureSet("country");
countrySet.addValue("no");
countrySet.addValue("se");
conjunction.addOperand(countrySet);
FeatureRange ageRange = new FeatureRange("age");
ageRange.setFromInclusive(20L);
conjunction.addOperand(ageRange);
FeatureRange heightRange = new FeatureRange("height");
heightRange.setToInclusive(160L);
conjunction.addOperand(heightRange);
assertEquals("country in [no, se] and age in [20..] and height in [..160]",
conjunction.toString());
}
}
|