package com.github.havarunner.rules;

import com.github.havarunner.HavaRunner;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;

import static com.github.havarunner.TestHelper.run;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

public class RuleTest {

    static Description firstRuleDescription;
    static Description secondRuleDescription;
    static boolean testRun;

    @Test
    public void HavaRunner_supports_JUnit_rules() {
        run(new HavaRunner(TestWithTwoRules.class));
        assertEquals("hello", firstRuleDescription.getMethodName());
        assertEquals("hello", secondRuleDescription.getMethodName());
        assertTrue(testRun);
    }

    static class TestWithTwoRules {
        @Rule
        TestRule first = new TestRule() {
            @Override
            public Statement apply(Statement base, Description description) {
                firstRuleDescription = description;
                return base;
            }
        };

        @Rule
        TestRule second = new TestRule() {
            @Override
            public Statement apply(Statement base, Description description) {
                secondRuleDescription = description;
                return base;
            }
        };

        @Test
        void hello() {
            testRun = true;
        }
    }
}

@@ -72,16 +72,56 @@
 Runner;%0A
+import com.google.common.collect.Lists;%0A
 import o
@@ -271,16 +271,69 @@
 ement;%0A%0A
+import java.util.Collection;%0Aimport java.util.List;%0A%0A
 import s
@@ -489,17 +489,16 @@
 eTest %7B%0A
-%0A
     stat
@@ -492,32 +492,43 @@
 st %7B%0A    static 
+Collection%3C
 Description firs
@@ -522,16 +522,17 @@
 cription
+%3E
  firstRu
@@ -529,35 +529,58 @@
 n%3E firstRule
-Description
+Applications= Lists.newArrayList()
 ;%0A    static
@@ -580,16 +580,27 @@
  static 
+Collection%3C
 Descript
@@ -602,16 +602,17 @@
 cription
+%3E
  secondR
@@ -614,26 +614,83 @@
 condRule
-Descriptio
+Applications = Lists.newArrayList();%0A    static boolean firstTestRu
 n;%0A    s
@@ -703,17 +703,23 @@
 boolean 
-t
+secondT
 estRun;%0A
@@ -846,116 +846,637 @@
     
-assertEquals(%22hello%22, firstRuleDescription.getMethodName());%0A        assertEquals(%22hello%22, secondRuleDescrip
+verify(firstRuleApplications);%0A        verify(secondRuleApplications);%0A        assertTrue(firstTestRun);%0A        assertTrue(secondTestRun);%0A    %7D%0A%0A    private void verify(Collection%3CDescription%3E ruleApplications) %7B%0A        assertEquals(2, ruleApplications.size());%0A        HavaRunner_applies_each_rule_on_each_test_method(ruleApplications);%0A    %7D%0A%0A    private void HavaRunner_applies_each_rule_on_each_test_method(Collection%3CDescription%3E ruleApplications) %7B%0A        List%3CString%3E calledTestMethods = Lists.newArrayList();%0A        for (Description ruleApplication : ruleApplications) %7B%0A            calledTestMethods.add(ruleApplica
 tion
@@ -1490,24 +1490,34 @@
 hodName());%0A
+        %7D%0A
         asse
@@ -1523,23 +1523,113 @@
 ertTrue(
-testRun
+calledTestMethods.contains(%22hello%22));%0A        assertTrue(calledTestMethods.contains(%22helloAgain%22)
 );%0A    %7D
@@ -1843,38 +1843,41 @@
    firstRule
-Description = 
+Applications.add(
 description;
@@ -1867,32 +1867,33 @@
 .add(description
+)
 ;%0A              
@@ -2120,22 +2120,25 @@
 Rule
-Description = 
+Applications.add(
 desc
@@ -2144,16 +2144,17 @@
 cription
+)
 ;%0A      
@@ -2251,17 +2251,109 @@
         
-t
+firstTestRun = true;%0A        %7D%0A%0A        @Test%0A        void helloAgain() %7B%0A            secondT
 estRun =

Test that each @Rule is applied on each test