<commit_before>'use strict'

const toHTML = require('vdom-to-html')
const parse5 = require('parse5-utils')
const assert = require('assert')
const path = require('path')
const fs = require('fs')

const render = require('..')

function renderFixture(fixtureName, locals) {
  const fn = eval(`(${render(fixture(fixtureName))})`)
  const root = fn.call({class: 'asdf'}, locals)
  const html = toHTML(root)
  parse5.parse(html, true)
  return html
}

describe('Render', function () {
  it('should render a template', function () {
    const html = renderFixture('item', {
      item: {
        id: '1234',
        title: 'some title',
        description: 'some description',
        active: true,
      }
    })

    assert(~html.indexOf('item active'))
    assert(~html.indexOf('class="title"'))
    assert(~html.indexOf('<h3'))
    assert(~html.indexOf('</h3>'))
    assert(~html.indexOf('some title'))
    assert(~html.indexOf('some description'))
  })

  it('should beautify when option is set', function () {
    const fn = render(fixture('item'), {pretty: true})
    const lines = fn.split('\n')
    assert(lines.length > 15)
    assert(lines[lines.length - 1].trim() === '}')
  })

  it('should run code blocks correctly', function () {
    const html = renderFixture('code')
    assert(~html.indexOf('<div class="example bar">'))
  })

  it('should use locals when evaluating code blocks', function () {
    let html = renderFixture('code', {x: 0})
    assert(~html.indexOf('<div class="baz">'))
    html = renderFixture('code', {x: -1})
    assert(!~html.indexOf('<div class="baz">'))
  })

  it('should run while loops correctly', function () {
    const html = renderFixture('while')
    assert(html.match(/<div class=\"item\">/g).length === 5)
  })
})

function fixture(name) {
  return fs.readFileSync(path.resolve(__dirname, 'fixtures/' + name + '.jade'), 'utf8')<commit_msg>integration tests for rendering case statements<commit_after>'use strict'

const toHTML = require('vdom-to-html')
const parse5 = require('parse5-utils')
const assert = require('assert')
const path = require('path')
const fs = require('fs')

const render = require('..')

function renderFixture(fixtureName, locals) {
  const fn = eval(`(${render(fixture(fixtureName))})`)
  const root = fn.call({class: 'asdf'}, locals)
  const html = toHTML(root)
  parse5.parse(html, true)
  return html
}

describe('Render', function () {
  it('should render a template', function () {
    const html = renderFixture('item', {
      item: {
        id: '1234',
        title: 'some title',
        description: 'some description',
        active: true,
      }
    })

    assert(~html.indexOf('item active'))
    assert(~html.indexOf('class="title"'))
    assert(~html.indexOf('<h3'))
    assert(~html.indexOf('</h3>'))
    assert(~html.indexOf('some title'))
    assert(~html.indexOf('some description'))
  })

  it('should beautify when option is set', function () {
    const fn = render(fixture('item'), {pretty: true})
    const lines = fn.split('\n')
    assert(lines.length > 15)
    assert(lines[lines.length - 1].trim() === '}')
  })

  it('should run code blocks correctly', function () {
    const html = renderFixture('code')
    assert(~html.indexOf('<div class="example bar">'))
  })

  it('should use locals when evaluating code blocks', function () {
    let html = renderFixture('code', {x: 0})
    assert(~html.indexOf('<div class="baz">'))
    html = renderFixture('code', {x: -1})
    assert(!~html.indexOf('<div class="baz">'))
  })

  it('should run while loops correctly', function () {
    const html = renderFixture('while')
    assert(html.match(/<div class=\"item\">/g).length === 5)
  })

  describe('case statements', function () {
    it('should not execute any cases when none match', function () {
      const html = renderFixture('case')
      assert(!html.includes('foo'))
      assert(!html.includes('bar'))
    })

    it('should execute default when no others match', function () {
      const html = renderFixture('case')
      assert(html.includes('llama'))
    })

    it('should execute only one match', function () {
      const html = renderFixture('case', {variable: 1})
      assert(html.includes('span'))
      assert(!html.includes('input'))
      assert(!html.includes('textarea'))
    })

    it('should fall through from the first case in a chain', function () {
      const html = renderFixture('case', {variable2: 'd'})
      assert(html.includes('bar'))
      assert(!html.includes('foo'))
    })

    it('should fall through from the middle case in a chain', function () {
      const html = renderFixture('case', {variable2: 'e'})
      assert(html.includes('bar'))
      assert(!html.includes('foo'))
    })

    it('should fall through from the last case in a chain', function () {
      const html = renderFixture('case', {variable2: 'f'})
      assert(html.includes('bar'))
      assert(!html.includes('foo'))
    })
  })
})

function fixture(name) {
  return fs.readFileSync(path.resolve(__dirname, 'fixtures/' + name + '.jade'), 'utf8')
{'fraction_non_alphanumeric': 0.18070625369895443, 'fraction_numerical': 0.007101992503452358, 'mean_word_length': 3.6857670979667283, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, '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': '51139', 'n_tokens_mistral': 1568, 'n_tokens_neox': 1526, 'n_words': 381}