public void match_JSON_objects() { { var j = @"{""A"":1,""B"":2}"; VirtualStringMatcher m = new VirtualStringMatcher(new FakeVirtualString(j)); m.MatchJSONObject(out object o).Should().BeTrue(); var list = o as List <KeyValuePair <string, object> >; list.Select(k => k.Key + '|' + k.Value).Concatenate().Should().Be("A|1, B|2"); } { var j = @"{ ""A"" : 1.0, ""B"" : 2 }"; VirtualStringMatcher m = new VirtualStringMatcher(new FakeVirtualString(j)); m.MatchJSONObject(out object o).Should().BeTrue(); var list = o as List <KeyValuePair <string, object> >; list.Select(k => k.Key + '|' + k.Value).Concatenate().Should().Be("A|1, B|2"); } { var j = @"{ ""A"" : [ ""a"" , 3 , null , 6], ""B"" : [ 2, 3, ""XX"" ] }"; VirtualStringMatcher m = new VirtualStringMatcher(new FakeVirtualString(j)); m.MatchJSONObject(out object o).Should().BeTrue(); var list = o as List <KeyValuePair <string, object> >; list.Select(k => k.Key + '|' + ((List <object>)k.Value).Select(v => v?.ToString()).Concatenate("+")) .Concatenate().Should().Be("A|a+3++6, B|2+3+XX"); } }
public void matching_texts_and_whitespaces() { FakeVirtualString v = new FakeVirtualString(" AB \t\r C"); var m = new VirtualStringMatcher(v); Action a; m.MatchText("A").Should().BeFalse(); m.StartIndex.Should().Be(0); m.MatchWhiteSpaces().Should().BeTrue(); m.StartIndex.Should().Be(1); m.MatchText("A").Should().BeTrue(); m.MatchText("B").Should().BeTrue(); m.StartIndex.Should().Be(3); m.MatchWhiteSpaces(6).Should().BeFalse(); m.MatchWhiteSpaces(5).Should().BeTrue(); m.StartIndex.Should().Be(8); m.MatchWhiteSpaces().Should().BeFalse(); m.StartIndex.Should().Be(8); m.MatchText("c").Should().BeTrue(); m.StartIndex.Should().Be(v.Length); m.IsEnd.Should().BeTrue(); a = () => m.MatchText("c"); a.ShouldNotThrow(); a = () => m.MatchWhiteSpaces(); a.ShouldNotThrow(); m.MatchText("A").Should().BeFalse(); m.MatchWhiteSpaces().Should().BeFalse(); }
public void match_JSON_skips_JS_comments(string jsonWithComment) { VirtualStringMatcher m = new VirtualStringMatcher(new FakeVirtualString(jsonWithComment)); m.MatchJSONObject(out object o).Should().BeTrue(); o.Should().Be(1.2); }
public void ToString_constains_the_text_and_the_error() { var m = new VirtualStringMatcher(new FakeVirtualString("The Text")); m.SetError("Plouf..."); m.ToString().Contains("The Text"); m.ToString().Contains("Plouf..."); }
public void matching_integers() { var m = new VirtualStringMatcher(new FakeVirtualString("X3712Y")); m.MatchChar('X').Should().BeTrue(); m.MatchInt32(out int i).Should().BeTrue(); i.Should().Be(3712); m.MatchChar('Y').Should().BeTrue(); }
public void match_methods_must_set_an_error() { var m = new VirtualStringMatcher(new FakeVirtualString("A")); CheckMatchError(m, () => m.MatchChar('B')); CheckMatchError(m, () => m.MatchInt32(out int i)); CheckMatchError(m, () => m.MatchText("PP")); CheckMatchError(m, () => m.MatchText("B")); CheckMatchError(m, () => m.MatchWhiteSpaces()); }
private static void CheckMatchError(VirtualStringMatcher m, Func <bool> fail) { long idx = m.StartIndex; long len = m.Length; fail().Should().BeFalse(); m.IsError.Should().BeTrue(); m.ErrorMessage.Should().NotBeNullOrEmpty(); m.StartIndex.Should().Be(idx); m.Length.Should().Be(len); m.SetSuccess(); }
public void virtualstring_matching() { FakeVirtualString v = new FakeVirtualString("Hello world!"); var m = new VirtualStringMatcher(v); m.Text.GetText(0, (int)m.Length).Should().Be("Hello world!"); m.Text.GetText(0, (int)m.Length - 1).Should().NotBe("Hello world!"); m.Text[5].Should().Be(' '); m.Text[v.Length - 1].Should().Be('!'); m.Text.GetText(0, 5).Should().Be("Hello"); m.Text.GetText(6, 5).Should().Be("world"); }
public void matching_JSONQUotedString(string s, string parsed, string textAfter) { FakeVirtualString v = new FakeVirtualString(s); var m = new VirtualStringMatcher(v); m.TryMatchJSONQuotedString(out string result, true).Should().BeTrue(); result.Should().Be(parsed); m.TryMatchText(textAfter).Should().BeTrue(); m = new VirtualStringMatcher(v); m.TryMatchJSONQuotedString(true).Should().BeTrue(); m.TryMatchText(textAfter).Should().BeTrue(); }
public void matching_double_values(string s, double d) { VirtualStringMatcher m = new VirtualStringMatcher(new FakeVirtualString("P" + s + "S")); m.MatchChar('P').Should().BeTrue(); long idx = m.StartIndex; m.TryMatchDoubleValue().Should().BeTrue(); m.UncheckedMove(idx - m.StartIndex); m.TryMatchDoubleValue(out double parsed).Should().BeTrue(); parsed.Should().BeApproximately(d, 1f); m.MatchChar('S').Should().BeTrue(); m.IsEnd.Should().BeTrue(); }
public void simple_char_matching() { var m = new VirtualStringMatcher(new FakeVirtualString("ABCD")); m.MatchChar('a').Should().BeFalse(); m.MatchChar('A').Should().BeTrue(); m.StartIndex.Should().Be(1); m.MatchChar('A').Should().BeFalse(); m.MatchChar('B').Should().BeTrue(); m.MatchChar('C').Should().BeTrue(); m.IsEnd.Should().BeFalse(); m.MatchChar('D').Should().BeTrue(); m.MatchChar('D').Should().BeFalse(); m.IsEnd.Should().BeTrue(); }
public void open_and_read_file() { using (Stream fileStream = new FileStream(Path.Combine(TestHelper.DataFolder, "basic.json"), FileMode.Open, FileAccess.Read)) { VirtualStringMatcher m = new VirtualStringMatcher(new VirtualString(fileStream, 0, 20)); m.Should().NotBeNull(); m.Text[0].Should().Be('{'); m.Text[12].Should().Be('n'); m.Text[m.Length - 1].Should().Be('}'); m.Text.GetText(342, 19).Should().Be("Name of the product"); m.Text.GetText(343, 19).Should().NotBe("Name of the product"); m.Text.GetText(172, 65).Should().Be("\"description\":\"Product identifier that responds to special needs\""); m.Text[755].Should().Be('"'); m.Text.GetText(756, 34).Should().Be("stringReallyReallyReallyReallyLong"); } }
public void match_JSON_empty_array_or_objects() { { var j = @"{}"; VirtualStringMatcher m = new VirtualStringMatcher(new FakeVirtualString(j)); m.MatchJSONObject(out object o).Should().BeTrue(); var list = o as List <KeyValuePair <string, object> >; list.Should().BeEmpty(); } { var j = @"[]"; VirtualStringMatcher m = new VirtualStringMatcher(new FakeVirtualString(j)); m.MatchJSONObject(out object o).Should().BeTrue(); var list = o as List <object>; list.Should().BeEmpty(); } }
public void matching_integers_with_min_max_values() { var m = new VirtualStringMatcher(new FakeVirtualString("3712 -435 56")); m.MatchInt32(out int i, -500, -400).Should().BeFalse(); m.MatchInt32(out i, 0, 3712).Should().BeTrue(); i.Should().Be(3712); m.MatchWhiteSpaces().Should().BeTrue(); m.MatchInt32(out i, 0).Should().BeFalse(); m.MatchInt32(out i, -500, -400).Should().BeTrue(); i.Should().Be(-435); m.MatchWhiteSpaces().Should().BeTrue(); m.MatchInt32(out i, 1000, 2000).Should().BeFalse(); m.MatchInt32(out i, 56, 56).Should().BeTrue(); i.Should().Be(56); m.IsEnd.Should().BeTrue(); }
public void open_and_read_file() { string content = File.ReadAllText(Path.Combine(TestHelper.DataFolder, "basic.json")).NormalizeEOLToCRLF(); using (Stream stream = new MemoryStream(Encoding.UTF8.GetBytes(content))) { VirtualStringMatcher m = new VirtualStringMatcher(new VirtualString(stream, 0, 20)); m.Text[0].Should().Be('{'); m.Text[12].Should().Be('n'); m.Text[m.Length - 1].Should().Be('}'); m.Text.GetText(342, 19).Should().Be("Name of the product"); m.Text.GetText(343, 19).Should().NotBe("Name of the product"); m.Text.GetText(172, 65).Should().Be("\"description\":\"Product identifier that responds to special needs\""); m.Text[755].Should().Be('"'); m.Text.GetText(756, 34).Should().Be("stringReallyReallyReallyReallyLong"); } }
public void matching_the_5_forms_of_guid(string form) { var id = Guid.NewGuid(); string sId = id.ToString(form); { var m = new VirtualStringMatcher(new FakeVirtualString(sId)); m.TryMatchGuid(out Guid readId).Should().BeTrue(); readId.Should().Be(id); } { var m = new VirtualStringMatcher(new FakeVirtualString("S" + sId)); m.TryMatchChar('S').Should().BeTrue(); m.TryMatchGuid(out Guid readId).Should().BeTrue(); readId.Should().Be(id); } { var m = new VirtualStringMatcher(new FakeVirtualString("S" + sId + "T")); m.MatchChar('S').Should().BeTrue(); m.TryMatchGuid(out Guid readId).Should().BeTrue(); readId.Should().Be(id); m.MatchChar('T').Should().BeTrue(); } sId = sId.Remove(sId.Length - 1); { var m = new VirtualStringMatcher(new FakeVirtualString(sId)); m.TryMatchGuid(out Guid readId).Should().BeFalse(); m.StartIndex.Should().Be(0); } sId = id.ToString().Insert(3, "K").Remove(4); { var m = new VirtualStringMatcher(new FakeVirtualString(sId)); m.TryMatchGuid(out Guid readId).Should().BeFalse(); m.StartIndex.Should().Be(0); } }
public JSONMinifier(VirtualStringMatcher m) : base(m) { _builder = new StringBuilder(); }
static public string Minify(VirtualStringMatcher m) { return(new JSONMinifier(m).Run()); }
public JSONProperties(VirtualStringMatcher m) : base(m) { Properties = new List <string>(); Paths = new List <string>(); }
public JSONDoubleSum(VirtualStringMatcher m) : base(m) { }
public void simple_json_test() { string s = @" { ""p1"": ""n"", ""p2"" : { ""p3"": [ ""p4"": { ""p5"" : 0.989, ""p6"": [], ""p7"": {} } ] } } "; var m = new VirtualStringMatcher(new FakeVirtualString(s)); m.MatchWhiteSpaces().Should().BeTrue(); m.MatchChar('{').Should().BeTrue(); m.MatchWhiteSpaces().Should().BeTrue(); m.TryMatchJSONQuotedString(out string pName).Should().BeTrue(); pName.Should().Be("p1"); m.MatchWhiteSpaces(0).Should().BeTrue(); m.MatchChar(':').Should().BeTrue(); m.MatchWhiteSpaces().Should().BeTrue(); m.TryMatchJSONQuotedString(out pName).Should().BeTrue(); pName.Should().Be("n"); m.MatchWhiteSpaces(0).Should().BeTrue(); m.MatchChar(',').Should().BeTrue(); m.MatchWhiteSpaces().Should().BeTrue(); m.TryMatchJSONQuotedString(out pName).Should().BeTrue(); pName.Should().Be("p2"); m.MatchWhiteSpaces(2).Should().BeTrue(); m.MatchChar(':').Should().BeTrue(); m.MatchWhiteSpaces().Should().BeTrue(); m.MatchChar('{').Should().BeTrue(); m.MatchWhiteSpaces().Should().BeTrue(); m.TryMatchJSONQuotedString(out pName).Should().BeTrue(); pName.Should().Be("p3"); m.MatchWhiteSpaces(0).Should().BeTrue(); m.MatchChar(':').Should().BeTrue(); m.MatchWhiteSpaces().Should().BeTrue(); m.MatchChar('[').Should().BeTrue(); m.MatchWhiteSpaces().Should().BeTrue(); m.TryMatchJSONQuotedString(out pName).Should().BeTrue(); pName.Should().Be("p4"); m.MatchWhiteSpaces(0).Should().BeTrue(); m.MatchChar(':').Should().BeTrue(); m.MatchWhiteSpaces().Should().BeTrue(); m.MatchChar('{').Should().BeTrue(); m.MatchWhiteSpaces().Should().BeTrue(); m.TryMatchJSONQuotedString().Should().BeTrue(); m.MatchWhiteSpaces(0).Should().BeTrue(); m.MatchChar(':').Should().BeTrue(); m.MatchWhiteSpaces().Should().BeTrue(); m.TryMatchDoubleValue().Should().BeTrue(); m.MatchWhiteSpaces(0).Should().BeTrue(); m.MatchChar(',').Should().BeTrue(); m.MatchWhiteSpaces().Should().BeTrue(); m.TryMatchJSONQuotedString(out pName).Should().BeTrue(); pName.Should().Be("p6"); m.MatchWhiteSpaces(0).Should().BeTrue(); m.MatchChar(':').Should().BeTrue(); m.MatchWhiteSpaces().Should().BeTrue(); m.MatchChar('[').Should().BeTrue(); m.MatchWhiteSpaces(0).Should().BeTrue(); m.MatchChar(']').Should().BeTrue(); m.MatchWhiteSpaces(0).Should().BeTrue(); m.MatchChar(',').Should().BeTrue(); m.MatchWhiteSpaces().Should().BeTrue(); m.TryMatchJSONQuotedString().Should().BeTrue(); m.MatchWhiteSpaces(0).Should().BeTrue(); m.MatchChar(':').Should().BeTrue(); m.MatchWhiteSpaces().Should().BeTrue(); m.MatchChar('{').Should().BeTrue(); m.MatchWhiteSpaces(0).Should().BeTrue(); m.MatchChar('}').Should().BeTrue(); m.MatchWhiteSpaces().Should().BeTrue(); m.MatchChar('}').Should().BeTrue(); m.MatchWhiteSpaces().Should().BeTrue(); m.MatchChar(']').Should().BeTrue(); m.MatchWhiteSpaces().Should().BeTrue(); m.MatchChar('}').Should().BeTrue(); m.MatchWhiteSpaces().Should().BeTrue(); m.MatchChar('}').Should().BeTrue(); m.MatchWhiteSpaces(2).Should().BeTrue(); m.IsEnd.Should().BeTrue(); }
public JSONDoubleRewriter(VirtualStringMatcher m, Func <double, string> rewriter) : base(m) { _rewriter = rewriter; _builder = new StringBuilder(); }