From 19ff4669ac39f6bb1aac738fccbd605cb3e32629 Mon Sep 17 00:00:00 2001 From: Michael Miller Date: Thu, 6 Jun 2024 20:13:55 -0600 Subject: [PATCH] Some initial work on examples and groups --- src/core/example.cr | 29 +++++++++++++++++++++++++++++ src/core/example_group.cr | 26 ++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 src/core/example.cr create mode 100644 src/core/example_group.cr diff --git a/src/core/example.cr b/src/core/example.cr new file mode 100644 index 0000000..9eb45da --- /dev/null +++ b/src/core/example.cr @@ -0,0 +1,29 @@ +module Spectator + # Information about a test case and functionality for running it. + class Example + # Name of the example. + # This may be nil if the example does not have a name. + # In that case, the description of the first matcher executed in the example should be used. + property! name : String + + # Creates a new example. + # The *name* may be nil if the example has no name. + def initialize(@name = nil, &@block : Example -> Nil) + end + + # Runs the example. + def run + @block.call(self) + end + + # Constructs a string representation of the example. + # The name will be used if it is set, otherwise the example will be anonymous. + def to_s(io : IO) : Nil + if (name = @name) + io << name + else + io << "" + end + end + end +end diff --git a/src/core/example_group.cr b/src/core/example_group.cr new file mode 100644 index 0000000..6fcc0f8 --- /dev/null +++ b/src/core/example_group.cr @@ -0,0 +1,26 @@ +module Spectator + # Information about a group of examples and functionality for running them. + # The group can be nested. + class ExampleGroup + # Name of the group. + # This may be nil if the group does not have a name. + property! name : String + + getter examples = [] of Example + + # Creates a new group. + # The *name* may be nil if the group has no name. + def initialize(@name = nil) + end + + # Constructs a string representation of the group. + # The name will be used if it is set, otherwise the group will be anonymous. + def to_s(io : IO) : Nil + if (name = @name) + io << name + else + io << "" + end + end + end +end