<commit_before>package com.sandwich.koan.runner;

import static com.sandwich.koan.KoanConstants.EXPECTATION_LEFT_ARG;
import static com.sandwich.koan.KoanConstants.__;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.logging.Handler;
import java.util.logging.LogRecord;
import java.util.logging.Logger;

import org.junit.Test;

import com.sandwich.koan.Koan;
import com.sandwich.koan.KoanConstants;
import com.sandwich.koan.KoanMethod;
import com.sandwich.koan.KoanResult;
import com.sandwich.koan.path.CommandLineTestCase;
import com.sandwich.koan.path.PathToEnlightenment;
import com.sandwich.koan.suite.BlowUpOnLineEleven;
import com.sandwich.koan.suite.BlowUpOnLineTen;
import com.sandwich.koan.suite.OneFailingKoan;
import com.sandwich.koan.suite.OneFailingKoanDifferentName;
import com.sandwich.koan.suite.OnePassingKoan;
import com.sandwich.koan.ui.SuitePresenter;


public class KoanSuiteRunnerTest extends CommandLineTestCase {
	
	@Test
	public void testMainMethodWithClassNameArg_qualifiedWithPkgName() throws Throwable {
		KoanSuiteRunner.main(OnePassingKoan.class.getName());
		assertSystemOutContains(KoanConstants.PASSING_SUITES+" "+OnePassingKoan.class.getSimpleName());
	}
	
	@Test
	public void testMainMethodWithClassNameArg_classSimpleName() throws Throwable {
		KoanSuiteRunner.main(OnePassingKoan.class.getPackage().getName()
				+ KoanConstants.PERIOD + OnePassingKoan.class.getSimpleName());
		assertSystemOutContains(KoanConstants.PASSING_SUITES+" "+OnePassingKoan.class.getSimpleName());
	}
	
	@Test
	public void testMainMethodWithClassNameArg_classNameAndMethod() throws Throwable {
		String failingKoanMethodName = TwoFailingKoans.class.getDeclaredMethod("koanTwo").getName();
		KoanSuiteRunner.main(TwoFailingKoans.class.getName(), failingKoanMethodName);
		assertSystemOutContains(failingKoanMethodName);
		assertSystemOutDoesntContain(OneFailingKoan.class.getDeclaredMethods()[0].getName());
	}
	
	static class TwoFailingKoans extends OneFailingKoan {
		@Koan
		public void koanTwo(){assertEquals(true, false);}
	}
	
	@Test
	public void testThatKoanSuite_methodInvokationOrderingIsSensible() throws Exception {
		// TODO: written before EasyMock was imported, clean up
		final List<Integer> orderInvoked = new ArrayList<Integer>();
		new KoanSuiteRunner(){
			@Override KoanResult runKoans(){
				orderInvoked.add(1);
				return null;
			}
			@Override SuitePresenter getPresenter(){
				return new SuitePresenter() {
					@Override public void displayResult(KoanResult result) {
						orderInvoked.add(2);
					}
				};
			}
		}.run();
		assertEquals(orderInvoked, Arrays.asList(Integer.valueOf(1), Integer.valueOf(2)));
	}
	
	@Test @SuppressWarnings("unchecked")
	public void testGetKoans() throws Exception {
		stubAllKoans(Arrays.asList(OnePassingKoan.class));
		Map<Object, List<KoanMethod>> koans = new KoanSuiteRunner()
				.getPathToEnlightenment().iterator().next().getValue();
		assertEquals(1, koans.size());
		Entry<Object, List<KoanMethod>> entry = koans.entrySet().iterator().next();
		assertEquals(OnePassingKoan.class, entry.getKey().getClass());
		assertEquals(OnePassingKoan.class.getDeclaredMethod("koan"), entry.getValue().get(0).getMethod());
	}
	
