<commit_before>'use strict'
const test = require('tape')
const subsTest = require('../util')
const field = require('../util/field')

module.exports = function (type) {
  test(type + ' - $switch', function (t) {
    const subscription = field({
      nest: {
        field: {
          $switch: {
            exec  (state, type, stamp, subs, tree, sType) {
              if (state.key === 'a') {
                return 'optionA'
              } else if (state.key === 'b') {
                return 'optionB'
              } else if (state.key === 'c') {
                return 'optionC'
              }
            },
            optionA: { $parent: { bla: { val: true } } },
            optionB: { $parent: { $parent: { etc: { val: true } } } },
            optionC: { val: true } // should fire wtf...
          }
        }
      }
    }, type, '$parent')

    const s = subsTest(
      t,
      {
        etc: true,
        nest: {
          bla: { val: true },
          a: {},
          b: {},
          c: 'hello',
          field: '$root.nest.a'
        }
      },
      subscription
    )

    const r = s('initial subscription', [ { path: 'nest/bla', type: 'new' } ])
    t.same(r.tree.nest.field.$switch.$c, { [type]: type }, 'got correct $c')
    s(
      'change to c (remove parent subs)',
      [ { path: 'nest/c', type: 'new' } ],
      { nest: { field: '$root.nest.c' } }
    )
    t.equal(r.tree.nest.field.$switch.$c, void 0, 'removed field.$switch.$c')
    s(
      'change to b (double parent)',
      [ { path: 'etc', type: 'new' } ],
      { nest: { field: '$root.nest.b' } }
    )
    s(
      'change to c (remove parent subs)',
      [ { path: 'nest/c', type: 'new' } ],
      { nest: { field: '$root.nest.c' } }
    )
    t.equal(r.tree.nest.field.$switch.$c, void 0, 'removed field.$switch.$c')
    t.equal(r.tree.nest.field.$c, void 0, 'removed field.$c')
    t.end()
  })
}<commit_msg>added tests for branching switch + $parent closes #123<commit_after>'use strict'
const test = require('tape')
const subsTest = require('../util')
const field = require('../util/field')

module.exports = function (type) {
  test(type + ' - $switch', function (t) {
    const subscription = field({
      nest: {
        field: {
          $switchX: {
            exec  (state, type, stamp, subs, tree, sType) {
              if (state.key === 'a') {
                return 'optionA'
              } else if (state.key === 'b') {
                return 'optionB'
              } else if (state.key === 'c') {
                return 'optionC'
              }
            },
            optionA: { $parent: { bla: { val: true } } },
            optionB: { $parent: { $parent: { etc: { val: true } } } },
            optionC: { val: true } // should fire wtf...
          }
        }
      }
    }, type, '$parent')

    const s = subsTest(
      t,
      {
        etc: true,
        nest: {
          bla: { val: true },
          a: {},
          b: {},
          c: 'hello',
          field: '$root.nest.a'
        }
      },
      subscription
    )

    const r = s('initial subscription', [ { path: 'nest/bla', type: 'new' } ])
    t.same(r.tree.nest.field.$switchX.$c, { [type]: type }, 'got correct $c')
    s(
      'change to c (remove parent subs)',
      [ { path: 'nest/c', type: 'new' } ],
      { nest: { field: '$root.nest.c' } }
    )
    t.equal(r.tree.nest.field.$switchX.$c, void 0, 'removed field.$switch.$c')
    s(
      'change to b (double parent)',
      [ { path: 'etc', type: 'new' } ],
      { nest: { field: '$root.nest.b' } }
    )
    s(
      'change to c (remove parent subs)',
      [ { path: 'nest/c', type: 'new' } ],
      { nest: { field: '$root.nest.c' } }
    )
    t.equal(r.tree.nest.field.$switchX.$c, void 0, 'removed field.$switch.$c')
    t.equal(r.tree.nest.field.$c, void 0, 'removed field.$c')
    t.end()
  })
}
{'fraction_non_alphanumeric': 0.189873417721519, 'fraction_numerical': 0.002324980625161457, 'mean_word_length': 1.6074074074074074, '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': '6574235', 'n_tokens_mistral': 1333, 'n_tokens_neox': 1293, 'n_words': 317}