<commit_before>import {TennisGame} from '../app/TennisGame';
import {TennisGame3Component} from '../app/tennis-game3/tennis-game3.component';
import {ComponentFixture, TestBed} from '@angular/core/testing';
import {FormsModule, ReactiveFormsModule} from '@angular/forms';
import {By} from '@angular/platform-browser';
import {MatCardModule, MatFormFieldModule, MatInputModule} from '@angular/material';
import {BrowserAnimationsModule} from '@angular/platform-browser/animations';
import {getScoreButton, player1ScoreInput, player2ScoreInput, errorLabelForPlayer} from './selectors';
import {ExpectedError} from './expectedResults';

export const tennisPlayerNumbers = [1, 2];
export const testImports = [
    FormsModule,
    ReactiveFormsModule,
    MatInputModule,
    MatFormFieldModule,
    BrowserAnimationsModule,
    MatCardModule
  ];

export class TennisComponentTester {
  component: TennisGame;
  fixture: ComponentFixture<TennisGame3Component>;
  element: any;
  private readonly tennisComponentIndex = 0;

  async beforeEach(declarations: any[]) {
    await TestBed.configureTestingModule({
      imports: testImports,
      declarations
    }).compileComponents();
    this.fixture = TestBed.createComponent(declarations[this.tennisComponentIndex]);
    this.component = this.fixture.componentInstance;
    this.element = this.fixture.nativeElement;
    this.fixture.detectChanges();
    // @ts-ignore
    expect(this.component).toBeTruthy();
  }

  public getElement(elementSelector: string) {
    return this.fixture.debugElement.query(By.css(elementSelector));
  }

  getStylesFor(selectors: string) {
    const element = this.element.querySelector(selectors);
    return getComputedStyle(element);
  }

  getParentStylesFor(selectors: string) {
    const element = this.getElement(selectors);
    return getComputedStyle(element.parent.nativeElement);
  }

  setInputValue(inputSelector: string, newValue: number | string) {
    const inputElement = this.element.querySelector(inputSelector);
    inputElement.value = newValue;
    inputElement.dispatchEvent(new Event('input'));
  }

  enterPlayersScores(player1Score: string | number, player2Score: string | number) {
    this.setInputValue(player1ScoreInput, player1Score);
    this.setInputValue(player2ScoreInput, player2Score);
    this.component.tennisGameForm.controls.player1Score.markAsTouched();
    this.component.tennisGameForm.controls.player2Score.markAsTouched();
  }

  selectElement(elementSelector: string) {
    const element = this.element.querySelector(elementSelector);
    element.click();
    this.fixture.detectChanges();
  }

  verifyLabelText(labelSelector: string, expectedText: number | string) {
    const label = this.getElement(labelSelector);
    // @ts-ignore
    expect(label.nativeElement.outerText).toBe(expectedText);
  }

  verifyInputValue(buttonElementSelector: string, expectedValue: string) {
    const buttonElement = this.getElement(buttonElementSelector);
    // @ts-ignore
    expect(buttonElement.properties.value.toString()).toBe(expectedValue);
  }

  verifyButtonIsEnabled(buttonElementSelector: string, isEnabled: boolean = true) {
    const buttonElement = this.getElement(buttonElementSelector);
    // @ts-ignore
    expect(buttonElement.properties.disabled).toBe(!isEnabled);
  }

  verifyElementExists(selector: string) {
    const element = this.getElement(selector);
    // @ts-ignore
    expect(element).toBeTruthy();
  }

  private verifyElementIsVisible(selector: string, isVisible: boolean = true) {
    const element = this.element.querySelector(selector);
    if (isVisible) {
      // @ts-ignore
      expect(element).not.toBeNull();
    } else {
      // @ts-ignore
      expect(element).toBeNull();
    }

  }

  verifyErrorLabelText(selector: string, expectedErrorMessage: string) {
    this.verifyElementExists(selector);
    this.verifyElementIsVisible(selector);
    const errorLabel = this.element.querySelector(selector);
    // @ts-ignore
    expect(errorLabel.innerText).toBe(expectedErrorMessage);
  }

