|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Text; |
| 5 | +using AS3Context; |
| 6 | +using ASCompletion.Context; |
| 7 | +using ASCompletion.Model; |
| 8 | +using ASCompletion.Settings; |
| 9 | +using ASCompletion.TestUtils; |
| 10 | +using FlashDevelop; |
| 11 | +using NSubstitute; |
| 12 | +using NUnit.Framework; |
| 13 | +using PluginCore; |
| 14 | +using ScintillaNet; |
| 15 | +using ScintillaNet.Enums; |
| 16 | + |
| 17 | +//TODO: Sadly most of ASComplete is currently untestable in a proper way. Work on this branch solves it: https://github.com/Neverbirth/flashdevelop/tree/completionlist |
| 18 | + |
| 19 | +namespace ASCompletion.Completion |
| 20 | +{ |
| 21 | + [TestFixture] |
| 22 | + public class ASCompleteTests |
| 23 | + { |
| 24 | + private MainForm mainForm; |
| 25 | + private ISettings settings; |
| 26 | + private ITabbedDocument doc; |
| 27 | + |
| 28 | + [TestFixtureSetUp] |
| 29 | + public void FixtureSetUp() |
| 30 | + { |
| 31 | + mainForm = new MainForm(); |
| 32 | + settings = Substitute.For<ISettings>(); |
| 33 | + settings.UseTabs = true; |
| 34 | + settings.IndentSize = 4; |
| 35 | + settings.SmartIndentType = SmartIndent.CPP; |
| 36 | + settings.TabIndents = true; |
| 37 | + settings.TabWidth = 4; |
| 38 | + doc = Substitute.For<ITabbedDocument>(); |
| 39 | + mainForm.Settings = settings; |
| 40 | + mainForm.CurrentDocument = doc; |
| 41 | + mainForm.StandaloneMode = false; |
| 42 | + PluginBase.Initialize(mainForm); |
| 43 | + FlashDevelop.Managers.ScintillaManager.LoadConfiguration(); |
| 44 | + } |
| 45 | + |
| 46 | + [TestFixtureTearDown] |
| 47 | + public void FixtureTearDown() |
| 48 | + { |
| 49 | + settings = null; |
| 50 | + doc = null; |
| 51 | + mainForm.Dispose(); |
| 52 | + mainForm = null; |
| 53 | + } |
| 54 | + |
| 55 | + private ScintillaControl GetBaseScintillaControl() |
| 56 | + { |
| 57 | + return new ScintillaControl |
| 58 | + { |
| 59 | + Encoding = System.Text.Encoding.UTF8, |
| 60 | + CodePage = 65001, |
| 61 | + Indent = settings.IndentSize, |
| 62 | + Lexer = 3, |
| 63 | + StyleBits = 7, |
| 64 | + IsTabIndents = settings.TabIndents, |
| 65 | + IsUseTabs = settings.UseTabs, |
| 66 | + TabWidth = settings.TabWidth |
| 67 | + }; |
| 68 | + } |
| 69 | + |
| 70 | + // TODO: Add more tests! |
| 71 | + [Test] |
| 72 | + public void GetExpressionType_SimpleTest() |
| 73 | + { |
| 74 | + //TODO: Improve this test with more checks! |
| 75 | + var pluginMain = Substitute.For<PluginMain>(); |
| 76 | + var pluginUiMock = new PluginUIMock(pluginMain); |
| 77 | + pluginMain.MenuItems.Returns(new List<System.Windows.Forms.ToolStripItem>()); |
| 78 | + pluginMain.Settings.Returns(new GeneralSettings()); |
| 79 | + pluginMain.Panel.Returns(pluginUiMock); |
| 80 | + ASContext.GlobalInit(pluginMain); |
| 81 | + ASContext.Context = Substitute.For<IASContext>(); |
| 82 | + ASContext.Context.CurrentLine = 9; |
| 83 | + var asContext = new AS3Context.Context(new AS3Settings()); |
| 84 | + ASContext.Context.Features.Returns(asContext.Features); |
| 85 | + ASContext.Context.GetCodeModel(null).ReturnsForAnyArgs(x => asContext.GetCodeModel(x.ArgAt<string>(0))); |
| 86 | + |
| 87 | + // Maybe we want to get the filemodel from ASFileParser even if we won't get a controlled environment? |
| 88 | + var member = new MemberModel("test1", "void", FlagType.Function, Visibility.Public) |
| 89 | + { |
| 90 | + LineFrom = 4, |
| 91 | + LineTo = 10, |
| 92 | + Parameters = new List<MemberModel> |
| 93 | + { |
| 94 | + new MemberModel("arg1", "String", FlagType.ParameterVar, Visibility.Default), |
| 95 | + new MemberModel("arg2", "Boolean", FlagType.ParameterVar, Visibility.Default) {Value = "false"} |
| 96 | + } |
| 97 | + }; |
| 98 | + |
| 99 | + var classModel = new ClassModel(); |
| 100 | + classModel.Name = "ASCompleteTest"; |
| 101 | + classModel.Members.Add(member); |
| 102 | + |
| 103 | + var fileModel = new FileModel(); |
| 104 | + fileModel.Classes.Add(classModel); |
| 105 | + |
| 106 | + classModel.InFile = fileModel; |
| 107 | + |
| 108 | + ASContext.Context.CurrentModel.Returns(fileModel); |
| 109 | + ASContext.Context.CurrentClass.Returns(classModel); |
| 110 | + ASContext.Context.CurrentMember.Returns(member); |
| 111 | + |
| 112 | + var sci = GetBaseScintillaControl(); |
| 113 | + sci.Text = TestFile.ReadAllText("ASCompletion.Test_Files.completion.as3.SimpleTest.as"); |
| 114 | + sci.ConfigurationLanguage = "as3"; |
| 115 | + sci.Colourise(0, -1); |
| 116 | + |
| 117 | + var result = ASComplete.GetExpressionType(sci, 185); |
| 118 | + |
| 119 | + Assert.True(result.Context != null && result.Context.LocalVars != null); |
| 120 | + Assert.AreEqual(4, result.Context.LocalVars.Count); |
| 121 | + } |
| 122 | + } |
| 123 | +} |
0 commit comments