package com.atlassian.stash.internal.web.soy.functions;

import com.atlassian.soy.renderer.JsExpression;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.runners.MockitoJUnitRunner;

import java.util.ArrayList;

import static org.junit.Assert.assertEquals;

@RunWith(MockitoJUnitRunner.class)
public class FirstNonNullFunctionTest {

    @InjectMocks
    private FirstNonNullFunction fn;

    @Test
    public void testBasicUsage() throws Exception {
        assertEquals("one", fn.apply("one", "two", "three"));
        assertEquals("two", fn.apply(null, "two"));
    }

    @Test
    public void testAllNulls() throws Exception {
        assertEquals(null, fn.apply(null, null, null));
    }

    @Test
    public void testFalseParams() throws Exception {
        assertEquals(false, fn.apply(null, false, "something"));
    }

    @Test
    public void testDifferentObjectTypes() throws Exception {
        ArrayList a = new ArrayList();
        Integer i = new Integer(5);
        assertEquals(a, fn.apply(null, a, i));
    }

    @Test
    public void testSimpleJavascriptGeneration() throws Exception {
        assertEquals("[\"one\",\"two\"].filter(function(x){return x != null;})[0]", fn.generate(new JsExpression("\"one\""), new JsExpression("\"two\"")).getText());
    }

    @Test
    public void testOptDataJavascriptGeneration() throws Exception {
        assertEquals("[opt_data ? opt_data.param : null,\"one\"].filter(function(x){return x != null;})[0]", fn.generate(new JsExpression("opt_data.param"), new JsExpression("\"one\"")).getText());
    }
}
