Skip to main content

shape_ast/ast/
tests.rs

1//! Test framework types for Shape AST
2
3use serde::{Deserialize, Serialize};
4
5use super::expressions::Expr;
6use super::statements::Statement;
7use super::time::Timeframe;
8
9/// Test definition containing test cases and fixtures
10#[derive(Debug, Clone, Serialize, Deserialize)]
11pub struct TestDef {
12    /// Test suite name
13    pub name: String,
14    /// Optional setup code run before each test
15    pub setup: Option<Vec<Statement>>,
16    /// Optional teardown code run after each test
17    pub teardown: Option<Vec<Statement>>,
18    /// Test cases in this suite
19    pub test_cases: Vec<TestCase>,
20}
21
22/// Individual test case
23#[derive(Debug, Clone, Serialize, Deserialize)]
24pub struct TestCase {
25    /// Test description
26    pub description: String,
27    /// Optional tags for categorization
28    pub tags: Vec<String>,
29    /// Test body containing assertions
30    pub body: Vec<TestStatement>,
31}
32
33/// Statements that can appear in tests
34#[derive(Debug, Clone, Serialize, Deserialize)]
35pub enum TestStatement {
36    /// Regular statement
37    Statement(Statement),
38    /// Assertion
39    Assert(AssertStatement),
40    /// Expect-style assertion
41    Expect(ExpectStatement),
42    /// Should-style assertion
43    Should(ShouldStatement),
44    /// Test fixture
45    Fixture(TestFixture),
46}
47
48/// Assert statement for simple assertions
49#[derive(Debug, Clone, Serialize, Deserialize)]
50pub struct AssertStatement {
51    /// Condition to assert
52    pub condition: Expr,
53    /// Optional failure message
54    pub message: Option<String>,
55}
56
57/// Expect-style assertion (BDD style)
58#[derive(Debug, Clone, Serialize, Deserialize)]
59pub struct ExpectStatement {
60    /// Expression to test
61    pub actual: Expr,
62    /// Matcher to apply
63    pub matcher: ExpectationMatcher,
64}
65
66/// Expectation matchers for expect statements
67#[derive(Debug, Clone, Serialize, Deserialize)]
68pub enum ExpectationMatcher {
69    /// Expect value to be exactly equal
70    ToBe(Expr),
71    /// Expect value to equal (with deep equality)
72    ToEqual(Expr),
73    /// Expect numeric value to be close to target
74    ToBeCloseTo {
75        expected: Expr,
76        tolerance: Option<f64>,
77    },
78    /// Expect value to be greater than
79    ToBeGreaterThan(Expr),
80    /// Expect value to be less than
81    ToBeLessThan(Expr),
82    /// Expect collection to contain element
83    ToContain(Expr),
84    /// Expect value to be truthy
85    ToBeTruthy,
86    /// Expect value to be falsy
87    ToBeFalsy,
88    /// Expect function to throw error
89    ToThrow(Option<String>),
90    /// Expect rows to match pattern
91    ToMatchPattern {
92        pattern: String,
93        options: TestMatchOptions,
94    },
95}
96
97/// Options for pattern matching in tests
98#[derive(Debug, Clone, Serialize, Deserialize, Default)]
99pub struct TestMatchOptions {
100    /// Fuzzy matching tolerance
101    pub fuzzy: Option<f64>,
102    /// Timeframe for pattern matching
103    pub timeframe: Option<Timeframe>,
104    /// Symbol to test against
105    pub symbol: Option<String>,
106}
107
108/// Should-style assertion (natural language)
109#[derive(Debug, Clone, Serialize, Deserialize)]
110pub struct ShouldStatement {
111    /// Expression being tested
112    pub subject: Expr,
113    /// Matcher to apply
114    pub matcher: ShouldMatcher,
115}
116
117/// Matchers for should statements
118#[derive(Debug, Clone, Serialize, Deserialize)]
119pub enum ShouldMatcher {
120    /// Should be equal to value
121    Be(Expr),
122    /// Should equal value
123    Equal(Expr),
124    /// Should contain element
125    Contain(Expr),
126    /// Should match pattern
127    Match(String),
128    /// Should be close to value
129    BeCloseTo {
130        expected: Expr,
131        tolerance: Option<f64>,
132    },
133}
134
135/// Test fixtures for setting up test data
136#[derive(Debug, Clone, Serialize, Deserialize)]
137pub enum TestFixture {
138    /// Provide test data (generic data rows)
139    WithData { data: Expr, body: Vec<Statement> },
140    /// Mock a function or indicator
141    WithMock {
142        target: String,
143        mock_value: Option<Expr>,
144        body: Vec<Statement>,
145    },
146}