public async Task Empty() { using var reader = Helper.CreateReader("<teste />"); var context = A.Fake <IParsingContext>(i => i.Strict()); var settings = A.Fake <IElementParserSettings>(i => i.Strict()); var parentContext = A.Fake <IParentParsingContext>(i => i.Strict()); var sut = new ElementParser(); await sut.ParseAsync(reader, context, parentContext, settings); }
public async Task AggregateNodeParser() { using var reader = Helper.CreateReader("<t><a /></t>"); var parentContext = new FakeParentParsingContext(); var context = A.Fake <IParsingContext>(i => i.Strict()); var node = A.Dummy <INode>(); var nodeParser = new FakeNodeParser("a", node); var sut = new ElementParser(); var settings = FakeSettings(new FakeAggregatedNodeParser(sut, nodeParser)); await sut.ParseAsync(reader, context, parentContext, settings); parentContext.ShouldContainSingle(node); }
public async Task InvalidContent(string xmlContent, string message) { using var reader = Helper.CreateReader(xmlContent); var parentContext = new FakeParentParsingContext(); var context = A.Fake <IParsingContext>(i => i.Strict()); A.CallTo(() => context.LogError(reader, message)).DoesNothing(); var textParser = A.Fake <ITextParser>(i => i.Strict()); var settings = FakeSettings(textParser); var sut = new ElementParser(); await sut.ParseAsync(reader, context, parentContext, settings); parentContext.ShouldBeEmpty(); A.CallTo(() => context.LogError(reader, message)).MustHaveHappenedOnceExactly(); }
public async Task Aggregated() { using var reader = Helper.CreateReader("<t><a /><b /></t>"); var parentContext = new FakeParentParsingContext(); var context = A.Fake <IParsingContext>(i => i.Strict()); var node = A.Dummy <INode>(); var settings = FakeSettings(new FakeNodeParser("a", node)); A.CallTo(() => settings.NoRepeatNode).Returns(false); A.CallTo(() => settings.ExitOnUnknownNode).Returns(true); var sut = new ElementParser(); await sut.ParseAsync(reader, context, parentContext, settings); reader.LocalName.Should().Be("b"); }
public async Task UnconfiguredText() { const string message = "Este elemento não permite texto."; using var reader = Helper.CreateReader("<t>Pindamonhangaba</t>"); var parentContext = new FakeParentParsingContext(); var context = A.Fake <IParsingContext>(i => i.Strict()); A.CallTo(() => context.LogError(reader, message)).DoesNothing(); var settings = FakeSettings(); var sut = new ElementParser(); await sut.ParseAsync(reader, context, parentContext, settings); parentContext.ShouldBeEmpty(); A.CallTo(() => context.LogError(reader, message)).MustHaveHappenedOnceExactly(); }
public async Task AttributeParsing(string xml, bool hasWhen, bool hasWhile) { using var reader = Helper.CreateReader(xml); var context = A.Dummy <IParsingContext>(); var whenAttribute = A.Dummy <WhenAttribute>(); var whileAttribute = A.Dummy <WhileAttribute>(); var parentContext = new FakeParentParsingContext(); var whenParser = Helper.FakeParser <IAttributeParser>("when"); A.CallTo(() => whenParser.Parse(reader, context)).Returns(whenAttribute); var whileParser = Helper.FakeParser <IAttributeParser>("while"); A.CallTo(() => whileParser.Parse(reader, context)).Returns(whileAttribute); var settings = FakeSettings(whenParser, whileParser); var sut = new ElementParser(); await sut.ParseAsync(reader, context, parentContext, settings); parentContext.ParsedText.Should().BeNull(); if (hasWhen) { parentContext.When.Should().BeSameAs(whenAttribute.Condition); } else { parentContext.When.Should().BeNull(); } if (hasWhile) { parentContext.While.Should().BeSameAs(whileAttribute.Condition); } else { parentContext.While.Should().BeNull(); } }
public async Task WithAggregateSettingsWithoutTextParserShouldReturnWhenTextIsFound() { var reader = A.Fake <XmlReader>(i => i.Strict()); A.CallTo(() => reader.ReadState).Returns(ReadState.Interactive); A.CallTo(() => reader.NodeType).Returns(XmlNodeType.Text); var parentContext = A.Fake <IParentParsingContext>(i => i.Strict()); var context = A.Fake <IParsingContext>(i => i.Strict()); var settings = A.Fake <IElementParserSettings>(i => i.Strict()); A.CallTo(() => settings.TextParser).Returns(null); A.CallTo(() => settings.NoRepeatNode).Returns(false); A.CallTo(() => settings.ExitOnUnknownNode).Returns(true); var sut = new ElementParser(); await sut.ParseAsync(reader, context, parentContext, settings); }
public async Task TextParsing() { using var reader = Helper.CreateReader("<t>Pindamonhangaba</t>"); var parentContext = new FakeParentParsingContext(); var context = A.Fake <IParsingContext>(i => i.Strict()); var textParser = A.Fake <ITextParser>(i => i.Strict()); A.CallTo(() => textParser.Parse(reader, context, "Pindamonhangaba")).Returns("Formatado"); var settings = FakeSettings(textParser); var sut = new ElementParser(); await sut.ParseAsync(reader, context, parentContext, settings); parentContext.When.Should().BeNull(); parentContext.While.Should().BeNull(); parentContext.Nodes.Should().BeEmpty(); parentContext.ParsedText.Should().Be("Formatado"); }
public async Task NonRepeat() { const string message = "Elemento repetido: a"; using var reader = Helper.CreateReader("<t><a /><a /></t>"); var parentContext = new FakeParentParsingContext(); var context = A.Fake <IParsingContext>(i => i.Strict()); A.CallTo(() => context.LogError(reader, message)).DoesNothing(); var node = A.Dummy <INode>(); var settings = FakeSettings(new FakeNodeParser("a", node)); A.CallTo(() => settings.NoRepeatNode).Returns(true); A.CallTo(() => settings.ExitOnUnknownNode).Returns(false); var sut = new ElementParser(); await sut.ParseAsync(reader, context, parentContext, settings); A.CallTo(() => context.LogError(reader, message)).MustHaveHappenedOnceExactly(); }
public async Task ElementParsing(string xml, bool hasA, bool hasB) { using var reader = Helper.CreateReader(xml); var parentContext = new FakeParentParsingContext(); var context = A.Fake <IParsingContext>(i => i.Strict()); A.CallTo(() => context.IsSuccess).Returns(true); var aNode = A.Dummy <INode>(); var bNode = A.Dummy <INode>(); var settings = FakeSettings( new FakeNodeParser("a", aNode), new FakeNodeParser("b", bNode) ); var sut = new ElementParser(); await sut.ParseAsync(reader, context, parentContext, settings); parentContext.ParsedText.Should().BeNull(); parentContext.When.Should().BeNull(); parentContext.While.Should().BeNull(); if (hasA) { parentContext.Nodes.Should().Contain(aNode); parentContext.Nodes.Remove(aNode); } if (hasB) { parentContext.Nodes.Should().Contain(bNode); parentContext.Nodes.Remove(bNode); } parentContext.Nodes.Should().BeEmpty(); }
public async Task TextAndElementSimultaneouslyIsNotAllowed(string xml, bool textFirst) { const string message = "Não é permitido texto dentro de elemento que tenha elementos filhos."; using var reader = Helper.CreateReader(xml); var parentContext = new FakeParentParsingContext(); var context = A.Fake <IParsingContext>(i => i.Strict()); A.CallTo(() => context.LogError(reader, message)).DoesNothing(); var textParser = A.Fake <ITextParser>(i => i.Strict()); A.CallTo(() => textParser.Parse(reader, context, "abc")).Returns("def"); var node = A.Dummy <INode>(); var settings = FakeSettings(textParser, new FakeNodeParser("a", node)); var sut = new ElementParser(); await sut.ParseAsync(reader, context, parentContext, settings); parentContext.When.Should().BeNull(); parentContext.While.Should().BeNull(); if (textFirst) { parentContext.Nodes.Should().BeEmpty(); parentContext.ParsedText.Should().Be("def"); } else { parentContext.ShouldContainSingle(node); parentContext.ParsedText.Should().BeNull(); } A.CallTo(() => context.LogError(reader, message)).MustHaveHappenedOnceExactly(); }
public async Task UnconfiguredElement() { const string aMessage = "Elemento não reconhecido: a"; const string bMessage = "Elemento não reconhecido: b"; using var reader = Helper.CreateReader("<t><a /><b /><c /></t>"); var parentContext = new FakeParentParsingContext(); var context = A.Fake <IParsingContext>(i => i.Strict()); A.CallTo(() => context.LogError(reader, aMessage)).DoesNothing(); A.CallTo(() => context.LogError(reader, bMessage)).DoesNothing(); var node = A.Dummy <INode>(); var settings = FakeSettings(new FakeNodeParser("c", node)); var sut = new ElementParser(); await sut.ParseAsync(reader, context, parentContext, settings); parentContext.Nodes.Single().Should().BeSameAs(node); A.CallTo(() => context.LogError(reader, aMessage)).MustHaveHappenedOnceExactly(); A.CallTo(() => context.LogError(reader, bMessage)).MustHaveHappenedOnceExactly(); }
public async Task UnconfiguredAttribute() { const string whenMessage = "Atributo não reconhecido: when"; const string whileMessage = "Atributo não reconhecido: while"; using var reader = Helper.CreateReader("<teste when=\"1\" while=\"0\" />"); var parentContext = new FakeParentParsingContext(); var context = A.Fake <IParsingContext>(i => i.Strict()); A.CallTo(() => context.LogError(reader, whenMessage)).DoesNothing(); A.CallTo(() => context.LogError(reader, whileMessage)).DoesNothing(); var settings = A.Dummy <IElementParserSettings>(); var sut = new ElementParser(); await sut.ParseAsync(reader, context, parentContext, settings); parentContext.When.Should().BeNull(); parentContext.While.Should().BeNull(); A.CallTo(() => context.LogError(reader, whenMessage)).MustHaveHappenedOnceExactly(); A.CallTo(() => context.LogError(reader, whileMessage)).MustHaveHappenedOnceExactly(); }