  // todo: lots of clean up here
  // todo: make generic reduces instead of hard coded array indexes
  verifyInputValidation(expectedScoreErrors: ExpectedError[]) {
    if (this.thereAreAny(expectedScoreErrors)) {
      this.verifyErrorMessageForInvalidScores(expectedScoreErrors);
      this.verifyNoErrorMessageForValidScores(expectedScoreErrors);
      this.verifyButtonIsEnabled(getScoreButton, false);
    } else {
      this.verifyNoErrorMessages();
      this.verifyButtonIsEnabled(getScoreButton, true);
    }
  }

  private verifyNoErrorMessages() {
    tennisPlayerNumbers.forEach(playerNumber => this.verifyElementIsVisible(errorLabelForPlayer[playerNumber], false));
  }

  private verifyNoErrorMessageForValidScores(expectedScoreErrors: ExpectedError[]) {
    const playerNumbersWithError = [];
    expectedScoreErrors.forEach(error => playerNumbersWithError.push(error.playerNumber));
    const playerNumbersWithoutErrors = tennisPlayerNumbers.filter(x => !playerNumbersWithError.includes(x));
    playerNumbersWithoutErrors.forEach(playerNumber => this.verifyElementIsVisible(errorLabelForPlayer[playerNumber], false));
  }

  private verifyErrorMessageForInvalidScores(expectedErrors: ExpectedError[]) {
    expectedErrors.forEach(error => this.verifyErrorLabelText(errorLabelForPlayer[error.playerNumber], error.expectedErrorMessage));
  }

  private thereAreAny(expectedErrors: ExpectedError[]) {
    return expectedErrors && expectedErrors.length > 0;
  }
}
<commit_msg>c -delete completed todos<commit_after>import {TennisGame} from '../app/TennisGame';
import {TennisGame3Component} from '../app/tennis-game3/tennis-game3.component';
import {ComponentFixture, TestBed} from '@angular/core/testing';
import {FormsModule, ReactiveFormsModule} from '@angular/forms';
import {By} from '@angular/platform-browser';
import {MatCardModule, MatFormFieldModule, MatInputModule} from '@angular/material';
import {BrowserAnimationsModule} from '@angular/platform-browser/animations';
import {getScoreButton, player1ScoreInput, player2ScoreInput, errorLabelForPlayer} from './selectors';
import {ExpectedError} from './expectedResults';

export const tennisPlayerNumbers = [1, 2];
export const testImports = [
    FormsModule,
    ReactiveFormsModule,
    MatInputModule,
    MatFormFieldModule,
    BrowserAnimationsModule,
    MatCardModule
  ];

export class TennisComponentTester {
  component: TennisGame;
  fixture: ComponentFixture<TennisGame3Component>;
  element: any;
  private readonly tennisComponentIndex = 0;

  async beforeEach(declarations: any[]) {
    await TestBed.configureTestingModule({
      imports: testImports,
      declarations
    }).compileComponents();
    this.fixture = TestBed.createComponent(declarations[this.tennisComponentIndex]);
    this.component = this.fixture.componentInstance;
    this.element = this.fixture.nativeElement;
    this.fixture.detectChanges();
    // @ts-ignore
    expect(this.component).toBeTruthy();
  }

  public getElement(elementSelector: string) {
    return this.fixture.debugElement.query(By.css(elementSelector));
  }

  getStylesFor(selectors: string) {
    const element = this.element.querySelector(selectors);
    return getComputedStyle(element);
  }

  getParentStylesFor(selectors: string) {
    const element = this.getElement(selectors);
    return getComputedStyle(element.parent.nativeElement);
  }

  setInputValue(inputSelector: string, newValue: number | string) {
    const inputElement = this.element.querySelector(inputSelector);
    inputElement.value = newValue;
    inputElement.dispatchEvent(new Event('input'));
  }

