/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

import java.util.Arrays;
import java.util.List;
import static junit.framework.Assert.assertEquals;
import org.junit.Test;

/**
 *
 * @author martin
 */
public class ArrayToJson {

    static final List<String> from = Arrays.asList("un","deux","trois","quatre");
    static final String to = "[un,deux,trois,quatre]";

    @Test
    public void algo1() {
        String result = "";
        boolean first=true;
        for (String val : from){
            result+=(first?"[":",")+val;
            first=false;
        }
        result += "]";
        System.out.println(result);
        assertEquals(to,result);
    }
    
    public static class Co{
        boolean first;
        public Co(){
            first=true;
        }
        public String next(){
            if (first){
                first=false;
                return "[";
            }
            return ",";
        }
    }
    
    @Test
    public void algo2(){
        String attendu = "";
        Co c = new Co();
        for (String s : from){
            attendu += c.next() + s;
        }
        attendu +="]";
        
        assertEquals(to,attendu);
    }
    
}
