<commit_before>assert = require('chai').assert
h = require('./test_helpers')
Jnoid = require '../jnoid.coffee.md'
{Event, Value, Error, End} = Jnoid

describe 'more and noMore', ->
  it 'is the same', ->
    assert.equal(Jnoid.more, Jnoid.more)
    assert.equal(Jnoid.noMore, Jnoid.noMore)

  it 'is different', ->
    assert.notEqual(Jnoid.more, Jnoid.noMore)


describe 'Event', ->
  describe 'Value', ->
    it 'is value, is not error, is not end', ->
      event = new Value 5
      assert.ok(event.isValue())
      assert.notOk(event.isError())
      assert.notOk(event.isEnd())

    it 'returns value', ->
      event = new Value 5
      assert.equal(event.value, 5)

    it 'filters value', ->
      assert.ok(new Value(5).filter((x)-> x < 10))
      assert.notOk(new Value(15).filter((x)-> x < 10))

    it 'fmaps value', ->
      assert.deepEqual(new Value(5).fmap((x) -> x + 5),
        new Value(10))

  describe 'Error', ->
    it 'is not a value, is error, is not end', ->
      event = new Error 'whut'
      assert.notOk(event.isValue())
      assert.ok(event.isError())
      assert.notOk(event.isEnd())

    it 'returns error', ->
      event = new Error 'whut'
      assert.equal(event.error, 'whut')

    it 'does not filter', ->
      assert.ok(new Error('whut').filter((x)-> x < 10))

    it 'does not fmap', ->
      assert.deepEqual(new Error('whut').fmap((x) -> x + 5),
        new Error('whut'))

  describe 'End', ->
    it 'is not a value, is not an error, is end', ->
      event = new End
      assert.notOk(event.isValue())
      assert.notOk(event.isError())
      assert.ok(event.isEnd())

    it 'does not filter', ->
      assert.ok(new End().filter((x)-> x < 10))

    it 'does not fmap', ->
      assert.deepEqual(new End().fmap((x) -> x + 5), new End)

describe 'fromList', ->
  it 'works', (done)->
    h.expectValues [1, 2, 3],
      Jnoid.fromList([1, 2, 3]),
      done

describe 'unit', ->
  it 'sends a single event', (done)->
    h.expectValues [1],
      Jnoid.unit(1),
      done

  it 'does not send anything with no arguments', (done)->
    h.expectValues [],
      Jnoid.unit(),
      done

describe 'map', ->
  it 'transforms values', (done)->
    stream = Jnoid.fromList([1, 2, 3])
    h.expectValues [2, 4, 6],
      stream.map((x)-> x * 2),
      done

describe 'flatMap', ->
  it 'combines spawned streams (trivial case)', (done)->
    stream = Jnoid.fromList([1, 2, 3])
    h.expectValues [1, 2, 3],
      stream.flatMap((x)-> Jnoid.unit(x)),
      done

describe 'merge', ->
  it 'merges lots of streams', (done)->
    first = Jnoid.fromList([1, 2, 3])
    second = Jnoid.fromList([10, 20, 30])
    third = Jnoid.fromList([100, 200, 300])
    h.expectValues [1, 2, 3, 10, 20, 30, 100, 200, 300],
      first.merge(second, third),
      done

describe 'delay', ->
  it 'sends all events after a delay', (done)->
    first = Jnoid.fromList([1, 2, 3]).delay(10)
    second = Jnoid.fromList([10, 20, 30])
    h.expectValues [10, 20, 30, 1, 2, 3],
      first.merge(second),
      done

describe 'zip', ->
  it 'zips streams', (done)->
    first = Jnoid.sequentially(10, [1, 2])
    second = Jnoid.sequentially(15, [100, 200])
    h.expectValues [[1, 100], [2, 100], [2, 200]],
      Jnoid.zip([first, second]),
      done

describe 'zipWith', ->
  it 'zips with function', (done)->
    first = Jnoid.sequentially(10, [1, 2])
    second = Jnoid.sequentially(15, [100, 200])
    h.expectValues [101, 102, 202],
      Jnoid.zipWith([first, second], (x, y) -> x + y),
      done

describe 'and', ->
  it 'executes boolean "and" between streams', (done)->
    first = Jnoid.sequentially(10, [false, true])
    second = Jnoid.sequentially(15, [false, true])
    h.expectValues [false, false, true],
      first.and(second),
      done

