<commit_before>import combinate from 'combinate'
import { equals, filter, omit, switcher, type } from 'rambdax'

const PENDING = 'PENDING'
const RESULTS_EQUAL = 'results are equal'
const RESULTS_MISMATCH = 'results are different'
const ERRORS_EQUAL = 'errors are equal'
const ERRORS_MISMATCH = 'errors are different'
const SHOULD_THROW = 'Rambda should throw'
const SHOULD_NOT_THROW = 'Rambda should not throw'

function parseError(err){
  const typeError = switcher(err)
    .is(x => x instanceof TypeError, 'TypeError')
    .is(x => x instanceof TypeError, 'TypeError')
    .is(x => x instanceof TypeError, 'TypeError')
    .default('Missing error handle in parseError')

  return {
    message : err.message,
    type    : typeError,
    ok      : true,
  }
}

export function compareToRamda(fn, fnRamda){
  return (...inputs) => {
    let ramdaResult = PENDING
    let result = PENDING
    let ramdaError = { ok : false }
    let error = { ok : false }
    try {
      result = fn(...inputs)
    } catch (e){
      error = parseError(e)
    }
    try {
      ramdaResult = fnRamda(...inputs)
    } catch (e){
      ramdaError = parseError(e)
    }
    const toReturn = {
      result,
      ramdaResult,
      ramdaError : ramdaError.ok ? omit('ok', ramdaError) : PENDING,
      error      : error.ok ? omit('ok', error) : PENDING,
    }

    if (result !== PENDING){
      if (ramdaError.ok){
        return {
          ...toReturn,
          ok    : false,
          label : SHOULD_THROW,
        }
      }

      if (equals(result, ramdaResult)){
        return {
          ...toReturn,
          ok    : true,
          label : RESULTS_EQUAL,
        }
      }

      return {
        ...toReturn,
        ok    : false,
        label : RESULTS_MISMATCH,
      }
    }

    if (equals(error, ramdaError)){
      return {
        ...toReturn,
        ok    : true,
        label : ERRORS_EQUAL,
      }
    }

    if (ramdaError.ok){
      return {
        ...toReturn,
        ok    : false,
        label : ERRORS_MISMATCH,
      }
    }

    return {
      ...toReturn,
      ok    : false,
      label : SHOULD_NOT_THROW,
    }
  }
}

export const getTestTitle = (...inputs) =>
  inputs.map(x => `${ type(x) } ${ x }`).join(' | ')

export const compareCombinations = ({
  firstInput,
  secondInput = undefined,
  thirdInput = undefined,
  setCounter = () => {},
  setGlobalCounter = () => {},
  fn,
  fnRamda,
}) => {
  const combinationsInput = filter(Boolean, {
    firstInput,
    secondInput,
    thirdInput,
  })
  const inputKeys = Object.keys(combinationsInput)
  const combinations = combinate(combinationsInput)
  const compareOutputs = compareToRamda(fn, fnRamda)

  combinations.forEach(combination => {
    const inputs = [
      combination.firstInput,
      combination.secondInput,
      combination.thirdInput,
    ].filter((_, i) => i < inputKeys.length)

    test(getTestTitle(...inputs), () => {
      const compared = compareOutputs(...inputs)
      setGlobalCounter()
      if (!compared.ok){
        setCounter()
        expect({
          ...compared,
          inputs,
        }).toMatchSnapshot()
      }
    })
  })
}
<commit_msg>chore@test add profile method test util<commit_after>import combinate from 'combinate'
import { equals, filter, omit, switcher, type } from 'rambdax'

const PENDING = 'PENDING'
const RESULTS_EQUAL = 'results are equal'
const RESULTS_MISMATCH = 'results are different'
const ERRORS_EQUAL = 'errors are equal'
const ERRORS_MISMATCH = 'errors are different'
const SHOULD_THROW = 'Rambda should throw'
const SHOULD_NOT_THROW = 'Rambda should not throw'

