/// <summary> /// Adds a requirement that a given property should exist. /// </summary> /// <param name="name">The name of the property.</param> /// <param name="elementMatcherBuilder">The additional requirements on the property value.</param> /// <returns>The builder, with the requirement.</returns> public JsonObjectMatcherBuilder WithProperty ( string name, Action <JsonElementMatcherBuilder>?elementMatcherBuilder = null ) { _matchers.Add ( obj => { if (!obj.TryGetProperty(name, out var property)) { throw new Xunit.Sdk.ContainsException(name, obj); } if (elementMatcherBuilder is null) { return(true); } var matcherBuilder = new JsonElementMatcherBuilder(); elementMatcherBuilder(matcherBuilder); var valueMatches = matcherBuilder.Build().Matches(property); if (!valueMatches) { throw new Xunit.Sdk.TrueException($"The value of \"{name}\" did not match.", valueMatches); } return(valueMatches); } ); return(this); }
/// <summary> /// Adds a requirement that a single element matches the given element builder. /// </summary> /// <param name="elementMatcherBuilder">The element matcher.</param> /// <returns>The builder, with the added requirement.</returns> public JsonArrayMatcherBuilder WithSingleElement(Action <JsonElementMatcherBuilder>?elementMatcherBuilder = null) { var elementMatcher = new JsonElementMatcherBuilder(); elementMatcherBuilder?.Invoke(elementMatcher); var matcher = elementMatcher.Build(); _matchers.Add(j => j.Count(matcher.Matches) == 1); return(this); }
/// <summary> /// Adds a requirement that an element at the given index matches the given element builder. /// </summary> /// <param name="index">The index of the element.</param> /// <param name="elementMatcherBuilder">The element matcher.</param> /// <returns>The builder, with the added requirement.</returns> public JsonArrayMatcherBuilder WithElement ( int index, Action <JsonElementMatcherBuilder>?elementMatcherBuilder = null ) { var elementMatcher = new JsonElementMatcherBuilder(); elementMatcherBuilder?.Invoke(elementMatcher); var matcher = elementMatcher.Build(); _matchers.Add(j => j.Count() > index && matcher.Matches(j.ElementAt(index))); return(this); }