Example #1
0
        /// <summary>
        /// The entry-point class for this application.
        /// </summary>
        /// <param name='args'>Command-line arguments</param>
        public static void Main(string[] args)
        {
            bool error = false;
            if (args == null || args.Length != 2)
            {
                Console.WriteLine("Arguments for testing not sent. The arguments are full paths to the aff and dict files (in that order)");
                error = true;
            }
            else
            {
                try
                {
                    // Load on construction
                    using (NHunspellWrapper wrap = new NHunspellWrapper(args[0], args[1]))
                    {
                        Test(wrap);
                    }

                    // Load after construction
                    using (NHunspellWrapper wrap = new NHunspellWrapper())
                    {
                        wrap.Load(args[0], args[1]);
                        Test(wrap);
                    }

                    // Never load
                    using (NHunspellWrapper wrap = new NHunspellWrapper())
                    {
                        try
                        {
                            Test(wrap);
                        }
                        catch (InvalidOperationException inv)
                        {
                            if (!inv.Message.Contains("Speller not initialized"))
                            {
                                throw;
                            }
                        }
                    }

                    // Invalid type
                    using (NHunspellWrapper wrap = new NHunspellWrapper())
                    {
                        wrap.OverrideType = "failure.type";
                        try
                        {
                            wrap.Load(args[0], args[1]);
                            Test(wrap);
                        }
                        catch (ArgumentException argExp)
                        {
                            if (!argExp.Message.Contains("Invalid Hunspell instance type"))
                            {
                                throw;
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                    error = true;
                }
            }

            if (error)
            {
                System.Environment.Exit(1);
            }
        }
Example #2
0
        /// <summary>
        /// Analyzes the document.
        /// </summary>
        /// <param name='document'>
        /// Document to analyze
        /// </param>
        public override void AnalyzeDocument(CodeDocument document)
        {
            this.WriteMessage("AnalyzeDocument");
            if (document == null)
            {
                this.WriteMessage("No document available");
                return;
            }

            string cultureName = (document.SourceCode.Project.Culture ?? new System.Globalization.CultureInfo("en-US")).Name;
            this.WriteMessage(cultureName);
            bool wasFound = false;
            this.WriteMessage("About to search paths");
            foreach (var path in this.GetPossiblePaths())
            {
                this.WriteMessage(path);
                try
                {
                    if (!Directory.Exists(path))
                    {
                        this.WriteMessage("Does not exist");
                        continue;
                    }

                    string affPath = Path.Combine(path, cultureName.Replace("-", "_") + ".aff");
                    this.WriteMessage(affPath);
                    string dictPath = Path.Combine(path, cultureName.Replace("-", "_") + ".dic");
                    this.WriteMessage(dictPath);

                    if (File.Exists(affPath) && File.Exists(dictPath))
                    {
                        this.WriteMessage("Both files exist");
                        wasFound = true;
                        this.speller = new NHunspellWrapper();
                        if (!this.logger.Restricted)
                        {
                            this.speller.LogAction = this.logger.WriteMessage;
                        }

                        string cacheSizeRaw = System.Configuration.ConfigurationManager.AppSettings[LinuxCopKey + ".CacheSize"];
                        int cacheSize;
                        if (int.TryParse(cacheSizeRaw, out cacheSize))
                        {
                            this.speller.CacheSize = cacheSize;
                        }

                        this.speller.Load(affPath, dictPath);
                    }
                }
                catch
                {
                    // Eat any exception, keep trying
                    this.WriteMessage("Something went wrong while searching the path");
                }
            }

            if (!wasFound)
            {
                this.WriteMessage("Required aff and dict files not found");
                throw new FileNotFoundException("Could not find aff and dict files for culture");
            }

            this.InitializeDictionaries(cultureName);
            CsDocument csdocument = (CsDocument)document;
            this.WriteMessage("Checking if the document is ready");
            if (csdocument.RootElement != null && !csdocument.RootElement.Generated)
            {
                this.WriteMessage("Preparing to walk the document");
                csdocument.WalkDocument(this.CheckDocumentationSpelling);
            }

            this.WriteMessage("Done analyzing the document");
        }
Example #3
0
        /// <summary>
        /// Run the wrapper through some tests
        /// </summary>
        /// <param name='wrap'>Wrapper to test</param>
        private static void Test(NHunspellWrapper wrap)
        {
            if (wrap.Spell("notaword"))
            {
                throw new Exception("notaword should not be considered spelled correctly");
            }

            // It was cached, no change
            wrap.Add("notaword");
            if (wrap.Spell("notaword"))
            {
                throw new Exception("notaword should not be considered spelled correctly");
            }

            // Clearing the cache
            wrap.Clear();
            if (!wrap.Spell("notaword"))
            {
                throw new Exception("notaword should be considered spelled correctly");
            }

            if (!wrap.Spell("word"))
            {
                throw new Exception("word should be considered spelled correctly");
            }

            // Set as valid and test after
            wrap.Add("notaword2");
            if (!wrap.Spell("notaword2"))
            {
                throw new Exception("notaword2 should be considered spelled correctly");
            }

            if (wrap.IsDisposed)
            {
                throw new Exception("Wrapper shouldn't be disposed yet");
            }

            string test = string.Empty;
            wrap.LogAction = (x, y) => { test = "done"; };
            wrap.LogAction.Invoke(typeof(Program), "done");
            if (test != "done")
            {
                throw new Exception("Unable to change logger, value was " + test);
            }

            test = null;
            wrap.LogAction = null;
            wrap.LogAction.Invoke(typeof(Program), "done");
            if (test != null)
            {
                throw new Exception("Value should not have been set");
            }

            if (wrap.Spell("flushCacheTest"))
            {
                throw new Exception("flushCacheTest should not be considered spelled correctly");
            }

            if (wrap.DisableCache)
            {
                throw new Exception("Cache should not be disabled");
            }

            // Limit the cache size, check the word is still not valid
            wrap.CacheSize = 1;
            wrap.Add("flushCacheTest");
            if (!wrap.Spell("flushCacheTest"))
            {
                throw new Exception("flushCacheTest should be considered spelled correctly");
            }

            if (!wrap.DisableCache)
            {
                throw new Exception("Cache should be disabled");
            }

            wrap.DisableCache = false;
            if (wrap.DisableCache)
            {
                throw new Exception("Cache should not be disabled");
            }

            wrap.DisableCache = true;
            if (!wrap.DisableCache)
            {
                throw new Exception("Cache should be disabled");
            }
        }
Example #4
0
        /// <summary>
        /// Post analyze run
        /// </summary>
        public override void PostAnalyze()
        {
            this.WriteMessage("PostAnalyze");
            if (this.speller != null)
            {
                this.WriteMessage("Disposing of spell checker");
                this.speller.Dispose();
                this.speller = null;
            }

            this.WriteMessage("Done on post analyze");
            this.logger.Flush();
        }