Add hash-based ISA lookup to RV of describe_class_methods
[dbsrgits/DBIx-Class.git] / xt / extra / internals / attributes.t
index 4e36e72..5169d23 100644 (file)
@@ -35,6 +35,7 @@ my $pkg_gen_history = {};
 
 { package UEBERVERSAL; sub ueber {} }
 @UNIVERSAL::ISA = "UEBERVERSAL";
+sub UNIVERSAL::uni { "unistuff" }
 
 sub grab_pkg_gen ($) {
   push @{ $pkg_gen_history->{$_[0]} }, [
@@ -285,13 +286,18 @@ sub add_more_attrs {
 
   # check that describe_class_methods returns the right stuff
   # ( on the simpler class )
-  my $expected_AttrTest_ISA = [qw(
+  my $expected_AttrTest_linear_ISA = [qw(
     DBICTest::SomeParentClass
     DBICTest::SomeGrandParentClass
     DBICTest::AnotherParentClass
     DBIx::Class::MethodAttributes
   )];
 
+  my $expected_AttrTest_full_ISA = { map { $_ => 1 } (
+    qw( UEBERVERSAL UNIVERSAL DBICTest::AttrTest ),
+    @$expected_AttrTest_linear_ISA,
+  )};
+
   my $expected_desc = {
     class => "DBICTest::AttrTest",
 
@@ -301,12 +307,8 @@ sub add_more_attrs {
       require Math::BigInt;
       my $gen = Math::BigInt->new(0);
 
-      $gen += DBIx::Class::_Util::get_real_pkg_gen($_) for (
-        'UEBERVERSAL',
-        'UNIVERSAL',
-        'DBICTest::AttrTest',
-        @$expected_AttrTest_ISA,
-      );
+      $gen += DBIx::Class::_Util::get_real_pkg_gen($_)
+        for keys %$expected_AttrTest_full_ISA;
 
       $gen;
     },
@@ -314,7 +316,8 @@ sub add_more_attrs {
       type => 'c3',
       is_c3 => 1,
     },
-    isa => $expected_AttrTest_ISA,
+    linear_isa => $expected_AttrTest_linear_ISA,
+    isa => $expected_AttrTest_full_ISA,
     methods => {
       FETCH_CODE_ATTRIBUTES => [
         {
@@ -379,6 +382,13 @@ sub add_more_attrs {
           via_class => "UEBERVERSAL",
         }
       ],
+      uni => [
+        {
+          attributes => {},
+          name => "uni",
+          via_class => "UNIVERSAL",
+        }
+      ],
       can => [
         {
           attributes => {},
@@ -424,6 +434,83 @@ sub add_more_attrs {
     $expected_desc,
     'describe_class_methods returns correct data',
   );
+
+  # ensure that asking with a different MRO will not perturb the cache
+  my $cached_desc = serialize(
+    $DBIx::Class::_Util::describe_class_query_cache->{"DBICTest::AttrTest|c3"}
+  );
+
+  # now try to ask for DFS explicitly, adjust our expectations
+  $expected_desc->{mro} = { type => 'dfs', is_c3 => 0 };
+
+  # due to DFS the last 2 entries of ISA and the VALID_DBIC_CODE_ATTRIBUTE
+  # sourcing-list will change places
+  splice @$_, -2, 2, @{$_}[-1, -2]
+    for $V_D_C_A_stack, $expected_AttrTest_linear_ISA;
+
+  is_deeply (
+    # work around taint, see TODO below
+    {
+      %{ describe_class_methods({ class => "DBICTest::AttrTest", use_mro => "dfs" }) },
+      cumulative_gen => $expected_desc->{cumulative_gen},
+    },
+    $expected_desc,
+    'describing with explicit mro returns correct data'
+  );
+
+  # FIXME: TODO does not work on new T::B under threads sigh
+  # https://github.com/Test-More/test-more/issues/683
+  unless(
+    ! DBIx::Class::_ENV_::OLD_MRO
+      and
+    ${^TAINT}
+  ) {
+    #local $TODO = "On 5.10+ -T combined with stash peeking invalidates the pkg_gen (wtf)" if ...
+
+    ok(
+      (
+        serialize( describe_class_methods("DBICTest::AttrTest") )
+          eq
+        $cached_desc
+      ),
+      "Asking for alternative mro type did not invalidate cache"
+    );
+  }
+
+  # setting mro explicitly still matches what we expect
+  mro::set_mro("DBICTest::AttrTest", "dfs");
+
+  is_deeply (
+    # in case set_mro starts increasing pkg_gen...
+    {
+      %{describe_class_methods("DBICTest::AttrTest")},
+      cumulative_gen => $expected_desc->{cumulative_gen},
+    },
+    $expected_desc,
+    'describing with implicit mro returns correct data'
+  );
+
+  # check that a UNIVERSAL-parent interrogation makes sense
+  # ( it should not list anything from UNIVERSAL itself )
+  is_deeply (
+    describe_class_methods("UEBERVERSAL"),
+    {
+      # should be cached by now, thus safe to rely on...?
+      cumulative_gen => DBIx::Class::_Util::get_real_pkg_gen('UEBERVERSAL'),
+
+      class => 'UEBERVERSAL',
+      mro => { is_c3 => 0, type => 'dfs' },
+      isa => { UEBERVERSAL => 1 },
+      linear_isa => [],
+      methods => {
+        ueber => $expected_desc->{methods}{ueber}
+      },
+      methods_defined_in_class => {
+        ueber => $expected_desc->{methods}{ueber}[0]
+      },
+    },
+    "Expected description of a parent-of-UNIVERSAL class (pathological case)",
+  );
 }
 
 if ($skip_threads) {