<commit_before>'use strict'

var i18n = undefined
  , opts = undefined
  , assert = require('assert')
  , path = require('path')
  , fs = require('fs')
  , msg = null

describe('i18n-light module', function() {
  beforeEach(function() {
    i18n = require('../index')
    opts = {
      defaultLocale: 'en',
      dir: path.join(__dirname, 'locale'),
      fallback: false,
      extension: '.js',
      cache: true,
      refresh: true,
      context: {
        en: {
          'greetings': 'hello'
        },
        it: {
          'greetings': 'ciao'
        }
      },
      resolve: function(locale) {
        return {data: 'JSON from a resolve.'}
      },
    }
  })

  afterEach(function() {
    i18n = undefined
    opts = undefined
  })

  describe('configure method', function() {
    msg = 'should set the default locale as the current locale'
    it(msg, function() {
      i18n.configure(opts)
      assert(i18n._currentLocale === opts.defaultLocale)
    })

    msg = 'when required attributes are not provided should throw an error:'
    describe(msg, function() {
      it('\'defaultLocale\'.', function() {
        delete opts.defaultLocale
        assert.throws(function() {
          i18n.configure(opts)
        }, Error, /defaultLocale/)
      })

      it('\'dir\' and \'context\'.', function() {
        delete opts.dir
        delete opts.context
        delete opts.resolve
        assert.throws(function() {
          i18n.configure(opts)
        }, Error, /dir/)
      })
    })

    msg = 'should assign the valid attributes provided to i18n-light instance:'
    describe(msg, function() {
      beforeEach(function() {
        i18n.configure(opts)
      })

      it('\'defaultLocale\'.', function() {
        assert(i18n._defaultLocale === opts.defaultLocale)
      })

      it('\'dir\'.', function() {
        assert(i18n._dir === opts.dir)
      })

      it('\'cache\'.', function() {
        assert(i18n._cache === opts.cache)
      })

      it('\'fallback\'.', function() {
        assert(i18n._fallback === opts.fallback)
      })

      it('\'extension\'.', function() {
        assert(i18n._extension === opts.extension)
      })

      it('\'refresh\'.', function() {
        assert(i18n._refresh === opts.refresh)
      })
    })

    describe('context attribute', function() {
      describe('if \'dir\' is omitted', function() {
        beforeEach(function() {
          delete opts.dir
          i18n.configure(opts)
        })
        it('should be assigned', function() {
          assert(i18n._context === opts.context)
        })
      })

      describe('if \'dir\' is not omitted', function() {
        it('shouldn\'t be assigned', function() {
          assert(i18n._context !== opts.context)
        })
      })
    })

    describe('resolve attribute', function() {
      describe('if \'dir\' and context are omitted', function() {
        beforeEach(function() {
          delete opts.dir
          delete opts.context
          i18n.configure(opts)
        })
        it('should be assigned', function() {
          assert(i18n._resolve === opts.resolve)
        })
      })

      describe('if \'dir\' is not omitted', function() {
        it('shouldn\'t be assigned', function() {
          assert(i18n._resolve !== opts.resolve)
        })
      })

      describe('if \'context\' is not omitted', function() {
        beforeEach(function() {
          delete opts.dir
        })
        it('shouldn\'t be assigned', function() {
          assert(i18n._resolve !== opts.resolve)
        })
      })
    })

    msg = 'when omittable fields are omitted they should use their defaults'
    describe(msg, function() {
      describe('extension attribute', function() {
        beforeEach(function() {
          delete opts.extension
          i18n.configure(opts)
        })

        describe('when it is omitted', function() {
          it('should default to \'.js\'', function() {
            assert(i18n._extension === '.js')
          })
        })
      })

      describe('refresh attribute', function() {
        beforeEach(function() {
          delete opts.refresh
          i18n.configure(opts)
        })

        describe('when it is omitted', function() {
          it('should default to \'false\'', function() {
            assert(!(i18n._refresh))
          })
        })
      })

      describe('cache attribute', function() {
        beforeEach(function() {
          delete opts.cache
          i18n.configure(opts)
        })

        describe('when it is omitted', function() {
          it('should default to \'true\'', function() {
            assert(i18n._cache === true)
          })
        })
      })
    })
  })

  describe('version', function() {
    beforeEach(function() {
      i18n.configure(opts)
    })

    it('should be the same as the version inside package.json.', function() {
      var packageJSON = require('fs')
        .readFileSync(path.join(__dirname, '../package.json'), 'utf-8')
      assert(i18n.version === JSON.parse(packageJSON).version)
    })
  })

  describe('setLocale', function() {
    var locale = null
    var altLocale = null
    beforeEach(function() {
      locale = Object.keys(opts.context)[0]
      altLocale = Object.keys(opts.context)[1]
      i18n.configure(opts)
      i18n.setLocale(altLocale)
      delete i18n._context[locale]
    })

    it('TEST INTERNAL: \'locale\' value should be defaultLocale.', function() {
      assert(locale === opts.defaultLocale)
    })

    describe('when current locale\'s context is not cached', function() {
      describe('and this._cache === false (clear cache)', function() {
        beforeEach(function() {
          i18n._cache = false
          i18n.setLocale(locale)
        })<commit_msg>Test that tidyDirPath method includes a trailer /.<commit_after>'use strict'

var i18n = undefined
  , opts = undefined
  , assert = require('assert')
  , path = require('path')
  , fs = require('fs')
  , msg = null

describe('i18n-light module', function() {
  beforeEach(function() {
    i18n = require('../index')
    opts = {
      defaultLocale: 'en',
      dir: path.join(__dirname, 'locale'),
      fallback: false,
      extension: '.js',
      cache: true,
      refresh: true,
      context: {
        en: {
          'greetings': 'hello'
        },
        it: {
          'greetings': 'ciao'
        }
      },
      resolve: function(locale) {
        return {data: 'JSON from a resolve.'}
      },
    }
  })

  afterEach(function() {
    i18n = undefined
    opts = undefined
  })

  describe('configure method', function() {
    msg = 'should set the default locale as the current locale'
    it(msg, function() {
      i18n.configure(opts)
      assert(i18n._currentLocale === opts.defaultLocale)
    })

    msg = 'when required attributes are not provided should throw an error:'
    describe(msg, function() {
      it('\'defaultLocale\'.', function() {
        delete opts.defaultLocale
        assert.throws(function() {
          i18n.configure(opts)
        }, Error, /defaultLocale/)
      })

      it('\'dir\' and \'context\'.', function() {
        delete opts.dir
        delete opts.context
        delete opts.resolve
        assert.throws(function() {
          i18n.configure(opts)
        }, Error, /dir/)
      })
    })

    msg = 'should assign the valid attributes provided to i18n-light instance:'
    describe(msg, function() {
      beforeEach(function() {
        i18n.configure(opts)
      })

      it('\'defaultLocale\'.', function() {
        assert(i18n._defaultLocale === opts.defaultLocale)
      })

      it('\'dir\'.', function() {
        assert(i18n._dir === opts.dir + '/')
      })

      it('\'cache\'.', function() {
        assert(i18n._cache === opts.cache)
      })

      it('\'fallback\'.', function() {
        assert(i18n._fallback === opts.fallback)
      })

      it('\'extension\'.', function() {
        assert(i18n._extension === opts.extension)
      })

      it('\'refresh\'.', function() {
        assert(i18n._refresh === opts.refresh)
      })
    })

    describe('context attribute', function() {
      describe('if \'dir\' is omitted', function() {
        beforeEach(function() {
          delete opts.dir
          i18n.configure(opts)
        })
        it('should be assigned', function() {
          assert(i18n._context === opts.context)
        })
      })

      describe('if \'dir\' is not omitted', function() {
        it('shouldn\'t be assigned', function() {
          assert(i18n._context !== opts.context)
        })
      })
    })

    describe('resolve attribute', function() {
      describe('if \'dir\' and context are omitted', function() {
        beforeEach(function() {
          delete opts.dir
          delete opts.context
          i18n.configure(opts)
        })
        it('should be assigned', function() {
          assert(i18n._resolve === opts.resolve)
        })
      })

      describe('if \'dir\' is not omitted', function() {
        it('shouldn\'t be assigned', function() {
          assert(i18n._resolve !== opts.resolve)
        })
      })

      describe('if \'context\' is not omitted', function() {
        beforeEach(function() {
          delete opts.dir
        })
        it('shouldn\'t be assigned', function() {
          assert(i18n._resolve !== opts.resolve)
        })
      })
    })

    msg = 'when omittable fields are omitted they should use their defaults'
    describe(msg, function() {
      describe('extension attribute', function() {
        beforeEach(function() {
          delete opts.extension
          i18n.configure(opts)
        })

        describe('when it is omitted', function() {
          it('should default to \'.js\'', function() {
            assert(i18n._extension === '.js')
          })
        })
      })

      describe('refresh attribute', function() {
        beforeEach(function() {
          delete opts.refresh
          i18n.configure(opts)
        })

        describe('when it is omitted', function() {
          it('should default to \'false\'', function() {
            assert(!(i18n._refresh))
          })
        })
      })

      describe('cache attribute', function() {
        beforeEach(function() {
          delete opts.cache
          i18n.configure(opts)
        })

        describe('when it is omitted', function() {
          it('should default to \'true\'', function() {
            assert(i18n._cache === true)
          })
        })
      })
    })
  })

  describe('version', function() {
    beforeEach(function() {
      i18n.configure(opts)
    })

    it('should be the same as the version inside package.json.', function() {
      var packageJSON = require('fs')
        .readFileSync(path.join(__dirname, '../package.json'), 'utf-8')
      assert(i18n.version === JSON.parse(packageJSON).version)
    })
  })

  describe('_tidyDirPath', function() {
    beforeEach(function() {
     i18n.configure(opts)
    })
    it('should include a trailing \'/\' to i18n.dir', function() {
      var newPath = 'not/tidy'
      i18n._tidyDirPath(newPath)
      console.log(i18n._dir)
      console.log((newPath + '/'))
      assert(i18n._dir === (newPath + '/'))
    })
  })

  describe('setLocale', function() {
    var locale = null
    var altLocale = null
    beforeEach(function() {
      locale = Object.keys(opts.context)[0]
      altLocale = Object.keys(opts.context)[1]
      i18n.configure(opts)
      i18n.setLocale(altLocale)
      delete i18n._context[locale]
    })

    it('TEST INTERNAL: \'locale\' value should be defaultLocale.', function() {
      assert(locale === opts.defaultLocale)
    })

    describe('when current locale\'s context is not cached', function() {
      describe('and this._cache === false (clear cache)', function() {
        beforeEach(function() {
          i18n._cache = false
          i18n.setLocale(locale)
        })
{'fraction_non_alphanumeric': 0.1524840550520309, 'fraction_numerical': 0.01342732460557234, 'mean_word_length': 2.0307731434384535, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '2678206', 'n_tokens_mistral': 3532, 'n_tokens_neox': 3341, 'n_words': 748}