[glibmm/gmmproc-refactor] Add some unit tests I wrote some time ago.



commit 586af4759a5137b0ab1c06598f72c802f8f2d57a
Author: Krzesimir Nowak <qdlacz gmail com>
Date:   Mon Jun 25 01:26:00 2012 +0200

    Add some unit tests I wrote some time ago.

 .../pm/tests/unit/gmmproc-shared/t01-type-fixup.t  |   41 +++++++++++
 tools/pm/tests/unit/gmmproc-shared/t02-is-in.t     |   74 +++++++++++++++++++
 .../tests/unit/gmmproc-shared/t03-cleanup-tokens.t |   69 ++++++++++++++++++
 .../gmmproc-shared/t04-string-split-func-params.t  |   54 ++++++++++++++
 .../tests/unit/gmmproc-shared/t05-parse-params.t   |   57 +++++++++++++++
 .../t06-parse-function-declaration.t               |   51 +++++++++++++
 .../t07-split-cxx-type-to-sub-types.t              |   33 +++++++++
 .../unit/gmmproc-shared/t08-split-string-commas.t  |   34 +++++++++
 .../tests/unit/gmmproc-shared/t09-extract-token.t  |   77 ++++++++++++++++++++
 .../gmmproc-shared/t10-extract-bracketed-text.t    |   64 ++++++++++++++++
 10 files changed, 554 insertions(+), 0 deletions(-)
