.NET API

Installing

The Coco Platform API for .NET is distributed as a NuGet package and is available for download from a custom NuGet repository. To use the Coco Platform API for .NET, add a custom package source with the following properties set:

  • Source: https://dl.cocotec.io/cp/nuget/stable/index.json

Microsoft provide guidence on how to add custom package sources for a variety of tools here. Having added the package source, search for “CocoPlatform” in order to find and install the Coco Platform API.

Note

Whilst the Coco Platform is still in beta, you will need to tick the Include preleases? box in order to see the latest version.

Getting Started

The following section shows how to build a simple example program that loads a Coco file and verifies if the file is correct. In addition to this, there is also documentation available on general advice on using the API.

The first step is to create the APIClient and to start the server. The server runs in the background locally on the user’s machine and handles requests:

APIClient client = new APIClient();
client.StartServer();

Before using any of API methods, we check to see if the user has a valid license, terminating gracefully if not:

UserInfo userInfo = new UserInfo(client);
LicenseResponse licenseResponse = userInfo.HasLicense(new List<ProductFeature> { ProductFeature.CocoPlatformBase });
if (!licenseResponse.Licensed)
{
  Console.WriteLine($"Could not acquire license: {licenseResponse.NonLicensedReason}");
  return;
}

Before loading the file, we create a StandaloneContext and add various loggers to it that print to the Console (see the downloadable example for implementations of these classes):

StandaloneContext context = new StandaloneContext(client);
CocoDiagnosticLogger diagnosticLogger = new CocoDiagnosticLogger(client);
VerificationLogger verificationLogger = new VerificationLogger(client);
context.AddDiagnosticsConsumer(diagnosticLogger);
context.AddVerificationProgressListener(verificationLogger);

In order to load the file, we have to add the folder that contains the file as an import path. For the purposes of this example, we assume that there is a file called TestFile.coco stored on the user’s desktop:

context.AddImportPath(Environment.GetFolderPath(Environment.SpecialFolder.Desktop));
Node moduleNode = context.LoadModule("TestFile.coco", ModulePathType.RelativeFilePath);
if (moduleNode == null)
{
  Console.WriteLine($"Could not load TestFile.coco");
  return;
}

Assuming that the load took place successfully, we can then execute all assertions as follows:

context.RootAssertion().Run();

This will print logging information to the console whilst the checks are executing.

The complete example is appended below:

using Cocotec.CocoPlatform;
using Cocotec.CocoPlatform.Coco;
using System;
using System.Collections.Generic;

namespace CocoExample
{
  class Program
  {
    static void Main(string[] args)
    {
      APIClient client = new APIClient();
      client.StartServer();

      UserInfo userInfo = new UserInfo(client);
      LicenseResponse licenseResponse = userInfo.HasLicense(new List<ProductFeature> { ProductFeature.CocoPlatformBase });
      if (!licenseResponse.Licensed)
      {
        Console.WriteLine($"Could not acquire license: {licenseResponse.NonLicensedReason}");
        return;
      }

      StandaloneContext context = new StandaloneContext(client);
      CocoDiagnosticLogger diagnosticLogger = new CocoDiagnosticLogger(client);
      VerificationLogger verificationLogger = new VerificationLogger(client);
      context.AddDiagnosticsConsumer(diagnosticLogger);
      context.AddVerificationProgressListener(verificationLogger);

      context.AddImportPath(Environment.GetFolderPath(Environment.SpecialFolder.Desktop));
      Node moduleNode = context.LoadModule("TestFile.coco", ModulePathType.RelativeFilePath);
      if (moduleNode == null)
      {
        Console.WriteLine($"Could not load TestFile.coco");
        return;
      }

      context.RootAssertion().Run();
    }
  }

  public class CocoDiagnosticLogger : DiagnosticConsumer
  {
    public CocoDiagnosticLogger(APIClient client) : base(client) { }

    public override void HandleDiagnostic(Diagnostic diagnostic)
    {
      Console.WriteLine(diagnostic.Message);
    }
  }

  public class VerificationLogger : VerificationProgressListener
  {
    public VerificationLogger(APIClient client) : base(client) { }

    private void Log(VerificationAssertion assertion, string message)
    {
      Console.WriteLine($"[{assertion.Name()}]: {message}");
    }
    public override void VerificationStarted(VerificationAssertion assertion)
    {
      Log(assertion, "started");
    }
    public override void VerificationLogMessage(VerificationAssertion assertion, string message)
    {
      Log(assertion, message);
    }
    public override void VerificationStatusUpdated(VerificationAssertion assertion) { }
    public override void VerificationStatisticsUpdated(VerificationAssertion assertion)
    {
      Log(assertion, "statistics updated");
    }
    public override void VerificationFinished(VerificationAssertion assertion)
    {
      Log(assertion, "finished");
    }
    public override void VerificationFinishedAll() { }
  }
}