<commit_before>import herd.asynctest {
	TestVariantResult,
	TestOutput
}
import ceylon.test {
	TestState
}


"Identifies if test has to be repeated or completed.  
 
 Pay attention: when implementing new strategy it has to take care to understand
 when it is started and when it is completed to be ready for the next repeating run.  
 "
tagged( "Repeat" )
since( "0.6.0" ) by( "Lis" )
shared interface RepeatStrategy {
	"Takes result of the latest run and identifies if test has to be repeat or completed:  
	 
	 * If returns `null` then the test is repeated.  
	 * If returns `TestVariantResult` then the test is completed and results are reported.
	 "
	shared formal TestVariantResult? completeOrRepeat (
		"Results of the test variant run." TestVariantResult variant
	);
}


"Repeats just once and returns the given test variant result."
tagged( "Repeat" )
since( "0.6.0" ) by( "Lis" )
shared object repeatOnce satisfies RepeatStrategy {
	"Always returns passed variant."
	shared actual TestVariantResult? completeOrRepeat( TestVariantResult variant ) => variant;
}


"Repeats up to the first successfull run but no more than `maxRepeats` times.  
 Reports result from the latest run."
tagged( "Repeat" )
since( "0.6.0" ) by( "Lis" )
shared class RepeatUpToSuccessRun( "Number of repeats limit." Integer maxRepeats = 1 ) satisfies RepeatStrategy {
	
	variable Integer? totalRuns = null;
	
	shared actual TestVariantResult? completeOrRepeat( TestVariantResult variant ) {
		Integer count = ( totalRuns else 1 ) + 1;
		if ( variant.overallState == TestState.success || count > maxRepeats ) {
			totalRuns = null;
			return variant;
		}
		else {
			totalRuns = count;
			return null;
		}
	}
	
}


"Repeats up to the first failed run but no more than `maxRepeats` times.  
 Reports result from the latest run."
tagged( "Repeat" )
since( "0.6.0" ) by( "Lis" )
shared class RepeatUpToFailedRun( "Number of repeats limit." Integer maxRepeats = 1 ) satisfies RepeatStrategy {
	
	variable Integer? totalRuns = null;
	
	shared actual TestVariantResult? completeOrRepeat( TestVariantResult variant ) {
		Integer count = ( totalRuns else 1 ) + 1;
		if ( variant.overallState > TestState.success || count > maxRepeats ) {
			totalRuns = null;
			return variant;
		}
		else {
			totalRuns = count;
			return null;
		}
	}
	
}


"Repeats up to the first failure message but no more than `maxRepeats` times.  
 Reports just this failed message."
tagged( "Repeat" )
since( "0.6.0" ) by( "Lis" )
shared class RepeatUpToFailureMessage( "Number of repeats limit." Integer maxRepeats = 1 ) satisfies RepeatStrategy {
	
	variable Integer? totalRuns = null;
	
	shared actual TestVariantResult? completeOrRepeat( TestVariantResult variant ) {
		Integer count = ( totalRuns else 1 ) + 1;
		if ( variant.overallState > TestState.success || count > maxRepeats ) {
			totalRuns = null;
			for ( item in variant.testOutput ) {
				if ( exists reason = item.error ) {
					return TestVariantResult (
						[TestOutput( item.state, item.error, item.elapsedTime,
							if ( item.title.empty ) then "failure" else item.title
						)], variant.overallElapsedTime, item.state
					);
				}
			}
			else {
				return TestVariantResult (
					[TestOutput( TestState.success, null, variant.overallElapsedTime, "success" )],
					variant.overallElapsedTime, TestState.success
				);
			}
		}<commit_msg>refactor<commit_after>import herd.asynctest {
	TestVariantResult,
	TestOutput
}
import ceylon.test {
	TestState
}


"Identifies if test has to be repeated or completed.  
 
 Pay attention: when implementing new strategy it has to take care to understand
 when it is started and when it is completed to be ready for the next repeating run.  
 "
tagged( "Repeat" )
since( "0.6.0" ) by( "Lis" )
shared interface RepeatStrategy {
	"Takes result of the latest run and identifies if test has to be repeat or completed:  
	 
	 * If returns `null` then the test is repeated.  
	 * If returns `TestVariantResult` then the test is completed and results are reported.
	 "
	shared formal TestVariantResult? completeOrRepeat (
		"Results of the test variant run." TestVariantResult variant
	);
}


"Repeats just once and returns the given test variant result."
tagged( "Repeat" )
since( "0.6.0" ) by( "Lis" )
shared object repeatOnce satisfies RepeatStrategy {
	"Always returns passed variant."
	shared actual TestVariantResult? completeOrRepeat( TestVariantResult variant ) => variant;
}


"Repeats up to the first successfull run but no more than `maxRepeats` times.  
 Reports result from the latest run."
tagged( "Repeat" )
since( "0.6.0" ) by( "Lis" )
shared class RepeatUpToSuccessRun( "Number of repeats limit." Integer maxRepeats = 1 ) satisfies RepeatStrategy {
	
	variable Integer totalRuns = 0;
	
	shared actual TestVariantResult? completeOrRepeat( TestVariantResult variant ) {
		Integer count = ( totalRuns > 0 then totalRuns else 1 ) + 1;
		if ( variant.overallState == TestState.success || count > maxRepeats ) {
			totalRuns = 0;
			return variant;
		}
		else {
			totalRuns = count;
			return null;
		}
	}
	
}


"Repeats up to the first failed run but no more than `maxRepeats` times.  
 Reports result from the latest run."
tagged( "Repeat" )
since( "0.6.0" ) by( "Lis" )
shared class RepeatUpToFailedRun( "Number of repeats limit." Integer maxRepeats = 1 ) satisfies RepeatStrategy {
	
	variable Integer totalRuns = 0;
	
	shared actual TestVariantResult? completeOrRepeat( TestVariantResult variant ) {
		Integer count = ( totalRuns > 0 then totalRuns else 1 ) + 1;
		if ( variant.overallState > TestState.success || count > maxRepeats ) {
			totalRuns = 0;
			return variant;
		}
		else {
			totalRuns = count;
			return null;
		}
	}
	
}


"Repeats up to the first failure message but no more than `maxRepeats` times.  
 Reports just this failed message."
tagged( "Repeat" )
since( "0.6.0" ) by( "Lis" )
shared class RepeatUpToFailureMessage( "Number of repeats limit." Integer maxRepeats = 1 ) satisfies RepeatStrategy {
	
	variable Integer totalRuns = 0;
	
	shared actual TestVariantResult? completeOrRepeat( TestVariantResult variant ) {
		Integer count = ( totalRuns > 0 then totalRuns else 1 ) + 1;
		if ( variant.overallState > TestState.success || count > maxRepeats ) {
			totalRuns = 0;
			for ( item in variant.testOutput ) {
				if ( exists reason = item.error ) {
					return TestVariantResult (
						[TestOutput( item.state, item.error, item.elapsedTime,
							if ( item.title.empty ) then "failure" else item.title
						)], variant.overallElapsedTime, item.state
					);
				}
			}
			else {
				return TestVariantResult (
					[TestOutput( TestState.success, null, variant.overallElapsedTime, "success" )],
					variant.overallElapsedTime, TestState.success
				);
			}
		}
{'fraction_non_alphanumeric': 0.0892830746489283, 'fraction_numerical': 0.008425720620842573, 'mean_word_length': 3.6823529411764704, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '4037225', 'n_tokens_mistral': 2320, 'n_tokens_neox': 2021, 'n_words': 729}