All files / Sources/Testing testSerialization.js

98.27% Statements 57/58
86.66% Branches 13/15
100% Functions 9/9
98.27% Lines 57/58

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186                                        1x   1x                 1x                                                               24x       12x             1x   6x 6x 6x   6x 6x 6x 6x   6x 6x   6x 6x 6x   6x       6x 6x     6x 6x   6x           6x 6x 6x 6x   6x 6x 6x   6x     6x 6x 6x 6x 6x 6x 6x         80x 647x 317x   330x 251x 251x     79x         1x 1x                     1x                 1x 1x   1x 232x       225x 225x       225x     1x    
import test from 'tape';
import vtk from 'vtk.js/Sources/vtk';
import macro from 'vtk.js/Sources/macros';
 
import vtkDataArray from 'vtk.js/Sources/Common/Core/DataArray';
import vtkImageData from 'vtk.js/Sources/Common/DataModel/ImageData';
import vtkLookupTable from 'vtk.js/Sources/Common/Core/LookupTable';
import vtkPoints from 'vtk.js/Sources/Common/Core/Points';
import vtkPolyData from 'vtk.js/Sources/Common/DataModel/PolyData';
import vtkScalarsToColors from 'vtk.js/Sources/Common/Core/ScalarsToColors';
 
import Common from 'vtk.js/Sources/Common';
import Filters from 'vtk.js/Sources/Filters';
import Imaging from 'vtk.js/Sources/Imaging';
import Interaction from 'vtk.js/Sources/Interaction';
import IO from 'vtk.js/Sources/IO';
import Rendering from 'vtk.js/Sources/Rendering';
import VTKProxy from 'vtk.js/Sources/Proxy';
import Widgets from 'vtk.js/Sources/Widgets';
 
const { vtkDebugMacro } = macro;
 
const classToTest = [
  'vtkDataArray',
  'vtkImageData',
  'vtkLookupTable',
  'vtkPoints',
  'vtkPolyData',
  'vtkScalarsToColors',
];
 
const SERIALIZABLE_CLASSES = {
  vtkDataArray: {
    class: vtkDataArray,
    data: { values: [1.1, 2.2, 3.3, 4.4, 5.5] },
  },
  vtkPoints: {
    class: vtkPoints,
    data: {
      values: [0, 0, 0, 1, 0, 0, 0, 1, 0],
    },
  },
  vtkLookupTable,
  vtkScalarsToColors,
  vtkPolyData: {
    class: vtkPolyData,
    data: {
      points: {
        vtkClass: 'vtkPoints',
        values: [0, 0, 0, 1, 1, 1, 2, 2, 2],
        numberOfComponents: 3,
      },
      polys: {
        vtkClass: 'vtkDataArray',
        values: [1, 0, 1, 2],
        dataType: 'Uint32Array',
      },
    },
  },
  vtkImageData,
};
 
function ignoreMTime(json) {
  return JSON.stringify(json).replace(/"mtime":[0-9]+/g, '"mtime":0');
}
 
function assertSameSerializedContent(t, state, state2) {
  t.deepEqual(
    ignoreMTime(state),
    ignoreMTime(state2),
    'But same serialized content'
  );
}
 
classToTest.forEach((testName) => {
  const klass =
    SERIALIZABLE_CLASSES[testName].class || SERIALIZABLE_CLASSES[testName];
  const initData = SERIALIZABLE_CLASSES[testName].data;
  const debug = SERIALIZABLE_CLASSES[testName].debug;
 
  test(`Test ${testName} serialization/deserialization`, (t) => {
    t.ok(klass, 'Make sure the class definition exist');
    const instance = klass.newInstance(initData);
    t.ok(instance, 'Make sure the instance exist');
 
    const state = instance.getState();
    t.ok(instance, 'Make sure we can get serialize data');
 
    const instance2 = vtk(state);
    t.ok(instance, 'Make sure we can get deserialize data');
    const state2 = instance2.getState();
 
    Iif (debug) {
      vtkDebugMacro(state);
    }
 
    t.notEqual(instance, instance2, 'We have two different instances');
    assertSameSerializedContent(t, state, state2);
 
    // Test JSON.stringify and JSON.parse behavior
    const jsonFromStringify = JSON.stringify(instance);
    const jsonFromGetState = JSON.stringify(state);
 
    t.equal(
      jsonFromStringify,
      jsonFromGetState,
      'We have the same json string.'
    );
 
    const state3 = JSON.parse(jsonFromStringify);
    const instance3 = vtk(state3);
    const state4 = JSON.parse(jsonFromGetState);
    const instance4 = vtk(state4);
 
    t.ok(instance3, 'Make sure we get a valid instance');
    t.notEqual(instance3, instance4, 'We have two different instances');
    assertSameSerializedContent(t, state3, state4);
 
    t.end();
  });
 
  test(`Test ${testName} serialization on deleted object`, (t) => {
    t.ok(klass, 'Make sure the class definition exist');
    const instance = klass.newInstance(initData);
    t.ok(instance, 'Make sure the instance exist');
    instance.delete();
    t.equal(JSON.stringify(instance), 'null');
    t.end();
  });
});
 
function findVTKObjects(objects) {
  return Object.entries(objects).reduce((res, [className, object]) => {
    if (typeof object !== 'object' || object == null) {
      return res;
    }
    if (object.newInstance) {
      res[className] = object;
      return res;
    }
 
    return Object.assign(res, findVTKObjects(object));
  }, {});
}
 
// If this test is hanging, it is due to a problem in `getState()`.
test(`Test serialization of`, (t) => {
  const VTK = {
    Common,
    Filters,
    Imaging,
    Interaction,
    IO,
    Rendering,
    VTKProxy,
    Widgets,
  };
  // List of classes that can't be instantiated with default arguments.
  const classesToIgnore = [
    'vtkDataArray',
    'vtkStringArray',
    'vtkVariantArray',
    'vtkITKImageReader',
    'vtkITKPolyDataReader',
    'vtkFullScreenRenderWindow',
    'vtkAbstractWidget',
  ];
  const onlyClasses = [];
  const allObjects = findVTKObjects(VTK);
 
  Object.entries(allObjects).forEach(([className, factory]) => {
    if (
      !classesToIgnore.includes(className) &&
      (onlyClasses.length === 0 || onlyClasses.includes(className))
    ) {
      t.comment(`${className}.newInstance()`);
      const instance = factory.newInstance();
      // If an error occurs here, you may have a cyclical dependency in the JSON
      // returned by getState(). This is typically fixed by making some model
      // variables protected (prefixed with _).
      JSON.stringify(instance);
    }
  });
  t.end();
});