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

import java.util.regex.Pattern;
import org.junit.Test;
import static org.junit.Assert.*;

/**
 *
 * @author Ranides Atterwim <ranides@gmail.com>
 */
@SuppressWarnings("PMD")
public class AnsiPatternTest {
    
    public AnsiPatternTest() {
    }

    @Test
    public void testMatchesString() {
        AnsiCharSequence text = new AnsiString("Hello world 23!");
        
        assertTrue(new AnsiPattern.MSPattern("llo").matches(text));
        assertTrue(new AnsiPattern.MSPattern("23!").matches(text));
        assertFalse(new AnsiPattern.MSPattern(".llo").matches(text));
        assertFalse(new AnsiPattern.MSPattern(".23").matches(text));
    }
    

    @Test
    public void testMatchesRE() {
        AnsiCharSequence text = new AnsiString("Hello world 23!");
        
        assertTrue( new AnsiPattern.REPattern("l+o").matches(text) );
        assertTrue( new AnsiPattern.REPattern("[0-9]+").matches(text) );
        
        assertTrue( new AnsiPattern.REPattern(".llo").matches(text) );
        assertTrue( new AnsiPattern.REPattern(".23").matches(text) );
        
        assertFalse( new AnsiPattern.REPattern("llo\\?").matches(text) );
        assertFalse( new AnsiPattern.REPattern("\\.23").matches(text) );
    }
    
    @Test
    public void testFindString() {
        AnsiCharSequence text = new AnsiString("HellTXTo TXT world TXT!");
        
        for(AnsiMatch item : new AnsiPattern.MSPattern("TXT").find(text)) {
            item.value().setCharAt(1, 'E');
        }
        for(AnsiMatch item : new AnsiPattern.MSPattern("TET").find(text)) {
            item.value().setCharAt(1, 'A');
        }
        for(AnsiMatch item : new AnsiPattern.MSPattern("TUT").find(text)) {
            item.value().setCharAt(1, 'Q');
        }
        assertEquals("HellTATo TAT world TAT!", text.toString());
    }
    
    @Test
    public void testFindRE() {
        AnsiCharSequence text = new AnsiString("Hell4403o 871 world 82!");
        
        for(AnsiMatch item : new AnsiPattern.REPattern("[0-9]+").find(text)) {
            item.value().setCharAt(0, '$');
        }
        for(AnsiMatch item : new AnsiPattern.REPattern("[0-9]").find(text)) {
            item.value().setCharAt(0, '#');
        }
        for(AnsiMatch item : new AnsiPattern.REPattern("[0-9]").find(text)) {
            item.value().setCharAt(0, '?');
        }
        
        assertEquals("Hell$###o $## world $#!", text.toString());
    }
    
    @Test
    public void testFindGroups() {
        AnsiCharSequence text = new AnsiString("Hell4403o 871 world 825!");
        
        for(AnsiMatch item : new AnsiPattern.REPattern("[0-9]([0-9]+)").find(text)) {
            item.group(1).setCharAt(0, '$');
        }
        
        assertEquals("Hell4$03o 8$1 world 8$5!", text.toString());
    }
    
    @Test
    public void testApplyString() {
        AnsiAttr a1 = AnsiStrings.attr(1, 2);
        AnsiAttr a2 = AnsiStrings.attr(3, 4);
        
        AnsiCharSequence text = new AnsiString("Hello world 23!");
        new AnsiPattern.MSPattern("o").apply(text, a1);
        new AnsiPattern.MSPattern("or").apply(text, a2);
        
        AnsiCharSequence exp = new AnsiString("Hello world 23!");
        exp.setAttr(4, 5, a1);
        exp.setAttr(7, 8, a1);
        exp.setAttr(7, 9, a2);
        assertEquals(exp, text);
    }
    
    @Test
    public void testApplyRE() {
        AnsiAttr a1 = AnsiStrings.attr(1, 2);
        
        AnsiCharSequence text = new AnsiString("Hello 170 world 23!");
        new AnsiPattern.REPattern("[0-9]+").apply(text, a1);
        
        AnsiCharSequence exp = new AnsiString("Hello 170 world 23!");
        exp.setAttr(6, 9, a1);
        exp.setAttr(16, 18, a1);
        assertEquals(exp, text);
    }
    
     @Test
    public void testApplyGroups() {
        AnsiAttr a1 = AnsiStrings.attr(1, 2);
        AnsiCharSequence text = new AnsiString("Hell4403o 871 world 825!");
        
        new AnsiPattern.REPattern("[0-9]([0-9]+)").apply(text, a1);
        
        AnsiCharSequence exp = new AnsiString("Hell4403o 871 world 825!");
        exp.setAttr(5, 8, a1);
        exp.setAttr(11, 13, a1);
        exp.setAttr(21, 23, a1);
        assertEquals(exp, text);
    }
    
}