describe 'or', ->
  it 'executes boolean "or" between streams', (done)->
    first = Jnoid.sequentially(10, [false, true])
    second = Jnoid.sequentially(15, [false, true])
    h.expectValues [false, true, true],
      first.or(second),
      done

describe 'not', ->
  it 'executes boolean "not" on stream', (done)->
    stream = Jnoid.fromList([false, true, false])
    h.expectValues [true, false, true],
      stream.not(),
      done

describe 'filter', ->
  it 'filters stream', (done)->
    stream = Jnoid.fromList([10, 100, 20])
    h.expectValues [10, 20],
      stream.filter((x)-> x < 50),
      done

describe 'take', ->
  it 'takes n', (done)->
    stream = Jnoid.fromList([10, 100, 20, 200])
    h.expectValues [10, 100],
      stream.take(2),
      done

describe 'takeWhile', ->
  it 'takes while', (done)->
    stream = Jnoid.fromList([10, 100, 20])
    h.expectValues [10],
      stream.takeWhile((x)-> x < 50),
      done

describe 'prepend', ->
  it 'prepends a value to a stream', (done)->
    stream = Jnoid.fromList([10, 20, 30])
    h.expectValues [1, 10, 20, 30],
      stream.prepend(1),
      done

describe 'takeUntil', ->
  it 'takes until other stream pops', (done)->
    stream = Jnoid.sequentially(10, [10, 20, 30, 40, 50])
    stopper = Jnoid.later(35, 100)
    h.expectValues [10, 20, 30],
      stream.takeUntil(stopper),
      done
<commit_msg>Add tests for onValue and onError.<commit_after>assert = require('chai').assert
h = require('./test_helpers')
Jnoid = require '../jnoid.coffee.md'
{Event, Value, Error, End, EventStream} = Jnoid

describe 'more and noMore', ->
  it 'is the same', ->
    assert.equal(Jnoid.more, Jnoid.more)
    assert.equal(Jnoid.noMore, Jnoid.noMore)

  it 'is different', ->
    assert.notEqual(Jnoid.more, Jnoid.noMore)


describe 'Event', ->
  describe 'Value', ->
    it 'is value, is not error, is not end', ->
      event = new Value 5
      assert.ok(event.isValue())
      assert.notOk(event.isError())
      assert.notOk(event.isEnd())

    it 'returns value', ->
      event = new Value 5
      assert.equal(event.value, 5)

    it 'filters value', ->
      assert.ok(new Value(5).filter((x)-> x < 10))
      assert.notOk(new Value(15).filter((x)-> x < 10))

    it 'fmaps value', ->
      assert.deepEqual(new Value(5).fmap((x) -> x + 5),
        new Value(10))

  describe 'Error', ->
    it 'is not a value, is error, is not end', ->
      event = new Error 'whut'
      assert.notOk(event.isValue())
      assert.ok(event.isError())
      assert.notOk(event.isEnd())

    it 'returns error', ->
      event = new Error 'whut'
      assert.equal(event.error, 'whut')

    it 'does not filter', ->
      assert.ok(new Error('whut').filter((x)-> x < 10))

    it 'does not fmap', ->
      assert.deepEqual(new Error('whut').fmap((x) -> x + 5),
        new Error('whut'))

  describe 'End', ->
    it 'is not a value, is not an error, is end', ->
      event = new End
      assert.notOk(event.isValue())
      assert.notOk(event.isError())
      assert.ok(event.isEnd())

    it 'does not filter', ->
      assert.ok(new End().filter((x)-> x < 10))

    it 'does not fmap', ->
      assert.deepEqual(new End().fmap((x) -> x + 5), new End)

describe 'fromList', ->
  it 'works', (done)->
    h.expectValues [1, 2, 3],
      Jnoid.fromList([1, 2, 3]),
      done

describe 'unit', ->
  it 'sends a single event', (done)->
    h.expectValues [1],
      Jnoid.unit(1),
      done

  it 'does not send anything with no arguments', (done)->
    h.expectValues [],
      Jnoid.unit(),
      done

