Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve and fix testing of jsonquery #240

Open
AntoineGautier opened this issue Aug 12, 2024 · 2 comments
Open

Improve and fix testing of jsonquery #240

AntoineGautier opened this issue Aug 12, 2024 · 2 comments

Comments

@AntoineGautier
Copy link
Contributor

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
Copy link
Member

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
Copy link
Contributor Author

@anandkp92 Can you look into that?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants