fluent-test 0.4.3

A fluent, Jest-like testing library for Rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
use std::fmt::{self, Display, Formatter};

/// Represents a complete sentence structure for an assertion
#[derive(Debug, Clone)]
pub struct AssertionSentence {
    /// The subject of the assertion (usually the variable name)
    pub subject: String,
    /// The verb of the assertion (e.g., "be", "have", "contain")
    pub verb: String,
    /// The object of the assertion (e.g., "greater than 42", "of length 5", "'test'")
    pub object: String,
    /// Optional qualifiers for the assertion (e.g., "within tolerance", "when rounded")
    pub qualifiers: Vec<String>,
    /// Whether the assertion is negated (e.g., "not be", "does not have")
    pub negated: bool,
}

impl AssertionSentence {
    /// Create a new assertion sentence
    pub fn new(verb: impl Into<String>, object: impl Into<String>) -> Self {
        return Self { subject: "".to_string(), verb: verb.into(), object: object.into(), qualifiers: Vec::new(), negated: false };
    }

    /// Set whether the assertion is negated
    pub fn with_negation(mut self, negated: bool) -> Self {
        self.negated = negated;
        return self;
    }

    /// Add a qualifier to the assertion
    pub fn with_qualifier(mut self, qualifier: impl Into<String>) -> Self {
        self.qualifiers.push(qualifier.into());
        return self;
    }

    /// Format the sentence into a readable string (raw format, without subject)
    pub fn format(&self) -> String {
        let mut result = if self.negated { format!("not {} {}", self.verb, self.object) } else { format!("{} {}", self.verb, self.object) };

        if !self.qualifiers.is_empty() {
            result.push(' ');
            result.push_str(&self.qualifiers.join(" "));
        }

        return result;
    }

    /// Format the sentence with grammatically correct 'not' placement (after the verb)
    /// This is used for display purposes where improved grammar is desired
    pub fn format_grammatical(&self) -> String {
        let mut result = if self.negated {
            // Place "not" after the verb for grammatical correctness
            format!("{} not {}", self.verb, self.object)
        } else {
            format!("{} {}", self.verb, self.object)
        };

        if !self.qualifiers.is_empty() {
            result.push(' ');
            result.push_str(&self.qualifiers.join(" "));
        }

        return result;
    }

    /// Format the sentence with the correct verb conjugation based on the subject
    pub fn format_with_conjugation(&self, subject: &str) -> String {
        // Determine if the subject is plural
        let is_plural = Self::is_plural_subject(subject);

        // Convert the infinitive verb to the correct form based on plurality
        let conjugated_verb = self.conjugate_verb(is_plural);

        let mut result = if self.negated {
            // Place "not" after the conjugated verb for grammatical correctness
            format!("{} not {}", conjugated_verb, self.object)
        } else {
            format!("{} {}", conjugated_verb, self.object)
        };

        if !self.qualifiers.is_empty() {
            result.push(' ');
            result.push_str(&self.qualifiers.join(" "));
        }

        return result;
    }

    /// Determine if a subject name is likely plural
    fn is_plural_subject(subject: &str) -> bool {
        // Extract the base variable name from expressions like "var.method()" or "&var"
        let base_name = Self::extract_base_name(subject);

        // Common plural endings in English
        let plural_endings = ["s", "es", "ies"];

        // Check for common plural variable naming patterns
        let is_plural_ending = plural_endings.iter().any(|ending| base_name.ends_with(ending));

        // Also check for commonly used pluralized variable names in programming
        let common_plurals = [
            "items",
            "elements",
            "values",
            "arrays",
            "lists",
            "maps",
            "sets",
            "objects",
            "attributes",
            "properties",
            "entries",
            "keys",
            "numbers",
            "strings",
            "data",
            "results",
            "options",
            "errors",
            "children",
        ];

        let is_common_plural = common_plurals.contains(&base_name.to_lowercase().as_str());

        // Return true if either condition is met
        return is_plural_ending || is_common_plural;
    }

    /// Extract the base variable name from expressions
    fn extract_base_name(expr: &str) -> String {
        // Remove reference symbols
        let without_ref = expr.trim_start_matches('&');

        // Handle method calls like "var.method()" - extract "var"
        if let Some(dot_pos) = without_ref.find('.') {
            return without_ref[0..dot_pos].to_string();
        }

        // Handle array/slice indexing like "var[0]" - extract "var"
        if let Some(bracket_pos) = without_ref.find('[') {
            return without_ref[0..bracket_pos].to_string();
        }

        // No special case, return as is
        return without_ref.to_string();
    }

