thinlinelib/
synthesis.rs

1use entity::Entity;
2use language_type::LanguageType;
3use std::marker::PhantomData;
4
5////////////////////////////////////////////////////////////////////////////////
6
7static STUB_ID_SET_UP_CONTEXT: &str = "#SET_UP";
8static STUB_ID_TEAR_DOWN_CONTEXT: &str = "#TEAR_DOWN";
9static STUB_ID_CONSTRUCTOR_CONTEXT: &str = "#CONSTRUCTOR";
10static STUB_ID_DESTRUCTOR_CONTEXT: &str = "#DESTRUCTOR";
11static STUB_ID_CLASS_CONTEXT: &str = "#CLASS_CONTEXT";
12
13type StubContext = String;
14
15pub trait StubContextConversion {
16    fn convert(stub_context_type: &StubContextType) -> Option<&StubContext>;
17}
18
19impl StubContextConversion for StubContext {
20    fn convert(stub_contex_type: &StubContextType) -> Option<&StubContext> {
21        match stub_contex_type {
22            StubContextType::SetUpContext(stub_context)
23            | StubContextType::TearDownContext(stub_context)
24            | StubContextType::ConstructorContext(stub_context)
25            | StubContextType::DestructorContext(stub_context)
26            | StubContextType::ClassContext(stub_context) => Some(stub_context),
27        }
28    }
29}
30
31#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
32pub enum StubContextType {
33    SetUpContext(StubContext),
34    TearDownContext(StubContext),
35    ConstructorContext(StubContext),
36    DestructorContext(StubContext),
37    ClassContext(StubContext),
38}
39
40////////////////////////////////////////////////////////////////////////////////
41
42#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
43pub struct TestFunction {
44    pub name: String,
45}
46
47////////////////////////////////////////////////////////////////////////////////
48
49/// The representation of a TestClass containing different
50/// contexts for con-/destructor, setUp, tearDown methods
51/// and so on and a vector of test functions.
52#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
53pub struct TestClass {
54    pub stub_context: Vec<StubContextType>,
55    pub test_functions: Vec<TestFunction>,
56}
57
58impl TestClass {
59    /// Creates a new StubContext instance.
60    ///
61    /// # Example
62    ///
63    /// ```
64    /// use thinlinelib::synthesis::{TestClass};
65    ///
66    /// let mut test_class = TestClass::new();
67    ///
68    /// assert!(test_class.stub_context.is_empty());
69    /// assert!(test_class.test_functions.is_empty());
70    /// ```
71    pub fn new() -> Self {
72        Self {
73            stub_context: Vec::new(),
74            test_functions: Vec::new(),
75        }
76    }
77
78    /// Adds a StubContext to the TestClass instance.
79    ///
80    /// With setting stub contexts to certain values, it is ensured, that these
81    /// contexts (e.g. test class constructor or setUp function) are replaced
82    /// with the values to generate specific test classes.
83    ///
84    /// # Example
85    ///
86    /// ```
87    /// use thinlinelib::synthesis::{StubContextType, TestClass};
88    ///
89    /// let mut test_class = TestClass::new();
90    /// let stub_context_type = StubContextType::SetUpContext(String::from("setup = new Setup();"));
91    /// test_class.add_stub_context(stub_context_type);
92    ///
93    /// assert_eq!(test_class.stub_context.len(), 1);
94    /// ```
95    pub fn add_stub_context(&mut self, context: StubContextType) -> Option<&StubContext> {
96        self.stub_context.push(context);
97        if let Some(stub_context) = self.stub_context.last() {
98            return StubContext::convert(stub_context);
99        }
100
101        None
102    }
103}
104
105////////////////////////////////////////////////////////////////////////////////
106
107#[derive(Default, Debug)]
108pub struct TestFile<T> {
109    pub pf_type: PhantomData<T>,
110    pub entities: Vec<Entity>,
111}
112
113/// Represents a test file.
114impl<T> TestFile<T> where T: LanguageType {}
115
116////////////////////////////////////////////////////////////////////////////////
117
118#[derive(Default, Debug)]
119pub struct Synthesis {}