blob: 2a3a16b5317e0e4a2e73bb00dd0af882be8fc6d1 [file]
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
using System.Collections.Generic;
using System.Linq;
using System;
using Newtonsoft.Json;
using Org.Apache.REEF.Client.API.Testing;
namespace Org.Apache.REEF.Client.Local.TestRunner.FileWritingAssert
{
internal sealed class TestResult : ITestResult, IEquatable<TestResult>
{
private readonly IList<AssertResult> _results;
public TestResult(IEnumerable<AssertResult> results)
{
_results = results.ToList();
}
public TestResult() : this(new List<AssertResult>())
{
// Intentionally empty
}
/// <inheritdoc />
public int NumberOfFailedAsserts
{
get
{
return _results.Count(_ => _.IsFalse);
}
}
/// <inheritdoc />
public int NumberOfPassedAsserts
{
get
{
return _results.Count(_ => _.IsTrue);
}
}
/// <inheritdoc />
public bool AllTestsSucceeded
{
get
{
return NumberOfFailedAsserts == 0;
}
}
private IEnumerable<AssertResult> FailedAsserts
{
get
{
return _results.Where(_ => _.IsFalse);
}
}
private IEnumerable<AssertResult> PassedAsserts
{
get
{
return _results.Where(_ => _.IsTrue);
}
}
/// <inheritdoc />
public string FailedTestMessage
{
get { return "Failed tests: " + string.Join(";", FailedAsserts.Select(_ => _.Message)); }
}
/// <summary>
/// Add a Assert result to the collection.
/// </summary>
/// <param name="condition">Whether or not the condition was met.</param>
/// <param name="format">The message of the assert.</param>
/// <param name="args">Parameters to `format`.</param>
/// <returns>this, for chain calls</returns>
public TestResult Add(bool condition, string format, params object[] args)
{
_results.Add(new AssertResult(format, condition));
return this;
}
/// <summary>
/// Record an assert that passed.
/// </summary>
/// <param name="format">The message to record</param>
/// <param name="args">Parameters for the format string.</param>
/// <returns>this, for chain calls</returns>
public TestResult IsTrue(string format, params object[] args)
{
return Add(true, format, args);
}
/// <summary>
/// Record an assert that failed.
/// </summary>
/// <param name="format">The message to record</param>
/// <param name="args">Parameters for the format string.</param>
/// <returns>this, for chain calls</returns>
public TestResult IsFalse(string format, params object[] args)
{
return Add(false, format, args);
}
/// <summary>
/// Serializes the data contained in this object to JSON.
/// </summary>
/// <returns>A string version of this object.</returns>
public string ToJson()
{
return JsonConvert.SerializeObject(_results);
}
/// <summary>
/// Deserializes an instance from a string generated by ToJson().
/// </summary>
/// <param name="serializedObject">The object to deserialize.</param>
/// <returns>The deserialized object or null if serializedObject is null or whitespace.</returns>
public static TestResult FromJson(string serializedObject)
{
if (string.IsNullOrWhiteSpace(serializedObject))
{
return null;
}
return new TestResult(JsonConvert.DeserializeObject<List<AssertResult>>(serializedObject));
}
/// <summary>
/// Creates a TestResult with a single failure inside.
/// </summary>
/// <param name="format">The message for the failure.</param>
/// <param name="args">Parameters, if `format` refers to them.</param>
/// <returns>A TestResult with a single failure inside.</returns>
public static TestResult Fail(string format, params object[] args)
{
return new TestResult().IsFalse(format, args);
}
public override bool Equals(object obj)
{
return Equals(obj as TestResult);
}
public bool Equals(TestResult other)
{
return other != null &&
EqualityComparer<IList<AssertResult>>.Default.Equals(_results, other._results);
}
public override int GetHashCode()
{
return -3177284 + EqualityComparer<IList<AssertResult>>.Default.GetHashCode(_results);
}
}
}