package org.smoothbuild.acceptance.lang;

import static org.hamcrest.Matchers.containsString;
import static org.smoothbuild.acceptance.ArrayMatcher.isArrayWith;
import static org.smoothbuild.acceptance.FileArrayMatcher.isFileArrayWith;
import static org.smoothbuild.acceptance.FileContentMatcher.hasContent;
import static org.testory.Testory.then;

import org.junit.Test;
import org.smoothbuild.acceptance.AcceptanceTestCase;

public class ArtifactTest extends AcceptanceTestCase {

  @Test
  public void store_string_artifact() throws Exception {
    givenScript("result = 'abc';");
    whenSmoothBuild("result");
    thenFinishedWithSuccess();
    then(artifact("result"), hasContent("abc"));
  }

  @Test
  public void store_blob_artifact() throws Exception {
    givenFile("file.txt", "abc");
    givenScript("result = file('//file.txt') | content;");
    whenSmoothBuild("result");
    thenFinishedWithSuccess();
    then(artifact("result"), hasContent("abc"));
  }

  @Test
  public void store_file_artifact() throws Exception {
    givenFile("file.txt", "abc");
    givenScript("result = file('//file.txt');");
    whenSmoothBuild("result");
    thenFinishedWithSuccess();
    then(artifact("result"), hasContent("abc"));
  }

  @Test
  public void store_array_of_strings_artifact() throws Exception {
    givenScript("result = ['abc', 'def'];");
    whenSmoothBuild("result");
    thenFinishedWithSuccess();
    then(artifact("result"), isArrayWith("abc", "def"));
  }

  @Test
  public void store_array_of_nothings_artifact() throws Exception {
    givenScript("result = [];");
    whenSmoothBuild("result");
    thenFinishedWithSuccess();
    then(artifact("result"), isArrayWith());
  }

  @Test
  public void store_array_of_blobs_artifact() throws Exception {
    givenFile("file1.txt", "abc");
    givenFile("file2.txt", "def");
    givenScript("result = [content(file('//file1.txt')), content(file('//file2.txt'))];");
    whenSmoothBuild("result");
    thenFinishedWithSuccess();
    then(artifact("result"), isArrayWith("abc", "def"));
  }

  @Test
  public void store_array_of_files_artifact() throws Exception {
    givenFile("file1.txt", "abc");
    givenFile("file2.txt", "def");
    givenScript("result = [file('//file1.txt'), file('//file2.txt')];");
    whenSmoothBuild("result");
    thenFinishedWithSuccess();
    then(artifact("result"), isFileArrayWith("file1.txt", "abc", "file2.txt", "def"));
  }

  @Test
  public void cannot_store_array_of_files_with_duplicated_paths() throws Exception {
    givenFile("file.txt", "abc");
    givenScript("result = [file('//file.txt'), file('//file.txt')];");
    whenSmoothBuild("result");
    thenFinishedWithError();
    then(output(), containsString(
        "Can't store result of 'result' as it contains files with duplicated paths:\n"
            + "  file.txt\n"));
  }

  @Test
  public void info_about_stored_artifacts_is_printed_to_console() throws Exception {
    givenScript("result1 = 'abc';"
        + "      result2 = 'abc';"
        + "      result3 = 'abc';");
    whenSmoothBuild("result2 result3 result1");
    thenFinishedWithSuccess();
    then(output(), containsString("built artifact(s):\n"
        + "result1 -> '.smooth/artifacts/result1'\n"
        + "result2 -> '.smooth/artifacts/result2'\n"
        + "result3 -> '.smooth/artifacts/result3'\n"));
  }
}

@@ -1219,32 +1219,267 @@
 t(%22abc%22));%0A  %7D%0A%0A
+  @Test%0A  public void store_empty_array_of_strings_artifact() throws Exception %7B%0A    givenScript(%22%5BString%5D result = %5B%5D;%22);%0A    whenSmoothBuild(%22result%22);%0A    thenFinishedWithSuccess();%0A    then(artifact(%22result%22), isArrayWith());%0A  %7D%0A%0A
   @Test%0A  public
@@ -1529,32 +1529,32 @@
 ows Exception %7B%0A
-
     givenScript(
@@ -1919,32 +1919,263 @@
 ayWith());%0A  %7D%0A%0A
+  @Test%0A  public void store_empty_array_of_blobs_artifact() throws Exception %7B%0A    givenScript(%22%5BBlob%5D result = %5B%5D;%22);%0A    whenSmoothBuild(%22result%22);%0A    thenFinishedWithSuccess();%0A    then(artifact(%22result%22), isArrayWith());%0A  %7D%0A%0A
   @Test%0A  public
@@ -2446,32 +2446,32 @@
 dWithSuccess();%0A
-
     then(artifac
@@ -2508,32 +2508,263 @@
 , %22def%22));%0A  %7D%0A%0A
+  @Test%0A  public void store_empty_array_of_files_artifact() throws Exception %7B%0A    givenScript(%22%5BFile%5D result = %5B%5D;%22);%0A    whenSmoothBuild(%22result%22);%0A    thenFinishedWithSuccess();%0A    then(artifact(%22result%22), isArrayWith());%0A  %7D%0A%0A
   @Test%0A  public

add tests for storing empty arrays of different types to ArtifactTest