	@Test	/** Ensures that koans are ready for packaging & distribution */
	public void testKoanSuiteRunner_firstKoanFail() throws Exception {
		final KoanResult[] result = new KoanResult[]{null};
		new KoanSuiteRunner(){
			@Override public KoanResult runKoans(){
				try {
					result[0] = super.runKoans();
				} catch (Exception e) {
					throw new RuntimeException(e);
				}
				return result[0];
			}
		}.run();
		assertEquals(result[0].getFailingCase(), PathToEnlightenment.getPathToEnlightment()
				.iterator().next().getValue().entrySet().iterator().next().getKey().getClass());
	}
	
	@Test	/** Ensures that koans are ready for packaging & distribution */
	public void testKoanSuiteRunner_allKoansFail() throws Exception {
		final KoanResult[] result = new KoanResult[]{null};
		new KoanSuiteRunner(){
			@Override SuitePresenter getPresenter(){
				return new SuitePresenter() {
					@Override public void displayResult(KoanResult r) {
						result[0] = r;
					}
				};
			}
		}.run();
		String message = "Not all koans need solving! Each should ship in a failing state.";
		assertEquals(message, 0,
				result[0].getNumberPassing());
	}
	
	@Test
	public void testKoanDescriptionAppearsOnFailure() throws Throwable {
		stubAllKoans(Arrays.asList(new Class<?>[] { 
				OneFailingKoanDifferentName.class }));
		KoanSuiteRunner.main();
		assertSystemOutContains("fake_description");
	}
	
	@Test @SuppressWarnings("unchecked")
	public void testLineExceptionIsThrownAtIsHintedAt() throws Exception {
		stubAllKoans(Arrays.asList(BlowUpOnLineTen.class));
		new KoanSuiteRunner().run();
		assertSystemOutContains("Line 10");
		assertSystemOutDoesntContain("Line 11");
	}
	
	@Test @SuppressWarnings("unchecked")
	public void testLineExceptionIsThrownAtIsHintedAtEvenIfThrownFromSuperClass() throws Exception {
		stubAllKoans(Arrays.asList(BlowUpOnLineEleven.class));
		new KoanSuiteRunner().run();
		assertSystemOutContains("Line 11");
		assertSystemOutDoesntContain("Line 10");
	}
	
	@Test @SuppressWarnings("unchecked")
	public void testWarningFromPlacingExpecationOnWrongSide() throws Throwable {
		final String[] message = new String[1];
		stubAllKoans(Arrays.asList(WrongExpectationOrderKoan.class));
		Logger.getLogger(KoanSuiteRunner.class.getSimpleName()).addHandler(new Handler(){
			@Override public void close() throws SecurityException {}
			@Override public void flush() {}
			@Override public void publish(LogRecord arg0) {
				message[0] = arg0.getMessage();
			}
		});
		KoanSuiteRunner.main();
		assertEquals(new StringBuilder(
				WrongExpectationOrderKoan.class.getSimpleName()).append(
				".expectationOnLeft ").append(
				EXPECTATION_LEFT_ARG).toString()
				, message[0]);
	}
	
	@Test @SuppressWarnings("unchecked")
	public void testNoWarningFromPlacingExpecationOnRightSide() throws Throwable {
		stubAllKoans(Arrays.asList(OnePassingKoan.class));
		Logger.getLogger(KoanSuiteRunner.class.getSimpleName()).addHandler(new Handler(){
			@Override public void close() throws SecurityException {}
			@Override public void flush() {}
			@Override public void publish(LogRecord arg0) {
				fail("No logging necessary when koan passes, otherwise - logging is new, adjust accordingly.");
			}
		});
		KoanSuiteRunner.main();
	}
	
	static class WrongExpectationOrderKoan {
		@Koan 
		public void expectationOnLeft(){
			assertEquals(__, false);
		}
	}
}<commit_msg>failed some test cases - yikes!<commit_after>package com.sandwich.koan.runner;

