HEX
Server: Apache/2.4.52 (Ubuntu)
System: Linux ip-172-31-4-197 6.8.0-1036-aws #38~22.04.1-Ubuntu SMP Fri Aug 22 15:44:33 UTC 2025 x86_64
User: ubuntu (1000)
PHP: 7.4.33
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Upload Files
File: /var/www/myc/api-management/node_modules/alce/test/ast.js
var ast = require('../lib/ast'),
    should = require('should');

describe('ast', function() {
  describe('#fromValue', function() {
    it('should handle existing AST instances', function() {
      var value = new ast.Array();
      ast.fromValue(value).should.equal(value);

      value = new ast.Object();
      ast.fromValue(value).should.equal(value);

      value = new ast.Value();
      ast.fromValue(value).should.equal(value);
    });
    it('should handle javascript objects', function() {
      var value = ast.fromValue({foo: 'bar'});
      value.should.be.instanceof(ast.Object);
      value.toString().should.equal('{"foo":"bar"}');

      value = ast.fromValue(['bar']);
      value.should.be.instanceof(ast.Array);
      value.toString().should.equal('["bar"]');
    });
    it('should handle primitive values', function() {
      function exec(expected, source) {
        var value = ast.fromValue(expected);
        value.should.be.instanceof(ast.Value);
        value.value.should.equal(expected);
        value.source.should.equal(source);
      }

      exec(true, 'true');
      exec(false, 'false');
      exec('true', '"true"');
      exec(0, '0');
      exec(1, '1');
    });
    it('should handle undefined values', function() {
        var value = ast.fromValue(undefined);
        value.should.be.instanceof(ast.Value);
        should.not.exist(value.value);
        value.source.should.equal('undefined');

        value = ast.fromValue(null);
        value.should.be.instanceof(ast.Value);
        should.not.exist(value.value);
        value.source.should.equal('null');
    });

    it('should maintain formatting for javascript objects', function() {
      var existing = new ast.Value();
      existing.preamble = '\n    ';
      existing.prologue = ' /* foo */';

      var newValue = ast.fromValue({foo: 'bar', baz: 'bat'}, existing);
      newValue.toString().should.equal('\n    {"foo":"bar","baz":"bat"} /* foo */');

      newValue = ast.fromValue(['foo', 'bar'], existing);
      newValue.toString().should.equal('\n    ["foo","bar"] /* foo */');
    });
    it('should maintain formatting of primitive values', function() {
      var existing = new ast.Value();
      existing.preamble = '\n    ';
      existing.prologue = ', /* foo */';

      var newValue = ast.fromValue('stringy', existing);
      newValue.toString().should.equal('\n    "stringy", /* foo */');
    });
  });
});