public void StructureTest(StructureTestData testData) { using (var sr = new StringReader(testData.Ftl)) { var resource = new Parser().Parse(sr); var resourceJson = AstToJson.ToJson(resource); bool resultsEqual = JToken.DeepEquals(resourceJson, testData.Expected); if (!resultsEqual) { Console.WriteLine("parsed ="); Console.WriteLine(resourceJson); Console.WriteLine("expected ="); Console.WriteLine(testData.Expected); var jdp = new JsonDiffPatch(); var diff = jdp.Diff(resourceJson, testData.Expected); Console.WriteLine("diff ="); Console.WriteLine(diff); } resultsEqual.Should().BeTrue(); // doesn't seem to work -- just returns true // resourceJson.Should().BeEquivalentTo(testData.Expected, // options => options.AllowingInfiniteRecursion().RespectingRuntimeTypes()); } }
public void BehaviourTest(StructureTestData testData) { using (var sr = new StringReader(testData.Ftl)) { var resource = new RuntimeParser().GetResource(sr); var entryJson = RuntimeAstToJson.ToJson(resource.Entries); entryJson.Should().BeOfType <JObject>(); var entries = (JObject)entryJson; // For some reason, this does a really shallow test -- just checks // keys in the objects rather than the contents. Probably better // to just replace the expected outputs with the whole serialized // json. foreach (var expectedKeyValue in testData.Expected) { entries.Should().ContainKey(expectedKeyValue.Key); var entry = entries[expectedKeyValue.Key]; expectedKeyValue.Value.Should().NotBeNull(); expectedKeyValue.Value.Should().BeOfType <JObject>(); var expected = (JObject)expectedKeyValue.Value; if (expected["value"].Type == JTokenType.Boolean && (bool)((JValue)expected["value"]).Value) { entry.Should().Match <JToken>( x => x.Type == JTokenType.String || x.Type == JTokenType.Object && ((JObject)x).ContainsKey("val")); } else { entry.Should().Match <JToken>( x => x.Type != JTokenType.String && !(x.Type == JTokenType.Object && ((JObject)x).ContainsKey("val"))); } if (expected.ContainsKey("attributes")) { entry.Type.Should().Be(JTokenType.Object); var entryObj = (JObject)entry; entryObj.Should().ContainKey("attrs"); var expectedKeys = ((JObject)expected["attributes"]) .Properties().Select(x => x.Name); var entryKeys = ((JObject)entryObj["attrs"]) .Properties().Select(x => x.Name); expectedKeys.Should().BeEquivalentTo(entryKeys); } else { entry.Should().Match <JToken>( x => x.Type == JTokenType.String || (x.Type == JTokenType.Object && !((JObject)x).ContainsKey("attrs"))); } } } }
public void ReferenceTest(StructureTestData testData) { // The following fixtures produce different ASTs in the tooling parser than // in the reference parser. Skip them for now. var skips = new HashSet <string>() { // Call arguments edge-cases. "call_expressions", // The tooling parser rejects variant keys which contain leading whitespace. // There's even a behavior fixture for this; it must have been a // deliberate decision. "select_expressions", // Broken Attributes break the entire Entry right now. // https://github.com/projectfluent/fluent.js/issues/237 "leading_dots", "variant_lists" }; if (skips.Contains(testData.TestName)) { Assert.Ignore(); } using (var sr = new StringReader(testData.Ftl)) { var resource = new Parser(false).Parse(sr); // Ignore Junk which is parsed differently by the tooling parser, and // which doesn't carry spans nor annotations in the reference parser. resource.Body = resource.Body.Where(x => !(x is Ast.Junk)).ToList(); testData.Expected["body"] = new JArray( ((JArray)testData.Expected["body"]).Where(x => x["type"].ToString() != "Junk")); var resourceJson = AstToJson.ToJson(resource); bool resultsEqual = JToken.DeepEquals(resourceJson, testData.Expected); if (!resultsEqual) { Console.WriteLine("parsed ="); Console.WriteLine(resourceJson); Console.WriteLine("expected ="); Console.WriteLine(testData.Expected); var jdp = new JsonDiffPatch(); var diff = jdp.Diff(resourceJson, testData.Expected); Console.WriteLine("diff ="); Console.WriteLine(diff); } resultsEqual.Should().BeTrue(); } }
public void StructureTest(StructureTestData testData) { using (var sr = new StringReader(testData.Ftl)) { var resource = new RuntimeParser().GetResource(sr); var entryJson = RuntimeAstToJson.ToJson(resource.Entries); bool resultsEqual = JToken.DeepEquals(entryJson, testData.Expected); if (!resultsEqual) { Console.WriteLine("parsed ="); Console.WriteLine(entryJson); Console.WriteLine("expected ="); Console.WriteLine(testData.Expected); var jdp = new JsonDiffPatch(); var diff = jdp.Diff(entryJson, testData.Expected); Console.WriteLine("diff ="); Console.WriteLine(diff); } resultsEqual.Should().BeTrue(); } }