// Copyright (C) 2003-2009 by Object Mentor, Inc. All rights reserved.
// Released under the terms of the CPL Common Public License version 1.0.
package nl.hsac.fitnesse.slimcoverage;

import fitnesse.testsystems.TestPage;
import fitnesse.testsystems.slim.SlimTestContextImpl;
import fitnesse.testsystems.slim.tables.ScenarioTable;
import fitnesse.testsystems.slim.tables.UsageHelper;

public class SlimCoverageTestContextImpl extends SlimTestContextImpl {
    private final SlimScenarioUsagePer usage;

    public SlimCoverageTestContextImpl(TestPage testPage, SlimScenarioUsagePer usageByPage) {
        super(testPage);
        usage = usageByPage;
    }

    @Override
    public void addScenario(String scenarioName, ScenarioTable scenarioTable) {
        if (usage != null) {
            String key = getGroupName(scenarioTable);
            usage.addDefinition(key);
        }
        super.addScenario(scenarioName, scenarioTable);
    }

    public ScenarioTable getScenario(String scenarioName) {
        ScenarioTable scenarioTable = super.getScenario(scenarioName);
        if (usage != null && scenarioTable != null) {
            String key = getGroupName(scenarioTable);
            usage.addUsage(key);
        }
        return scenarioTable;
    }

    protected String getGroupName(ScenarioTable scenarioTable) {
        String name = scenarioTable.getName();
        UsageHelper.resolveArguments(scenarioTable);
        int inputCount = scenarioTable.getInputs().size();
        int outputCount = scenarioTable.getOutputs().size();
        String keyPattern;
        if (inputCount == 0 && outputCount == 0) {
            keyPattern = "%s";
        } else if (outputCount == 0) {
            keyPattern = "%s[%s]";
        } else {
            keyPattern = "%s[%s,%s]";
        }
        return String.format(keyPattern, name, inputCount, outputCount);
    }
}

@@ -1067,24 +1067,396 @@
 narioName);%0A
+        trackUsage(scenarioTable);%0A        return scenarioTable;%0A    %7D%0A%0A    @Override%0A    public ScenarioTable getScenarioByPattern(String invokingString) %7B%0A        ScenarioTable scenarioTable = super.getScenarioByPattern(invokingString);%0A        trackUsage(scenarioTable);%0A        return scenarioTable;%0A    %7D%0A%0A    protected void trackUsage(ScenarioTable scenarioTable) %7B%0A
         if (
@@ -1489,32 +1489,32 @@
 able != null) %7B%0A
-
             Stri
@@ -1580,24 +1580,24 @@
 Usage(key);%0A
+
         %7D%0A  
@@ -1598,38 +1598,8 @@
   %7D%0A
-        return scenarioTable;%0A
     

Address #7