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
111
112
113
114
115
116
117
118
119
120
|
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.indexinglanguage.expressions;
import com.yahoo.document.DataType;
import com.yahoo.document.datatypes.FieldValue;
import com.yahoo.document.datatypes.StringFieldValue;
import com.yahoo.language.Language;
import com.yahoo.language.Linguistics;
import com.yahoo.language.process.Transformer;
import com.yahoo.language.simple.SimpleLinguistics;
import com.yahoo.vespa.indexinglanguage.SimpleTestAdapter;
import org.junit.Test;
import org.mockito.Mockito;
import static com.yahoo.vespa.indexinglanguage.expressions.ExpressionAssert.assertVerify;
import static com.yahoo.vespa.indexinglanguage.expressions.ExpressionAssert.assertVerifyThrows;
import static org.junit.Assert.*;
/**
* @author Simon Thoresen Hult
*/
public class NormalizeTestCase {
@Test
public void requireThatAccessorsWork() {
Linguistics linguistics = new SimpleLinguistics();
NormalizeExpression exp = new NormalizeExpression(linguistics);
assertSame(linguistics, exp.getLinguistics());
}
@Test
public void requireThatHashCodeAndEqualsAreImplemented() {
Linguistics linguistics = new SimpleLinguistics();
NormalizeExpression exp = new NormalizeExpression(linguistics);
assertFalse(exp.equals(new Object()));
assertFalse(exp.equals(new NormalizeExpression(Mockito.mock(Linguistics.class))));
assertEquals(exp, new NormalizeExpression(linguistics));
assertEquals(exp.hashCode(), new NormalizeExpression(linguistics).hashCode());
}
@Test
public void requireThatExpressionCanBeVerified() {
Expression exp = new NormalizeExpression(new SimpleLinguistics());
assertVerify(DataType.STRING, exp, DataType.STRING);
assertVerifyThrows(null, exp, "Expected string input, but no input is specified");
assertVerifyThrows(DataType.INT, exp, "Expected string input, got int");
}
@Test
public void requireThatInputIsNormalized() {
ExecutionContext ctx = new ExecutionContext(new SimpleTestAdapter());
ctx.setLanguage(Language.ENGLISH);
ctx.setValue(new StringFieldValue("b\u00e9yonc\u00e8"));
new NormalizeExpression(new SimpleLinguistics()).execute(ctx);
FieldValue val = ctx.getValue();
assertTrue(val instanceof StringFieldValue);
assertEquals("beyonce", ((StringFieldValue)val).getString());
}
class MyMockTransformer implements Transformer {
public boolean first = true;
@Override
public String accentDrop(String input, Language language) {
if (first) {
first = false;
return "\u0008";
} else {
return input.replace(' ', '/');
}
}
}
boolean getFirst(Transformer t) {
assertTrue(t instanceof MyMockTransformer);
var mmt = (MyMockTransformer)t;
return mmt.first;
}
class MyMockLinguistics extends SimpleLinguistics {
private Transformer transformer = new MyMockTransformer();
@Override
public Transformer getTransformer() {
return transformer;
}
@Override
public boolean equals(Linguistics other) { return (other instanceof MyMockLinguistics); }
}
@Test
public void requireThatBadNormalizeRetries() {
ExecutionContext ctx = new ExecutionContext(new SimpleTestAdapter());
ctx.setLanguage(Language.ENGLISH);
ctx.setValue(new StringFieldValue("bad norm"));
var linguistics = new MyMockLinguistics();
assertTrue(getFirst(linguistics.getTransformer()));
new NormalizeExpression(linguistics).execute(ctx);
FieldValue val = ctx.getValue();
assertTrue(val instanceof StringFieldValue);
assertEquals("bad/norm", ((StringFieldValue)val).getString());
assertFalse(getFirst(linguistics.getTransformer()));
}
@Test
public void requireThatEmptyIsNop() {
ExecutionContext ctx = new ExecutionContext(new SimpleTestAdapter());
ctx.setLanguage(Language.ENGLISH);
var orig = new StringFieldValue("");
ctx.setValue(orig);
var linguistics = new MyMockLinguistics();
assertTrue(getFirst(linguistics.getTransformer()));
new NormalizeExpression(linguistics).execute(ctx);
FieldValue val = ctx.getValue();
assertTrue(val == orig);
assertTrue(getFirst(linguistics.getTransformer()));
}
}
|