View Source Noizu.ElixirCore.PartialObjectCheck (noizu_core v1.0.28)
The Noizu.ElixirCore.PartialObjectCheck module provides a way to
define partial object constraints and perform assertions against
actual objects. It allows for specifying field presence, types, and values,
enabling granular control over object validation during testing.
The module consists of the main PartialObjectCheck struct and its associated helper structs: FieldConstraint, TypeConstraint, and ValueConstraint. These structs work together to define the expected shape and constraints of an object for asserting purposes.
Link to this section Summary
Functions
Checks an actual object against the prepared partial object constraints.
The check/2 function takes a prepared PartialObjectCheck struct and an
actual object (subject under test) and compares the object against the defined
constraints. It checks field presence, field types, and field values based on
the specified constraints and returns the result of the assertion.
The returned PartialObjectCheck struct will have its assert field set to :met
if all constraints are satisfied, or :unmet if any constraint fails.
Prepares a partial object definition by specifying the expected shape and constraints. The prepare/1 function takes a map or struct representing the partial object definition and recursively prepares the field constraints, type constraints, and value constraints. It returns a PartialObjectCheck struct that encapsulates the defined constraints.
Link to this section Types
@type t() :: %Noizu.ElixirCore.PartialObjectCheck{ assert: :pending | :met | :unmet | :not_applicable, field_constraints: %{ required(any()) => Noizu.ElixirCore.PartialObjectCheck.FieldConstraint.t() }, type_constraint: Noizu.ElixirCore.PartialObjectCheck.TypeConstraint.t() }
Link to this section Functions
Checks an actual object against the prepared partial object constraints.
The check/2 function takes a prepared PartialObjectCheck struct and an
actual object (subject under test) and compares the object against the defined
constraints. It checks field presence, field types, and field values based on
the specified constraints and returns the result of the assertion.
The returned PartialObjectCheck struct will have its assert field set to :met
if all constraints are satisfied, or :unmet if any constraint fails.
example
Example
iex> poc = Noizu.ElixirCore.PartialObjectCheck.prepare(%{a: 1, b: %{c: "hello"}}) iex> o = Noizu.ElixirCore.PartialObjectCheck.check(poc, %{a: 1, b: %{c: "hello"}}) %Noizu.ElixirCore.PartialObjectCheck{ assert: :met, } = o
iex> poc = Noizu.ElixirCore.PartialObjectCheck.prepare(%{a: 1, b: %{c: "hello"}}) iex> o = Noizu.ElixirCore.PartialObjectCheck.check(poc, %{a: 2, b: %{c: "world"}}) %Noizu.ElixirCore.PartialObjectCheck{ assert: :unmet, } = o
Prepares a partial object definition by specifying the expected shape and constraints. The prepare/1 function takes a map or struct representing the partial object definition and recursively prepares the field constraints, type constraints, and value constraints. It returns a PartialObjectCheck struct that encapsulates the defined constraints.
examples
Examples
iex> poc = Noizu.ElixirCore.PartialObjectCheck.prepare(%{a: 1, b: %{c: "hello"}}) %Noizu.ElixirCore.PartialObjectCheck{ field_constraints: %{ a: %Noizu.ElixirCore.PartialObjectCheck.FieldConstraint{
required: true,
type_constraint: %Noizu.ElixirCore.PartialObjectCheck.TypeConstraint{
constraint: {:basic, :integer}
},
value_constraint: %Noizu.ElixirCore.PartialObjectCheck.ValueConstraint{
constraint: {:value, 1}
}}, b: %Noizu.ElixirCore.PartialObjectCheck.FieldConstraint{
required: true,
type_constraint: %Noizu.ElixirCore.PartialObjectCheck.TypeConstraint{
constraint: {:basic, :map}
},
value_constraint: %Noizu.ElixirCore.PartialObjectCheck.ValueConstraint{
constraint: %Noizu.ElixirCore.PartialObjectCheck{
field_constraints: %{
c: %Noizu.ElixirCore.PartialObjectCheck.FieldConstraint{
required: true,
type_constraint: %Noizu.ElixirCore.PartialObjectCheck.TypeConstraint{
constraint: {:basic, :bitstring},
},
value_constraint: %Noizu.ElixirCore.PartialObjectCheck.ValueConstraint{
constraint: {:value, "hello"}
}
}
},
type_constraint: %Noizu.ElixirCore.PartialObjectCheck.TypeConstraint{
constraint: {:basic, :map}
}
}
}} }, type_constraint: %Noizu.ElixirCore.PartialObjectCheck.TypeConstraint{ constraint: {:basic, :map} } }