/*
 * @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 java.util.regex.Matcher;
import java.util.regex.Pattern;
import net.ranides.assira.generic.Serializer;
import org.junit.Test;
import static org.junit.Assert.*;

/**
 *
 * @author Ranides Atterwim <ranides@gmail.com>
 */
public class ResolveFormatterTest {
    
    public ResolveFormatterTest() {
    }
    
    @Test
    public void testSerialization() throws IOException {
        ResolveFormatter a = ResolveFormatter.compile("Hello {1} world!");
        ResolveFormatter b = Serializer.copy(a);
        
        assertNotSame(a, b);
        assertEquals(a, b);
    }

    @Test
    public void testArray() {
        String ret = ResolveFormatter.format("Hello {1} world {2}{3}!", new int[]{17, 19, 47, 33});
        assertEquals("Hello 19 world 4733!", ret);
    }
    
    @Test
    public void testMatcher() {
        String source ="AH-ello";
        Matcher hit = Pattern.compile("(.)(.)-(.+)").matcher(source);
        assertTrue(hit.find());
        
        String ret = ResolveFormatter.format("Hello {1} world {2}{3}!", hit);
        assertEquals("Hello A world Hello!", ret);
    }
    
    @Test
    public void testComplex() {
        String source ="XY-point";
        Matcher hit = Pattern.compile("(.)(.)-(.+)").matcher(source);
        assertTrue(hit.find());
        
        Wrapper wrapper = new Wrapper("Hello", 77, hit, new Wrapper("world", '?'));
        String r1 = ResolveFormatter.format("{[0]} {[1]} {[2][3]} {[3].items[0]} {[3].items[1]}", wrapper.items);
        assertEquals("Hello 77 point world ?", r1);
        
        String r2 = ResolveFormatter.format("{0} {1} {2.3.4} {3.items.0} {3.items.1}", wrapper.items);
        assertEquals("Hello 77 t world ?", r2);
    }
    
    private static final class Wrapper {
        public Object[] items;

        public Wrapper(Object... items) {
            this.items = items;
        }
        
    }
    
}