function parseError(err){
  const typeError = switcher(err)
    .is(x => x instanceof TypeError, 'TypeError')
    .is(x => x instanceof TypeError, 'TypeError')
    .is(x => x instanceof TypeError, 'TypeError')
    .default('Missing error handle in parseError')

  return {
    message : err.message,
    type    : typeError,
    ok      : true,
  }
}

function executeSync(fn, inputs){
    let result = PENDING
    let error = { ok : false }
    try {
      result = fn(...inputs)
    } catch (e){
      error = parseError(e)
    }
   
    return {result, error}
}

export function profileMethod(
  firstInput,
  secondInput = undefined,
  thirdInput = undefined,
  fn,
){
  const combinationsInput = filter(Boolean, {
    firstInput,
    secondInput,
    thirdInput,
  })
  const inputKeys = Object.keys(combinationsInput)
  const combinations = combinate(combinationsInput)

  combinations.forEach(combination => {
    const inputs = [
      combination.firstInput,
      combination.secondInput,
      combination.thirdInput,
    ].filter((_, i) => i < inputKeys.length)

    test(getTestTitle(...inputs), () => {
      const {result, error} = executeSync(fn,inputs)
      expect({
        result,
        error,
        inputs,
      }).toMatchSnapshot()
    })
  })
}

export function compareToRamda(fn, fnRamda){
  return (...inputs) => {
    const {result, error} = executeSync(fn, inputs)
    const {result: ramdaResult, error: ramdaError} = executeSync(fnRamda, inputs)

    const toReturn = {
      result,
      ramdaResult,
      ramdaError : ramdaError.ok ? omit('ok', ramdaError) : PENDING,
      error      : error.ok ? omit('ok', error) : PENDING,
    }

    if (result !== PENDING){
      if (ramdaError.ok){
        return {
          ...toReturn,
          ok    : false,
          label : SHOULD_THROW,
        }
      }

      if (equals(result, ramdaResult)){
        return {
          ...toReturn,
          ok    : true,
          label : RESULTS_EQUAL,
        }
      }

      return {
        ...toReturn,
        ok    : false,
        label : RESULTS_MISMATCH,
      }
    }

    if (equals(error, ramdaError)){
      return {
        ...toReturn,
        ok    : true,
        label : ERRORS_EQUAL,
      }
    }

    if (ramdaError.ok){
      return {
        ...toReturn,
        ok    : false,
        label : ERRORS_MISMATCH,
      }
    }

    return {
      ...toReturn,
      ok    : false,
      label : SHOULD_NOT_THROW,
    }
  }
}

export const getTestTitle = (...inputs) =>
  inputs.map(x => `${ type(x) } ${ x }`).join(' | ')

export const compareCombinations = ({
  firstInput,
  secondInput = undefined,
  thirdInput = undefined,
  setCounter = () => {},
  setGlobalCounter = () => {},
  fn,
  fnRamda,
}) => {
  const combinationsInput = filter(Boolean, {
    firstInput,
    secondInput,
    thirdInput,
  })
  const inputKeys = Object.keys(combinationsInput)
  const combinations = combinate(combinationsInput)
  const compareOutputs = compareToRamda(fn, fnRamda)

  combinations.forEach(combination => {
    const inputs = [
      combination.firstInput,
      combination.secondInput,
      combination.thirdInput,
    ].filter((_, i) => i < inputKeys.length)

    test(getTestTitle(...inputs), () => {
      const compared = compareOutputs(...inputs)
      setGlobalCounter()
      if (!compared.ok){
        setCounter()
        expect({
          ...compared,
          inputs,
        }).toMatchSnapshot()
      }
    })
  })
}

{'fraction_non_alphanumeric': 0.12450704225352113, 'fraction_numerical': 0.0, 'mean_word_length': 2.2306642402183803, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 30, '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': '3603637', 'n_tokens_mistral': 2287, 'n_tokens_neox': 2225, 'n_words': 516}