<commit_before>// Modified or written by Object Mentor, Inc. for inclusion with FitNesse.
// Copyright (c) 2002 Cunningham & Cunningham, Inc.
// Released under the terms of the GNU General Public License version 2 or later.
package fit;

import fit.exception.FitParseException;
import org.junit.Before;
import org.junit.Test;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

import static org.junit.Assert.*;

public class ColumnFixtureTest extends ColumnFixture {
  private ColumnFixtureTest selfShuntFixture;
  public int field;
  public String stringField;
  private boolean resetCalledBeforeRow = false;
  private boolean resetCalled = false;
  private int executeCalled = 0;
  private int counter = 0;
  private boolean executeCalledBeforeMethod = false;
  private boolean executeWillThrow = false;

  public int method() {
    return 86;
  }

  public int method2() {
    return 88;
  }

  public int count() {
    counter++;
    return counter;
  }

  @Override
  public void execute() throws Exception {
    if (executeWillThrow)
      throw new Exception("Execute threw.");
    executeCalled++;
  }

  @Override
  public void reset() throws Exception {
    resetCalled = true;
  }

  public String resetCheck() {
    resetCalledBeforeRow = resetCalled;
    return "";
  }

  public boolean checkExecute() {
    executeCalledBeforeMethod = (executeCalled != 0);
    return executeCalledBeforeMethod;
  }

  public String stringMethod() {
    return null;
  }

  @Before
  public void setUp() throws Exception {
    selfShuntFixture = this;
  }

  private String getStringFor(Parse table) {
    StringWriter stringWriter = new StringWriter();
    PrintWriter out = new PrintWriter(stringWriter);
    table.print(out);
    out.flush();
    out.close();
    return stringWriter.toString();
  }

  private Parse tableOf(String rows) throws FitParseException {
    return new Parse("<table>" + rows + "</table>");
  }

  private String row(String... cells) {
    String row = "<tr>";
    for (String cell : cells)
      row += "<td>" + cell + "</td>";
    row += "</tr>";
    return row;
  }

  private Parse doTableOf(String rows) throws FitParseException {
    Parse table = tableOf(rows);
    selfShuntFixture.doRows(table.parts);
    return table;
  }

  private Parse bindTableOf(String rows) throws FitParseException {
    Parse table = tableOf(rows);
    selfShuntFixture.bindColumnHeadersToMethodsAndFields(table.parts.parts);
    return table;
  }

  @Test
  public void testBindColumnToMethod() throws Exception {
    String[] methodSpecifiers = new String[]
      {"method()", "method?", "method!", "string method()", "string method?", "string method!"};
    String[] resultingMethodName = new String[]
      {"method", "method", "method", "stringMethod", "stringMethod", "stringMethod"};
    for (int i = 0; i < methodSpecifiers.length; i++) {
      bindTableOf(row(methodSpecifiers[i]));
      assertNotNull(methodSpecifiers[i] + " no binding found.", selfShuntFixture.columnBindings[0]);
      Method method = selfShuntFixture.columnBindings[0].adapter.method;
      assertNotNull(methodSpecifiers[i] + "no method found.", method);
      assertEquals(resultingMethodName[i], method.getName());
    }
  }

  @Test
  public void testBindColumnToField() throws Exception {
    bindTableOf(row("field"));
    assertNotNull(selfShuntFixture.columnBindings[0]);
    Field field = selfShuntFixture.columnBindings[0].adapter.field;
    assertNotNull(field);
    assertEquals("field", field.getName());
  }

  @Test
  public void testGracefulColumnNames() throws Exception {
    bindTableOf(row("string field"));
    assertNotNull(selfShuntFixture.columnBindings[0]);
    Field field = selfShuntFixture.columnBindings[0].adapter.field;
    assertNotNull(field);
    assertEquals("stringField", field.getName());
  }

  @Test
  public void testBindColumnToFieldSymbol() throws Exception {
    Fixture.setSymbol("Symbol", "42");
    doTableOf(row("field=") + row("Symbol"));
    Binding binding = selfShuntFixture.columnBindings[0];
    assertNotNull(binding);
    assertEquals(Binding.RecallBinding.class, binding.getClass());
    Field field = binding.adapter.field;
    assertNotNull(field);
    assertEquals("field", field.getName());
    assertEquals(42, selfShuntFixture.field);
  }

