<commit_before>Promise = require('promise')
_ = require('../lib/util.coffee')

describe "Util", ->
  describe "#defaults", ->
    it "extends an object from the left to right", ->
      incomeOptions =
        a: 1
        b: 2

      options = _.defaults incomeOptions,
        b: 3
        c: 4

      expect(options).eql {a: 1, b: 2, c: 4}

  describe "#isString", ->
    it "correctly detects strings", ->
      expect(_.isString(null)).false
      expect(_.isString(false)).false
      expect(_.isString({})).false
      expect(_.isString([])).false
      expect(_.isString(undefined)).false
      expect(_.isString('')).true
      expect(_.isString(' ')).true

  describe "#contains", ->
    it "test if an element is present on a list", ->
      expect(_.contains([], 1)).false
      expect(_.contains([1], 1)).true
      expect(_.contains([0, 1, 2], 0)).true
      expect(_.contains([0, 1, 2], 1)).true
      expect(_.contains([0, 1, 2], 2)).true
      expect(_.contains([0, 1, 2], 3)).false

  describe "#map", ->
    it "maps a list into another", ->
      expect(_.map([1, 2, 3], (x) -> x * 2)).eql [2, 4, 6]

  describe "#has", ->
    it "detects if a key is present on the object", ->
      obj =
        a: undefined
        b: null
        d: false
        c: true
        e: "string"
        f: 4

      expect(_.has(obj, 'a')).true
      expect(_.has(obj, 'b')).true
      expect(_.has(obj, 'c')).true
      expect(_.has(obj, 'd')).true
      expect(_.has(obj, 'e')).true
      expect(_.has(obj, 'f')).true
      expect(_.has(obj, 'g')).false

  describe "#lift", ->
    describe "dealing with values", ->
      it "converts the return into a promise", ->
        fn = (input) -> "#{input}-value"

        _.lift(fn)('in').then (value) ->
          expect(value).eq 'in-value'

      it "raises a rejected promise when the function throw an error", ->
        fn = -> throw new Error('err')

        expect(_.lift(fn)()).hold.reject('err')

    it "returns a promise if it's already one", ->
      fn = -> Promise.resolve('value')

      _.lift(fn)().then (value) ->
        expect(value).eq 'value'<commit_msg>added another safe test on the lift function<commit_after>Promise = require('promise')
_ = require('../lib/util.coffee')

describe "Util", ->
  describe "#defaults", ->
    it "extends an object from the left to right", ->
      incomeOptions =
        a: 1
        b: 2

      options = _.defaults incomeOptions,
        b: 3
        c: 4

      expect(options).eql {a: 1, b: 2, c: 4}

  describe "#isString", ->
    it "correctly detects strings", ->
      expect(_.isString(null)).false
      expect(_.isString(false)).false
      expect(_.isString({})).false
      expect(_.isString([])).false
      expect(_.isString(undefined)).false
      expect(_.isString('')).true
      expect(_.isString(' ')).true

  describe "#contains", ->
    it "test if an element is present on a list", ->
      expect(_.contains([], 1)).false
      expect(_.contains([1], 1)).true
      expect(_.contains([0, 1, 2], 0)).true
      expect(_.contains([0, 1, 2], 1)).true
      expect(_.contains([0, 1, 2], 2)).true
      expect(_.contains([0, 1, 2], 3)).false

  describe "#map", ->
    it "maps a list into another", ->
      expect(_.map([1, 2, 3], (x) -> x * 2)).eql [2, 4, 6]

  describe "#has", ->
    it "detects if a key is present on the object", ->
      obj =
        a: undefined
        b: null
        d: false
        c: true
        e: "string"
        f: 4

      expect(_.has(obj, 'a')).true
      expect(_.has(obj, 'b')).true
      expect(_.has(obj, 'c')).true
      expect(_.has(obj, 'd')).true
      expect(_.has(obj, 'e')).true
      expect(_.has(obj, 'f')).true
      expect(_.has(obj, 'g')).false

  describe "#lift", ->
    describe "dealing with values", ->
      it "converts the return into a promise", ->
        fn = (input) -> "#{input}-value"

        _.lift(fn)('in').then (value) ->
          expect(value).eq 'in-value'

      it "raises a rejected promise when the function throw an error", ->
        fn = -> throw new Error('err')

        expect(_.lift(fn)()).hold.reject('err')

    describe "dealing with promises", ->
      it "returns a promise if it's already one", ->
        fn = -> Promise.resolve('value')

        _.lift(fn)().then (value) ->
          expect(value).eq 'value'

      it "raises a rejected promise when the given promise is failed throw an error", ->
        fn = -> Promise.reject(new Error('err'))

        expect(_.lift(fn)()).hold.reject('err')
{'fraction_non_alphanumeric': 0.1836870026525199, 'fraction_numerical': 0.015030946065428824, 'mean_word_length': 2.2437275985663083, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 50, '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': '5793288', 'n_tokens_mistral': 1517, 'n_tokens_neox': 1435, 'n_words': 402}