<commit_before>/*
 * Copyright 2013 Red Hat, Inc. and/or its affiliates.
 *
 * Licensed under the Eclipse Public License version 1.0, available at
 * http://www.eclipse.org/legal/epl-v10.html
 */
package org.jboss.forge.addon.shell.parser;

import java.io.IOException;
import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import javax.inject.Inject;

import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.forge.addon.shell.mock.wizard.MockWizardBegin;
import org.jboss.forge.addon.shell.mock.wizard.MockWizardStep;
import org.jboss.forge.addon.shell.test.ShellTest;
import org.jboss.forge.arquillian.AddonDependency;
import org.jboss.forge.arquillian.Dependencies;
import org.jboss.forge.arquillian.archive.ForgeArchive;
import org.jboss.forge.furnace.repositories.AddonDependencyEntry;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.junit.After;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;

/**
 * @author <a href="mailto:<EMAIL>"><NAME>, III</a>
 */
@RunWith(Arquillian.class)
public class WizardCompletionTest
{
   private static final int TIMEOUT = 5;

   @Deployment
   @Dependencies({
            @AddonDependency(name = "org.jboss.forge.addon:shell-test-harness")
   })
   public static ForgeArchive getDeployment()
   {
      ForgeArchive archive = ShrinkWrap.create(ForgeArchive.class)
               .addClasses(MockWizardBegin.class, MockWizardStep.class)
               .addBeansXML()
               .addAsAddonDependencies(
                        AddonDependencyEntry.create("org.jboss.forge.addon:shell-test-harness"),
                        AddonDependencyEntry.create("org.jboss.forge.furnace.container:cdi")
               );

      return archive;
   }

   @Inject
   private ShellTest test;

   @After
   public void after() throws IOException
   {
      test.clearScreen();
   }

   @Test(timeout = 10000)
   public void testWizardInitialStepAutocomplete() throws Exception
   {
      test.clearScreen();
      assertCompletionStep("mockwizard ", "mockw");
      assertCompletionStep("mockwizard --values ", "--v");
      String stdout = assertCompletionStepWithSuggestions("mockwizard --values foo --", "foo --");

      Assert.assertTrue(stdout.contains("--proceed"));
      Assert.assertTrue(stdout.contains("--key"));
      Assert.assertFalse(stdout.contains("--selections"));
      Assert.assertFalse(stdout.contains("--done"));

      assertCompletionStep("mockwizard --values foo --proceed ", "p");
      stdout = assertCompletionStepWithSuggestions("mockwizard --values foo --proceed --", "--");

      Assert.assertTrue(stdout.contains("--key"));
      Assert.assertTrue(stdout.contains("--done"));
      Assert.assertTrue(stdout.contains("--selections"));

      assertCompletionStep("mockwizard --values foo --proceed --selections ", "sel");
      stdout = assertCompletionStepWithSuggestions("mockwizard --values foo --proceed --selections blah --", "blah --");
      Assert.assertFalse(stdout.contains("--key"));
      Assert.assertTrue(stdout.contains("--done"));
   }

   @Test(timeout = 10000)
   public void testWizardInitialStepAutocompleteBooleanFlagWithValue() throws Exception
   {
      test.clearScreen();
      assertCompletionStep("mockwizard ", "mockw");
      assertCompletionStep("mockwizard --values ", "--v");
      String stdout = assertCompletionStepWithSuggestions("mockwizard --values foo --", "foo --");

      Assert.assertTrue(stdout.contains("--proceed"));
      Assert.assertTrue(stdout.contains("--key"));
      Assert.assertFalse(stdout.contains("--selections"));
      Assert.assertFalse(stdout.contains("--done"));

      assertCompletionStep("mockwizard --values foo --proceed ", "p");
      stdout = assertCompletionStepWithSuggestions("mockwizard --values foo --proceed true --", "true --");

      Assert.assertTrue(stdout.contains("--key"));
      Assert.assertTrue(stdout.contains("--done"));
      Assert.assertTrue(stdout.contains("--selections"));

      assertCompletionStep("mockwizard --values foo --proceed true --selections ", "sel");
   }

   private void assertCompletionStep(final String expected, final String write) throws TimeoutException
   {
      test.waitForBufferValue(new Callable<String>()
      {
         @Override
         public String call() throws Exception
         {
            test.write(write);
            test.sendCompletionSignal();
            return null;
         }
      }, TIMEOUT, TimeUnit.SECONDS, expected);
      Assert.assertEquals(expected, test.getBuffer().getLine());
   }