  @Test
  public void testBindColumnToMethodSymbol() throws Exception {
    doTableOf(row("=method?") + row("MethodSymbol"));
    Binding binding = selfShuntFixture.columnBindings[0];
    assertNotNull(binding);
    assertEquals(Binding.SaveBinding.class, binding.getClass());
    Method method = binding.adapter.method;
    assertEquals("method", method.getName());
    assertEquals("86", Fixture.getSymbol("MethodSymbol"));
  }

  @Test
  public void ensureResetCalled() throws Exception {
    doTableOf(row("resetCheck?") + row("whatever"));
    assertTrue(resetCalled);
  }

  @Test
  public void ensureResetCalledBeforeRows() throws Exception {
    doTableOf(row("resetCheck?") + row("whatever"));
    assertTrue(resetCalledBeforeRow);
  }

  @Test
  public void ensureExecuteIsCalledForRowWithMethod() throws Exception {
    doTableOf(row("resetCheck?") + row("whatever"));
    assertEquals(1, executeCalled);
  }

  @Test
  public void ensureExecuteIsCalledForRowWithOnlyVariables() throws Exception {
    doTableOf(row("stringField") + row("whatever"));
    assertEquals(1, executeCalled);
    ;
  }

  @Test
  public void ensureExecuteIsCalledOnlyOnceIfOneRow() throws Exception {
    doTableOf(row("count?", "count?") + row("1", "2"));
    assertEquals(2, counter);
    assertEquals(1, executeCalled);
  }

  @Test
  public void ensureExecuteIsCalledOncePerRow() throws Exception {
    doTableOf(row("count?", "count?") + row("1", "2") + row("3","4"));
    assertEquals(4, counter);
    assertEquals(2, executeCalled);
  }

  @Test
  public void ensureExecuteIsCalledBeforeAnyMethods() throws Exception {
    doTableOf(row("field", "checkExecute?") + row("1", "true"));
    assertTrue(executeCalledBeforeMethod);
  }

  @Test
  public void exceptionForBadMethod() throws Exception {
    Parse table = doTableOf(row("noSuchMethod?") + row("1"));
    String s = getStringFor(table);
    assertTrue(s.contains("Could not find method: noSuchMethod?."));
  }<commit_msg>Remove redundant semicolon<commit_after>// Modified or written by Object Mentor, Inc. for inclusion with FitNesse.
// Copyright (c) 2002 Cunningham & Cunningham, Inc.
// Released under the terms of the GNU General Public License version 2 or later.
package fit;

import fit.exception.FitParseException;
import org.junit.Before;
import org.junit.Test;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

import static org.junit.Assert.*;

public class ColumnFixtureTest extends ColumnFixture {
  private ColumnFixtureTest selfShuntFixture;
  public int field;
  public String stringField;
  private boolean resetCalledBeforeRow = false;
  private boolean resetCalled = false;
  private int executeCalled = 0;
  private int counter = 0;
  private boolean executeCalledBeforeMethod = false;
  private boolean executeWillThrow = false;

  public int method() {
    return 86;
  }

  public int method2() {
    return 88;
  }

  public int count() {
    counter++;
    return counter;
  }

  @Override
  public void execute() throws Exception {
    if (executeWillThrow)
      throw new Exception("Execute threw.");
    executeCalled++;
  }

  @Override
  public void reset() throws Exception {
    resetCalled = true;
  }

  public String resetCheck() {
    resetCalledBeforeRow = resetCalled;
    return "";
  }

  public boolean checkExecute() {
    executeCalledBeforeMethod = (executeCalled != 0);
    return executeCalledBeforeMethod;
  }

  public String stringMethod() {
    return null;
  }

  @Before
  public void setUp() throws Exception {
    selfShuntFixture = this;
  }

  private String getStringFor(Parse table) {
    StringWriter stringWriter = new StringWriter();
    PrintWriter out = new PrintWriter(stringWriter);
    table.print(out);
    out.flush();
    out.close();
    return stringWriter.toString();
  }

  private Parse tableOf(String rows) throws FitParseException {
    return new Parse("<table>" + rows + "</table>");
  }

  private String row(String... cells) {
    String row = "<tr>";
    for (String cell : cells)
      row += "<td>" + cell + "</td>";
    row += "</tr>";
    return row;
  }

  private Parse doTableOf(String rows) throws FitParseException {
    Parse table = tableOf(rows);
    selfShuntFixture.doRows(table.parts);
    return table;
  }

  private Parse bindTableOf(String rows) throws FitParseException {
    Parse table = tableOf(rows);
    selfShuntFixture.bindColumnHeadersToMethodsAndFields(table.parts.parts);
    return table;
  }