---
diff --git a/tools/pm/tests/unit/gmmproc-shared/t01-type-fixup.t b/tools/pm/tests/unit/gmmproc-shared/t01-type-fixup.t
new file mode 100755
index 0000000..b9eff84
--- /dev/null
+++ b/tools/pm/tests/unit/gmmproc-shared/t01-type-fixup.t
@@ -0,0 +1,41 @@
+#!/usr/bin/perl
+# -*- mode: perl; perl-indent-level: 2; indent-tabs-mode: nil -*-
+
+use strict;
+use warnings;
+
+# TODO: use '$(srcdir)/../../../'
+push (@INC, '../../../');
+
+require Common::Shared;
+
+use Test::More;
+
+my @types =
+(
+ ['', ''],
+ ['int', 'int'],
+ ['  int  ', 'int'],
+ ['const Glib::ustring&', 'const Glib::ustring&'],
+ ['const gchar*', 'const gchar*'],
+ ['const Glib::ustring &', 'const Glib::ustring&'],
+ ['const std::vector<Foo::Bar::Baz> &', 'const std::vector< Foo::Bar::Baz >&'],
+ ['A<B<C> *>&', 'A< B< C >* >&'],
+ ['gchar * *', 'gchar**'],
+ ['int *   &', 'int*&'],
+ ['Glib :: ustring', 'Glib::ustring'],
+ [':: Glib :: ustring', '::Glib::ustring'],
+ [' std :: vector <  :: Glib :: ustring  > &', 'std::vector< ::Glib::ustring >&'],
+ ['a<b,c,d<e,f,g> >', 'a< b, c, d< e, f, g > >']
+);
+
+plan (tests => @types * 1);
+
+foreach my $entry (@types)
+{
+  my $type = $entry->[0];
+  my $expected = $entry->[1];
+  my $result = Common::Shared::_type_fixup ($type);
+
+  is ($result, $expected, join ('', '\'', $type, '\' => \'', $expected, '\''));
+}
diff --git a/tools/pm/tests/unit/gmmproc-shared/t02-is-in.t b/tools/pm/tests/unit/gmmproc-shared/t02-is-in.t
new file mode 100755
index 0000000..fc492c0
--- /dev/null
+++ b/tools/pm/tests/unit/gmmproc-shared/t02-is-in.t
@@ -0,0 +1,74 @@
+#!/usr/bin/perl
+# -*- mode: perl; perl-indent-level: 2; indent-tabs-mode: nil -*-
+
+use strict;
+use warnings;
+
+use Test::More;
+
+sub atos
+{
+  my ($array) = @_;
+
+  return 'undef' unless (defined ($array));
+
+  foreach my $str (@{$array})
+  {
+    unless (defined ($str))
+    {
+      $str = 'undef';
+    }
+  }
+
+  return '[' . join (', ', @{$array}) . ']';
+}
+
+sub u
+{
+  unless (defined ($_[0]))
+  {
+    return 'undef';
+  }
+
+  return $_[0];
+}
+
+my @params =
+(
+ [undef, [], 0],
+ [undef, ['a'], 0],
+ [undef, [undef], 1],
+ [undef, ['a', 'b'], 0],
+ [undef, ['a', undef, 'b'], 1],
+ ['a', [], 0],
+ ['a', ['a'], 1],
+ ['a', [undef], 0],
+ ['a', [undef, 'b'], 0],
+ ['a', ['a', undef, 'b'], 1],
+ ['a b', [], 0],
+ ['a b', ['a b'], 1],
+ ['a b', [undef], 0],
+ ['a b', [undef, 'b a'], 0],
+ ['a b', ['a b', undef, 'b a'], 1]
+);
+
+plan (tests => @params * 1);
+
+foreach my $entry (@params)
+{
+  my $needle = $entry->[0];
+  my $haystack = $entry->[1];
+  my $expected = $entry->[2];
+  my $result = $needle ~~ @{$haystack};
+
+  if ($result)
+  {
+    $result = 1;
+  }
+  else
+  {
+    $result = 0;
+  }
+
+  is ($result, $expected, join ('', u ($needle), ' in ', atos ($haystack)));
+}
diff --git a/tools/pm/tests/unit/gmmproc-shared/t03-cleanup-tokens.t b/tools/pm/tests/unit/gmmproc-shared/t03-cleanup-tokens.t
new file mode 100755
index 0000000..d8963c8
--- /dev/null
+++ b/tools/pm/tests/unit/gmmproc-shared/t03-cleanup-tokens.t
@@ -0,0 +1,69 @@
+#!/usr/bin/perl
+# -*- mode: perl; perl-indent-level: 2; indent-tabs-mode: nil -*-
+
+use strict;
+use warnings;
+
+use Test::More;
+
+# TODO: use '$(srcdir)/../../../'
+push (@INC, '../../../');
+
+require Common::Shared;
+
+sub atos
+{
+  my ($array) = @_;
+  my @new_array = ();
+
+  return 'undef' unless (defined ($array));
+
+  foreach my $str (@{$array})
+  {
+    my $new = '';
+
+    if (defined ($str))
+    {
+      if ($str eq "\n")
+      {
+        $new = '\'\n\'';
+      }
+      else
+      {
+        $new = '\'' . $str . '\'';
+      }
+    }
+    else
+    {
+      $new = 'undef';
+    }
+
+    push (@new_array, $new);
+  }
+
+  return '[' . join (', ', @new_array) . ']';
+}
+
+my @sets =
+(
+ [[], []],
+ [[''], []],
+ [[undef], []],
+ [['', undef], []],
+ [['a'], ['a']],
+ [['a', '', undef], ['a']],
+ [['a', 'b'], ['a', 'b']],
+ [['a', "\n", 'b'], ['a', "\n", 'b']]
+);
+
+plan (tests => scalar (@sets) * 1);
+
+foreach my $entry (@sets)
+{
+  my $tokens = $entry->[0];
+  my $expected = $entry->[1];
+  my $label = atos ($tokens) . ' -> ' . atos ($expected);
+  my @result = Common::Shared::cleanup_tokens (@{$tokens});
+
+  is_deeply (\ result, $expected, $label);
+}
diff --git a/tools/pm/tests/unit/gmmproc-shared/t04-string-split-func-params.t b/tools/pm/tests/unit/gmmproc-shared/t04-string-split-func-params.t
new file mode 100755
index 0000000..6ea0f43
--- /dev/null
+++ b/tools/pm/tests/unit/gmmproc-shared/t04-string-split-func-params.t
@@ -0,0 +1,54 @@
+#!/usr/bin/perl
+# -*- mode: perl; perl-indent-level: 2; indent-tabs-mode: nil -*-
+
+use strict;
+use warnings;
+
+use Test::More;
+
+# TODO: use '$(srcdir)/../../../'
+push (@INC, '../../../');
+
+require Common::Shared;
+
+sub h
+{
+  croak (join ('', 'Expected 5 parameters, got ', scalar (@_))) if (@_ != 5);
+
+  return
+  {
+    'type' => shift,
+    'name' => shift,
+    'value' => shift,
+    'nullable' => shift,
+    'out' => shift
+  };
+}
+
+my @params =
+(
+ ['', []],
+ ['int a', ['int a']],
+ ['const Glib::ustring& str', ['const Glib::ustring& str']],
+ ['const gchar* str = " ajwaj "', ['const gchar* str = " ajwaj "']],
+ ['const gchar c = \'o\'', ['const gchar c = \'o\'']],
+ ['gchar c = \',\'', ['gchar c = \',\'']],
+ ['const Glib::ustring& str{?}', ['const Glib::ustring& str{?}']],
+ ['int a{OUT}', ['int a{OUT}']],
+ ['int a, int b', ['int a', 'int b']],
+ ['double& b{RET}, const std::vector<Foo::Bar::Baz>& v, A<B<C> >& stuff{?}, int i = 42', ['double& b{RET}', 'const std::vector<Foo::Bar::Baz>& v', 'A<B<C> >& stuff{?}', 'int i = 42']],
+ ['int &a', ['int &a']],
+ ['gchar * *foo', ['gchar * *foo']],
+ [':: a :: b < c::d<e>, f, g<h,i>, j>& v1{?}, k *v2 = "a, \'b, c", l v3 = \'"\', m<n> v4= "}\\"{"', [':: a :: b < c::d<e>, f, g<h,i>, j>& v1{?}', 'k *v2 = "a, \'b, c"', 'l v3 = \'"\'', 'm<n> v4= "}\\"{"']]
+);
+
+plan (tests => @params * 1);
+
+foreach my $entry (@params)
+{
+  my $line = $entry->[0];
+  my $expected = $entry->[1];
+  my $result = Common::Shared::string_split_func_params ($line);
+
+  is_deeply ($result, $expected, '\'' . $line . '\'');
+}
diff --git a/tools/pm/tests/unit/gmmproc-shared/t05-parse-params.t b/tools/pm/tests/unit/gmmproc-shared/t05-parse-params.t
new file mode 100755
index 0000000..8f16fa3
--- /dev/null
+++ b/tools/pm/tests/unit/gmmproc-shared/t05-parse-params.t
@@ -0,0 +1,57 @@
+#!/usr/bin/perl
+# -*- mode: perl; perl-indent-level: 2; indent-tabs-mode: nil -*-
+
+use strict;
+use warnings;
+
+use Test::More;
+
+# TODO: use '$(srcdir)/../../../'
+push (@INC, '../../../');
+
+require Common::Shared;
+
+sub h
+{
+  croak (join ('', 'Expected 5 parameters, got ', scalar (@_))) if (@_ != 5);
+
+  return
+  {
+    'type' => shift,
+    'name' => shift,
+    'value' => shift,
+    'nullable' => shift,
+    'out' => shift
+  };
+}
+
+my @params =
+(
+ ['()', []],
+ ['', []],
+ ['int a', [h('int', 'a', undef, 0, 0)]],
+ ['(int a)', [h('int', 'a', undef, 0, 0)]],
+ ['const Glib::ustring& str', [h('const Glib::ustring&', 'str', undef, 0, 0)]],
+ ['const gchar* str = " ajwaj "', [h('const gchar*', 'str', '" ajwaj "', 0, 0)]],
+ ['const gchar c = \'o\'', [h('const gchar', 'c', '\'o\'', 0, 0)]],
+ ['gchar c = \',\'', [h('gchar', 'c', '\',\'', 0, 0)]],
+ ['const Glib::ustring& str{?}', [h('const Glib::ustring&', 'str', undef, 1, 0)]],
+ ['int a{OUT}', [h('int', 'a', undef, 0, 1)]],
+ ['int a{RET}', [h('int', 'a', undef, 0, 1)]],
+ ['int a, int b', [h('int', 'a', undef, 0, 0), h('int', 'b', undef, 0, 0)]],
+ ['double& b{RET}, const std::vector<Foo::Bar::Baz>& v, A<B<C> >& stuff{?}, int i = 42', [h('double&', 'b', undef, 0, 1), h('const std::vector< Foo::Bar::Baz >&', 'v', undef, 0, 0), h('A< B< C > >&', 'stuff', undef, 1, 0), h('int', 'i', '42', 0, 0)]],
+ ['int &a', [h('int&', 'a', undef, 0, 0)]],
+ ['gchar * *foo', [h('gchar**', 'foo', undef, 0, 0)]],
+ [':: a :: b < c::d<e>, f, g<h,i>, j>& v1{?}, k *v2 = "a, \'b, c", l v3 = \'"\', m<n> v4= "}\"{"', [h('::a::b< c::d< e >, f, g< h, i >, j >&', 'v1', undef, 1, 0), h('k*', 'v2', '"a, \'b, c"', 0, 0), h('l', 'v3', '\'"\'', 0, 0), h('m< n >',  'v4' ,'"}\"{"', 0, 0)]]
+);
+
+plan (tests => @params * 1);
+
+foreach my $entry (@params)
+{
+  my $line = $entry->[0];
+  my $expected = $entry->[1];
+  my $result = Common::Shared::parse_params ($line);
+
+  is_deeply ($result, $expected, '\'' . $line . '\'');
+}
diff --git a/tools/pm/tests/unit/gmmproc-shared/t06-parse-function-declaration.t b/tools/pm/tests/unit/gmmproc-shared/t06-parse-function-declaration.t
new file mode 100755
index 0000000..3198f77
--- /dev/null
+++ b/tools/pm/tests/unit/gmmproc-shared/t06-parse-function-declaration.t
@@ -0,0 +1,51 @@
+#!/usr/bin/perl
+# -*- mode: perl; perl-indent-level: 2; indent-tabs-mode: nil -*-
+
+use strict;
+use warnings;
+
+use Test::More;
+
+# TODO: use '$(srcdir)/../../../'
+push (@INC, '../../../');
+
+require Common::Shared;
+
+sub h
+{
+  croak (join ('', 'Expected 5 parameters, got ', scalar (@_))) if (@_ != 5);
+
+  return
+  {
+    'type' => shift,
+    'name' => shift,
+    'value' => shift,
+    'nullable' => shift,
+    'out' => shift
+  };
+}
+
+my @params =
+(
+ ['', ['', '', '', '', '']],
+ ['void f()', ['', 'void', 'f', '()', '']],
+ ['static void f()', ['static', 'void', 'f', '()', '']],
+ ['void f() const', ['', 'void', 'f', '()', 'const']],
+ ['static void f() const', ['static', 'void', 'f', '()', 'const']],
+ ['int f(int a)', ['', 'int', 'f', '(int a)', '']],
+ ['int f(:: a :: b < c::d<e>, f, g<h,i>, j>& v1{?}, k *v2 = "a, \'b, c", l v3 = \'"\', m<n> v4= "}\\"{", o v5 = \'\\\'\')', ['', 'int', 'f', '(:: a :: b < c::d<e>, f, g<h,i>, j>& v1{?}, k *v2 = "a, \'b, c", l v3 = \'"\', m<n> v4= "}\\"{", o v5 = \'\\\'\')', '']],
+ ['int operator==(int a, int b)', ['', 'int', 'operator==', '(int a, int b)', '']],
+ ['int &operator ()(int a)', ['', 'int&', 'operator ()', '(int a)', '']],
+ [':: a :: b < c::d<e>, f, g<h,i>, j> &f()', ['', '::a::b< c::d< e >, f, g< h, i >, j >&', 'f', '()', '']]
+);
+
+plan (tests => @params * 1);
+
+foreach my $entry (@params)
+{
+  my $line = $entry->[0];
+  my $expected = $entry->[1];
+  my $result = Common::Shared::parse_function_declaration ($line);
+
+  is_deeply ($result, $expected, '\'' . $line . '\'');
+}
diff --git a/tools/pm/tests/unit/gmmproc-shared/t07-split-cxx-type-to-sub-types.t b/tools/pm/tests/unit/gmmproc-shared/t07-split-cxx-type-to-sub-types.t
new file mode 100755
index 0000000..ed91325
--- /dev/null
+++ b/tools/pm/tests/unit/gmmproc-shared/t07-split-cxx-type-to-sub-types.t
@@ -0,0 +1,33 @@
+#!/usr/bin/perl
+# -*- mode: perl; perl-indent-level: 2; indent-tabs-mode: nil -*-
+
+use strict;
+use warnings;
+
+use Test::More;
+
+# TODO: use '$(srcdir)/../../../'
+push (@INC, '../../../');
+
+require Common::Shared;
+
+my @params =
+(
+ ['a', ['a']],
+ ['a::b', ['a::b', 'b']],
+ ['a::b::c', ['a::b::c', 'b::c', 'c']],
+ ['::a', ['::a', 'a']],
+ ['::a::b', ['::a::b', 'a::b', 'b']],
+ ['::a::b::c', ['::a::b::c', 'a::b::c', 'b::c', 'c']]
+);
+
+plan (tests => @params * 1);
+
+foreach my $entry (@params)
+{
+  my $cxx_type = $entry->[0];
+  my $expected = $entry->[1];
+  my $result = Common::Shared::split_cxx_type_to_sub_types ($cxx_type);
+
+  is_deeply ($result, $expected, '\'' . $cxx_type . '\'');
+}
diff --git a/tools/pm/tests/unit/gmmproc-shared/t08-split-string-commas.t b/tools/pm/tests/unit/gmmproc-shared/t08-split-string-commas.t
new file mode 100755
index 0000000..ccf0113
--- /dev/null
+++ b/tools/pm/tests/unit/gmmproc-shared/t08-split-string-commas.t
@@ -0,0 +1,34 @@
+#!/usr/bin/perl
+# -*- mode: perl; perl-indent-level: 2; indent-tabs-mode: nil -*-
+
+use strict;
+use warnings;
+
+use Test::More;
+
+# TODO: use '$(srcdir)/../../../'
+push (@INC, '../../../');
+
+require Common::Shared;
+
+my @params =
+(
+ ['', []],
+ ['foo', ['foo']],
+ ['foo, bar', ['foo', 'bar']],
+ ['foo(bar), baz', ['foo(bar)', 'baz']],
+ ['foo(bar, baz), biff', ['foo(bar, baz)', 'biff']],
+ ['foo, opt=\',\\\'\'', ['foo', 'opt=\',\\\'\'']],
+ ['foo, opt=",\""', ['foo', 'opt=",\""']]
+);
+
+plan (tests => @params * 1);
+
+foreach my $entry (@params)
+{
+  my $line = $entry->[0];
+  my $expected = $entry->[1];
+  my @result = Common::Shared::string_split_commas ($line);
+
+  is_deeply (\ result, $expected, '\'' . $line . '\'');
+}
diff --git a/tools/pm/tests/unit/gmmproc-shared/t09-extract-token.t b/tools/pm/tests/unit/gmmproc-shared/t09-extract-token.t
new file mode 100755
index 0000000..fd11d13
--- /dev/null
+++ b/tools/pm/tests/unit/gmmproc-shared/t09-extract-token.t
@@ -0,0 +1,77 @@
+#!/usr/bin/perl
+# -*- mode: perl; perl-indent-level: 2; indent-tabs-mode: nil -*-
+
+use strict;
+use warnings;
+
+use Test::More;
+
+# TODO: use '$(srcdir)/../../../'
+push (@INC, '../../../');
+
+sub atos
+{
+  my ($array) = @_;
+  # TODO: do it with map
+  my @new_array = ();
+
+  foreach my $entry (@{$array})
+  {
+    my $new = '';
+
+    if ($entry eq "\n")
+    {
+      $new = '\n';
+    }
+    else
+    {
+      $new = $entry;
+    }
+    push (@new_array, $new);
+  }
+
+  return '[' . join (', ', @new_array) . ']';
+}
+
+require Common::Shared;
+
+my @params =
+(
+ [[], [undef, 0], 0, undef],
+ [['foo'], ['foo', 0], 0, undef],
+ [['foo', 'bar'], ['foo', 0], 1, 'bar'],
+ [['foo', "\n", 'bar'], ['foo', 0], 2, "\n"],
+ [["\n", 'foo'], ["\n", 1], 1, 'foo'],
+ [["\n", "\n"], ["\n", 1], 1, "\n"],
+ [["\n"], ["\n", 1], 0, undef]
+);
+my $test_plan = @params * 2;
+
+foreach my $entry (@params)
+{
+  my $next_val = $entry->[3];
+
+  if (defined ($next_val))
+  {
+    ++$test_plan;
+  }
+}
+
+plan (tests => $test_plan);
+
+foreach my $entry (@params)
+{
+  my $tokens = $entry->[0];
+  my $expected = $entry->[1];
+  my $remaining_count = $entry->[2];
+  my $next_val = $entry->[3];
+  my $label = atos ($tokens);
+  my $result = Common::Shared::extract_token ($tokens);
+
+  is_deeply ($result, $expected, $label);
+  is (scalar (@{$tokens}), $remaining_count, 'remaining count: ' . $remaining_count);
+  if ($remaining_count)
+  {
+    is ($tokens->[0], $next_val, 'next val: \'' . ($next_val eq "\n" ? '\n' : $next_val) . '\'');
+  }
+}
diff --git a/tools/pm/tests/unit/gmmproc-shared/t10-extract-bracketed-text.t b/tools/pm/tests/unit/gmmproc-shared/t10-extract-bracketed-text.t
new file mode 100755
index 0000000..6c133d2
--- /dev/null
+++ b/tools/pm/tests/unit/gmmproc-shared/t10-extract-bracketed-text.t
@@ -0,0 +1,64 @@
+#!/usr/bin/perl
+# -*- mode: perl; perl-indent-level: 2; indent-tabs-mode: nil -*-
+
+use strict;
+use warnings;
+
+use Test::More;
+
+# TODO: use '$(srcdir)/../../../'
+push (@INC, '../../../');
+
+sub atos
+{
+  my ($array) = @_;
+  # TODO: do it with map
+  my @new_array = ();
+
+  foreach my $entry (@{$array})
+  {
+    my $new = '';
+
+    if ($entry eq "\n")
+    {
+      $new = '\n';
+    }
+    else
+    {
+      $new = $entry;
+    }
+    push (@new_array, $new);
+  }
+
+  return join ('', @new_array);
+}
+
+require Common::Shared;
+
+my @params =
+(
+ [[], undef],
+ [['(', ')'], ['', 0]],
+ [['ab', '(', 'x', ')'], undef],
+ [['(', 'x', ')', 'ab'], ['x', 0]],
+ [['(', 'x', '(', 'ab', ')', ', cd', ')'], ['x(ab), cd', 0]],
+ [['('], undef],
+ [[')'], undef],
+ [['(', '(', ')'], undef],
+ [['(', 'void', ' ', 'func', '(', 'char', ' ', 'a', ' ', '=', ' ', '\'', '(', '\'', ')', ',', ' ', 'c_func', ')'], ['void func(char a = \'(\'), c_func', 0]],
+ [['(', "\n", ')'], ["\n", 1]],
+ [['(', 'a', "\n", ')'], ["a\n", 1]],
+ [['(', 'ab', "\n", 'cd', ')'], ["ab\ncd", 1]]
+);
+
+plan (tests => scalar (@params));
+
+foreach my $entry (@params)
+{
+  my $tokens = $entry->[0];
+  my $expected = $entry->[1];
+  my $label = atos ($tokens);
+  my $result = Common::Shared::extract_bracketed_text ($tokens);
+
+  is_deeply ($result, $expected, '\'' . $label . '\'');
+}



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]