<commit_before>/* eslint-env mocha */
/** @jsx createElement */

import {createElement, compile} from 'elliptical'
import chai, {expect} from 'chai'
import createProcessor from '../src/processor'
import {spy} from 'sinon'
import sinonChai from 'sinon-chai'

chai.use(sinonChai)

describe('process', () => {
  it('calls register with the results of observe', () => {
    const Test = {
      observe () {
        return 3
      }
    }
    const register = spy()
    const process = createProcessor(register)
    compile(<Test />, process)

    expect(register).to.have.been.calledWith(3)
  })

  it('passes props to observe', () => {
    const Test = {
      observe ({props, children}) {
        expect(props).to.eql({num: 3})
        expect(children).to.eql([])
        return props.num + 3
      }
    }
    const register = spy()
    const process = createProcessor(register)
    compile(<Test num={3} />, process)

    expect(register).to.have.been.calledWith(6)
  })

  it('passes result of register to describe as data', () => {
    const Root = {
      observe () {
        return 3
      },
      describe ({data}) {
        expect(data).to.eql(6)
        return <literal text='test' value={data} />
      }
    }

    const register = spy((num) => num + 3)
    const process = createProcessor(register)
    compile(<Root />, process)

    expect(register).to.have.been.calledWith(3)
  })

  it('does not recompile unless changed', () => {
    const describeSpy = spy()
    const Root = {
      observe () {},
      describe ({data}) {
        describeSpy()
        return <literal text='test' value={data} />
      }
    }

    const register = () => 6
    const process = createProcessor(register)
    const parse = compile(<Root />, process)

    parse('')
    parse('t')
    parse('te')
    expect(describeSpy).to.have.been.calledOnce
  })

  it('passes result of register to visit as data', () => {
    const Root = {
      observe () {
        return 3
      },
      visit (opt, {data}, traverse) {
        expect(opt.text).to.equal('test')
        expect(data).to.eql(6)
        return traverse(<literal text='test' value={data}/>, opt)
      }
    }

    const register = spy((num) => num + 3)
    const process = createProcessor(register)
    const parse = compile(<Root />, process)

    expect(register).to.have.been.calledWith(3)
    
    const options = parse('test')
    expect(options).to.have.length(1)
    expect(options[0].result).to.equal(6)
  })

  it('can process sources', () => {
    function sourceProcessor (element) {
      expect(element).to.equal(3)
      return 6
    }

    const Test = {}
    const Root = {
      observe () {
        return 3
      },
      describe ({data}) {
        expect(data).to.eql(9)
        return <Test test={data} />
      }
    }

    const register = spy((num) => num + 3)
    const process = createProcessor(register, sourceProcessor)
    compile(<Root />, process)

    expect(register).to.have.been.calledWith(6)
  })
})
<commit_msg>added test for sub-tree recompile<commit_after>/* eslint-env mocha */
/** @jsx createElement */

import {createElement, compile} from 'elliptical'
import chai, {expect} from 'chai'
import createProcessor from '../src/processor'
import {spy} from 'sinon'
import sinonChai from 'sinon-chai'

chai.use(sinonChai)

describe('process', () => {
  it('calls register with the results of observe', () => {
    const Test = {
      observe () {
        return 3
      }
    }
    const register = spy()
    const process = createProcessor(register)
    compile(<Test />, process)

    expect(register).to.have.been.calledWith(3)
  })

  it('passes props to observe', () => {
    const Test = {
      observe ({props, children}) {
        expect(props).to.eql({num: 3})
        expect(children).to.eql([])
        return props.num + 3
      }
    }
    const register = spy()
    const process = createProcessor(register)
    compile(<Test num={3} />, process)

    expect(register).to.have.been.calledWith(6)
  })

  it('passes result of register to describe as data', () => {
    const Root = {
      observe () {
        return 3
      },
      describe ({data}) {
        expect(data).to.eql(6)
        return <literal text='test' value={data} />
      }
    }

    const register = spy((num) => num + 3)
    const process = createProcessor(register)
    compile(<Root />, process)

    expect(register).to.have.been.calledWith(3)
  })

  it('does not recompile unless changed', () => {
    const describeSpy = spy()
    const Root = {
      observe () {},
      describe ({data}) {
        describeSpy()
        return <literal text='test' value={data} />
      }
    }

    const register = () => 6
    const process = createProcessor(register)
    const parse = compile(<Root />, process)

    parse('')
    parse('t')
    parse('te')
    expect(describeSpy).to.have.been.calledOnce
  })

  it('does not recompile children unless changed', () => {
    const describeSpy = spy()
    const Child = {
      describe ({ props }) {
        describeSpy()
        return <literal text='test' value={props.data} />
      }
    }
    const Root = {
      observe () {},
      describe ({data}) {
        return (
          <sequence>
            <literal text='test' />
            <Child data={data} />
          </sequence>
        )
      }
    }

    const register = () => 6
    const process = createProcessor(register)
    const parse = compile(<Root />, process)

    parse('')
    parse('t')
    parse('te')
    expect(describeSpy).to.have.been.calledOnce
  })

  it('passes result of register to visit as data', () => {
    const Root = {
      observe () {
        return 3
      },
      visit (opt, {data}, traverse) {
        expect(opt.text).to.equal('test')
        expect(data).to.eql(6)
        return traverse(<literal text='test' value={data}/>, opt)
      }
    }

    const register = spy((num) => num + 3)
    const process = createProcessor(register)
    const parse = compile(<Root />, process)

    expect(register).to.have.been.calledWith(3)
    
    const options = parse('test')
    expect(options).to.have.length(1)
    expect(options[0].result).to.equal(6)
  })

  it('can process sources', () => {
    function sourceProcessor (element) {
      expect(element).to.equal(3)
      return 6
    }

    const Test = {}
    const Root = {
      observe () {
        return 3
      },
      describe ({data}) {
        expect(data).to.eql(9)
        return <Test test={data} />
      }
    }

    const register = spy((num) => num + 3)
    const process = createProcessor(register, sourceProcessor)
    compile(<Root />, process)

    expect(register).to.have.been.calledWith(6)
  })
})

{'fraction_non_alphanumeric': 0.1541213295573111, 'fraction_numerical': 0.007303622000298107, 'mean_word_length': 2.3432984554060785, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 53, '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': '3465230', 'n_tokens_mistral': 2137, 'n_tokens_neox': 2056, 'n_words': 476}