lbl-srg / modelica-json

Modelica to JSON Parser
Other
22 stars 17 forks source link

Improve and fix testing of jsonquery #240

Open AntoineGautier opened 2 months ago

AntoineGautier commented 2 months ago

This ticket covers two issues.

First, there seems to be a bug in the function equalObjects defined in test/test_jsonquery.js and used in all unit tests.

const rawJson = {
    encapsulated: true,
    class_prefixes: 'partial',
    class_specifier: 'test class_specifier'
}
const referenceJsonOutput = {
    class_prefixes: 'partial',
    class_specifier: 'mocked class_specifier',
    encapsulated: true
}
equalObjects(rawJson, referenceJsonOutput)  // returns true

Whereas

assert.deepStrictEqual(rawJson, referenceJsonOutput)
// returns:
Stack trace:
AssertionError [ERR_ASSERTION]: Values are not equal:

    [Diff] Actual / Expected

    {
      class_prefixes: "partial",
-     class_specifier: "test class_specifier",
+     class_specifier: "mocked class_specifier",
      encapsulated: true,
    }

Why assert.deepStrictEqual() is not used instead of equalObjects?

Second, within the module lib/jsonquery.js, all local functions are invoked using the keyword this, i.e., assimilating the module as an object to resolve local references. This appears to be only required by the fact that the tests in test/test_jsonquery.js rely on sinon stubs which indeed do not support local function calls. But there is a workaround (using rewire) that can be implemented in the test script and avoids modifying the tested module.

const rewire = require('rewire')
const jq = rewire('../lib/jsonquery.js')

let unset

mo.afterEach(() => {
  sinon.restore()
  if (unset != null) { unset.call() }
})

mo.describe('jsonquery.js', function () {
  mo.describe('testing classDefinition', function () {
    mo.it('testing structure', function () {
      unset = jq.__set__('classSpecifier',
        sinon.stub(jq, 'classSpecifier').returns('mocked class_specifier')
      )
...

With the above code, the call to classSpecifier within classDefinition can be made directly, without this.classSpecifier. Similarly, modifying each unit test with the above pattern enables getting rid of this in all function calls within lib/jsonquery.js.

anandkp92 commented 2 months ago

const rawJson = { encapsulated: undefined, class_prefixes: 'partial', class_specifier: 'test class_specifier' } const referenceJsonOutput = { class_specifier: 'test class_specifier', encapsulated: true } equalObjects(rawJson, referenceJsonOutput) // returns false --> should return true

AntoineGautier commented 2 months ago

@anandkp92 Can you look into that?