Shlomi Fish avatar Shlomi Fish committed 3b000a8

Start adding the counter pair generator.

Comments (0)

Files changed (1)

 use Test::More tests => 29;
 use XML::LibXML;
 
+sub _create_counter_pair
+{
+    my ($worker_cb, $predicate_cb) = @_;
+
+    my $non_global_counter = Counter->new(
+        {
+            gen_cb => sub {
+                my $inc_cb = shift;
+                return sub {
+                    return $worker_cb->(
+                        sub { 
+                            if (!$predicate_cb->())
+                            { 
+                                $inc_cb->() 
+                            }
+                            return;
+                        }
+                    )->(@_);
+                }
+            },
+        }
+    );
+
+    my $global_counter = Counter->new(
+        {
+            gen_cb => sub {
+                my $inc_cb = shift;
+                return sub {
+                    return $worker_cb->(
+                        sub { 
+                            if ($predicate_cb->())
+                            { 
+                                $inc_cb->() 
+                            }
+                            return;
+                        }
+                    )->(@_);
+                }
+            },
+        }
+    );
+
+    return ($non_global_counter, $global_counter);
+}
+
 my $using_globals = '';
 
 my $open1_counter = Counter->new(
     }
 );
 
-my $close1_non_global_counter = Counter->new(
-    {
-        gen_cb => sub {
-            my $inc_cb = shift;
+my ($close1_non_global_counter, $close1_global_counter) =
+    _create_counter_pair(
+        sub {
+            my $cond_cb = shift;
             return sub {
                 my ($fh) = @_;
                 # warn("open: $f\n");
 
-                if (!defined($XML::LibXML::match_cb))
-                {
-                    $inc_cb->();
-                }
+                $cond_cb->();
 
                 if ($fh)
                 {
                 return 1;
             };
         },
-    }
-);
-
-my $close1_global_counter = Counter->new(
-    {
-        gen_cb => sub {
-            my $inc_cb = shift;
-            return sub {
-                my ($fh) = @_;
-                # warn("open: $f\n");
-
-                if (defined($XML::LibXML::match_cb))
-                {
-                    $inc_cb->();
-                }
-
-                if ($fh)
-                {
-                    $fh->close();
-                }
-
-                return 1;
-            };
-        },
-    }
-);
+        sub { return defined($XML::LibXML::match_cb); },
+    );
 
 {
     # first test checks if local callbacks work
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.