<commit_before>require 'moon.all'
either_mod = require'ngx-oauth.either'
import Left, Right, either, encase, encase2 from either_mod
import mtype from require 'ngx-oauth.util'


behaves_like_either = (type_func, type_name) ->
  other = type_func == Right and Left or Right

  describe 'metatable.__eq', ->
    context 'same Either types with same value', ->
      it 'returns true', -> assert.is_true type_func(42) == type_func(42)

    context 'same Either types with different value', ->
      it 'returns false', -> assert.is_false type_func(42) == type_func(66)

    context 'different Either types with same value', ->
      it 'returns false', -> assert.is_false type_func(42) == other(42)

    context 'Either with non-Either', ->
      it 'returns false', -> assert.is_false type_func(66) == 66

  describe 'metatable.__tostring', ->
    it "returns #{type_name}(<value>)", ->
      assert.same "#{type_name}(42)", tostring(type_func(42))

  describe 'metatable.__type', ->
    it "is #{type_name}", ->
      assert.same type_name, getmetatable(type_func(42)).__type


describe '__call', ->
  it 'is alias for either', ->
    onleft, onright, right = ->, ->, Right(42)
    spy.on(either_mod, 'either')

    either_mod(onleft, onright, right)
    assert.stub(either_mod.either).called_with(onleft, onright, right)


describe 'Left', ->
  behaves_like_either Left, 'Left'

  left = Left(66)

  for func_name in *{'ap', 'map', 'chain'} do
    describe func_name, ->
      it 'returns self', ->
        assert.equal left, left[func_name](Right(42))


describe 'Right', ->
  behaves_like_either Right, 'Right'

  right = Right(42)

  describe 'ap', ->
    fright = Right((x) -> x * 2)

    context 'given Right', ->
      it "returns Right which value is result of applying this Right's value to given Right's value", ->
        given = Right(42)
        result = fright.ap(given)
        assert.equal Right(84), result

    context 'given Left', ->
      it 'returns given Left', ->
        given = Left(66)
        assert.equal given, fright.ap(given)

    context 'neither Left, nor Right', ->
      it 'throws error', ->
        assert.has_error -> fright.ap(66)

    context 'when this value is not a function', ->
      it 'throws error', ->
        assert.has_error -> right.ap(Left(66))

  describe 'map', ->
    it "returns Right which value is result of applying given function to this Right's value", ->
      result = right.map((x) -> x * 2)
      assert.equal Right(84), result

  describe 'chain', ->
    it "returns result of applying given function to this Right's value", ->
      assert.same 84, right.chain((x) -> x * 2)


describe 'either', ->

  before_each ->
    export onleft = mock(->)
    export onright = mock(->)

  context 'given Left', ->
    it "calls onleft handler with Left's value", ->
      either(onleft, onright, Left(66))
      assert.stub(onleft).called_with(66)
      assert.stub(onright).not_called()

  context 'given Right', ->
    it "calls onright handler with Right's value", ->
      either(onleft, onright, Right(42))
      assert.stub(onleft).not_called()
      assert.stub(onright).called_with(42)

  context 'given neither Left, nor Right', ->
    it 'throws error and does not call any handler', ->
      assert.has_error -> either(->, ->, {})
      assert.stub(onleft).not_called()
      assert.stub(onright).not_called()


shared_encase = (encase_func) ->
  it 'returns a function that wraps the given func and passes its arguments to it', ->
    func = mock(->)
    func2 = encase_func(func)
    assert.is_function func2
    func2(1, 2, 3)
    assert.stub(func).called_with(1, 2, 3)


describe 'encase', ->

  shared_encase encase

  context 'when given func has not raised error', ->
    it "nested function returns Right with the func's return value", ->
      result = encase(-> 'hai!')()
      assert.equal Right('hai!'), result<commit_msg>Some cosmetic changes in either_spec<commit_after>require 'moon.all'
either_mod = require'ngx-oauth.either'
import Left, Right, either, encase, encase2 from either_mod
import mtype from require 'ngx-oauth.util'


