CheckUtils { public static ClassDef getParentClassDef(Tree tree) { Tree current = tree.parent(); while (current != null) { if (current.is(Tree.Kind.CLASSDEF)) { return (ClassDef) current; } else if (current.is(Tree.Kind.FUNCDEF, Tree.Kind.LAMBDA)) { return null; } current = current.parent(); } return null; } private  CheckUtils(); static boolean areEquivalent(@Nullable Tree leftTree, @Nullable Tree rightTree); static ClassDef getParentClassDef(Tree tree); static boolean classHasInheritance(ClassDef classDef);  }
@Test public void no_parent_class() { FileInput file = (FileInput) parse("" + "def f():\n" + " pass\n"); FunctionDef f = descendantFunction(file, "f"); assertThat(f).isNotNull(); assertThat(CheckUtils.getParentClassDef(f)).isNull(); }
@Test public void parent_class() { FileInput file = (FileInput) parse("" + "class A:\n" + " def f():\n" + " def g():\n" + " pass\n" + " pass\n" + "\n" + " if x:\n" + " def h():\n" + " pass\n"); FunctionDef f = descendantFunction(file, "f"); FunctionDef g = descendantFunction(file, "g"); FunctionDef h = descendantFunction(file, "h"); assertThat(f).isNotNull(); assertThat(g).isNotNull(); assertThat(h).isNotNull(); ClassDef parent = CheckUtils.getParentClassDef(f); assertThat(parent).isNotNull(); assertThat(parent.name().name()).isEqualTo("A"); parent = CheckUtils.getParentClassDef(g); assertThat(parent).isNull(); parent = CheckUtils.getParentClassDef(h); assertThat(parent).isNotNull(); assertThat(parent.name().name()).isEqualTo("A"); }