How to change the "data provider" returned from a closure into a "data provider"?

时间:2018-05-06 17:03:54

标签: groovy closures spock iterable data-driven-tests

This is in the context of "data driven" testing with Spock:

    public void button1_Click(object sender, EventArgs e)
    {
        if (n < 1)
        {
            Double AD = 0;
            Double BD = 0;
            n++;
        }
        else if (n > 1)
        {
            Double AD = 0;
            Double BD = 0;
            n++;
        }
        using (var g = Graphics.FromImage(pictureBox1.Image))
        {
            int L = Convert.ToInt32(numericUpDown1.Value);
            int T = Convert.ToInt32(numericUpDown2.Value);
            Double TH = System.Convert.ToDouble(T);
            Double LD = System.Convert.ToDouble(L);
            float AF = System.Convert.ToSingle(AD);
            float BF = System.Convert.ToSingle(BD);
            float CF = System.Convert.ToSingle(CD);
            float DF = System.Convert.ToSingle(DD);
            double THP = (TH / 180) * (Math.PI);
            DD = Math.Sin(THP) * LD + AF;
            CD = Math.Cos(THP) * LD + BF;
            g.DrawLine(Pens.Blue, AF, BF, CF, DF);
            pictureBox1.Refresh();
        }


    }

... works fine: the key values are delivered, the parameterised testing works.

But if I try this:

SELECT * FROM `users` where Number='1212' AND Number='0921' 

it doesn't work. The object then on the RHS of the where: key << myDriver.myMap.keySet() is a where: key << { myDriver.myMap.keySet() } . I've then tried numerous things to try to coax a "data provider" out of this <<.

I want to put a closure there firstly so that I can debug a bit but also to provide a more complex set of permutations (not just of "key" values but values of other test parameters... see a recent question of mine here).

Perhaps it's not helped by the class delivered by Closure: turns out this is a Closure... but it's obviously keySet().

I'm a little bit mystified by this "data provider" category. In the Spock "tutorial" it says that this must implement LinkedHashMap$LinkedKeySet. It doesn't say whether that's all its characteristics. Going

Iterable

doesn't help... Iterable obviously delivers { myDriver.myMap.keySet() }.iterator() , not iterator() anyway. What I then get is horrors like this:

groovy.lang.MissingMethodException: No signature of method: core.MyModule.processCommand() is applicable for argument types: (core.UT_ForMyModule$__spock_feature_1_12prov0_closure8, joptsimple.OptionSet, java.util.ArrayList) values: [core.UT_ForMyModule$__spock_feature_1_12prov0_closure8@65fdd86b, ...]

... which clearly means it's delivering an Iterator which iterates over a Iterable of precisely one Iterator.

So, in short, how do I get an Collection from a closure returning an Closure?

1 个答案:

答案 0 :(得分:1)

Unless you have delayed execution objectives here, I believe just running the closure should do:

Environment:

Request Method: GET
Request URL: http://Some_URL

Django Version: 2.0.4
Python Version: 3.5.2
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django.contrib.sitemaps',
 'blog',
 'markdown',
 'taggit']
 Installed Middleware:
 ['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']

 Traceback:

 File "/usr/local/lib/python3.5/dist-
packages/django/core/handlers/exception.py" in inner
  35.             response = get_response(request)

File "/usr/local/lib/python3.5/dist-
 packages/django/core/handlers/base.py" in _get_response
   139.                 "returned None instead." % 
(callback.__module__, view_name)

Exception Type: ValueError at /
Exception Value: The view blog.views.post_list didn't return an 
HttpResponse object. It returned None instead.

But note that this will execute it in the statement.

相关问题