behaves_like_either = (type_func, type_name) ->
  other = type_func == Right and Left or Right

  describe 'metatable.__eq', ->
    context 'same Either types with same value', ->
      it 'returns true', -> assert.is_true type_func(42) == type_func(42)

    context 'same Either types with different value', ->
      it 'returns false', -> assert.is_false type_func(42) == type_func(66)

    context 'different Either types with same value', ->
      it 'returns false', -> assert.is_false type_func(42) == other(42)

    context 'Either with non-Either', ->
      it 'returns false', -> assert.is_false type_func(66) == 66

  describe 'metatable.__tostring', ->
    it "returns #{type_name}(<value>)", ->
      assert.same "#{type_name}(42)", tostring(type_func(42))

  describe 'metatable.__type', ->
    it "is #{type_name}", ->
      assert.same type_name, getmetatable(type_func(42)).__type


describe '__call', ->
  it 'is alias for either', ->
    onleft, onright, right = ->, ->, Right(42)
    spy.on(either_mod, 'either')

    either_mod(onleft, onright, right)
    assert.stub(either_mod.either).called_with(onleft, onright, right)


describe 'Left', ->
  behaves_like_either Left, 'Left'

  left = Left(66)

  for func_name in *{'ap', 'map', 'chain'} do
    describe func_name, ->
      it 'returns self', ->
        assert.equal left, left[func_name](Right(42))


describe 'Right', ->
  behaves_like_either Right, 'Right'

  right = Right(42)

  describe 'ap', ->
    fright = Right((x) -> x * 2)

    context 'given Right', ->
      it "returns Right which value is result of applying this Right's value to given Right's value", ->
        given = Right(42)
        result = fright.ap(given)
        assert.equal Right(84), result

    context 'given Left', ->
      it 'returns given Left', ->
        given = Left(66)
        assert.equal given, fright.ap(given)

    context 'neither Left, nor Right', ->
      it 'throws error', ->
        assert.has_error -> fright.ap(66)

    context 'when this value is not a function', ->
      it 'throws error', ->
        assert.has_error -> right.ap(Left(66))

  describe 'map', ->
    it "returns Right which value is result of applying given function to this Right's value", ->
      result = right.map((x) -> x * 2)
      assert.equal Right(84), result

  describe 'chain', ->
    it "returns result of applying given function to this Right's value", ->
      assert.same 84, right.chain((x) -> x * 2)


describe 'either', ->

  before_each ->
    export onleft = mock(->)
    export onright = mock(->)

  context 'given Left', ->
    it "calls onleft handler with Left's value", ->
      either(onleft, onright, Left(66))
      assert.stub(onleft).called_with(66)
      assert.stub(onright).not_called!

  context 'given Right', ->
    it "calls onright handler with Right's value", ->
      either(onleft, onright, Right(42))
      assert.stub(onleft).not_called!
      assert.stub(onright).called_with(42)

  context 'given neither Left, nor Right', ->
    it 'throws error and does not call any handler', ->
      assert.has_error -> either(->, ->, {})
      assert.stub(onleft).not_called!
      assert.stub(onright).not_called!


shared_encase = (encase_func) ->
  it 'returns a function that wraps the given func and passes its arguments to it', ->
    func = mock(->)
    func2 = encase_func(func)
    assert.is_function func2
    func2(1, 2, 3)
    assert.stub(func).called_with(1, 2, 3)


describe 'encase', ->

  shared_encase encase

  context 'when given func has not raised error', ->
    it "nested function returns Right with the func's return value", ->
      result = encase(-> 'hai!')()
      assert.equal Right('hai!'), result
{'fraction_non_alphanumeric': 0.14031948881789139, 'fraction_numerical': 0.016613418530351438, 'mean_word_length': 3.0528223718280683, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 125, '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': '2952780', 'n_tokens_mistral': 2622, 'n_tokens_neox': 2444, 'n_words': 735}