    /// Conjugate the verb based on plurality
    fn conjugate_verb(&self, is_plural: bool) -> String {
        // Special case handling for common verbs
        match self.verb.as_str() {
            "be" => {
                if is_plural {
                    "are".to_string()
                } else {
                    "is".to_string()
                }
            }
            "have" => {
                if is_plural {
                    "have".to_string()
                } else {
                    "has".to_string()
                }
            }
            "contain" => {
                if is_plural {
                    "contain".to_string()
                } else {
                    "contains".to_string()
                }
            }
            "start with" => {
                if is_plural {
                    "start with".to_string()
                } else {
                    "starts with".to_string()
                }
            }
            "end with" => {
                if is_plural {
                    "end with".to_string()
                } else {
                    "ends with".to_string()
                }
            }
            // For other verbs, add 's' in singular form
            verb => {
                if is_plural {
                    verb.to_string()
                } else {
                    // Handle special cases for verbs ending in certain characters
                    if verb.ends_with('s') || verb.ends_with('x') || verb.ends_with('z') || verb.ends_with("sh") || verb.ends_with("ch") {
                        format!("{}es", verb)
                    } else if verb.ends_with('y')
                        && !verb.ends_with("ay")
                        && !verb.ends_with("ey")
                        && !verb.ends_with("oy")
                        && !verb.ends_with("uy")
                    {
                        format!("{}ies", &verb[0..verb.len() - 1])
                    } else {
                        format!("{}s", verb)
                    }
                }
            }
        }
    }
}

impl Display for AssertionSentence {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        return write!(f, "{}", self.format());
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_assertion_sentence_new() {
        let sentence = AssertionSentence::new("be", "positive");

        assert_eq!(sentence.subject, "");
        assert_eq!(sentence.verb, "be");
        assert_eq!(sentence.object, "positive");
        assert_eq!(sentence.qualifiers.len(), 0);
        assert_eq!(sentence.negated, false);
    }

    #[test]
    fn test_with_negation() {
        let sentence = AssertionSentence::new("be", "positive").with_negation(true);

        assert_eq!(sentence.negated, true);

        // Test chaining and toggle
        let toggled_sentence = sentence.with_negation(false);

        assert_eq!(toggled_sentence.negated, false);
    }

    #[test]
    fn test_with_qualifier() {
        let sentence = AssertionSentence::new("be", "in range").with_qualifier("when rounded");

        assert_eq!(sentence.qualifiers, vec!["when rounded"]);

        // Test multiple qualifiers
        let updated_sentence = sentence.with_qualifier("with tolerance");

        assert_eq!(updated_sentence.qualifiers, vec!["when rounded", "with tolerance"]);
    }

    #[test]
    fn test_format_basic() {
        let sentence = AssertionSentence::new("be", "positive");

        assert_eq!(sentence.format(), "be positive");
    }

    #[test]
    fn test_format_with_negation() {
        let sentence = AssertionSentence::new("be", "positive").with_negation(true);

        assert_eq!(sentence.format(), "not be positive");
    }

    #[test]
    fn test_format_with_qualifiers() {
        let sentence = AssertionSentence::new("be", "in range").with_qualifier("when rounded").with_qualifier("with tolerance");

        assert_eq!(sentence.format(), "be in range when rounded with tolerance");
    }

    #[test]
    fn test_format_with_negation_and_qualifiers() {
        let sentence = AssertionSentence::new("be", "in range").with_negation(true).with_qualifier("when rounded");

        assert_eq!(sentence.format(), "not be in range when rounded");
    }

    #[test]
    fn test_format_grammatical() {
        let sentence = AssertionSentence::new("be", "positive");

        assert_eq!(sentence.format_grammatical(), "be positive");

        let negated = sentence.clone().with_negation(true);

        // The "not" should be after the verb for grammatical correctness
        assert_eq!(negated.format_grammatical(), "be not positive");
    }

    #[test]
    fn test_format_grammatical_with_qualifiers() {
        let sentence = AssertionSentence::new("be", "in range").with_negation(true).with_qualifier("when rounded");

        assert_eq!(sentence.format_grammatical(), "be not in range when rounded");
    }

