From d19b55e2480f65753fe4fbfb925aa9840514cb7e Mon Sep 17 00:00:00 2001 From: Dario-DC <105294544+Dario-DC@users.noreply.github.com> Date: Thu, 25 Apr 2024 11:47:18 +0200 Subject: [PATCH] docs: add reference to ast-based helpers (#98) Co-authored-by: Oliver Eyton-Williams --- docs/curriculum-help.md | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/docs/curriculum-help.md b/docs/curriculum-help.md index efb27401..f07b001b 100644 --- a/docs/curriculum-help.md +++ b/docs/curriculum-help.md @@ -159,3 +159,32 @@ Using `__helpers.removeCssComments()`, `__helpers.removeHTMLComments()`, or `__h ### Removing Whitespace Using `__helpers.removeWhitespace()` allows you to pass the camper's code (through the `code` variable) to remove all whitespace. + +## AST-based Helpers + +These helpers need to be run in Python and tests that use them need to be in the format: + +```js +({test: () => assert(runPython(``))}) +``` + +`_Node` is a chainable class that allows you to call methods on the result of parsing a string. To create an instance of `_Node` that parses the camper's code use `_Node(_code)`. + +To access a specific statement within the code you need to chain method calls until you reach the desired scope. + +For example, if the camper has written the following code: + +```py +class Spam: + def __str__(self): + return 'spam!' +``` +To check the return value of `__str__` you would write: + +```js +({test: () => assert(runPython(`_Node(_code).find_class("Spam").find_function("__str__").has_return("'spam!'")`))}) +``` + +### Methods + +The exstensive list of methods for parsing and testing Python code is available [here](https://opensource.freecodecamp.org/curriculum-helpers/python.html#ast-based-helpers) \ No newline at end of file