  @Test
  public void testBindColumnToMethod() throws Exception {
    String[] methodSpecifiers = new String[]
      {"method()", "method?", "method!", "string method()", "string method?", "string method!"};
    String[] resultingMethodName = new String[]
      {"method", "method", "method", "stringMethod", "stringMethod", "stringMethod"};
    for (int i = 0; i < methodSpecifiers.length; i++) {
      bindTableOf(row(methodSpecifiers[i]));
      assertNotNull(methodSpecifiers[i] + " no binding found.", selfShuntFixture.columnBindings[0]);
      Method method = selfShuntFixture.columnBindings[0].adapter.method;
      assertNotNull(methodSpecifiers[i] + "no method found.", method);
      assertEquals(resultingMethodName[i], method.getName());
    }
  }

  @Test
  public void testBindColumnToField() throws Exception {
    bindTableOf(row("field"));
    assertNotNull(selfShuntFixture.columnBindings[0]);
    Field field = selfShuntFixture.columnBindings[0].adapter.field;
    assertNotNull(field);
    assertEquals("field", field.getName());
  }

  @Test
  public void testGracefulColumnNames() throws Exception {
    bindTableOf(row("string field"));
    assertNotNull(selfShuntFixture.columnBindings[0]);
    Field field = selfShuntFixture.columnBindings[0].adapter.field;
    assertNotNull(field);
    assertEquals("stringField", field.getName());
  }

  @Test
  public void testBindColumnToFieldSymbol() throws Exception {
    Fixture.setSymbol("Symbol", "42");
    doTableOf(row("field=") + row("Symbol"));
    Binding binding = selfShuntFixture.columnBindings[0];
    assertNotNull(binding);
    assertEquals(Binding.RecallBinding.class, binding.getClass());
    Field field = binding.adapter.field;
    assertNotNull(field);
    assertEquals("field", field.getName());
    assertEquals(42, selfShuntFixture.field);
  }

  @Test
  public void testBindColumnToMethodSymbol() throws Exception {
    doTableOf(row("=method?") + row("MethodSymbol"));
    Binding binding = selfShuntFixture.columnBindings[0];
    assertNotNull(binding);
    assertEquals(Binding.SaveBinding.class, binding.getClass());
    Method method = binding.adapter.method;
    assertEquals("method", method.getName());
    assertEquals("86", Fixture.getSymbol("MethodSymbol"));
  }

  @Test
  public void ensureResetCalled() throws Exception {
    doTableOf(row("resetCheck?") + row("whatever"));
    assertTrue(resetCalled);
  }

  @Test
  public void ensureResetCalledBeforeRows() throws Exception {
    doTableOf(row("resetCheck?") + row("whatever"));
    assertTrue(resetCalledBeforeRow);
  }

  @Test
  public void ensureExecuteIsCalledForRowWithMethod() throws Exception {
    doTableOf(row("resetCheck?") + row("whatever"));
    assertEquals(1, executeCalled);
  }

  @Test
  public void ensureExecuteIsCalledForRowWithOnlyVariables() throws Exception {
    doTableOf(row("stringField") + row("whatever"));
    assertEquals(1, executeCalled);
  }

  @Test
  public void ensureExecuteIsCalledOnlyOnceIfOneRow() throws Exception {
    doTableOf(row("count?", "count?") + row("1", "2"));
    assertEquals(2, counter);
    assertEquals(1, executeCalled);
  }

  @Test
  public void ensureExecuteIsCalledOncePerRow() throws Exception {
    doTableOf(row("count?", "count?") + row("1", "2") + row("3","4"));
    assertEquals(4, counter);
    assertEquals(2, executeCalled);
  }

  @Test
  public void ensureExecuteIsCalledBeforeAnyMethods() throws Exception {
    doTableOf(row("field", "checkExecute?") + row("1", "true"));
    assertTrue(executeCalledBeforeMethod);
  }

  @Test
  public void exceptionForBadMethod() throws Exception {
    Parse table = doTableOf(row("noSuchMethod?") + row("1"));
    String s = getStringFor(table);
    assertTrue(s.contains("Could not find method: noSuchMethod?."));
  }
{'fraction_non_alphanumeric': 0.1243404992519096, 'fraction_numerical': 0.006614694070399244, 'mean_word_length': 4.287260616153206, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 15, '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': '2863771', 'n_tokens_mistral': 3698, 'n_tokens_neox': 3624, 'n_words': 894}