describe 'onValue', ->
  it 'listens to values only', ->
    stream = new EventStream (sink)->
      sink new Value 5
      sink new Error 'whut'
      sink new Value 10

    events = []
    stream.onValue (e)-> events.push e
    setTimeout ->
      assert.equal [5, 10], events
    , 1000

describe 'onError', ->
  it 'listens to values only', ->
    stream = new EventStream (sink)->
      sink new Value 5
      sink new Error 'whut'
      sink new Value 10

    events = []
    stream.onError (e)-> events.push e
    setTimeout ->
      assert.equal ['whut'], events
    , 1000

describe 'map', ->
  it 'transforms values', (done)->
    stream = Jnoid.fromList([1, 2, 3])
    h.expectValues [2, 4, 6],
      stream.map((x)-> x * 2),
      done

describe 'flatMap', ->
  it 'combines spawned streams (trivial case)', (done)->
    stream = Jnoid.fromList([1, 2, 3])
    h.expectValues [1, 2, 3],
      stream.flatMap((x)-> Jnoid.unit(x)),
      done

describe 'merge', ->
  it 'merges lots of streams', (done)->
    first = Jnoid.fromList([1, 2, 3])
    second = Jnoid.fromList([10, 20, 30])
    third = Jnoid.fromList([100, 200, 300])
    h.expectValues [1, 2, 3, 10, 20, 30, 100, 200, 300],
      first.merge(second, third),
      done

describe 'delay', ->
  it 'sends all events after a delay', (done)->
    first = Jnoid.fromList([1, 2, 3]).delay(10)
    second = Jnoid.fromList([10, 20, 30])
    h.expectValues [10, 20, 30, 1, 2, 3],
      first.merge(second),
      done

describe 'zip', ->
  it 'zips streams', (done)->
    first = Jnoid.sequentially(10, [1, 2])
    second = Jnoid.sequentially(15, [100, 200])
    h.expectValues [[1, 100], [2, 100], [2, 200]],
      Jnoid.zip([first, second]),
      done

describe 'zipWith', ->
  it 'zips with function', (done)->
    first = Jnoid.sequentially(10, [1, 2])
    second = Jnoid.sequentially(15, [100, 200])
    h.expectValues [101, 102, 202],
      Jnoid.zipWith([first, second], (x, y) -> x + y),
      done

describe 'and', ->
  it 'executes boolean "and" between streams', (done)->
    first = Jnoid.sequentially(10, [false, true])
    second = Jnoid.sequentially(15, [false, true])
    h.expectValues [false, false, true],
      first.and(second),
      done

describe 'or', ->
  it 'executes boolean "or" between streams', (done)->
    first = Jnoid.sequentially(10, [false, true])
    second = Jnoid.sequentially(15, [false, true])
    h.expectValues [false, true, true],
      first.or(second),
      done

describe 'not', ->
  it 'executes boolean "not" on stream', (done)->
    stream = Jnoid.fromList([false, true, false])
    h.expectValues [true, false, true],
      stream.not(),
      done

describe 'filter', ->
  it 'filters stream', (done)->
    stream = Jnoid.fromList([10, 100, 20])
    h.expectValues [10, 20],
      stream.filter((x)-> x < 50),
      done

describe 'take', ->
  it 'takes n', (done)->
    stream = Jnoid.fromList([10, 100, 20, 200])
    h.expectValues [10, 100],
      stream.take(2),
      done

describe 'takeWhile', ->
  it 'takes while', (done)->
    stream = Jnoid.fromList([10, 100, 20])
    h.expectValues [10],
      stream.takeWhile((x)-> x < 50),
      done

describe 'prepend', ->
  it 'prepends a value to a stream', (done)->
    stream = Jnoid.fromList([10, 20, 30])
    h.expectValues [1, 10, 20, 30],
      stream.prepend(1),
      done

describe 'takeUntil', ->
  it 'takes until other stream pops', (done)->
    stream = Jnoid.sequentially(10, [10, 20, 30, 40, 50])
    stopper = Jnoid.later(35, 100)
    h.expectValues [10, 20, 30],
      stream.takeUntil(stopper),
      done

{'fraction_non_alphanumeric': 0.17664260257698985, 'fraction_numerical': 0.043041213561180666, 'mean_word_length': 2.6322602057749753, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 139, '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': '5864154', 'n_tokens_mistral': 4286, 'n_tokens_neox': 3847, 'n_words': 1085}