public void AnonymousType() { var list = new[] { new { FirstName = "Jon", Surname = "Skeet" }, new { FirstName = "Holly", Surname = "Skeet" } }.ToList(); // We can't cast to IEnumerable<T> as we can't express T. var sequence = list.AsEnumerable(); // This will now use Enumerable.Contains instead of List.Contains Assert.IsFalse(sequence.Contains(new { FirstName = "Tom", Surname = "Skeet" })); }
public void FormatMessage() { const string Pattern = "{name} has {messages, plural, 123}."; const string Expected = "Jeff has 123 messages."; var args = new Dictionary<string, object> { { "name", "Jeff" }, { "messages", 1 } }; var requests = new[] { new FormatterRequest( new Literal(0, 5, 1, 7, new StringBuilder("name")), "name", null, null), new FormatterRequest( new Literal(11, 33, 1, 7, new StringBuilder("messages, plural, 123")), "messages", "plural", " 123") }; this.formatterMock1.Setup(x => x.Format("en", requests[0], args, "Jeff", this.subject)).Returns("Jeff"); this.formatterMock2.Setup(x => x.Format("en", requests[1], args, 1, this.subject)).Returns("123 messages"); this.collectionMock.Setup(x => x.GetEnumerator()).Returns(requests.AsEnumerable().GetEnumerator()); this.libraryMock.Setup(x => x.GetFormatter(requests[0])).Returns(this.formatterMock1.Object); this.libraryMock.Setup(x => x.GetFormatter(requests[1])).Returns(this.formatterMock2.Object); this.patternParserMock.Setup(x => x.Parse(It.IsAny<StringBuilder>())).Returns(this.collectionMock.Object); // First request, and "name" is 4 chars. this.collectionMock.Setup(x => x.ShiftIndices(0, 4)).Callback( // The '- 2' is also done in the used implementation. (int index, int length) => requests[1].SourceLiteral.ShiftIndices(length - 2, requests[0].SourceLiteral)); var actual = this.subject.FormatMessage(Pattern, args); this.collectionMock.Verify(x => x.ShiftIndices(0, 4), Times.Once); this.libraryMock.VerifyAll(); this.formatterMock1.VerifyAll(); this.formatterMock2.VerifyAll(); this.patternParserMock.VerifyAll(); Assert.Equal(Expected, actual); }
public void VerifyFormatMessageThrowsWhenVariablesAreMissing() { const string Pattern = "{name} has {messages, plural, 123}."; // Note the missing "name" variable. var args = new Dictionary<string, object> { { "messages", 1 } }; var requests = new[] { new FormatterRequest( new Literal(0, 5, 1, 7, new StringBuilder("name")), "name", null, null), new FormatterRequest( new Literal(11, 33, 1, 7, new StringBuilder("messages, plural, 123")), "messages", "plural", " 123") }; this.collectionMock.Setup(x => x.GetEnumerator()).Returns(() => requests.AsEnumerable().GetEnumerator()); this.patternParserMock.Setup(x => x.Parse(It.IsAny<StringBuilder>())).Returns(this.collectionMock.Object); // First request, and "name" is 4 chars. this.collectionMock.Setup(x => x.ShiftIndices(0, 4)).Callback( // The '- 2' is also done in the used implementation. (int index, int length) => requests[1].SourceLiteral.ShiftIndices(length - 2, requests[0].SourceLiteral)); var ex = Assert.Throws<VariableNotFoundException>(() => this.subject.FormatMessage(Pattern, args)); Assert.Equal("name", ex.MissingVariable); }