Skip to content

Add tests about missing getter property #197

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/jsonpath.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
const {hasOwnProperty: hasOwnProp} = Object.prototype;

/**
* @typedef {null|boolean|number|string|PlainObject|GenericArray} JSONObject
Expand All @@ -12,6 +11,17 @@ const {hasOwnProperty: hasOwnProp} = Object.prototype;
* @typedef {any} AnyResult
*/

/**
* Check if the provided property name exists on the object.
* @param {string} name Name of the property to check
* @returns {boolean} Whether the object has the property or not
*/
function hasOwnProp (name) {
return typeof this === 'object'
? name in this
: Object.prototype.hasOwnProperty.call(this, name);
}

/**
* Copies array and then pushes item into it.
* @param {GenericArray} arr Array to copy and into which to push
Expand Down
43 changes: 43 additions & 0 deletions test/test.class.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import {checkBuiltInVMAndNodeVM} from '../test-helpers/checkVM.js';

checkBuiltInVMAndNodeVM(function (vmType, setBuiltInState) {
describe(`JSONPath - Properties (${vmType})`, function () {
before(setBuiltInState);

/**
*
*/
class Test1 {
/**
* Test2.
*/
constructor () {
this.test2 = "test2";
}

/**
* Test3.
* @returns {string}
*/
// eslint-disable-next-line class-methods-use-this
get test3 () {
return "test3";
}
}
const json = new Test1();

it("Checking simple property", () => {
assert.equal(
jsonpath({json, path: "$.test2", wrap: false}),
"test2"
);
});

it("Checking getter property", () => {
assert.equal(
jsonpath({json, path: "$.test3", wrap: false}),
"test3"
);
});
});
});