/*
 * @author Ranides Atterwim <ranides@gmail.com>
 * @copyright Ranides Atterwim
 * @license WTFPL
 * @url http://ranides.net/projects/assira
 */
package net.ranides.assira.text;

import java.io.IOException;
import org.junit.Test;
import static org.junit.Assert.*;

/**
 *
 * @author Ranides Atterwim <ranides@gmail.com>
 */
public class ResolveFormatUtilsTest {
    
    @Test
    public void testFormat_Array() {
        String r1 = ResolveFormatUtils.format("Hello {1} world {2}{3}!", new int[]{17, 19, 47, 33});
        String r2 = ResolveFormatUtils.vformat("Hello {1} world {2}{3}!", 17, 19, 47, 33);
        String r3 = ResolveFormatUtils.vformat("Hello {0} world!", 17);
        
        assertEquals("Hello 19 world 4733!", r1);
        assertEquals("Hello 19 world 4733!", r2);
        assertEquals("Hello 17 world!", r3);
    }
    
    @Test
    public void testFormat_Append() throws IOException {
        StringBuilder r1 = new StringBuilder().append("r1 = ");
        StringBuilder r2 = new StringBuilder().append("r2 = ");
        StringBuilder r3 = new StringBuilder().append("r3 = ");
        ResolveFormatUtils.format(r1, "Hello {1} world {2}{3}!", new int[]{17, 19, 47, 33});
        ResolveFormatUtils.vformat(r2, "Hello {1} world {2}{3}!", 17, 19, 47, 33);
        ResolveFormatUtils.vformat(r3, "Hello {0} world!", 17);
        
        assertEquals("r1 = Hello 19 world 4733!", r1.toString());
        assertEquals("r2 = Hello 19 world 4733!", r2.toString());
        assertEquals("r3 = Hello 17 world!", r3.toString());
    }
    
    @Test
    public void testCompileFormat_Array() {
        String r2 = ResolveFormat.compile("Hello {1} world {2}{3}!").vformat(17, 19, 47, 33);
        String r3 = ResolveFormat.compile("Hello {0} world!").vformat(17);
        
        assertEquals("Hello 19 world 4733!", r2);
        assertEquals("Hello 17 world!", r3);
    }
    
    @Test
    public void testCompileFormat_Append() throws IOException {
        StringBuilder r2 = new StringBuilder().append("r2 = ");
        StringBuilder r3 = new StringBuilder().append("r3 = ");
        ResolveFormat.compile("Hello {1} world {2}{3}!").vformat(r2, 17, 19, 47, 33);
        ResolveFormat.compile("Hello {0} world!").vformat(r3, 17);
        
        assertEquals("r2 = Hello 19 world 4733!", r2.toString());
        assertEquals("r3 = Hello 17 world!", r3.toString());
    }
    
    @Test
    public void testParse() {
        int[] out = new int[2];
        ResolveFormatUtils.parse("{0,number} / {1,number}", "7 / 9", out);
        assertArrayEquals(new int[]{7,9}, out);
    }
}