import static com.sandwich.koan.KoanConstants.EXPECTATION_LEFT_ARG;
import static com.sandwich.koan.KoanConstants.__;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.logging.Handler;
import java.util.logging.LogRecord;
import java.util.logging.Logger;

import org.junit.Test;

import com.sandwich.koan.Koan;
import com.sandwich.koan.KoanConstants;
import com.sandwich.koan.KoanMethod;
import com.sandwich.koan.KoanResult;
import com.sandwich.koan.path.CommandLineTestCase;
import com.sandwich.koan.path.PathToEnlightenment;
import com.sandwich.koan.suite.BlowUpOnLineEleven;
import com.sandwich.koan.suite.BlowUpOnLineTen;
import com.sandwich.koan.suite.OneFailingKoan;
import com.sandwich.koan.suite.OneFailingKoanDifferentName;
import com.sandwich.koan.suite.OnePassingKoan;
import com.sandwich.koan.ui.SuitePresenter;


public class KoanSuiteRunnerTest extends CommandLineTestCase {
	
	@Test
	public void testMainMethodWithClassNameArg_qualifiedWithPkgName() throws Throwable {
		KoanSuiteRunner.main(OnePassingKoan.class.getName());
		assertSystemOutContains(KoanConstants.PASSING_SUITES+" "+OnePassingKoan.class.getSimpleName());
	}
	
	@Test
	public void testMainMethodWithClassNameArg_classSimpleName() throws Throwable {
		KoanSuiteRunner.main(OnePassingKoan.class.getPackage().getName()
				+ KoanConstants.PERIOD + OnePassingKoan.class.getSimpleName());
		assertSystemOutContains(KoanConstants.PASSING_SUITES+" "+OnePassingKoan.class.getSimpleName());
	}
	
	@Test
	public void testMainMethodWithClassNameArg_classNameAndMethod() throws Throwable {
		String failingKoanMethodName = TwoFailingKoans.class.getDeclaredMethod("koanTwo").getName();
		KoanSuiteRunner.main(TwoFailingKoans.class.getName(), failingKoanMethodName);
		assertSystemOutContains(failingKoanMethodName);
		assertSystemOutDoesntContain(OneFailingKoan.class.getDeclaredMethods()[0].getName());
	}
	
	public static class TwoFailingKoans extends OneFailingKoan {
		@Koan
		public void koanTwo(){assertEquals(true, false);}
	}
	
	@Test
	public void testThatKoanSuite_methodInvokationOrderingIsSensible() throws Exception {
		// TODO: written before EasyMock was imported, clean up
		final List<Integer> orderInvoked = new ArrayList<Integer>();
		new KoanSuiteRunner(){
			@Override KoanResult runKoans(){
				orderInvoked.add(1);
				return null;
			}
			@Override SuitePresenter getPresenter(){
				return new SuitePresenter() {
					@Override public void displayResult(KoanResult result) {
						orderInvoked.add(2);
					}
				};
			}
		}.run();
		assertEquals(orderInvoked, Arrays.asList(Integer.valueOf(1), Integer.valueOf(2)));
	}
	
	@Test @SuppressWarnings("unchecked")
	public void testGetKoans() throws Exception {
		stubAllKoans(Arrays.asList(OnePassingKoan.class));
		Map<Object, List<KoanMethod>> koans = new KoanSuiteRunner()
				.getPathToEnlightenment().iterator().next().getValue();
		assertEquals(1, koans.size());
		Entry<Object, List<KoanMethod>> entry = koans.entrySet().iterator().next();
		assertEquals(OnePassingKoan.class, entry.getKey().getClass());
		assertEquals(OnePassingKoan.class.getDeclaredMethod("koan"), entry.getValue().get(0).getMethod());
	}
	
