Skip to content

Commit

Permalink
Create test to describe the behavior of TreeReference.contextualize
Browse files Browse the repository at this point in the history
Also, take the chance to fix the new ref creation to copy not only the context's multiplicity value, but its predicates too.
  • Loading branch information
ggalmazor committed Dec 11, 2019
1 parent 96ec9a6 commit 11262ae
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,7 @@ public TreeReference contextualize (TreeReference contextRef) {
//may have been done.
if(newRef.getPredicate(i) == null) {
newRef.setMultiplicity(i, contextRef.getMultiplicity(i));
newRef.addPredicate(i, contextRef.getPredicate(i));
}
} else {
break;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Copyright 2019 Nafundi
*
* 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 org.javarosa.core.model.instance;

import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.nullValue;
import static org.javarosa.core.model.instance.TestHelpers.buildRef;
import static org.junit.Assert.assertThat;

import java.util.Arrays;
import java.util.Collection;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

@RunWith(Parameterized.class)
public class TreeReferenceContextualizeTest {
@Parameterized.Parameter(value = 0)
public String testCase;

@Parameterized.Parameter(value = 1)
public TreeReference a;

@Parameterized.Parameter(value = 2)
public TreeReference b;

@Parameterized.Parameter(value = 3)
public TreeReference expectedResult;

@Parameterized.Parameters(name = "{0}")
public static Collection<Object[]> data() {
return Arrays.asList(new Object[][]{
// Self references
{"Relative context ref", buildRef("bar"), buildRef("foo"), null},
{"Non-matching absolute refs", buildRef("/foo"), buildRef("/bar"), buildRef("/foo")},
{"Contextualizing a ref with itself and multiplicity #1", buildRef("/foo"), buildRef("/foo[2]"), buildRef("/foo[2]")},
{"Contextualizing a ref with itself and multiplicity #2", buildRef("/foo[-1]"), buildRef("/foo[2]"), buildRef("/foo[-1]")},
{"Contextualizing a ref with itself and multiplicity #3", buildRef("/foo[0]"), buildRef("/foo[2]"), buildRef("/foo[0]")},
{"Contextualizing a ref with itself and multiplicity #4", buildRef("/foo[3]"), buildRef("/foo[2]"), buildRef("/foo[3]")},
{"Contextualizing a ref with itself and predicate #1", buildRef("/foo"), buildRef("/foo[position() = 3]"), buildRef("/foo[position() = 3]")},
{"Contextualizing a ref with itself and predicate #2", buildRef("/foo[position() = 2]"), buildRef("/foo[position() = 3]"), buildRef("/foo[position() = 2]")},
{"Wildcards #1", buildRef("bar"), buildRef("/foo/*"), buildRef("/foo/*/bar")},
{"Wildcards #2", buildRef("../baz"), buildRef("/foo/*/bar"), buildRef("/foo/*/baz")},
{"Wildcards #3", buildRef("*/baz"), buildRef("/foo/*/bar"), buildRef("/foo/*/bar/*/baz")},
{"Wildcards #4", buildRef("*/bar"), buildRef("/foo"), buildRef("/foo/*/bar")},
});
}


@Test
public void contextualize() {
assertThat(
a.contextualize(b),
expectedResult == null
? nullValue()
: is(expectedResult)
);
}

}

0 comments on commit 11262ae

Please sign in to comment.