xunit.TypeTool

Facilitate working with arbitrary types.

Currently supports a Python-like repr() method. Better output for built-in container types is the primary goal.

Some built-in types (e.g., Map, Set), do not have good string representations when showing up in error messages. While user classes can provide a toString() method, sometimes they may not be available. To help with this situation, this class provides a registration system to extend support.

The addReprFunc method can allow users to register their own.

Migrating equality testing out of TestCase is a future goal.

It was originally designed for testing, but there can be use cases such as human readable logging as well.

TypeTool

To Do:

Methods

addReprFunc(type, func)

Parameters:
Name Type Description
type string

Type of interest.

func module:xunit.TypeTool~ReprFunc

Function for this type.

getReprFunc(item) → {module:xunit.TypeTool~ReprFunc}

Find a ReprFunc for the given item.

Parameters:
Name Type Description
item object

Item of interest.

Returns:
module:xunit.TypeTool~ReprFunc -

Function for this item.

getType(item) → {string}

Attempt to get the type of item.

This is NOT equivalent to the built-in typeof operator. Hence, results are explicitly NOT lower-cased in order to reduce chances of conflicts.

Parameters:
Name Type Description
item object

Item to inspect.

Returns:
string -

The likely type of item.

repr(item) → {string}

Returns a string representation of the item using the registration system.

Parameters:
Name Type Description
item object

Anything.

Returns:
string -

String version of item.

reprArray(array) → {string}

Parameters:
Name Type Description
array Array.<object>

Array of anything.

Returns:
string -

String version of item.

Implements:

reprMap(map) → {string}

Parameters:
Name Type Description
map Map.<object, object>

Any Map.

Returns:
string -

String version of map.

Implements:

reprObject(obj) → {string}

Parameters:
Name Type Description
obj object

Any object.

Returns:
string -

String version of obj.

Implements:

reprSet(set) → {string}

Parameters:
Name Type Description
set Set.<object>

Any Set.

Returns:
string -

String version of set.

Implements:

reprString(item) → {string}

Parameters:
Name Type Description
item string

String to wrap.

Returns:
string -

Wrapped version of item.

Implements:

Type Definitions

ReprFunc(item) → {string}

Parameters:
Name Type Description
item object

Anything.

Returns:
string -

String version of item.