package org.commcare.android.tests;

import org.commcare.suite.model.graph.Graph;
import org.commcare.suite.model.graph.XYSeries;

import org.javarosa.core.model.instance.TreeReference;
import org.javarosa.core.model.data.IntegerData;
import org.javarosa.core.model.data.StringData;
import org.javarosa.core.model.condition.EvaluationContext;
import org.javarosa.core.model.instance.FormInstance;
import org.javarosa.xpath.test.XPathEvalTest;
import org.javarosa.xpath.parser.XPathSyntaxException;

import org.commcare.CommCareApplication;
import org.commcare.android.CommCareTestRunner;
import org.commcare.dalvik.BuildConfig;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.annotation.Config;

import java.util.Vector;


@Config(application = CommCareApplication.class,
        constants = BuildConfig.class)
@RunWith(CommCareTestRunner.class)
/* TODO: don't extend XPathEvalTest, split out a utils file */
public class GraphTest extends XPathEvalTest {

    @Test
    private void testSeriesNodeSetExpansion() throws XPathSyntaxException {
        EvaluationContext ec = getFunctionHandlers();
        FormInstance instance = createTestInstance();

        addDataRef(instance, "/data/flag", new IntegerData(2));
        addDataRef(instance, "/data/three[1]", new StringData("alpha"));
        addDataRef(instance, "/data/three[2]", new StringData("beta"));
        addDataRef(instance, "/data/three[3]", new StringData("delta"));
        addDataRef(instance, "/data/two[0]", new StringData("omega"));
        addDataRef(instance, "/data/two[1]", new StringData("psi"));

        XYSeries pathSeries = new XYSeries("/data/two");
        Vector<TreeReference> pathNodes = Graph.expandNodeSet(pathSeries, ec);
        Assert.assertEquals(pathNodes.size(), 2);

        XYSeries expressionSeries = new XYSeries("if(/data/flag mod 2 = 1, /data/three, /data/two)");
        Vector<TreeReference> expressionNodes = Graph.expandNodeSet(expressionSeries, ec);
        Assert.assertEquals(expressionNodes.size(), 3);
    }

}
@@ -1025,14 +1025,13 @@
    p
-rivate
+ublic
  voi
@@ -1099,16 +1099,18 @@
         
+//
 Evaluati
@@ -1200,16 +1200,80 @@
 tance();
+%0A        EvaluationContext ec = new EvaluationContext(instance);
 %0A%0A      
@@ -1325,17 +1325,17 @@
 gerData(
-2
+1
 ));%0A    
@@ -1592,9 +1592,9 @@
 two%5B
-0
+1
 %5D%22, 
@@ -1651,33 +1651,33 @@
 nce, %22/data/two%5B
-1
+2
 %5D%22, new StringDa
@@ -1853,16 +1853,19 @@
 tEquals(
+2, 
 pathNode
@@ -1876,11 +1876,8 @@
 ze()
-, 2
 );%0A%0A
@@ -1944,19 +1944,14 @@
 lag 
-mod 2 
 = 1, 
+'
 /dat
@@ -1957,18 +1957,20 @@
 ta/three
+'
 , 
+'
 /data/tw
@@ -1970,16 +1970,17 @@
 data/two
+'
 )%22);%0A   
@@ -2095,16 +2095,19 @@
 tEquals(
+3, 
 expressi
@@ -2124,11 +2124,8 @@
 ze()
-, 3
 );%0A 

Make test pass