"Fossies" - the Fresh Open Source Software Archive

Member "Atom/resources/app/apm/node_modules/array-index/test.js" (8 Mar 2017, 2110 Bytes) of archive /windows/misc/atom-windows.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Javascript source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 var ArrayIndex = require('./')
    2 var inherits = require('util').inherits
    3 var assert = require('assert')
    4 
    5 
    6 /**
    7  * Create a "subclass".
    8  */
    9 
   10 function Arrayish (length) {
   11   ArrayIndex.call(this, length)
   12   this.sets = Object.create(null)
   13 }
   14 
   15 // inherit from `ArrayIndex`
   16 inherits(Arrayish, ArrayIndex)
   17 
   18 
   19 // create an instance and run some tests
   20 var a = new Arrayish(11)
   21 assert.equal(a.length, 11);
   22 
   23 assert.throws(function () {
   24   a[0]
   25 }, /you must implement the `ArrayIndex.get` Symbol/)
   26 
   27 assert.throws(function () {
   28   a[0] = 0
   29 }, /you must implement the `ArrayIndex.set` Symbol/)
   30 
   31 
   32 /**
   33  * This "getter" function checks if the index has previosly been "set", and if so
   34  * returns the index * the value previously set. If the index hasn't been set,
   35  * return the index as-is.
   36  */
   37 
   38 Arrayish.prototype[ArrayIndex.get] = function get (index) {
   39   if (index in this.sets) {
   40     return +this.sets[index] * index
   41   } else {
   42     return index
   43   }
   44 }
   45 
   46 /**
   47  * Store the last value set for this index.
   48  */
   49 
   50 Arrayish.prototype[ArrayIndex.set] = function set (index, value) {
   51   this.sets[index] = value
   52 }
   53 
   54 
   55 // test getters without being "set"
   56 assert.equal(0, a[0])
   57 assert.equal(1, a[1])
   58 assert.equal(2, a[2])
   59 assert.equal(3, a[3])
   60 assert.equal(4, a[4])
   61 
   62 // test setters, followed by getters
   63 a[10] = 1
   64 assert.equal(10, a[10])
   65 a[10] = 2
   66 assert.equal(20, a[10])
   67 a[10] = 3
   68 assert.equal(30, a[10])
   69 
   70 // test "length"
   71 assert.equal(11, a.length)
   72 
   73 a[4] = 20
   74 a[6] = 5.55432
   75 var b = [0, 1, 2, 3, 80, 5, 33.325919999999996, 7, 8, 9, 30]
   76 assert.equal(JSON.stringify(b), JSON.stringify(a))
   77 
   78 
   79 /**
   80  * It should work when invoking as a Mixin.
   81  */
   82 
   83 function Foo () {
   84   ArrayIndex.call(this, 5);
   85 }
   86 var f = new Foo();
   87 
   88 // these throw because there's no __get__ and __set__ function defined
   89 assert.throws(function () {
   90   f[0];
   91 });
   92 assert.throws(function () {
   93   f[0] = 0
   94 });
   95 
   96 f[ArrayIndex.get] = function (index) {
   97   return index * 2;
   98 };
   99 
  100 assert.equal(f[0], 0);
  101 assert.equal(f[1], 2);
  102 assert.equal(f[2], 4);
  103 assert.equal(f[3], 6);
  104 
  105 f[ArrayIndex.set] = function (index, value) {
  106   this['foo' + index] = value;
  107 };
  108 
  109 f[1] = 'bar';
  110 assert.equal(f.foo1, 'bar');