예제 #1
0
파일: VkApiTest.cs 프로젝트: igofed/vk
        public void GetApiUrl_IntArray()
        {
            int[] arr = new[] { 1, 65 };

            //var parameters = new VkParameters { { "country_ids", arr } };
            var parameters = new VkParameters();

            parameters.Add <int>("country_ids", arr);

            const string expected = "https://api.vk.com/method/database.getCountriesById?country_ids=1,65&access_token=token";

            string url = _vk.GetApiUrl("database.getCountriesById", parameters);

            Assert.That(url, Is.EqualTo(expected));
        }
예제 #2
0
파일: MainForm.cs 프로젝트: ddfx/vk
        private void btnRun_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(tbMethodName.Text))
            {
                MessageBox.Show("Не указано имя метода!");
                return;
            }

            // get authorization values from app.config
            int appId;

            try
            {
                string appIdValue = ConfigurationManager.AppSettings["appId"];
                appId = Convert.ToInt32(appIdValue);
            }
            catch (FormatException)
            {
                MessageBox.Show("Ошибка в разборе appId из app.config", "Ошибка приложения", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            string login    = ConfigurationManager.AppSettings["login"];
            string password = ConfigurationManager.AppSettings["password"];

            btnRun.Enabled = btnGetTest.Enabled = false;

            // Authorize on vk server
            var api = new VkApi();

            try
            {
                api.Authorize(appId, login, password, Settings.All);
            }
            catch (VkApiException)
            {
                MessageBox.Show("Ошибка авторизации. Проверьте данные в app.config.", "Ошибка приложения", MessageBoxButtons.OK, MessageBoxIcon.Error);
                btnRun.Enabled = btnGetTest.Enabled = true;
                return;
            }

            // Combine parameters
            var parameters = new VkParameters();

            for (int i = 1; i < 11; i++)
            {
                var name  = (TextBox)pnlParams.Controls.Find(string.Format("tbParamName{0}", i), false)[0];
                var value = (TextBox)pnlParams.Controls.Find(string.Format("tbParamValue{0}", i), false)[0];

                string paramName  = name.Text.Trim();
                string paramValue = value.Text.Trim();

                if (string.IsNullOrEmpty(paramName))
                {
                    continue;
                }

                parameters.Add(paramName, paramValue);
            }

            string methodName = tbMethodName.Text.Trim();

            VkResponse response;

            try
            {
                response = api.Call(methodName, parameters);
            }
            catch (VkApiException ex)
            {
                MessageBox.Show(ex.InnerException != null ? ex.InnerException.Message : ex.Message, "Ошибка приложения",
                                MessageBoxButtons.OK, MessageBoxIcon.Error);
                btnRun.Enabled = btnGetTest.Enabled = true;
                return;
            }

            tbJson.Text = response.RawJson;

            _apiUrl         = api.GetApiUrl(methodName, parameters);
            llVkApiUrl.Text = _apiUrl.Length > 80 ? _apiUrl.Substring(0, 80) + "..." : _apiUrl;

            btnRun.Enabled = btnGetTest.Enabled = true;
        }
예제 #3
0
        public string GenerateUnitTest(string categoryName, string accessToken)
        {
            // 1. read assembly
            // 2. find methods in particular category
            // 3. find methods with not input parameters (it's the most simple case)
            // 4. invoke a method
            // 5. construct unit-test
            // 6. return it

            Assembly dll = Assembly.LoadFrom("VkNet.dll");

            // todo refactor it later
            List <Type> classes = dll.GetTypes().Where(t => t.Name.Contains("Category") && t.Name.Contains(categoryName)).ToList();

            Type category = classes.FirstOrDefault();

            MethodInfo[] methods = category.GetMethods(BindingFlags.Instance | BindingFlags.DeclaredOnly | BindingFlags.Public);

            var genMethods = new List <VkMethodGenInfo>();

            foreach (var methodInfo in methods)
            {
                var genInfo = GetMethodData(methodInfo);

                if (string.IsNullOrEmpty(genInfo.ApiMethod))
                {
                    continue;
                }

                Debug.WriteLine(genInfo.ToString());

                genMethods.Add(genInfo);
            }

            // invoke and get json and url
            var api = new VkApi();

            api.Authorize(accessToken);
            // TODO must be authorized

            var unittests = new List <UnitTestInfo>();

            var testCategory = new StringBuilder();

            foreach (var m in genMethods)
            {
                if (m.Skip)
                {
                    continue;
                }

                var test = new UnitTestInfo
                {
                    Name = string.Format("{0}_", m.Name),
                    Url  = Utilities.PreetyPrintApiUrl(api.GetApiUrl(m.ApiMethod, m.Params))
                };

                // TODO refactor this shit
                int index = test.Url.IndexOf("access_token", StringComparison.InvariantCulture);
                if (index != -1)
                {
                    test.Url = test.Url.Substring(0, index) + "access_token=token\";";
                }

                test.Json = Utilities.PreetyPrintJson(api.Invoke(m.ApiMethod, m.Params));

                unittests.Add(test);

                testCategory.Append(test);
            }

            File.WriteAllText(@"d:\vk.txt", testCategory.ToString());

            return(string.Empty);

            //throw new NotImplementedException();
        }