   private String assertCompletionStepWithSuggestions(final String expected, final String write) throws TimeoutException
   {
      test.waitForStdOutValue(new Callable<Void>()
      {
         @Override
         public Void call() throws Exception
         {
            test.waitForBufferValue(new Callable<String>()
            {
               @Override
               public String call() throws Exception
               {
                  test.write(write);
                  test.sendCompletionSignal();
                  return null;
               }
            }, TIMEOUT, TimeUnit.SECONDS, expected);
            Assert.assertEquals(expected, test.getBuffer().getLine());
            return null;
         }
      }, TIMEOUT, TimeUnit.SECONDS, expected);
<commit_msg>Using Assert.assertThat for better error messages<commit_after>/*
 * Copyright 2013 Red Hat, Inc. and/or its affiliates.
 *
 * Licensed under the Eclipse Public License version 1.0, available at
 * http://www.eclipse.org/legal/epl-v10.html
 */
package org.jboss.forge.addon.shell.parser;

import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.not;

import java.io.IOException;
import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import javax.inject.Inject;

import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.forge.addon.shell.mock.wizard.MockWizardBegin;
import org.jboss.forge.addon.shell.mock.wizard.MockWizardStep;
import org.jboss.forge.addon.shell.test.ShellTest;
import org.jboss.forge.arquillian.AddonDependency;
import org.jboss.forge.arquillian.Dependencies;
import org.jboss.forge.arquillian.archive.ForgeArchive;
import org.jboss.forge.furnace.repositories.AddonDependencyEntry;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.junit.After;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;

/**
 * @author <a href="mailto:<EMAIL>"><NAME>, III</a>
 */
@RunWith(Arquillian.class)
public class WizardCompletionTest
{
   private static final int TIMEOUT = 5;

   @Deployment
   @Dependencies({
            @AddonDependency(name = "org.jboss.forge.addon:shell-test-harness")
   })
   public static ForgeArchive getDeployment()
   {
      ForgeArchive archive = ShrinkWrap.create(ForgeArchive.class)
               .addClasses(MockWizardBegin.class, MockWizardStep.class)
               .addBeansXML()
               .addAsAddonDependencies(
                        AddonDependencyEntry.create("org.jboss.forge.addon:shell-test-harness"),
                        AddonDependencyEntry.create("org.jboss.forge.furnace.container:cdi")
               );

      return archive;
   }

   @Inject
   private ShellTest test;

   @After
   public void after() throws IOException
   {
      test.clearScreen();
   }

   @Test(timeout = 10000)
   public void testWizardInitialStepAutocomplete() throws Exception
   {
      test.clearScreen();
      assertCompletionStep("mockwizard ", "mockw");
      assertCompletionStep("mockwizard --values ", "--v");
      String stdout = assertCompletionStepWithSuggestions("mockwizard --values foo --", "foo --");

      Assert.assertThat(stdout, containsString("--proceed"));
      Assert.assertThat(stdout, containsString("--key"));
      Assert.assertThat(stdout, not(containsString("--selections")));
      Assert.assertThat(stdout, not(containsString("--done")));

      assertCompletionStep("mockwizard --values foo --proceed ", "p");
      stdout = assertCompletionStepWithSuggestions("mockwizard --values foo --proceed --", "--");

      Assert.assertThat(stdout, containsString("--key"));
      Assert.assertThat(stdout, containsString("--done"));
      Assert.assertThat(stdout, containsString("--selections"));

      assertCompletionStep("mockwizard --values foo --proceed --selections ", "sel");
      stdout = assertCompletionStepWithSuggestions("mockwizard --values foo --proceed --selections blah --", "blah --");
      Assert.assertThat(stdout, not(containsString("--key")));
      Assert.assertThat(stdout, containsString("--done"));
   }

   @Test(timeout = 10000)
   public void testWizardInitialStepAutocompleteBooleanFlagWithValue() throws Exception
   {
      test.clearScreen();
      assertCompletionStep("mockwizard ", "mockw");
      assertCompletionStep("mockwizard --values ", "--v");
      String stdout = assertCompletionStepWithSuggestions("mockwizard --values foo --", "foo --");

      Assert.assertThat(stdout, containsString("--proceed"));
      Assert.assertThat(stdout, containsString("--key"));
      Assert.assertThat(stdout, not(containsString("--selections")));
      Assert.assertThat(stdout, not(containsString("--done")));

      assertCompletionStep("mockwizard --values foo --proceed ", "p");
      stdout = assertCompletionStepWithSuggestions("mockwizard --values foo --proceed true --", "true --");

      Assert.assertThat(stdout, containsString("--key"));
      Assert.assertThat(stdout, containsString("--done"));
      Assert.assertThat(stdout, containsString("--selections"));

      assertCompletionStep("mockwizard --values foo --proceed true --selections ", "sel");
   }

   private void assertCompletionStep(final String expected, final String write) throws TimeoutException
   {
      test.waitForBufferValue(new Callable<String>()
      {
         @Override
         public String call() throws Exception
         {
            test.write(write);
            test.sendCompletionSignal();
            return null;
         }
      }, TIMEOUT, TimeUnit.SECONDS, expected);
      Assert.assertThat(test.getBuffer().getLine(), equalTo(expected));
   }

   private String assertCompletionStepWithSuggestions(final String expected, final String write)
            throws TimeoutException
   {
      test.waitForStdOutValue(new Callable<Void>()
      {
         @Override
         public Void call() throws Exception
         {
            test.waitForBufferValue(new Callable<String>()
            {
               @Override
               public String call() throws Exception
               {
                  test.write(write);
                  test.sendCompletionSignal();
                  return null;
               }
            }, TIMEOUT, TimeUnit.SECONDS, expected);
            Assert.assertThat(test.getBuffer().getLine(), equalTo(expected));
            return null;
         }
      }, TIMEOUT, TimeUnit.SECONDS, expected);

{'fraction_non_alphanumeric': 0.12838738338516215, 'fraction_numerical': 0.0033762772101288314, 'mean_word_length': 3.105032822757112, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5455859', 'n_tokens_mistral': 3250, 'n_tokens_neox': 3083, 'n_words': 566}