[Test] //ExSkip public void Content() { // Open a document with a couple paragraphs of content Document doc = new Document(MyDir + "Properties.Content.docx"); // The properties we will work with are members of the BuiltInDocumentProperties attribute BuiltInDocumentProperties properties = doc.BuiltInDocumentProperties; // By using built in properties, // we can treat document statistics such as word/page/character counts as metadata that can be glanced at without opening the document // These properties are accessed by right-clicking the file in Windows Explorer and navigating to Properties > Details > Content // If we want to display this data inside the document, we can use fields such as NUMPAGES, NUMWORDS, NUMCHARS etc. // Also, these values can also be viewed in Microsoft Word by navigating File > Properties > Advanced Properties > Statistics // Page count: The PageCount attribute shows the page count in real time and its value can be assigned to the Pages property properties.Pages = doc.PageCount; Assert.AreEqual(2, properties.Pages); // Word count: The UpdateWordCount() automatically assigns the real time word/character counts to the respective built in properties doc.UpdateWordCount(); Assert.AreEqual(198, properties.Words); Assert.AreEqual(1114, properties.Characters); Assert.AreEqual(1310, properties.CharactersWithSpaces); // Line count: Count the lines in a document and assign value to the Lines property\ LineCounter lineCounter = new LineCounter(doc); properties.Lines = lineCounter.GetLineCount(); Assert.AreEqual(14, properties.Lines); // Paragraph count: Assign the size of the count of child Paragraph-nodes to the Paragraphs built in property properties.Paragraphs = doc.GetChildNodes(NodeType.Paragraph, true).Count; Assert.AreEqual(2, properties.Paragraphs); // Bytes: Use a stream to find out the real file size of our document and assign it to the Property using (MemoryStream stream = new MemoryStream()) { doc.Save(stream, SaveFormat.Docx); properties.Bytes = (int)stream.Length; Assert.AreEqual(10870, properties.Bytes); } // Template: The Template attribute can reflect the filename of the attached template document doc.AttachedTemplate = MyDir + "Document.BusinessBrochureTemplate.dot"; Assert.AreEqual("Normal", properties.Template); properties.Template = doc.AttachedTemplate; // Content status: This is a descriptive field properties.ContentStatus = "Draft"; // Content type: Upon saving, any value we assign to this field will be overwritten by the MIME type of the output save format Assert.AreEqual("", properties.ContentType); // If the document contains links and they are all up to date, we can set this to true Assert.False(properties.LinksUpToDate); doc.Save(ArtifactsDir + "Properties.Content.docx"); }
public void Should_Count_Lines_Of_Code() { LineCounter lineCounter = CreateLineCounter(); int result = lineCounter.CountLinesOfCode( _lines); result.Should().Be(2); }
public UnitSizeMetric(SyntaxNode unitNode) { string rawCode = ExtractRawCode(unitNode); var sanitizedCode = new Sanitizer().Sanitize(rawCode); LineCounter counter = new LineCounter(sanitizedCode); this.Size = counter.NumberOfLines; }
public void Should_Count_Lines() { LineCounter lineCounter = CreateLineCounter(); int result = lineCounter.CountLines( _lines); result.Should().Be(7); }
public void ountLines_SingleLineNoEndingBreak() { using var sample = new MemoryStream(Encoding.UTF8.GetBytes("Simple line")); var sut = new LineCounter(); var want = 0; var got = sut.CountLines(sample); Assert.Equal(want, got); }
/// <summary> /// Parses all lines using regex and outputs a single Json document with nested documents for each line. /// </summary> /// <param name="reader"></param> /// <returns></returns> public override JObject ParseLogDocument(TextReader reader) { // We have two possible output schema: complex records or simple key/value pairs. IList <IDictionary <string, object> > records = new List <IDictionary <string, object> >(); IDictionary <string, object> pairs = new Dictionary <string, object>(); // Parse each line using the regex into a set of key/value pairs. string line; while ((line = ReadLine(reader)) != null) { LineCounter.Increment(); IDictionary <string, object> fields = FindAndApplyRegexMatch(line); if (fields.Count > 0) { // In certain cases, we just want to write a key/value pair. if (fields.Count == 2 && fields.ContainsKey("key") && fields.ContainsKey("value")) { pairs.Add(fields["key"].ToString(), fields["value"]); } else { fields.Add("line", LineCounter.CurrentValue.ToString()); records.Add(fields); } } } // Bail out if we didn't successfully parse anything. if (records.Count == 0 && pairs.Count == 0) { return(null); } // Serialize and return result. JToken jtoken; if (pairs.Count > 0) { // jtoken = pairs.SerializeToJson(); jtoken = JObject.FromObject(pairs); } else { jtoken = JArray.FromObject(records); } var json = new JObject { { "contents", jtoken } }; FinishedParsing = true; return(InsertMetadata(json)); }
public void ScrollToSearchResult(FileContentSearchResult searchResult) { ContentTextBox.ScrollToLine(searchResult.Line); var index = LineCounter.GetCharacterIndex(ContentTextBox.Text, searchResult.Line, searchResult.Column); var contentLength = ContentTextBox.Text.Length; ContentTextBox.Select(index, searchResult.Query.Expression.Length); ContentTextBox.Focus(); }
public override void Populate(TextWriter trapFile) { trapFile.files(this, TransformedPath.Value, TransformedPath.NameWithoutExtension, TransformedPath.Extension); if (TransformedPath.ParentDirectory is PathTransformer.ITransformedPath dir) { trapFile.containerparent(Extraction.Entities.Folder.Create(Context, dir), this); } var trees = Context.Compilation.SyntaxTrees.Where(t => t.FilePath == originalPath); if (trees.Any()) { foreach (var text in trees.Select(tree => tree.GetText())) { var rawText = text.ToString() ?? ""; var lineCounts = LineCounter.ComputeLineCounts(rawText); if (rawText.Length > 0 && rawText[rawText.Length - 1] != '\n') { lineCounts.Total++; } trapFile.numlines(this, lineCounts); Context.TrapWriter.Archive(originalPath, TransformedPath, text.Encoding ?? System.Text.Encoding.Default); } } else if (IsPossiblyTextFile()) { try { System.Text.Encoding encoding; var lineCount = 0; using (var sr = new StreamReader(originalPath, detectEncodingFromByteOrderMarks: true)) { while (sr.ReadLine() is not null) { lineCount++; } encoding = sr.CurrentEncoding; } trapFile.numlines(this, new LineCounts() { Total = lineCount, Code = 0, Comment = 0 }); Context.TrapWriter.Archive(originalPath, TransformedPath, encoding ?? System.Text.Encoding.Default); } catch (Exception exc) { Context.ExtractionError($"Couldn't read file: {originalPath}. {exc.Message}", null, null, exc.StackTrace); } } trapFile.file_extraction_mode(this, Context.Extractor.Standalone ? 1 : 0); }
public void DirectoryTest() { // Arrange LineCounter lineCounter3 = new LineCounter("TestFiles/Java"); int expectedResult = 15; // Act int result = lineCounter3.totalLinesOfCode; // Assert Assert.AreEqual(expectedResult, result); }
public static LineCounts Visit(SyntaxToken identifier, SyntaxNode body) { var start = identifier.GetLocation().SourceSpan.Start; var end = body.GetLocation().SourceSpan.End - 1; var textSpan = new Microsoft.CodeAnalysis.Text.TextSpan(start, end - start); var text = body.SyntaxTree.GetText().GetSubText(textSpan) + "\r\n"; return(LineCounter.ComputeLineCounts(text)); }
public void RandomChanges() { const string Tag = "TestWatcherRandomChanges_"; string folderName = Directory.GetCurrentDirectory(); string pattern = Tag + "*"; foreach (string file in Directory.EnumerateFiles(folderName, pattern)) { File.Delete(file); } var ff = new FileFiller(); var initialPopulation = new List <FileEntry>(); for (int i = 1; i <= 99; i++) { string fName = Tag + i + ".txt"; ff.FillFile(fName, numberOfLines: i, lineLength: FileFiller.RandomLength); initialPopulation.Add(new FileEntry(fName, DateTime.UtcNow, i, 0)); } for (int i = 100; i <= 102400; i *= 2) { string fName = Tag + i + ".txt"; ff.FillFile(fName, numberOfLines: i, lineLength: FileFiller.RandomLength); initialPopulation.Add(new FileEntry(fName, DateTime.UtcNow, i, 0)); } _RandomCounted = 0; var model = new FolderModel(); var view = new Log(toConsole: false, fileName: "out.txt"); var counter = new LineCounter(); var loader = new BulkLoader(); var controller = new Watcher(view, model, counter, loader, folderName, Tag + "*"); controller.FolderChanged += Controller_FolderChanged_Random; controller.Status += Controller_Status; System.Threading.Tasks.Task w = controller.StartAsync(); int loops = 0; while (_lastStatus != Watcher.Monitoring) { loops++; Thread.Sleep(1000); if (loops > 100) { Assert.Fail("Some Ting Wong"); break; } } }
public void SimpleTest() { LineCounter lineCounter3 = new LineCounter("Empty.txt"); int expectedResult = 3; // Act int result = lineCounter3.GetAmountOfCodeLines <CSharpFile>(new List <string>() { "TestFiles/CSharp/SimpleTest.cs" }); // Assert Assert.AreEqual(expectedResult, result); }
public void UltimateTest() { LineCounter lineCounter3 = new LineCounter("Empty.txt"); int expectedResult = 7; // Act int result = lineCounter3.GetAmountOfCodeLines <JavaFile>(new List <string>() { "TestFiles/Java/UltimateTest.java" }); // Assert Assert.AreEqual(expectedResult, result); }
public IEnumerable <LinesOfCodeStat> GetLinesOfCodeStats(string directory) { var fileCrawler = new FileCrawler(new DirectoryReader()); var fileReader = new FileReader(); var lineCounter = new LineCounter(); foreach (string filePath in fileCrawler.FindSourceFileNames(directory)) { string[] lines = fileReader.ReadLinesFromFile(filePath); int linesCount = lineCounter.CountLines(lines); int linesOfCodeCount = lineCounter.CountLinesOfCode(lines); yield return(new LinesOfCodeStat(filePath, linesCount, linesOfCodeCount)); } }
public void AdvancedTest() { // Arrange LineCounter lineCounter3 = new LineCounter("Empty.txt"); int expectedResult = 5; // Act int result = lineCounter3.GetAmountOfCodeLines <JavaFile>(new List <string>() { "TestFiles/Java/AdvancedTest.java" }); // Assert Assert.AreEqual(expectedResult, result); }
public void Initialize() { const string Tag = "TestWatcherInitialize_"; var x = new FileFiller(); x.FillFile(Tag + "1" + ".txt", numberOfLines: 1, lineLength: FileFiller.RandomLength); x.FillFile(Tag + "2" + ".txt", numberOfLines: 2, lineLength: FileFiller.RandomLength); x.FillFile(Tag + "3" + ".txt", numberOfLines: 3, lineLength: FileFiller.RandomLength); x.FillFile(Tag + "4" + ".txt", numberOfLines: 4, lineLength: FileFiller.RandomLength); x.FillFile(Tag + "5" + ".txt", numberOfLines: 5, lineLength: FileFiller.RandomLength); x.FillFile(Tag + "6" + ".txt", numberOfLines: 6, lineLength: FileFiller.RandomLength); x.FillFile(Tag + "7" + ".txt", numberOfLines: 7, lineLength: FileFiller.RandomLength); x.FillFile(Tag + "8" + ".txt", numberOfLines: 8, lineLength: FileFiller.RandomLength); x.FillFile(Tag + "9" + ".txt", numberOfLines: 9, lineLength: FileFiller.RandomLength); x.FillFile(Tag + "10" + ".txt", numberOfLines: 10, lineLength: FileFiller.RandomLength); x.FillFile(Tag + "100" + ".txt", numberOfLines: 100, lineLength: FileFiller.RandomLength); x.FillFile(Tag + "1000" + ".txt", numberOfLines: 1000, lineLength: FileFiller.RandomLength); x.FillFile(Tag + "10000" + ".txt", numberOfLines: 10000, lineLength: FileFiller.RandomLength); var model = new FolderModel(); var view = new Log(toConsole: false, fileName: "out.txt"); var counter = new LineCounter(); var loader = new BulkLoader(); string dir = Directory.GetCurrentDirectory(); var controller = new Watcher(view, model, counter, loader, dir, Tag + "*"); System.Threading.Tasks.Task w = controller.StartAsync(); w.Wait(10000); var q = model[Tag + "1" + ".txt"]; Assert.AreEqual(model.Count(), 13); Assert.AreEqual(model[Tag + "1" + ".txt"].LineCount, 1); Assert.AreEqual(model[Tag + "2" + ".txt"].LineCount, 2); Assert.AreEqual(model[Tag + "3" + ".txt"].LineCount, 3); Assert.AreEqual(model[Tag + "4" + ".txt"].LineCount, 4); Assert.AreEqual(model[Tag + "5" + ".txt"].LineCount, 5); Assert.AreEqual(model[Tag + "6" + ".txt"].LineCount, 6); Assert.AreEqual(model[Tag + "7" + ".txt"].LineCount, 7); Assert.AreEqual(model[Tag + "8" + ".txt"].LineCount, 8); Assert.AreEqual(model[Tag + "9" + ".txt"].LineCount, 9); Assert.AreEqual(model[Tag + "10" + ".txt"].LineCount, 10); Assert.AreEqual(model[Tag + "100" + ".txt"].LineCount, 100); Assert.AreEqual(model[Tag + "1000" + ".txt"].LineCount, 1000); Assert.AreEqual(model[Tag + "10000" + ".txt"].LineCount, 10000); Console.WriteLine("DONE"); }
public async Task SampleWithExplicitEncoding(Encoding encoding) { var lines = File.ReadAllLines("Samples/utf8samples.txt"); // write the content with the encoding to a byte stream var bytes = WriteToMemory(lines, encoding); using var stream = new MemoryStream(bytes); // create the counter with the specified encoding using var counter = new LineCounter(stream, encoding); var count = await counter.CountAllLinesAsync(); // confirm content match Assert.Equal(lines.Length, count); }
public void CountZeroLengthFile() { const string Test_0 = "Test_0.txt"; if (!File.Exists(Test_0)) { var ff = new FileFiller(); ff.FillFile(Test_0, 0, -2); } var fc = new LineCounter(); var model = new FileEvent(FileEventType.Create, new FileEntry(Test_0)); LineCountProgress result = fc.Count(model); Assert.AreEqual(result.Status, LineCountStatus.Success); Assert.AreEqual(0, result.Count); }
public void Main_WritePath() { string currentDirectory = Environment.CurrentDirectory; int expected = 0; foreach (string file in Directory.EnumerateFiles(currentDirectory, "*.cs")) { expected += File.ReadAllLines(file).Count(line => line.Trim().Length > 0); } IntelliTect.TestTools.Console.ConsoleAssert.Expect(expected.ToString(), () => { LineCounter.Main(new string[] { currentDirectory }); }); }
private static void AnalyzeSymbol(SyntaxTreeAnalysisContext context) { var classes = context.Tree.GetRoot().DescendantNodes().OfType <BaseTypeDeclarationSyntax>(); foreach (var @class in classes) { string text = @class.GetText().ToString(); string sanitizedText = new Sanitizer().Sanitize(text); long moduleSize = new LineCounter(sanitizedText).NumberOfLines; if (moduleSize > MaxModuleSize) { var identifier = @class.Identifier; context.ReportDiagnostic(Diagnostic.Create(Rule, identifier.GetLocation(), moduleSize, MaxModuleSize)); } } }
static void Main(string[] args) { try { if (args.Length != 2) { Console.WriteLine(Usage); return; } string path = args[0]; string filter = args[1]; if (!Directory.Exists(path)) { Console.WriteLine(InvalidPath, path); return; } Console.WriteLine("UTAU Line Counter"); Console.WriteLine("================="); Console.WriteLine("Folder: " + path); Console.WriteLine("Filter: " + filter); var model = new FolderModel(); var view = new Log(toConsole: true, fileName: null); var counter = new LineCounter(); var loader = new BulkLoader(); var controller = new Watcher(view, model, counter, loader, path, filter); controller.FolderChanged += Controller_FolderChanged; controller.Status += Controller_Status; Console.WriteLine("Starting watcher."); Task t = controller.StartAsync(); Console.WriteLine("Running. Ctrl-C or close the console window to exit."); Console.WriteLine(); t.Wait(); Console.WriteLine("DONE"); } finally { WaitForExit(); } }
public override void Populate(TextWriter trapFile) { if (Path == null) { trapFile.files(this, "", "", ""); } else { var fi = new FileInfo(Path); string extension = fi.Extension ?? ""; string name = fi.Name; name = name.Substring(0, name.Length - extension.Length); int fromSource = extension.ToLowerInvariant().Equals(".cs") ? 1 : 2; // remove the dot from the extension if (extension.Length > 0) { extension = extension.Substring(1); } trapFile.files(this, PathAsDatabaseString(Path), name, extension); trapFile.containerparent(Folder.Create(Context, fi.Directory), this); if (fromSource == 1) { foreach (var text in Context.Compilation.SyntaxTrees. Where(t => t.FilePath == Path). Select(tree => tree.GetText())) { var rawText = text.ToString() ?? ""; var lineCounts = LineCounter.ComputeLineCounts(rawText); if (rawText.Length > 0 && rawText[rawText.Length - 1] != '\n') { lineCounts.Total++; } trapFile.numlines(this, lineCounts); Context.TrapWriter.Archive(fi.FullName, text.Encoding ?? System.Text.Encoding.Default); } } trapFile.file_extraction_mode(this, Context.Extractor.Standalone ? 1 : 0); } }
public async Task CountToPosition(int linesBefore, int linesAfter) { var texts = new string[] { LineReaderTestData.FormatStringOneObject, LineReaderTestData.FormatStringTwoObjects, LineReaderTestData.FormatStringThreeObjects, LineReaderTestData.FormatStringMultipleObjects }; long position = 0; var j = 0; byte[] data; using (var memStream = new MemoryStream()) using (var writer = new StreamWriter(memStream)) { for (var i = 0; i < linesBefore; i++) { await writer.WriteLineAsync(texts[j++ % texts.Length]); } // remember the stream position at this point await writer.FlushAsync(); position = memStream.Position; for (var i = 0; i < linesAfter; i++) { await writer.WriteLineAsync(texts[j++ % texts.Length]); } await writer.FlushAsync(); data = memStream.ToArray(); } using var readStream = new MemoryStream(data); using var counter = new LineCounter(readStream, null); var count = await counter.CountLinesAsync(position); Assert.Equal(linesBefore, count); }
/// <summary> /// /// </summary> /// <param name="line"></param> private void DoWriteLine(string line) { LineCounter++; line = DateTime.Now.ToString(AppConstants.GenericTimestampFormat, System.Globalization.CultureInfo.InvariantCulture) + ";" + line; toolStripLabel1.Text = "APP TRACE " + LineCounter.ToString(); if (line.Contains("ERROR")) { richTextBox1.AppendText(line, Color.Red, true); } else if (line.Contains("Layout")) { richTextBox1.AppendText(line, Color.Aqua, true); } else { richTextBox1.AppendText(line, Color.White, true); } }
public void CountLockedFile() { const string Test_Locked = "Test_Locked.txt"; if (!File.Exists(Test_Locked)) { var ff = new FileFiller(); ff.FillFile(Test_Locked, 10, -2); } var x = Task.Run(() => LockFile(Test_Locked, seconds: 30)); Thread.Sleep(1000); // give it time to open and lock the file var fc = new LineCounter(); var model = new FileEvent(FileEventType.Create, new FileEntry(Test_Locked)); LineCountProgress result = fc.Count(model); Assert.AreEqual(result.Status, LineCountStatus.TimedOut); x.Wait(); }
/// <summary> /// Starts the task and writes the results to the standard output stream. /// </summary> public void Run() { if (parameters.ShowLogo) { WriteLogo(); } if (parameters.ShowHelp) { WriteHelp(); } else if (parameters.FileSet.FileCount == 0) // If there is no file to analyse { System.Console.Error.WriteLine("Error: No input files were specified.\n"); WriteHelp(); } else { int fileCount = parameters.FileSet.FileCount; if (fileCount == 1) { System.Console.WriteLine("1 file is being parsed.\n"); } else { System.Console.WriteLine("{0} files are being parsed.\n", fileCount); } try { LineCounter counter = new LineCounter(parameters.FileSet); counter.Count(); CreateXmlReportFile(); ShowResults(); } catch (InvalidFileException ex) { System.Console.Error.WriteLine("Error: " + ex.Message); } } }
public static void Init() { blacklist = new List <string>(); blacklist.Add("Editor"); sortMethod = (SortMethod)EditorPrefs.GetInt("LC_SortMethod", 0); displayMethod = (DisplayMethod)EditorPrefs.GetInt("LC_DisplayMode", 0); ignoreEmptyLines = EditorPrefs.GetBool("LC_IgnoreEmptyLines", false); displayMatchesOnly = EditorPrefs.GetBool("LC_DisplayMatchesOnly", true); comparisonMode = EditorPrefs.GetBool("LC_ComparisonMode", false); if (displayMethod != DisplayMethod.Graph) { search = EditorPrefs.GetString("LC_SavedSearch", ""); } LineCounter window = GetWindow <LineCounter>("Line Counter"); window.Show(); window.Focus(); window.CountLines(); }
/// <summary> /// A default implementation for safely reading a single line from the reader. Handles line counting and EOF detection. /// </summary> /// <param name="reader">An open reader to a log file.</param> /// <returns>A line of text, or null if EOF or an exception is encountered.</returns> protected virtual string ReadLine(TextReader reader) { if (UseLineNumbers) { LineCounter.Increment(); } string line; try { line = reader.ReadLine(); } catch (Exception) { return null; } if (line == null) { FinishedParsing = true; } return line; }
public static string InsertLineNumbers(string source) { Regex regex = new Regex("\n"); LineCounter counter = new LineCounter(); return "000 "+regex.Replace(source, new MatchEvaluator(counter.AddLineCount)); }
public override LineCounts DefaultVisit(SyntaxNode node) { var text = node.SyntaxTree.GetText().GetSubText(node.GetLocation().SourceSpan).ToString(); return(LineCounter.ComputeLineCounts(text)); }
private static LineCounter CreateLineCounter() { var linecounter = new LineCounter(); return(linecounter); }