package helloworld;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;

import org.junit.Test;
import org.requirementsascode.AbstractActor;
import org.requirementsascode.ModelRunner;

import helloworld.actor.InvalidUser;
import helloworld.actor.ValidUser;
import helloworld.command.EnterText;

public class HelloWorldTest {
	@Test
	public void testHelloWorld01() {
		HelloWorldActor01 actor = new HelloWorldActor01(() -> {});
		
		recordStepNamesOf(actor);
		actor.run();
		assertRecordedStepNames(actor, "S1");
	}

	@Test
	public void testHelloWorld02() {
	  HelloWorldActor02 actor = new HelloWorldActor02(() -> {});
	  
    recordStepNamesOf(actor);
    actor.run();
		assertRecordedStepNames(actor, "S1", "S1", "S1");
	}

	@Test
	public void testHelloWorld03() {
		HelloWorldActor03 actor = new HelloWorldActor03(et -> {});
		
    recordStepNamesOf(actor);
		actor.reactTo(new EnterText("John Q. Public"));
		assertRecordedStepNames(actor, "S1");
	}

  @Test
  public void testHelloWorld03a_validUser() {
    HelloWorldActor03a helloWorldActor = new HelloWorldActor03a(et -> {});
    ValidUser validUser03a = new ValidUser(helloWorldActor);
    helloWorldActor.setValidUser(validUser03a);
    
    recordStepNamesOf(helloWorldActor);
    validUser03a.run();
    assertRecordedStepNames(helloWorldActor, "S1b");
  }

  @Test
  public void testHelloWorld03a_invalidUser() {
    HelloWorldActor03a helloWorldActor = new HelloWorldActor03a(et -> {});
    ValidUser validUser03a = new ValidUser(helloWorldActor);
    InvalidUser invalidUser03a = new InvalidUser(helloWorldActor);
    helloWorldActor.setValidUser(validUser03a);

    recordStepNamesOf(helloWorldActor);
    invalidUser03a.run();
    assertRecordedStepNames(helloWorldActor);
  }

  @Test
	public void testHelloWorld04() {
		HelloWorldActor04 actor = new HelloWorldActor04(et -> {}, et -> {}, () -> {});
    
		recordStepNamesOf(actor);
		actor.reactTo(new EnterText("John"));
    actor.reactTo(new EnterText("39"));
		assertRecordedStepNames(actor, "S1", "S2", "S3");
	}

	@Test
	public void testHelloWorld05_WithCorrectNameAndAge() {
		HelloWorld05 actor = new HelloWorld05();
    ModelRunner modelRunner = actor.getModelRunner();
    modelRunner.startRecording();
    
    actor.run();
    
    actor.reactTo(new EnterText("John"));
    actor.reactTo(new EnterText("39"));
    
		assertRecordedStepNames(actor, "S1", "S2", "S3", "S4", "S5", "S6");
	}

	@Test
	public void testHelloWorld05_WithOutOfBoundsAge() {
    HelloWorld05 actor = new HelloWorld05();
    ModelRunner modelRunner = actor.getModelRunner();
    modelRunner.startRecording();
    
    actor.run();
    
    actor.reactTo(new EnterText("John"));
    actor.reactTo(new EnterText("1000"));

		assertRecordedStepNames(actor, "S1", "S2", "S3", "S4", "S5a_1", "S5a_2", "S3");
	}

	@Test
	public void testHelloWorld05_WithNonNumericalAge() {
    HelloWorld05 actor = new HelloWorld05();
    ModelRunner modelRunner = actor.getModelRunner();
    modelRunner.startRecording();
    
    actor.run();

    actor.reactTo(new EnterText("John"));
    actor.reactTo(new EnterText("NON-NUMERICAL-AGE"));

		assertRecordedStepNames(actor, "S1", "S2", "S3", "S4", "S5b_1", "S5b_2", "S3");
	}

	@Test
	public void testHelloWorld06_AsNormalUser() {
	  HelloWorld06 actor = new HelloWorld06();
    ModelRunner modelRunner = actor.getModelRunner();
    modelRunner.startRecording();
        
    actor.getModelRunner().as(actor.normalUser()).run(actor.behavior());

    actor.reactTo(new EnterText("John"));
    actor.reactTo(new EnterText("39"));

		assertRecordedStepNames(actor, "S1", "S2", "S3", "S4", "S5", "S6", "S7");
	}

	@Test
	public void testHelloWorld06_AsAnonymousUserAgeIsOk() {
    HelloWorld06 actor = new HelloWorld06();
    ModelRunner modelRunner = actor.getModelRunner();
    modelRunner.startRecording();
        
    actor.getModelRunner().as(actor.anonymousUser()).run(actor.behavior());

    actor.reactTo(new EnterText("39"));

		assertRecordedStepNames(actor, "S1a_1", "S3", "S4", "S5c_1", "S6", "S7");
	}

	@Test
	public void testHelloWorld06_AsAnonymousUserHandleNonNumericalAge() {
    HelloWorld06 actor = new HelloWorld06();
    ModelRunner modelRunner = actor.getModelRunner();
    modelRunner.startRecording();
        
    actor.getModelRunner().as(actor.anonymousUser()).run(actor.behavior());

    actor.reactTo(new EnterText("NotANumber"));

		assertRecordedStepNames(actor, "S1a_1", "S3", "S4", "S5b_1", "S5b_2", "S3");
	}
	
  @Test
  public void testHelloWorld07() {
    HelloWorld07 actor = new HelloWorld07();

    actor.inputColor = "yellow";
    actor.run();

    assertEquals("yellow", actor.outputColor);
  }
  
  private void recordStepNamesOf(AbstractActor actor) {
    actor.getModelRunner().startRecording();
  }

	protected void assertRecordedStepNames(AbstractActor actor, String... expectedStepNames) {
		String[] actualStepNames = actor.getModelRunner().getRecordedStepNames();
		assertArrayEquals(expectedStepNames, actualStepNames);
	}
}

@@ -2185,100 +2185,40 @@
 ();%0A
+%09%09%0A
     
-ModelRunner modelRunner = actor.getModelRunner();%0A    modelRunner.startRecording();%0A    
+recordStepNamesOf(actor);
 %0A   
@@ -2223,37 +2223,32 @@
    actor.run();%0A
-    %0A
     actor.reactT
@@ -2313,21 +2313,16 @@
 %2239%22));%0A
-    %0A
 %09%09assert
@@ -2500,96 +2500,38 @@
     
-ModelRunner modelRunner = actor.getModelRunner();%0A    modelRunner.startRecording();%0A    
+%0A    recordStepNamesOf(actor);
 %0A   
@@ -2540,29 +2540,24 @@
 ctor.run();%0A
-    %0A
     actor.re
@@ -2624,25 +2624,24 @@
 t(%221000%22));%0A
-%0A
 %09%09assertReco
@@ -2828,96 +2828,38 @@
     
-ModelRunner modelRunner = actor.getModelRunner();%0A    modelRunner.startRecording();%0A    
+%0A    recordStepNamesOf(actor);
 %0A   
@@ -2868,25 +2868,24 @@
 ctor.run();%0A
-%0A
     actor.re
@@ -2965,25 +2965,24 @@
 CAL-AGE%22));%0A
-%0A
 %09%09assertReco

Move helloworld examples to actor syntax