	@Test	/** Ensures that koans are ready for packaging & distribution */
	public void testKoanSuiteRunner_firstKoanFail() throws Exception {
		final KoanResult[] result = new KoanResult[]{null};
		new KoanSuiteRunner(){
			@Override public KoanResult runKoans(){
				try {
					result[0] = super.runKoans();
				} catch (Exception e) {
					throw new RuntimeException(e);
				}
				return result[0];
			}
		}.run();
		assertEquals(result[0].getFailingCase(), PathToEnlightenment.getPathToEnlightment()
				.iterator().next().getValue().entrySet().iterator().next().getKey().getClass());
	}
	
	@Test	/** Ensures that koans are ready for packaging & distribution */
	public void testKoanSuiteRunner_allKoansFail() throws Exception {
		final KoanResult[] result = new KoanResult[]{null};
		new KoanSuiteRunner(){
			@Override SuitePresenter getPresenter(){
				return new SuitePresenter() {
					@Override public void displayResult(KoanResult r) {
						result[0] = r;
					}
				};
			}
		}.run();
		String message = "Not all koans need solving! Each should ship in a failing state.";
		assertEquals(message, 0,
				result[0].getNumberPassing());
	}
	
	@Test
	public void testKoanDescriptionAppearsOnFailure() throws Throwable {
		stubAllKoans(Arrays.asList(new Class<?>[] { 
				OneFailingKoanDifferentName.class }));
		KoanSuiteRunner.main();
		assertSystemOutContains("fake_description");
	}
	
	@Test @SuppressWarnings("unchecked")
	public void testLineExceptionIsThrownAtIsHintedAt() throws Exception {
		stubAllKoans(Arrays.asList(BlowUpOnLineTen.class));
		new KoanSuiteRunner().run();
		assertSystemOutContains("Line 10");
		assertSystemOutDoesntContain("Line 11");
	}
	
	@Test @SuppressWarnings("unchecked")
	public void testLineExceptionIsThrownAtIsHintedAtEvenIfThrownFromSuperClass() throws Exception {
		stubAllKoans(Arrays.asList(BlowUpOnLineEleven.class));
		new KoanSuiteRunner().run();
		assertSystemOutContains("Line 11");
		assertSystemOutDoesntContain("Line 10");
	}
	
	@Test @SuppressWarnings("unchecked")
	public void testWarningFromPlacingExpecationOnWrongSide() throws Throwable {
		final String[] message = new String[1];
		stubAllKoans(Arrays.asList(WrongExpectationOrderKoan.class));
		Logger.getLogger(KoanSuiteRunner.class.getSimpleName()).addHandler(new Handler(){
			@Override public void close() throws SecurityException {}
			@Override public void flush() {}
			@Override public void publish(LogRecord arg0) {
				message[0] = arg0.getMessage();
			}
		});
		KoanSuiteRunner.main();
		assertEquals(new StringBuilder(
				WrongExpectationOrderKoan.class.getSimpleName()).append(
				".expectationOnLeft ").append(
				EXPECTATION_LEFT_ARG).toString()
				, message[0]);
	}
	
	@Test @SuppressWarnings("unchecked")
	public void testNoWarningFromPlacingExpecationOnRightSide() throws Throwable {
		stubAllKoans(Arrays.asList(OnePassingKoan.class));
		Logger.getLogger(KoanSuiteRunner.class.getSimpleName()).addHandler(new Handler(){
			@Override public void close() throws SecurityException {}
			@Override public void flush() {}
			@Override public void publish(LogRecord arg0) {
				fail("No logging necessary when koan passes, otherwise - logging is new, adjust accordingly.");
			}
		});
		KoanSuiteRunner.main();
	}
	
	public static class WrongExpectationOrderKoan {
		@Koan 
		public void expectationOnLeft(){
			assertEquals(__, false);
		}
	}
}
{'fraction_non_alphanumeric': 0.11619666322161523, 'fraction_numerical': 0.0039864166543629115, 'mean_word_length': 5.643943109367337, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, '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': '2491856', 'n_tokens_mistral': 4750, 'n_tokens_neox': 4176, 'n_words': 784}