std.unittest Package

Function Description

The Unittest library is used to compile unit test code for the Cangjie project. It provides basic functions including code writing, running, and debugging, and some advanced functions for experienced users. The Cangjie unit test supports the CJC compiler (single-package compilation mode) and CJPM package manager (multi-package mode).

You can write the first unit test program following the Quick Start Guide. The document describes some basic concepts and usage and provides sample code. It also describes some advanced features, such as parameterized tests.

The following APIs are re-exported from other packages. You may import unittest alone.

APIs re-exported from the unittest.common package:

Interface

InterfaceDescription
DataProviderSpecifies a component of DataStrategy, which is used to provide test data. T specifies the data type provided by the provider.
DataShrinkerSpecifies a component of DataStrategy, which is used to reduce data during testing. T specifies the type of data processed by the shrinker.
DataStrategySpecifies a strategy that provides data for parameterized tests. T specifies the data type that the strategy operates on.

Class

ClassDescription
ConfigurationSpecifies an object for storing the unittest configuration data generated by the @Configure macro. Configuration is a class similar to HashMap, but its key is not of a key or value type but of a String type and its value is of any given type.
ConfigurationKeySpecifies a key value of a configuration item. Provides the equality comparison and hashCode methods.

APIs re-exported from the unittest.prop_test package

Function

FunctionDescription
random<T>()Generates random data of type T. T must implement the Arbitrary<T> interface. The returned value of this function is a parameter source for parameterized tests.

Interface

InterfaceDescription
ArbitraryGenerates a random value of type T.
ShrinkShrinks a T-type value to multiple "smaller" values.

API List

Function

NameDescription
assertCaughtUnexpectedE(String,String,String)Records information and throws an exception if a caught exception does not meet the expectation.
assertEqual<T>(String, String, T, T): Unit where T <: Equatable<T>Checks whether the expected and actual values are equal. Throws an exception if they are not.
csv<T>(String, Rune, Rune, Rune, Option<Rune>, Option<Array<String>>, Array<UInt64>, Array<UInt64>, Bool)Reads the data value of type T from a CSV file, where T must be serializable. The returned value is a parameter source of the parameterized tests.
defaultConfiguration()Generates default configuration information.
entryMain(TestPackage)Specifies an entry function for the framework to execute test cases, for use by cjc --test.
expectCaughtUnexpectedE(String,String,String)Records information without throwing any exception if a caught exception does not meet the expectation.
expectEqual<T>(String, String, T, T): Unit where T <: Equatable<T>Checks whether the expected and actual values are equal. Records the comparison result without throwing any exception.
fail(String)Makes the case fail and throws an exception.
failExpect(String)Makes the case fail and records the information without throwing any exception.
json<T>(String)Reads the data value of type T from a JSON file. T must be serializable. The returned value of this function is a parameter source of the parameterized test.
tsv<T>(String, Rune, Rune, Option<Rune>, Option<Array<String>>, Array<UInt64>, Array<UInt64>, Bool)Reads the data value of type T from the TSV file. T must be serializable. The returned value of this function is a parameter source of the parameterized test.

Interface

NameDescription
BenchInputProviderSpecifies an interface used to handle performance testing. It requires executing specific code before each invocation of the performance test. When the input for the performance test changes, the input data must be regenerated from scratch each time.
BenchmarkConfigSpecifies an empty interface that differentiates certain Configuration functions as performance-related configurations.
BenchmarkInputMarkerDetects BenchInputProvider<T> when T is not known.
GeneratorGenerates a value of type T.
MeasurementSpecifies an interface for collecting and analyzing various data during performance testing. The specific instance used by the performance test framework is specified by the measurement parameter of the @Configure macro.
TestClassProvides the method for creating a TestSuite.

Class

NameDescription
BenchmarkProvides methods for creating and running individual performance test cases.
BenchReportProvides capabilities for handling performance test case execution result reports.
CartesianProductProcessorSpecifies the Cartesian product processor.
ConsoleReporterPrints the unit test case results or performance test case results to the console.
CsvReporterPrints the performance test case results to a CSV file.
CsvRawReporterPrints the performance test case results, which contain only a batch of raw measurement data, to a CSV file.
CsvStrategyIndicates serialization implementation by DataStrategy on the CSV data format.
DataStrategyProcessorSpecifies a base class for all DataStrategy components. Instances of this class are created by the @Strategy macro or member function.
FlatMapProcessorSpecifies a processor for handling parameter data with FlatMap.
FlatMapStrategyProcessorSpecifies a processor for handling parameter data with FlatMap.
InputParameterSpecifies an input parameter object type.
JsonStrategyIndicates serialization implementation by DataStrategy on the JSON data format.
LazyCyclicNodeSpecifies an internal lazy iterator used to advance type erasure one after another in a loop.
MapProcessorProcessor for handling parameter data with Map.
PowerAssertDiagramBuilderSpecifies a builder of the PowerAssert output result.
RandomDataProviderIndicates DataProvider implementation generated using random data.
RandomDataShrinkerIndicates DataShrinker implementation generated using random data.
RandomDataStrategyIndicates DataStrategy implementation generated using random data.
RawStatsReporterSpecifiesa reporter for raw performance test data. It is used only within the framework.
ReportSpecifies a base class for printing test case result reports.
SerializableProviderIndicates DataProvider implementation for obtaining serialized data.
SimpleProcessorSpecifies a simple data strategy processor. Implementation of DataStrategyProcessor.
TestGroupProvides methods for building and running test groups.
TestGroupBuilderProvides methods for configuring test groups.
TestPackageSpecifies the test case package object.
TestReportProvides the unit test result report.
TestSuiteProvides methods for building and executing test suites.
TestSuiteBuilderProvides methods for configuring test suites.
UnitTestCaseProvides methods for creating and executing unit test cases.
XmlReporterPrints the unit test case result data to an XML file.

Enumeration

NameDescription
ExplicitGcTypeSpecifies explicitGC configuration parameters for the @Configure macro. Indicates three GC execution methods.
TimeUnitSpecifies a time unit that can be used in the TimeNow class constructor.

Struct

NameDescription
BatchInputProviderSpecifies an input provider that generates the input for the entire benchmark batch in the buffer before execution.
BatchSizeOneInputProviderSpecifies a benchmark input provider that generates input before each benchmark execution.
GenerateEachInputProviderSpecifies a benchmark input provider that generates input before each benchmark execution.
ImmutableInputProviderSpecifies the simplest input provider that merely copies data for each benchmark call. It is applicable to the scenario where the benchmark test does not change the input. This is the default provider used within the framework.
TimeNowIndicates implementation of Measurement, used to measure the time taken to execute a function.

Exception Class

NameDescription
AssertExceptionSpecifies an exception thrown when an @Expect / @Assert check fails.
AssertIntermediateExceptionSpecifies an exception thrown when a @PowerAssert check fails.
UnittestCliOptionsFormatExceptionSpecifies an exception thrown when there is a format error in the console options.
UnittestExceptionSpecifies a general exception for the framework.