<commit_before>//-----------------------------------------------------------------------------

QUnit.module("basic");

//-----------------------------------------------------------------------------

test("standalone state machine", function() {

  var fsm = StateMachine.create({
    initial: 'green',
    events: [
      { name: 'warn',  from: 'green',  to: 'yellow' },
      { name: 'panic', from: 'yellow', to: 'red'    },
      { name: 'calm',  from: 'red',    to: 'yellow' },
      { name: 'clear', from: 'yellow', to: 'green'  }
  ]});

  equal(fsm.current, 'green', "initial state should be green");

  fsm.warn();  equal(fsm.current, 'yellow', "warn  event should transition from green  to yellow");
  fsm.panic(); equal(fsm.current, 'red',    "panic event should transition from yellow to red");
  fsm.calm();  equal(fsm.current, 'yellow', "calm  event should transition from red    to yellow");
  fsm.clear(); equal(fsm.current, 'green',  "clear event should transition from yellow to green");

});

//-----------------------------------------------------------------------------

test("targeted state machine", function() {

  StateMachine.create({
    target:  this,
    initial: 'green',
    events: [
      { name: 'warn',  from: 'green',  to: 'yellow' },
      { name: 'panic', from: 'yellow', to: 'red'    },
      { name: 'calm',  from: 'red',    to: 'yellow' },
      { name: 'clear', from: 'yellow', to: 'green'  }
  ]});

  equal(this.current, 'green', "initial state should be green");

  this.warn();  equal(this.current, 'yellow', "warn  event should transition from green  to yellow");
  this.panic(); equal(this.current, 'red',    "panic event should transition from yellow to red");
  this.calm();  equal(this.current, 'yellow', "calm  event should transition from red    to yellow");
  this.clear(); equal(this.current, 'green',  "clear event should transition from yellow to green");
});

//-----------------------------------------------------------------------------

test("can & cannot", function() {

  var fsm = StateMachine.create({
    initial: 'green',
    events: [
      { name: 'warn',  from: 'green',  to: 'yellow' },
      { name: 'panic', from: 'yellow', to: 'red'    },
      { name: 'calm',  from: 'red',    to: 'yellow' },
  ]});

  equal(fsm.current, 'green', "initial state should be green");

  ok(fsm.can('warn'),     "should be able to warn from green state")
  ok(fsm.cannot('panic'), "should NOT be able to panic from green state")
  ok(fsm.cannot('calm'),  "should NOT be able to calm from green state")

  fsm.warn();
  equal(fsm.current, 'yellow', "current state should be yellow");
  ok(fsm.cannot('warn'),  "should NOT be able to warn from yellow state")
  ok(fsm.can('panic'),    "should be able to panic from yellow state")
  ok(fsm.cannot('calm'),  "should NOT be able to calm from yellow state")

  fsm.panic();
  equal(fsm.current, 'red',  "current state should be red");
  ok(fsm.cannot('warn'),  "should NOT be able to warn from red state")
  ok(fsm.cannot('panic'), "should NOT be able to panic from red state")
  ok(fsm.can('calm'),     "should be able to calm from red state")

  equal(fsm.can('jibber'),    false, "unknown event should not crash")
  equal(fsm.cannot('jabber'), true,  "unknown event should not crash")

});

//-----------------------------------------------------------------------------

test("is", function() {

  var fsm = StateMachine.create({
    initial: 'green',
    events: [
      { name: 'warn',  from: 'green',  to: 'yellow' },
      { name: 'panic', from: 'yellow', to: 'red'    },
      { name: 'calm',  from: 'red',    to: 'yellow' },
      { name: 'clear', from: 'yellow', to: 'green'  }
  ]});

  equal(fsm.current, 'green', "initial state should be green");

  equal(fsm.is('green'),           true,   'current state should match');
  equal(fsm.is('yellow'),          false,  'current state should NOT match');
  equal(fsm.is(['green',  'red']), true,   'current state should match when included in array');
  equal(fsm.is(['yellow', 'red']), false,  'current state should NOT match when not included in array');

  fsm.warn();

  equal(fsm.current, 'yellow', "current state should be yellow");

  equal(fsm.is('green'),           false, 'current state should NOT match');
  equal(fsm.is('yellow'),          true,  'current state should match');
  equal(fsm.is(['green',  'red']), false, 'current state should NOT match when not included in array');
  equal(fsm.is(['yellow', 'red']), true,  'current state should match when included in array');

});

