/*******************************************************************************
 * Copyright (C) 2011, Google Inc.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *******************************************************************************/
package com.google.eclipse.mechanic.internal;

import java.util.Arrays;

import junit.framework.TestCase;

import com.google.eclipse.mechanic.tests.internal.RunAsJUnitTest;

/**
 * Tests for {@link TaskSourceParser}.
 */
@RunAsJUnitTest
public class TaskSourceParserTest extends TestCase {
  private static class TestParser extends TaskSourceParser {
    @Override
    public String doVariableSubstitution(String val) {
      return val;
    }
  };
  private TaskSourceParser parser = new TestParser();

  public void testParse() {
    assertResults(parser.parse(""));
    assertResults(parser.parse("x"), "x");
    assertResults(parser.parse("x:y"), "x", "y");
    assertResults(parser.parse(
        "['/home/user/path/.eclipse','http://www.google.com/directory'," +
            "'https://www.yahoo.com/directory?param\\u003dvalue%amp;term']"),
        "/home/user/path/.eclipse", "http://www.google.com/directory",
            "https://www.yahoo.com/directory?param=value%amp;term");
  }

  public void testUnparse() {
    assertEquals("[]", createList());
    assertEquals("[\"x\"]", createList("x"));
    assertEquals("[\"x\",\"x\"]", createList("x", "x"));
    assertEquals(
        "[\"/home/user/path/.eclipse\",\"http://www.google.com/directory\"," +
            "\"https://www.yahoo.com/directory?param\\u003dvalue%amp;term\"]",
        createList("/home/user/path/.eclipse", "http://www.google.com/directory",
            "https://www.yahoo.com/directory?param=value%amp;term"));
  }

  public void testRoundTrip() {
    // TODO before submit
  }

  private String createList(String... items) {
    return parser.unparse(items);
  }

  private void assertResults(String[] actual, String... expected) {
    if (!Arrays.deepEquals(actual, expected)) {
      fail("Expected " + Arrays.deepToString(actual) + " but got " +
          Arrays.deepToString(actual));
    }
    assertEquals(actual.length, expected.length);
    for (int i = 0; i < actual.length; i++) {
      assertEquals(actual[i], expected[i]);
    }
  }
}

@@ -1976,29 +1976,471 @@
     
-// TODO before submit
+String json = %22%5B%5C%22/home/user/path/.eclipse%5C%22,%5C%22http://www.google.com/directory%5C%22,%22 +%0A        %22%5C%22https://www.yahoo.com/directory?param%5C%5Cu003dvalue%25amp;term%5C%22%5D%22;%0A    assertEquals(json, parser.unparse(parser.parse(json)));%0A%0A    String%5B%5D items = new String%5B%5D %7B %22/home/user/path/.eclipse%22, %22http://www.google.com/directory%22,%0A        %22https://www.yahoo.com/directory?param=value%25amp;term%22%7D;%0A    assertTrue(Arrays.deepEquals(items, parser.parse(parser.unparse(items))));
 %0A  %7D

Add missing test to TaskSourceParserTest