Skip to content

ExpressionTreeTestObjects

Zev Spitz edited this page Dec 19, 2019 · 12 revisions

NuGet TestObjects Status

As part of the development of this project, I needed a comprehensive set of expression trees to test the formatters against. This set of objects is available as a Nuget package.

Expression trees (and related objects) can be written in three ways:

  • The C# compiler
  • The VB compiler
  • Factory methods at System.Linq.Expressions.Expression

Each expression object is a field with the TestObject attribute applied to it.

The objects are available by referencing the NuGet package, and calling the static Get method:

// using ExpressionTreeTestObjects;

(string category, string source, string name, object o)[] lst = Objects.Get();

where source is one of the following:

Source Description
CSCompiler C# compiler
VBCompiler VB compiler
Factory methods Factory methods

and category is one of these values:

  • Binary
  • Blocks
  • Conditionals
  • Constants
  • DebugInfos
  • Defaults
  • Dynamics
  • Gotos
  • Indexer
  • Invocation
  • Labels
  • Lambda
  • Literal
  • Loops
  • Member access (+ closed variables)
  • Member bindings
  • Method call
  • New array
  • Object creation and initialization
  • Quoted
  • Runtime variables
  • Switch, CatchBlock
  • Try, Catch, Finally
  • Type check
  • Unary

The C# and VB compiler test expressions were added while I was working on the corresponding formatters. For the factory methods, I used reflection to build a list of all the methods and overloads at System.Linq.Expressions.Expression and created expressions by calling each one.

The result is pretty comprehensive, with one limitation: I didn't write test expressions for overloads which take an additional MethodInfo (such as this one. (See https://github.com/zspitz/ExpressionTreeToString/issues/20.)