class_docstr: Test for Magic
  The test counts the number of substitutions that magic.add_code do for
  a few code snippets. The test passes if the expected number of
  substitutions have been made.
class_docstr_tok: ["Test","for","Magic","The","test","counts","the","number","of","substitutions","that","magic.add_code","do","for","a","few","code","snippets",".","The","test","passes","if","the","expected","number","of","substitutions","have","been","made","."]
code: def _testCode(self, code: str, expected_count: int) -> None:
      tree = magic.add_magic(code, "./")
      count = 0
      for node in ast.walk(tree):
          # count the nodes where a substitution has been made, i.e.
          # look for 'calls' to a '__streamlitmagic__' function
          if type(
              node
          ) is ast.Call and magic.MAGIC_MODULE_NAME in ast.dump(
              node.func
          ):
              count += 1
      self.assertEqual(
          expected_count,
          count,
          f"There must be exactly {expected_count} {magic.MAGIC_MODULE_NAME} nodes, but found {count}",
      )
  
imports: import ast
  import unittest
  import streamlit.runtime.scriptrunner.magic
  from tests.testutil import patch_config_options
label_desc: Use image node_id 1 for calling the MagicTest obj's underlying member method code with example usage: obj._testCode(code, expected_count) without return types