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 package gov.nist.secauto.metaschema.core.metapath;
28
29 import static org.junit.jupiter.api.Assertions.assertEquals;
30
31 import gov.nist.secauto.metaschema.core.metapath.item.atomic.IBooleanItem;
32 import gov.nist.secauto.metaschema.core.util.ObjectUtils;
33
34 import org.jmock.Expectations;
35 import org.jmock.Mockery;
36 import org.junit.jupiter.params.ParameterizedTest;
37 import org.junit.jupiter.params.provider.Arguments;
38 import org.junit.jupiter.params.provider.MethodSource;
39
40 import java.util.List;
41 import java.util.stream.Stream;
42
43 class AndTest
44 extends ExpressionTestBase {
45
46 private static Stream<Arguments> testAnd() {
47 return Stream.of(
48 Arguments.of(IBooleanItem.TRUE, IBooleanItem.TRUE, IBooleanItem.TRUE),
49 Arguments.of(IBooleanItem.TRUE, IBooleanItem.FALSE, IBooleanItem.FALSE),
50 Arguments.of(IBooleanItem.FALSE, IBooleanItem.TRUE, IBooleanItem.FALSE),
51 Arguments.of(IBooleanItem.FALSE, IBooleanItem.FALSE, IBooleanItem.FALSE));
52 }
53
54 @ParameterizedTest
55 @MethodSource
56 void testAnd(IBooleanItem bool1, IBooleanItem bool2, IBooleanItem expectedResult) {
57 DynamicContext dynamicContext = newDynamicContext();
58
59 Mockery context = getContext();
60
61 ISequence<?> focus = ISequence.empty();
62
63 IExpression exp1 = context.mock(IExpression.class, "exp1");
64 IExpression exp2 = context.mock(IExpression.class, "exp2");
65
66 context.checking(new Expectations() {
67 {
68 atMost(1).of(exp1).accept(dynamicContext, focus);
69 will(returnValue(ISequence.of(bool1)));
70 atMost(1).of(exp2).accept(dynamicContext, focus);
71 will(returnValue(ISequence.of(bool2)));
72 }
73 });
74
75 List<IExpression> list = List.of(exp1, exp2);
76 assert list != null;
77 And expr = new And(list);
78
79 ISequence<?> result = expr.accept(dynamicContext, focus);
80 assertEquals(ISequence.of(expectedResult), result);
81
82 result = MetapathExpression.compile(ObjectUtils.notNull(
83 new StringBuilder()
84 .append(bool1.toBoolean() ? "true()" : "false()")
85 .append(" and ")
86 .append(bool2.toBoolean() ? "true()" : "false()")
87 .toString()))
88 .evaluate();
89 assertEquals(ISequence.of(expectedResult), result, "Sequence does not match");
90 }
91 }