package org.tshlabs.baja;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import static org.junit.Assert.assertEquals;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;


public class RedisCommandTest {

    private static <T> List<T> newList(T... things) {
        final List<T> out = new ArrayList<>();
        Collections.addAll(out, things);
        return out;
    }

    @Test
    public void testSingleArg() {
        final RedisCommand cmd = RedisCommand.cmd("INFO");
        assertEquals(newList("INFO"), cmd.getArgs());
    }

    @Test
    public void testArgString() {
        final RedisCommand cmd = RedisCommand.cmd("GET").arg("foo");
        assertEquals(newList("GET", "foo"), cmd.getArgs());
    }

    @Test
    public void testArgLong() {
        final RedisCommand cmd = RedisCommand.cmd("SETEX").arg("foo").arg(123L);
        assertEquals(newList("SETEX", "foo", "123"), cmd.getArgs());
    }

    @Test
    public void testArgInt() {
        final RedisCommand cmd = RedisCommand.cmd("SETEX").arg("foo").arg(123);
        assertEquals(newList("SETEX", "foo", "123"), cmd.getArgs());
    }

    @Test
    public void testArgFloat() {
        final RedisCommand cmd = RedisCommand.cmd("INCRBYFLOAT").arg("foo").arg(3.14);
        assertEquals(newList("INCRBYFLOAT", "foo", "3.14"), cmd.getArgs());
    }

    @Test
    public void testArgDouble() {
        final RedisCommand cmd = RedisCommand.cmd("INCRBYFLOAT").arg("foo").arg(3.14D);
        assertEquals(newList("INCRBYFLOAT", "foo", "3.14"), cmd.getArgs());
    }

    @Test
    public void testQuery() {
        final RedisConnection conn = mock(RedisConnection.class);
        final RedisCommand.ExecutedRedisCommand res = RedisCommand.cmd("SET")
                .arg("foo")
                .arg(123)
                .query(conn);

        verify(conn).writeCommand(eq(newList("SET", "foo", "123")));
    }
}

@@ -70,65 +70,9 @@
 rray
-List;%0Aimport java.util.Collections;%0Aimport java.util.List
+s
 ;%0A%0Ai
@@ -275,177 +275,8 @@
  %7B%0A%0A
-    private static %3CT%3E List%3CT%3E newList(T... things) %7B%0A        final List%3CT%3E out = new ArrayList%3C%3E();%0A        Collections.addAll(out, things);%0A        return out;%0A    %7D%0A%0A
     
@@ -387,35 +387,41 @@
    assertEquals(
-new
+Arrays.as
 List(%22INFO%22), cm
@@ -571,27 +571,33 @@
 ssertEquals(
-new
+Arrays.as
 List(%22GET%22, 
@@ -763,35 +763,41 @@
    assertEquals(
-new
+Arrays.as
 List(%22SETEX%22, %22f
@@ -970,27 +970,33 @@
 ssertEquals(
-new
+Arrays.as
 List(%22SETEX%22
@@ -1178,35 +1178,41 @@
    assertEquals(
-new
+Arrays.as
 List(%22INCRBYFLOA
@@ -1407,19 +1407,25 @@
 tEquals(
-new
+Arrays.as
 List(%22IN
@@ -1785,11 +1785,17 @@
 (eq(
-new
+Arrays.as
 List
@@ -1825,10 +1825,12 @@
 ;%0A    %7D%0A
+%0A%0A
 %7D%0A

Use builtin method for creating ad-hoc lists