    #[test]
    fn test_is_plural_subject() {
        // Test singular subjects
        assert_eq!(AssertionSentence::is_plural_subject("value"), false);
        assert_eq!(AssertionSentence::is_plural_subject("number"), false);
        assert_eq!(AssertionSentence::is_plural_subject("count"), false);
        assert_eq!(AssertionSentence::is_plural_subject("item"), false);

        // Test plural subjects
        assert_eq!(AssertionSentence::is_plural_subject("values"), true);
        assert_eq!(AssertionSentence::is_plural_subject("numbers"), true);
        assert_eq!(AssertionSentence::is_plural_subject("items"), true);
        assert_eq!(AssertionSentence::is_plural_subject("lists"), true);

        // Test common plural variable names
        assert_eq!(AssertionSentence::is_plural_subject("data"), true);
        assert_eq!(AssertionSentence::is_plural_subject("children"), true);
    }

    #[test]
    fn test_extract_base_name() {
        // Test reference extraction
        assert_eq!(AssertionSentence::extract_base_name("&value"), "value");

        // Test method call extraction
        assert_eq!(AssertionSentence::extract_base_name("values.len()"), "values");

        // Test array indexing extraction
        assert_eq!(AssertionSentence::extract_base_name("items[0]"), "items");

        // Test combined cases
        assert_eq!(AssertionSentence::extract_base_name("&items[0]"), "items");
        assert_eq!(AssertionSentence::extract_base_name("&values.len()"), "values");
    }

    #[test]
    fn test_conjugate_verb() {
        // Create a test sentence
        let sentence = AssertionSentence::new("", "");

        // Test special case verbs
        let special_verbs = [
            ("be", "is", "are"),
            ("have", "has", "have"),
            ("contain", "contains", "contain"),
            ("start with", "starts with", "start with"),
            ("end with", "ends with", "end with"),
        ];

        for (base, singular, plural) in special_verbs.iter() {
            let mut test_sentence = sentence.clone();
            test_sentence.verb = base.to_string();

            assert_eq!(test_sentence.conjugate_verb(false), *singular);
            assert_eq!(test_sentence.conjugate_verb(true), *plural);
        }

        // Test regular verbs
        let regular_verbs = [("match", "matches"), ("exceed", "exceeds"), ("include", "includes")];

        for (base, singular) in regular_verbs.iter() {
            let mut test_sentence = sentence.clone();
            test_sentence.verb = base.to_string();

            assert_eq!(test_sentence.conjugate_verb(false), *singular);
            assert_eq!(test_sentence.conjugate_verb(true), *base);
        }

        // Test verbs with special spelling rules
        let special_spelling = [
            // Verbs ending in s, x, z, sh, ch get 'es'
            ("pass", "passes"),
            ("fix", "fixes"),
            ("buzz", "buzzes"),
            ("wash", "washes"),
            ("match", "matches"),
            // Verbs ending in y (not preceded by a vowel) get 'ies'
            ("try", "tries"),
            ("fly", "flies"),
            ("comply", "complies"),
            // Verbs ending in y preceded by a vowel just get 's'
            ("play", "plays"),
            ("enjoy", "enjoys"),
        ];

        for (base, singular) in special_spelling.iter() {
            let mut test_sentence = sentence.clone();
            test_sentence.verb = base.to_string();

            assert_eq!(test_sentence.conjugate_verb(false), *singular);
            assert_eq!(test_sentence.conjugate_verb(true), *base);
        }
    }

    #[test]
    fn test_format_with_conjugation() {
        // Test singular subject conjugation
        let sentence = AssertionSentence::new("be", "positive");
        assert_eq!(sentence.format_with_conjugation("value"), "is positive");

        // Test plural subject conjugation
        assert_eq!(sentence.format_with_conjugation("values"), "are positive");

        // Test with negation - Note: we need to clone since with_negation consumes self
        let negated = sentence.clone().with_negation(true);
        assert_eq!(negated.format_with_conjugation("value"), "is not positive");
        assert_eq!(negated.format_with_conjugation("values"), "are not positive");

        // Test with qualifiers - Note: we need to clone since with_qualifier consumes self
        let qualified = sentence.clone().with_qualifier("always");
        assert_eq!(qualified.format_with_conjugation("value"), "is positive always");

        // Test different verbs
        let contain_sentence = AssertionSentence::new("contain", "element");
        assert_eq!(contain_sentence.format_with_conjugation("list"), "contains element");
        assert_eq!(contain_sentence.format_with_conjugation("lists"), "contain element");
    }

    #[test]
    fn test_display_trait() {
        let sentence = AssertionSentence::new("be", "positive");
        assert_eq!(format!("{}", sentence), "be positive");

        let negated = sentence.clone().with_negation(true);
        assert_eq!(format!("{}", negated), "not be positive");
    }
}