public void Should_Return_Correct_Tokens_For_Message_Which_Mixes_Properties_And_Literals() { // Given, When var result = FormatParser.Parse("Hello {0}! My name is {1}!").ToArray(); // Then Assert.Equal(5, result.Length); Assert.IsType <LiteralToken>(result[0]); Assert.IsType <PropertyToken>(result[1]); Assert.IsType <LiteralToken>(result[2]); Assert.IsType <PropertyToken>(result[3]); Assert.IsType <LiteralToken>(result[4]); }
public void Should_Return_Literal_Tokens_For_Multiple_Unbalanced_Escaped_Opening_Curly_Braces() { // Given, When var result = FormatParser.Parse("{{{{ test {0}").ToArray(); // Then Assert.Equal(2, result.Length); Assert.IsType <LiteralToken>(result[0]); Assert.Equal("{{ test ", ((LiteralToken)result[0]).Text); Assert.IsType <PropertyToken>(result[1]); Assert.Equal(0, ((PropertyToken)result[1]).Position); Assert.Equal(null, ((PropertyToken)result[1]).Format); }
/// <summary> /// Format the message with the given string format. /// </summary> /// <param name="format">String format for the message.</param> /// <returns>Message data formatted according wih given format string.</returns> public string ApplyFormat(string format) { //convert tags to indexes string parsedFormat = FormatParser.Parse(format, c_FormatKeys); try { return(string.Format(parsedFormat, Time, Level, ProcessName, MethodName, Text)); } catch (FormatException) { return("Bad format string sintax!"); } }
public void Should_Return_Literal_Tokens_For_Message_With_Escaped_Braces_And_Properties() { // Given, When var result = FormatParser.Parse("{{}} {0}").ToArray(); // Then Assert.Equal(2, result.Length); Assert.IsType <LiteralToken>(result[0]); Assert.Equal("{} ", ((LiteralToken)result[0]).Text); Assert.IsType <PropertyToken>(result[1]); Assert.Equal(0, ((PropertyToken)result[1]).Position); Assert.Equal(null, ((PropertyToken)result[1]).Format); }
public static bool TryFormatWithObject(this string format, object obj, out string result) { var parser = new FormatParser(format, new Record(obj)); if (parser.Parse(false)) { result = parser.Result; return(true); } else { result = null; return(false); } }
public void TestStartNumberSetting() { Dispenser dispenser = FormatParser.Parse(_testSerailNumberFormatString); var expectedSerialNumber = "TEST20180517-乙Y39"; dispenser.SetStartNumber(expectedSerialNumber); var startSerialNumber = dispenser.First(); Assert.AreEqual(expectedSerialNumber, startSerialNumber); var sencondSerialNumber = dispenser.Skip(1).First(); Assert.AreEqual("TEST20180517-乙Y40", sencondSerialNumber); }
private static List <UvEntry> CreateChecks(DateTime currentDate) { var asm = typeof(DguvTextCheckProvider).GetTypeInfo().Assembly; var resName = $"Dguv.Validator.Format.Data.formats.txt"; var usedBbnr = new Dictionary <string, FormatRecord>(); // Eventuel sollte man die Textdatei auf einen Server legen und diese von dort laden. using (var formatsStream = asm.GetManifestResourceStream(resName)) { using (var streamReader = new StreamReader(formatsStream)) { string line; while ((line = streamReader.ReadLine()) != null) { if (string.IsNullOrEmpty(line)) { continue; } if (!line.StartsWith("UVSD")) { continue; } var record = FormatRecord.Parse(line); if (record.ValidSince <= currentDate) { if (usedBbnr.TryGetValue(record.Bnr, out var prevRecord)) { if (prevRecord.ValidSince < record.ValidSince) { usedBbnr[record.Bnr] = record; } } else { usedBbnr.Add(record.Bnr, record); } } } } } var checks = usedBbnr.Values.Select(x => new UvEntry(x, FormatParser.ParseFormat(x.Format))).ToList(); return(checks); }
public void Render(LogLevel level, string format, params object[] args) { var palette = _palette[level]; var tokens = FormatParser.Parse(format); var colorize = !"{0}".Equals(format, StringComparison.Ordinal); foreach (var token in tokens) { if (colorize) { var colorEscapeCode = GetColorEscapeCode(token, palette); var content = token.Render(args); if (level > LogLevel.Error) { _console.Write("{0}", $"{colorEscapeCode}{content}{ResetEscapeCode}"); } else { _console.WriteError("{0}", $"{colorEscapeCode}{content}{ResetEscapeCode}"); } } else { // Render without colorization. if (level > LogLevel.Error) { _console.Write("{0}", token.Render(args)); } else { _console.WriteError("{0}", token.Render(args)); } } } // Append a new line if (level > LogLevel.Error) { _console.WriteLine(); } else { _console.WriteErrorLine(); } }
public void Should_Return_Literal_Token_For_Unbalanced_Escaped_Closing_Curly_Braces() { // Given, When var result = FormatParser.Parse("test {0:d} }}").ToArray(); // Then Assert.Equal(3, result.Length); Assert.IsType <LiteralToken>(result[0]); Assert.Equal("test ", ((LiteralToken)result[0]).Text); Assert.IsType <PropertyToken>(result[1]); Assert.Equal(0, ((PropertyToken)result[1]).Position); Assert.Equal("d", ((PropertyToken)result[1]).Format); Assert.IsType <LiteralToken>(result[2]); Assert.Equal(" }", ((LiteralToken)result[2]).Text); }
public void DealerResetTest() { var dealer = new Dealer(FormatParser.Parse(@"Test-\a\d")); dealer.Dispenser.SetStartNumber("Test-A9"); var firstNumber = dealer.Next(); var secondNumber = dealer.Next(); Assert.AreEqual(2, dealer.AlreadyDealCount); dealer.Reset(); Assert.AreEqual(null, dealer.Current()); Assert.AreEqual(0, dealer.AlreadyDealCount); }
/// <summary> /// Writes the text representation of the specified array of objects to the /// log using the specified verbosity, log level and format information. /// </summary> /// <param name="verbosity">The verbosity.</param> /// <param name="level">The log level.</param> /// <param name="format">A composite format string.</param> /// <param name="args">An array of objects to write using format.</param> public void Write(Verbosity verbosity, LogLevel level, string format, params object[] args) { if (verbosity > Verbosity) { return; } lock (_lock) { try { var palette = _palettes[level]; var tokens = FormatParser.Parse(format); // if the entirety of the formatting string is a single token, as is coming from // the LogExtensions implementation of (string) and (object), don't attempt to colorize the output. var colorize = !"{0}".Equals(format, StringComparison.OrdinalIgnoreCase); foreach (var token in tokens) { SetPalette(token, palette, colorize); if (level > LogLevel.Error) { _console.Write("{0}", token.Render(args)); } else { _console.WriteError("{0}", token.Render(args)); } } } finally { _console.ResetColor(); if (level > LogLevel.Error) { _console.WriteLine(); } else { _console.WriteErrorLine(); } } } }
/// <summary> /// Gets the log file writer. /// </summary> /// <returns>The <see cref="StreamWriter"/> that allows to wite logs to file.</returns> private StreamWriter GetLogFile() { FileStream stream = null; bool stop = false; while (!stop) { string parsedFormat = FormatParser.Parse(m_FileNameTemplate, c_FormatKeys); string fileName = string.Format(parsedFormat, DateTime.UtcNow, m_FileCounter++); stream?.Flush(); stream?.Close(); stream?.Dispose(); stream = File.Open(fileName, FileMode.Append, FileAccess.Write, FileShare.Read); //check if a restriction about file size exists if (m_MaxFileSize != 0) { if (m_MaxFileSize > 0) { stop = stream.Length < m_MaxFileSize; } else { m_MsgCounter = 0; using (StreamReader reader = new StreamReader(File.Open(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))) { while (reader.ReadLine() != null) { m_MsgCounter++; } } stop = m_MsgCounter + m_MaxFileSize < 0; //m_MaxFileSize is negative! } } else { stop = true; } } return(new StreamWriter(stream) { AutoFlush = true }); }
public TransformNodeChunk(byte[] data) : base(data) { Attributes.TryGetValue("_name", out string name); Name = name; Attributes.TryGetValue("_hidden", out string hidden); IsHidden = hidden == "1"; ChildNodeId = FormatParser.ParseInt32(); ReservedId = FormatParser.ParseInt32(); LayerId = FormatParser.ParseInt32(); int frameCount = FormatParser.ParseInt32(); if (frameCount > 0) { //TODO: implement frame parsing Frames = Enumerable.Repeat(new Frame(), frameCount).ToArray(); } }
public void Write(Verbosity verbosity, LogLevel level, string format, params object[] args) { if (verbosity > Verbosity) { return; } lock (_lock) { try { var palette = _palettes[level]; var tokens = FormatParser.Parse(format); foreach (var token in tokens) { SetPalette(token, palette); if (level > LogLevel.Error) { _console.Write("{0}", token.Render(args)); } else { _console.WriteError("{0}", token.Render(args)); } } } finally { _console.ResetColor(); if (level > LogLevel.Error) { _console.WriteLine(); } else { _console.WriteErrorLine(); } } } }
public void Render(LogLevel level, string format, params object[] args) { try { var palette = _palette[level]; var tokens = FormatParser.Parse(format); var colorize = !"{0}".Equals(format, StringComparison.OrdinalIgnoreCase); foreach (var token in tokens) { SetPalette(token, palette, colorize); if (level > LogLevel.Error) { _console.Write("{0}", token.Render(args)); } else { _console.WriteError("{0}", token.Render(args)); } } } finally { _console.ResetColor(); } // Append a new line if (level > LogLevel.Error) { _console.WriteLine(); } else { _console.WriteErrorLine(); } }
/// <summary> /// Creates a new chunk using the given data. /// </summary> /// <param name="data">Data starting at the first byte of the chunk</param> public Chunk(byte[] data) { if (data == null) { throw new ArgumentNullException(nameof(data), $"{nameof(data)} is null!"); } if (data.Length == 0) { throw new ArgumentException($"{nameof(data)} is empty!"); } var formatParser = new FormatParser(data); Type = ChunkTypeMapping.GetChunkId(formatParser.ParseString(4)); int contentLength = formatParser.ParseInt32(); int childrenLength = formatParser.ParseInt32(); Content = formatParser.ParseBytes(contentLength); Children = formatParser.ParseChunks(childrenLength); TotalBytes = formatParser.CurrentOffset; }
public static IList <IFormat> SelectFormats(this IList <IFormat> formats, string format_spec, string mergeOutputFormat = null) => FormatParser.SelectFormats(formats, format_spec, mergeOutputFormat);
public PackChunk(byte[] data) : base(data) { var formatParser = new FormatParser(Content); ModelCount = formatParser.ParseInt32(); }
public IList <IFormat> SelectFormats(string format_spec, string mergeOutputFormat = null) => FormatParser.SelectFormats(Items, format_spec, mergeOutputFormat);
public SizeChunk(byte[] data) : base(data) { var formatParser = new FormatParser(Content); Size = formatParser.ParseVector3(); }