<commit_before>const expect     = require('expect.js');
const path       = require('path');
const cheerio    = require('cheerio');
const _          = require('lodash');
const fs         = require('fs-extra');
const tmp        = require('tmp');
const staticI18n = require('../lib/index');

describe('processor', function() {
  const basepath = path.join(__dirname, 'data');

  const file = path.join(basepath, 'index.html');
  let options = {};

  beforeEach(function() {
    options = {
      localesPath: path.join(__dirname, 'data', 'locales'),
      outputDir: false
    };
  });

  describe('#process', function() {
    const input = '<p data-t="foo.bar"></p>';

    it('should process all locales', async function() {
      _.merge(options, {locales: ['en', 'ja']});
      const results = await staticI18n.process(input, options);
      expect(results).to.only.have.keys(['ja', 'en']);
      expect(results.ja).to.be('<p>ja_bar</p>');
      expect(results.en).to.be('<p>bar</p>');
    });

    it('should work with yaml', async function() {
      const input = '<p data-t="yaml.bar"></p>';
      _.merge(options, {fileFormat: 'yml'});
      const results = await staticI18n.process(input, options);
      expect(results).to.only.have.keys(['en']);
      expect(results.en).to.be('<p>bar</p>');
    });

    it('should translate attributes', async function() {
      const input = '<input class="foo" id="ok" data-attr-t value-t="foo.bar">';
      const results = await staticI18n.process(input, options);
      expect(results).to.only.have.keys(['en']);
      const $ = cheerio.load(results.en);
      expect($('input').attr('value')).to.be('bar');
      expect($('input').attr('id')).to.be('ok');
    });

    it('should translate attributes with interpolation', async function() {
      options = _.merge({}, options, {locales: ['en', 'ja']});
      const input = '<input data-attr-t data-attr-t-interpolate href-t="{{links.baseAbsolute}}filename.{{links.extension}}">';
      const results = await staticI18n.process(input, options);
      let $ = cheerio.load(results.en);
      expect($('input').attr('href')).to.be('http://www.example.com/filename.html');
      expect($('input').attr('data-attr-t-interpolate')).to.be(undefined);
      $ = cheerio.load(results.ja);
      expect($('input').attr('href')).to.be('http://www.example.com/ja/filename.htm');
      expect($('input').attr('data-attr-t-interpolate')).to.be(undefined);
    });

    it('should remove interpolation related attributes', async function() {
      options = _.merge({}, options, {locales: ['en']});

      const input = '<p data-t data-t-interpolate>texto aqui<div>Whatever here</div></p>';
      const results = await staticI18n.process(input, options);
      const $ = cheerio.load(results.en);
      expect($('p').attr('data-t-interpolate')).to.be(undefined);
    });
<commit_msg>add test for attr include 't'<commit_after>const expect     = require('expect.js');
const path       = require('path');
const cheerio    = require('cheerio');
const _          = require('lodash');
const fs         = require('fs-extra');
const tmp        = require('tmp');
const staticI18n = require('../lib/index');

describe('processor', function() {
  const basepath = path.join(__dirname, 'data');

  const file = path.join(basepath, 'index.html');
  let options = {};

  beforeEach(function() {
    options = {
      localesPath: path.join(__dirname, 'data', 'locales'),
      outputDir: false
    };
  });

  describe('#process', function() {
    const input = '<p data-t="foo.bar"></p>';

    it('should process all locales', async function() {
      _.merge(options, {locales: ['en', 'ja']});
      const results = await staticI18n.process(input, options);
      expect(results).to.only.have.keys(['ja', 'en']);
      expect(results.ja).to.be('<p>ja_bar</p>');
      expect(results.en).to.be('<p>bar</p>');
    });

    it('should work with yaml', async function() {
      const input = '<p data-t="yaml.bar"></p>';
      _.merge(options, {fileFormat: 'yml'});
      const results = await staticI18n.process(input, options);
      expect(results).to.only.have.keys(['en']);
      expect(results.en).to.be('<p>bar</p>');
    });

    it('should translate attributes', async function() {
      const input = '<input class="foo" id="ok" data-attr-t value-t="foo.bar">';
      const results = await staticI18n.process(input, options);
      expect(results).to.only.have.keys(['en']);
      const $ = cheerio.load(results.en);
      expect($('input').attr('value')).to.be('bar');
      expect($('input').attr('id')).to.be('ok');
    });

    it('should translate attributes include "t"', async function() {
      const img = '<img src="example.png" class="foo" id="ok" data-attr-t alt-t="foo.bar">';
      const results = await staticI18n.process(img, options);
      const $ = cheerio.load(results.en);
      expect(results).to.only.have.keys(['en']);
      expect($('img').attr('alt')).to.be('bar');
      expect($('img').attr('id')).to.be('ok');
    });

    it('should translate attributes with interpolation', async function() {
      options = _.merge({}, options, {locales: ['en', 'ja']});
      const input = '<input data-attr-t data-attr-t-interpolate href-t="{{links.baseAbsolute}}filename.{{links.extension}}">';
      const results = await staticI18n.process(input, options);
      let $ = cheerio.load(results.en);
      expect($('input').attr('href')).to.be('http://www.example.com/filename.html');
      expect($('input').attr('data-attr-t-interpolate')).to.be(undefined);
      $ = cheerio.load(results.ja);
      expect($('input').attr('href')).to.be('http://www.example.com/ja/filename.htm');
      expect($('input').attr('data-attr-t-interpolate')).to.be(undefined);
    });

    it('should remove interpolation related attributes', async function() {
      options = _.merge({}, options, {locales: ['en']});

      const input = '<p data-t data-t-interpolate>texto aqui<div>Whatever here</div></p>';
      const results = await staticI18n.process(input, options);
      const $ = cheerio.load(results.en);
      expect($('p').attr('data-t-interpolate')).to.be(undefined);
    });

{'fraction_non_alphanumeric': 0.21254863813229571, 'fraction_numerical': 0.004215304798962386, 'mean_word_length': 3.509502923976608, 'pattern_counts': {'":': 0, '<': 36, '<?xml version=': 0, '>': 36, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, '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': '3782812', 'n_tokens_mistral': 1961, 'n_tokens_neox': 1928, 'n_words': 360}