//-----------------------------------------------------------------------------

test("transitions", function() {

  var fsm = StateMachine.create({
    initial: 'green',
    events: [
      { name: 'warn',  from: 'green',  to: 'yellow' },
      { name: 'panic', from: 'yellow', to: 'red'    },
      { name: 'calm',  from: 'red',    to: 'yellow' },
      { name: 'clear', from: 'yellow', to: 'green'  }
  ]});

  equal(fsm.current, 'green', 'current state should be yellow');
  deepEqual(fsm.transitions(), ['warn'], 'current transition(s) should be yellow');

  fsm.warn();
  equal(fsm.current, 'yellow', 'current state should be yellow');
  deepEqual(fsm.transitions(), ['panic', 'clear'], 'current transition(s) should be panic and clear');

  fsm.panic();
  equal(fsm.current, 'red', 'current state should be red');
  deepEqual(fsm.transitions(), ['calm'], 'current transition(s) should be calm');

  fsm.calm();
  equal(fsm.current, 'yellow', 'current state should be yellow');
  deepEqual(fsm.transitions(), ['panic', 'clear'], 'current transion(s) should be panic and clear');

  fsm.clear();
  equal(fsm.current, 'green', 'current state should be green');
  deepEqual(fsm.transitions(), ['warn'], 'current transion(s) should be warn');
});

//-----------------------------------------------------------------------------