  enterPlayersScores(player1Score: string | number, player2Score: string | number) {
    this.setInputValue(player1ScoreInput, player1Score);
    this.setInputValue(player2ScoreInput, player2Score);
    this.component.tennisGameForm.controls.player1Score.markAsTouched();
    this.component.tennisGameForm.controls.player2Score.markAsTouched();
  }

  selectElement(elementSelector: string) {
    const element = this.element.querySelector(elementSelector);
    element.click();
    this.fixture.detectChanges();
  }

  verifyLabelText(labelSelector: string, expectedText: number | string) {
    const label = this.getElement(labelSelector);
    // @ts-ignore
    expect(label.nativeElement.outerText).toBe(expectedText);
  }

  verifyInputValue(buttonElementSelector: string, expectedValue: string) {
    const buttonElement = this.getElement(buttonElementSelector);
    // @ts-ignore
    expect(buttonElement.properties.value.toString()).toBe(expectedValue);
  }

  verifyButtonIsEnabled(buttonElementSelector: string, isEnabled: boolean = true) {
    const buttonElement = this.getElement(buttonElementSelector);
    // @ts-ignore
    expect(buttonElement.properties.disabled).toBe(!isEnabled);
  }

  verifyElementExists(selector: string) {
    const element = this.getElement(selector);
    // @ts-ignore
    expect(element).toBeTruthy();
  }

  private verifyElementIsVisible(selector: string, isVisible: boolean = true) {
    const element = this.element.querySelector(selector);
    if (isVisible) {
      // @ts-ignore
      expect(element).not.toBeNull();
    } else {
      // @ts-ignore
      expect(element).toBeNull();
    }

  }

  verifyErrorLabelText(selector: string, expectedErrorMessage: string) {
    this.verifyElementExists(selector);
    this.verifyElementIsVisible(selector);
    const errorLabel = this.element.querySelector(selector);
    // @ts-ignore
    expect(errorLabel.innerText).toBe(expectedErrorMessage);
  }

  verifyInputValidation(expectedScoreErrors: ExpectedError[]) {
    if (this.thereAreAny(expectedScoreErrors)) {
      this.verifyErrorMessageForInvalidScores(expectedScoreErrors);
      this.verifyNoErrorMessageForValidScores(expectedScoreErrors);
      this.verifyButtonIsEnabled(getScoreButton, false);
    } else {
      this.verifyNoErrorMessages();
      this.verifyButtonIsEnabled(getScoreButton, true);
    }
  }

  private verifyNoErrorMessages() {
    tennisPlayerNumbers.forEach(playerNumber => this.verifyElementIsVisible(errorLabelForPlayer[playerNumber], false));
  }

  private verifyNoErrorMessageForValidScores(expectedScoreErrors: ExpectedError[]) {
    const playerNumbersWithError = [];
    expectedScoreErrors.forEach(error => playerNumbersWithError.push(error.playerNumber));
    const playerNumbersWithoutErrors = tennisPlayerNumbers.filter(x => !playerNumbersWithError.includes(x));
    playerNumbersWithoutErrors.forEach(playerNumber => this.verifyElementIsVisible(errorLabelForPlayer[playerNumber], false));
  }

  private verifyErrorMessageForInvalidScores(expectedErrors: ExpectedError[]) {
    expectedErrors.forEach(error => this.verifyErrorLabelText(errorLabelForPlayer[error.playerNumber], error.expectedErrorMessage));
  }

  private thereAreAny(expectedErrors: ExpectedError[]) {
    return expectedErrors && expectedErrors.length > 0;
  }
}

{'fraction_non_alphanumeric': 0.10783510783510783, 'fraction_numerical': 0.003276003276003276, 'mean_word_length': 5.115748469671675, 'pattern_counts': {'":': 0, '<': 5, '<?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': '949443', 'n_tokens_mistral': 3067, 'n_tokens_neox': 3002, 'n_words': 509}