test("transitions with multiple from states", function() {

  var fsm = StateMachine.create({
    events: [
      { name: 'start', from: 'none',              to: 'green'  },
      { name: 'warn',  from: ['green', 'red'],    to: 'yellow' },
      { name: 'panic', from: ['green', 'yellow'], to: 'red'    },
      { name: 'clear', from: ['red', 'yellow'],   to: 'green'  }
    ]
  });

  equal(fsm.current, 'none', 'current state should be none');
  deepEqual(fsm.transitions(), ['start'], 'current transition(s) should be start');
<commit_msg>added unit test to ensure issue #90 is fixed<commit_after>//-----------------------------------------------------------------------------

QUnit.module("basic");

//-----------------------------------------------------------------------------

test("standalone state machine", function() {

  var fsm = StateMachine.create({
    initial: 'green',
    events: [
      { name: 'warn',  from: 'green',  to: 'yellow' },
      { name: 'panic', from: 'yellow', to: 'red'    },
      { name: 'calm',  from: 'red',    to: 'yellow' },
      { name: 'clear', from: 'yellow', to: 'green'  }
  ]});

  equal(fsm.current, 'green', "initial state should be green");

  fsm.warn();  equal(fsm.current, 'yellow', "warn  event should transition from green  to yellow");
  fsm.panic(); equal(fsm.current, 'red',    "panic event should transition from yellow to red");
  fsm.calm();  equal(fsm.current, 'yellow', "calm  event should transition from red    to yellow");
  fsm.clear(); equal(fsm.current, 'green',  "clear event should transition from yellow to green");

});

//-----------------------------------------------------------------------------

test("targeted state machine", function() {

  StateMachine.create({
    target:  this,
    initial: 'green',
    events: [
      { name: 'warn',  from: 'green',  to: 'yellow' },
      { name: 'panic', from: 'yellow', to: 'red'    },
      { name: 'calm',  from: 'red',    to: 'yellow' },
      { name: 'clear', from: 'yellow', to: 'green'  }
  ]});

  equal(this.current, 'green', "initial state should be green");

  this.warn();  equal(this.current, 'yellow', "warn  event should transition from green  to yellow");
  this.panic(); equal(this.current, 'red',    "panic event should transition from yellow to red");
  this.calm();  equal(this.current, 'yellow', "calm  event should transition from red    to yellow");
  this.clear(); equal(this.current, 'green',  "clear event should transition from yellow to green");
});

//-----------------------------------------------------------------------------

test("can & cannot", function() {

  var fsm = StateMachine.create({
    initial: 'green',
    events: [
      { name: 'warn',  from: 'green',  to: 'yellow' },
      { name: 'panic', from: 'yellow', to: 'red'    },
      { name: 'calm',  from: 'red',    to: 'yellow' },
  ]});

  equal(fsm.current, 'green', "initial state should be green");

  ok(fsm.can('warn'),     "should be able to warn from green state")
  ok(fsm.cannot('panic'), "should NOT be able to panic from green state")
  ok(fsm.cannot('calm'),  "should NOT be able to calm from green state")

  fsm.warn();
  equal(fsm.current, 'yellow', "current state should be yellow");
  ok(fsm.cannot('warn'),  "should NOT be able to warn from yellow state")
  ok(fsm.can('panic'),    "should be able to panic from yellow state")
  ok(fsm.cannot('calm'),  "should NOT be able to calm from yellow state")

  fsm.panic();
  equal(fsm.current, 'red',  "current state should be red");
  ok(fsm.cannot('warn'),  "should NOT be able to warn from red state")
  ok(fsm.cannot('panic'), "should NOT be able to panic from red state")
  ok(fsm.can('calm'),     "should be able to calm from red state")

  equal(fsm.can('jibber'),    false, "unknown event should not crash")
  equal(fsm.cannot('jabber'), true,  "unknown event should not crash")

});

//-----------------------------------------------------------------------------

test("is", function() {

  var fsm = StateMachine.create({
    initial: 'green',
    events: [
      { name: 'warn',  from: 'green',  to: 'yellow' },
      { name: 'panic', from: 'yellow', to: 'red'    },
      { name: 'calm',  from: 'red',    to: 'yellow' },
      { name: 'clear', from: 'yellow', to: 'green'  }
  ]});

  equal(fsm.current, 'green', "initial state should be green");

  equal(fsm.is('green'),           true,   'current state should match');
  equal(fsm.is('yellow'),          false,  'current state should NOT match');
  equal(fsm.is(['green',  'red']), true,   'current state should match when included in array');
  equal(fsm.is(['yellow', 'red']), false,  'current state should NOT match when not included in array');

  fsm.warn();

  equal(fsm.current, 'yellow', "current state should be yellow");

  equal(fsm.is('green'),           false, 'current state should NOT match');
  equal(fsm.is('yellow'),          true,  'current state should match');
  equal(fsm.is(['green',  'red']), false, 'current state should NOT match when not included in array');
  equal(fsm.is(['yellow', 'red']), true,  'current state should match when included in array');

});

//-----------------------------------------------------------------------------

test("transitions", function() {

  var fsm = StateMachine.create({
    initial: 'green',
    events: [
      { name: 'warn',   from: 'green',  to: 'yellow' },
      { name: 'panic',  from: 'yellow', to: 'red'    },
      { name: 'calm',   from: 'red',    to: 'yellow' },
      { name: 'clear',  from: 'yellow', to: 'green'  },
      { name: 'finish', from: 'green',  to: 'done'   },
  ]});

  equal(fsm.current, 'green', 'current state should be yellow');
  deepEqual(fsm.transitions(), ['warn', 'finish'], 'current transition(s) should be yellow');

  fsm.warn();
  equal(fsm.current, 'yellow', 'current state should be yellow');
  deepEqual(fsm.transitions(), ['panic', 'clear'], 'current transition(s) should be panic and clear');

  fsm.panic();
  equal(fsm.current, 'red', 'current state should be red');
  deepEqual(fsm.transitions(), ['calm'], 'current transition(s) should be calm');

  fsm.calm();
  equal(fsm.current, 'yellow', 'current state should be yellow');
  deepEqual(fsm.transitions(), ['panic', 'clear'], 'current transion(s) should be panic and clear');

  fsm.clear();
  equal(fsm.current, 'green', 'current state should be green');
  deepEqual(fsm.transitions(), ['warn', 'finish'], 'current transion(s) should be warn');

  fsm.finish();
  equal(fsm.current, 'done', 'current state should be done');
  deepEqual(fsm.transitions(), [], 'current transition(s) should be empty');

});

//-----------------------------------------------------------------------------

test("transitions with multiple from states", function() {

  var fsm = StateMachine.create({
    events: [
      { name: 'start', from: 'none',              to: 'green'  },
      { name: 'warn',  from: ['green', 'red'],    to: 'yellow' },
      { name: 'panic', from: ['green', 'yellow'], to: 'red'    },
      { name: 'clear', from: ['red', 'yellow'],   to: 'green'  }
    ]
  });

  equal(fsm.current, 'none', 'current state should be none');
  deepEqual(fsm.transitions(), ['start'], 'current transition(s) should be start');

{'fraction_non_alphanumeric': 0.2526091262283843, 'fraction_numerical': 0.00015235773596404358, 'mean_word_length': 2.5158007498660955, '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': '1636520', 'n_tokens_mistral': 4038, 'n_tokens_neox': 3897, 'n_words': 1246}