zamm_yang 0.2.0

A basic, experimental code generator
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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
use super::util::kb_test_function;
use crate::codegen::template::basic::{
    AtomicFragment, FileFragment, FunctionCallFragment, FunctionFragment, ImplementationFragment,
    ItemDeclarationAPI, SelfReference,
};
use crate::codegen::StructConfig;
use indoc::formatdoc;
use std::cell::RefCell;
use std::rc::Rc;

/// Prefix for attribute setter function name.
const SETTER_PREFIX: &str = "set_";

struct PrimitiveValueConfig {
    pub value: String,
    pub value_get: String,
    pub value_set: String,
}

fn primitive_config(
    attr_cfg: &AttributePropertyConfig,
    primitive_value: &Option<Rc<str>>,
    value_var: &str,
) -> PrimitiveValueConfig {
    match primitive_value {
        Some(primitive_value) => PrimitiveValueConfig {
            value: primitive_value.to_string(),
            value_set: format!("{}.clone()", value_var),
            value_get: format!("Rc::from({})", value_var),
        },
        None => PrimitiveValueConfig {
            value: format!("{}::new()", attr_cfg.value_type.name),
            value_set: format!("&{}", value_var),
            value_get: value_var.to_owned(),
        },
    }
}

/// Config values at the time of Attribute getter/setter code generation.
pub struct AttributePropertyConfig {
    /// The public name to serve as a basis for the getter/setter function names.
    pub property_name: Rc<str>,
    /// Whether or not the getters and setters should be marked public. False if this is to be
    /// defined within a trait or an impl for a trait, true if defined within a pure impl.
    pub public: bool,
    /// An explanation of what this property is. Will be prepended by different strings for the
    /// setter and getter documentation.
    pub doc: Rc<str>,
    /// Concept representing the attribute.
    pub attr: StructConfig,
    /// Concept representing the owner of the attribute.
    pub owner_type: StructConfig,
    /// Concept representing the value of the attribute.
    pub value_type: StructConfig,
    /// The Rust primitive that this represents.
    pub rust_primitive: Option<Rc<str>>,
    /// Code for the Rust primitive when in an unboxed representation.
    pub rust_primitive_unboxed: Option<Rc<str>>,
    /// Dummy default test value to set the primitive to.
    pub primitive_test_value: Option<Rc<str>>,
    /// Dummy override test value to set the primitive to.
    pub dummy_test_value: Option<Rc<str>>,
    /// Whether or not the flag will be passed on to the owner's children via the `Inherits`
    /// attribute.
    pub hereditary: bool,
    /// Whether or not this attribute can contain multiple values.
    pub multi_valued: bool,
}

impl Default for AttributePropertyConfig {
    fn default() -> Self {
        Self {
            property_name: Rc::from(""),
            public: false,
            doc: Rc::from(""),
            attr: StructConfig::default(),
            owner_type: StructConfig::default(),
            value_type: StructConfig::default(),
            rust_primitive: None,
            rust_primitive_unboxed: None,
            primitive_test_value: None,
            dummy_test_value: None,
            hereditary: true,
            multi_valued: false,
        }
    }
}

fn getter_name(cfg: &AttributePropertyConfig) -> Rc<str> {
    if cfg.multi_valued {
        // todo: generate plurals more properly, and add manual override option
        let plural = if cfg.property_name.ends_with('s') {
            format!("{}es", cfg.property_name)
        } else {
            format!("{}s", cfg.property_name)
        };
        Rc::from(plural.as_str())
    } else {
        cfg.property_name.clone()
    }
}

fn setter_name(cfg: &AttributePropertyConfig) -> String {
    if cfg.multi_valued {
        format!("add_{}", cfg.property_name)
    } else {
        format!("{}{}", SETTER_PREFIX, cfg.property_name)
    }
}

/// Get the setter fragment for the attribute property.
fn setter_fragment(cfg: &AttributePropertyConfig) -> FunctionFragment {
    let mut f = FunctionFragment::new(setter_name(cfg));

    f.add_import(cfg.attr.import.clone());
    f.add_import(cfg.value_type.import.clone());
    f.add_import("zamm_yin::tao::archetype::ArchetypeTrait".to_owned());
    f.add_import("zamm_yin::tao::form::FormTrait".to_owned());
    f.add_import("zamm_yin::node_wrappers::BaseNodeTrait".to_owned());
    f.add_import("zamm_yin::node_wrappers::CommonNodeTrait".to_owned());

    if cfg.public {
        f.mark_as_public();
    }
    if cfg.multi_valued {
        f.document(format!("Add one of {}", cfg.doc));
    } else {
        f.document(format!("Set {}", cfg.doc));
    }
    f.set_self_reference(SelfReference::Mutable);

    let arg_name = cfg.property_name.to_string();
    match &cfg.rust_primitive_unboxed {
        Some(unboxed_primitive) => {
            f.add_arg(arg_name, unboxed_primitive.to_string());
            f.append(Rc::new(RefCell::new(AtomicFragment::new(formatdoc! {"
                let mut value_concept = {value_concept}::new();
                value_concept.set_value({value});",
                value_concept = cfg.value_type.name,
                value = cfg.property_name
            }))));
        }
        None => f.add_arg(arg_name, format!("&{}", cfg.value_type.name)),
    };

    let final_value = if cfg.rust_primitive.is_some() {
        "value_concept.deref()".to_owned()
    } else {
        format!("{}.deref()", cfg.property_name)
    };
    let mut add_outgoing = FunctionCallFragment::new(AtomicFragment::new(
        "self.deref_mut().add_outgoing".to_owned(),
    ));
    add_outgoing.add_argument(Rc::new(RefCell::new(AtomicFragment::new(format!(
        "{}::TYPE_ID",
        cfg.attr.name
    )))));
    add_outgoing.add_argument(Rc::new(RefCell::new(AtomicFragment::new(final_value))));
    f.append(Rc::new(RefCell::new(add_outgoing)));
    f
}

/// Get the getter fragment for the attribute property.
fn getter_fragment(cfg: &AttributePropertyConfig) -> FunctionFragment {
    let mut f = FunctionFragment::new(getter_name(cfg).to_string());

    f.add_import(cfg.attr.import.clone());
    f.add_import("zamm_yin::tao::archetype::ArchetypeTrait".to_owned());
    f.add_import("zamm_yin::tao::form::FormTrait".to_owned());
    f.add_import("zamm_yin::node_wrappers::BaseNodeTrait".to_owned());

    if cfg.public {
        f.mark_as_public();
    }
    f.document(format!("Get {}", cfg.doc));
    if cfg.rust_primitive.is_some() {
        f.add_attribute("allow(clippy::rc_buffer)".to_owned());
    }
    f.set_self_reference(SelfReference::Immutable);
    let base_return_type = match &cfg.rust_primitive {
        Some(primitive) => {
            f.add_import("std::rc::Rc".to_owned());
            format!("Rc<{}>", primitive)
        }
        None => cfg.value_type.name.clone(),
    };
    if cfg.multi_valued {
        f.set_return(format!("Vec<{}>", base_return_type));
    } else {
        f.set_return(format!("Option<{}>", base_return_type));
    }

    let nonhereditary_access = if cfg.hereditary {
        ""
    } else {
        "\n    .base_wrapper()"
    };
    let primitive_map = if cfg.rust_primitive.is_some() {
        ".value().unwrap()"
    } else {
        ""
    };
    let collection = if cfg.multi_valued {
        ".into_iter()"
    } else {
        // outgoing nodes are sorted by ID, and more specific nodes are created later, resulting in
        // higher IDs. This is how overrides happen.
        //
        // todo: implement this properly, because this forces things to be defined in a certain
        // order in yin.md
        ".last()"
    };
    let post_collection = if cfg.multi_valued {
        "\n    .collect()"
    } else {
        ""
    };

    f.append(Rc::new(RefCell::new(AtomicFragment::new(formatdoc! {"
        self.deref(){inheritance}
            .outgoing_nodes({attr}::TYPE_ID)
            {collection}
            .map(|f| {value_type}::from(f.id()){primitive_map}){post_collection}",
        inheritance = nonhereditary_access,
        attr = cfg.attr.name,
        value_type = cfg.value_type.name,
        primitive_map = primitive_map,
        collection = collection,
        post_collection = post_collection
    }))));

    f
}

/// Test that the getter and setter work as intended.
fn test_fragment(cfg: &AttributePropertyConfig) -> FunctionFragment {
    let setter = setter_name(cfg);
    let getter = getter_name(cfg);

    let mut f = kb_test_function(&format!("test_set_and_get_{}", cfg.property_name));
    f.add_attribute("allow(clippy::clone_double_ref)".to_owned());
    f.add_import(cfg.owner_type.import.clone());
    if cfg.rust_primitive.is_none() {
        // if some, will use that directly instead of the concept
        f.add_import(cfg.value_type.import.clone());
    }
    let value_cfg = primitive_config(cfg, &cfg.primitive_test_value, "value");
    let empty_value = if cfg.multi_valued {
        "vec![]".to_owned()
    } else {
        "None".to_owned()
    };
    let final_value_get = if cfg.multi_valued {
        format!("vec![{}]", value_cfg.value_get)
    } else {
        format!("Some({})", value_cfg.value_get)
    };
    // todo: only clone when it's not a Copy type, to avoid the clone_on_copy warning. The thing
    // is, this is specific to Rust, so it should be Yang-only knowledge.
    f.append(Rc::new(RefCell::new(AtomicFragment::new(formatdoc! {"
        let mut new_instance = {owner}::new();
        assert_eq!(new_instance.{getter}(), {empty});

        let value = {value};
        #[allow(clippy::clone_on_copy)]
        new_instance.{setter}({value_set});
        assert_eq!(new_instance.{getter}(), {value_get});",
        owner = cfg.owner_type.name,
        getter = getter,
        setter = setter,
        empty = empty_value,
        value = value_cfg.value,
        value_set = value_cfg.value_set,
        value_get = final_value_get,
    }))));
    f
}

/// Test that the getter and setter work as intended when inherited.
fn test_inheritance_fragment(cfg: &AttributePropertyConfig) -> FunctionFragment {
    let setter = setter_name(cfg);
    let getter = getter_name(cfg);

    let empty_value = if cfg.multi_valued {
        "vec![]".to_owned()
    } else {
        "None".to_owned()
    };
    let inheritance_name = if cfg.hereditary {
        "inheritance"
    } else {
        "non_inheritance"
    };
    let value_cfg = primitive_config(cfg, &cfg.primitive_test_value, "value");
    let value_set = if cfg.primitive_test_value.is_some() && !cfg.hereditary {
        "value".to_owned() // value won't get used again, so just use it directly
    } else {
        value_cfg.value_set
    };
    let inheritance_check = if cfg.hereditary {
        if cfg.multi_valued {
            format!("vec![{}]", value_cfg.value_get)
        } else {
            format!("Some({})", value_cfg.value_get)
        }
    } else {
        empty_value.clone()
    };
    let mut f = FunctionFragment::new(format!("test_{}_{}", cfg.property_name, inheritance_name));
    f.mark_as_test();
    f.add_attribute("allow(clippy::clone_double_ref)".to_owned());
    f.add_import("crate::tao::initialize_kb".to_owned());
    f.add_import(cfg.owner_type.import.clone());
    // todo: only clone when it's not a Copy type, to avoid the clone_on_copy warning. The thing
    // is, this is specific to Rust, so it should be Yang-only knowledge.
    f.append(Rc::new(RefCell::new(AtomicFragment::new(formatdoc! {"
        initialize_kb();
        let new_type = {owner}::archetype().individuate_as_archetype();
        let new_instance = {owner}::from(new_type.individuate_as_form().id());
        assert_eq!(new_instance.{getter}(), {empty});

        let value = {value};
        #[allow(clippy::clone_on_copy)]
        {owner}::from(new_type.id()).{setter}({value_set});
        assert_eq!(new_instance.{getter}(), {inheritance});
    ", owner = cfg.owner_type.name,
        getter = getter,
        setter = setter,
        empty = empty_value,
        inheritance = inheritance_check,
        value = value_cfg.value,
        value_set = value_set,
    }))));
    f
}

/// Test that calling the setter twice results in expected behavior. For backwards compatibility,
/// this returns None if a dummy value is not provided.
fn test_multi_set_fragment(cfg: &AttributePropertyConfig) -> Option<FunctionFragment> {
    if cfg.rust_primitive.is_some() && cfg.dummy_test_value.is_none() {
        return None; // backwards compatibility check
    }

    let setter = setter_name(cfg);
    let getter = getter_name(cfg);

    let mut f = kb_test_function(&format!("test_set_{}_multiple_times", cfg.property_name));
    f.add_attribute("allow(clippy::clone_double_ref)".to_owned());
    f.add_import(cfg.owner_type.import.clone());
    if cfg.rust_primitive.is_none() {
        // if some, will use that directly instead of the concept
        f.add_import(cfg.value_type.import.clone());
    }

    let default_value_cfg = primitive_config(cfg, &cfg.primitive_test_value, "default");
    let default_value_get = if cfg.multi_valued {
        if cfg.rust_primitive.is_some() {
            // clone here, because if it's multi-valued, then it will get used again later
            "vec![Rc::from(default.clone())]".to_owned()
        } else {
            "vec![default]".to_owned()
        }
    } else {
        format!("Some({})", default_value_cfg.value_get)
    };

    let new_value_cfg = primitive_config(cfg, &cfg.dummy_test_value, "new_value");
    let expected_get = if cfg.multi_valued {
        format!(
            "vec![{}, {}]",
            default_value_cfg.value_get, new_value_cfg.value_get
        )
    } else {
        format!("Some({})", new_value_cfg.value_get)
    };

    // todo: only clone when it's not a Copy type, to avoid the clone_on_copy warning. The thing
    // is, this is specific to Rust, so it should be Yang-only knowledge.
    //
    // also todo: there should be explicit Yin support for overridden values, instead of this
    // workaround that's contingent on attribute insertion order
    f.append(Rc::new(RefCell::new(AtomicFragment::new(formatdoc! {"
        let mut new_instance = {owner}::new();
        let default = {default_value};
        #[allow(clippy::clone_on_copy)]
        new_instance.{setter}({default_set});
        #[allow(clippy::clone_on_copy)]
        assert_eq!(new_instance.{getter}(), {default_get});

        let new_value = {new_value};
        #[allow(clippy::clone_on_copy)]
        new_instance.{setter}({new_value_set});
        assert_eq!(new_instance.{getter}(), {expected_get});",
        owner = cfg.owner_type.name,
        getter = getter,
        setter = setter,
        default_value = default_value_cfg.value,
        default_set = default_value_cfg.value_set,
        default_get = default_value_get,
        new_value = new_value_cfg.value,
        new_value_set = new_value_cfg.value_set,
        expected_get = expected_get,
    }))));
    Some(f)
}

/// Add these flags to an implementation and its corresponding test module.
pub fn add_attr_to_impl(
    cfg: &AttributePropertyConfig,
    implementation: &mut ImplementationFragment,
    file: &mut FileFragment,
) {
    implementation.append(Rc::new(RefCell::new(getter_fragment(cfg))));
    implementation.append(Rc::new(RefCell::new(setter_fragment(cfg))));
    file.append_test(Rc::new(RefCell::new(test_fragment(cfg))));
    file.append_test(Rc::new(RefCell::new(test_inheritance_fragment(cfg))));
    if let Some(f) = test_multi_set_fragment(cfg) {
        file.append_test(Rc::new(RefCell::new(f)));
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::codegen::template::basic::CodeFragment;
    use indoc::indoc;

    fn concept_attr_config() -> AttributePropertyConfig {
        AttributePropertyConfig {
            public: false,
            property_name: Rc::from("associated_crate"),
            doc: Rc::from("the crate associated with the struct."),
            attr: StructConfig {
                name: "AssociatedCrate".to_owned(),
                import: "crate::tao::relations::attribute::AssociatedCrate".to_owned(),
            },
            owner_type: StructConfig {
                name: "Form".to_owned(),
                import: "zamm_yin::tao::form::Form".to_owned(),
            },
            value_type: StructConfig {
                name: "Crate".to_owned(),
                import: "zamm_yin::tao::form::Crate".to_owned(),
            },
            ..AttributePropertyConfig::default()
        }
    }

    fn primitive_attr_config() -> AttributePropertyConfig {
        AttributePropertyConfig {
            rust_primitive: Some(Rc::from("str")),
            rust_primitive_unboxed: Some(Rc::from("&str")),
            primitive_test_value: Some(Rc::from("\"\"")),
            dummy_test_value: Some(Rc::from("\"a\"")),
            ..concept_attr_config()
        }
    }

    fn multi_valued_config() -> AttributePropertyConfig {
        AttributePropertyConfig {
            doc: Rc::from("the crates associated with the struct."),
            multi_valued: true,
            ..concept_attr_config()
        }
    }

    #[test]
    fn test_setter_fragment_body() {
        assert_eq!(
            setter_fragment(&concept_attr_config()).body(80),
            indoc! {"
                /// Set the crate associated with the struct.
                fn set_associated_crate(&mut self, associated_crate: &Crate) {
                    self.deref_mut().add_outgoing(
                        AssociatedCrate::TYPE_ID,
                        associated_crate.deref(),
                    );
                }"}
        );
    }

    #[test]
    fn test_primitive_setter_fragment_body() {
        assert_eq!(
            setter_fragment(&primitive_attr_config()).body(80),
            indoc! {"
                /// Set the crate associated with the struct.
                fn set_associated_crate(&mut self, associated_crate: &str) {
                    let mut value_concept = Crate::new();
                    value_concept.set_value(associated_crate);
                    self.deref_mut().add_outgoing(
                        AssociatedCrate::TYPE_ID,
                        value_concept.deref(),
                    );
                }"}
        );
    }

    #[test]
    fn test_multi_valued_setter_fragment_body() {
        assert_eq!(
            setter_fragment(&multi_valued_config()).body(80),
            indoc! {"
                /// Add one of the crates associated with the struct.
                fn add_associated_crate(&mut self, associated_crate: &Crate) {
                    self.deref_mut().add_outgoing(
                        AssociatedCrate::TYPE_ID,
                        associated_crate.deref(),
                    );
                }"}
        );
    }

    #[test]
    fn test_getter_fragment_body() {
        assert_eq!(
            getter_fragment(&concept_attr_config()).body(80),
            indoc! {"
                /// Get the crate associated with the struct.
                fn associated_crate(&self) -> Option<Crate> {
                    self.deref()
                        .outgoing_nodes(AssociatedCrate::TYPE_ID)
                        .last()
                        .map(|f| Crate::from(f.id()))
                }"}
        );
    }

    #[test]
    fn test_primitive_getter_fragment_body() {
        assert_eq!(
            getter_fragment(&primitive_attr_config()).body(80),
            indoc! {"
                /// Get the crate associated with the struct.
                #[allow(clippy::rc_buffer)]
                fn associated_crate(&self) -> Option<Rc<str>> {
                    self.deref()
                        .outgoing_nodes(AssociatedCrate::TYPE_ID)
                        .last()
                        .map(|f| Crate::from(f.id()).value().unwrap())
                }"}
        );
    }

    #[test]
    fn test_primitive_nonhereditary_getter_fragment_body() {
        assert_eq!(
            getter_fragment(&AttributePropertyConfig {
                hereditary: false,
                ..primitive_attr_config()
            })
            .body(80),
            indoc! {"
                /// Get the crate associated with the struct.
                #[allow(clippy::rc_buffer)]
                fn associated_crate(&self) -> Option<Rc<str>> {
                    self.deref()
                        .base_wrapper()
                        .outgoing_nodes(AssociatedCrate::TYPE_ID)
                        .last()
                        .map(|f| Crate::from(f.id()).value().unwrap())
                }"}
        );
    }

    #[test]
    fn test_multi_valued_getter_fragment_body() {
        assert_eq!(
            getter_fragment(&multi_valued_config()).body(80),
            indoc! {"
                /// Get the crates associated with the struct.
                fn associated_crates(&self) -> Vec<Crate> {
                    self.deref()
                        .outgoing_nodes(AssociatedCrate::TYPE_ID)
                        .into_iter()
                        .map(|f| Crate::from(f.id()))
                        .collect()
                }"}
        );
    }

    #[test]
    fn test_test_fragment_body() {
        assert_eq!(
            test_fragment(&concept_attr_config()).body(80),
            indoc! {"
                #[test]
                #[allow(clippy::clone_double_ref)]
                fn test_set_and_get_associated_crate() {
                    initialize_kb();
                    let mut new_instance = Form::new();
                    assert_eq!(new_instance.associated_crate(), None);

                    let value = Crate::new();
                    #[allow(clippy::clone_on_copy)]
                    new_instance.set_associated_crate(&value);
                    assert_eq!(new_instance.associated_crate(), Some(value));
                }"}
        );
    }

    #[test]
    fn test_primitive_test_fragment_body() {
        assert_eq!(
            test_fragment(&primitive_attr_config()).body(80),
            indoc! {r#"
                #[test]
                #[allow(clippy::clone_double_ref)]
                fn test_set_and_get_associated_crate() {
                    initialize_kb();
                    let mut new_instance = Form::new();
                    assert_eq!(new_instance.associated_crate(), None);

                    let value = "";
                    #[allow(clippy::clone_on_copy)]
                    new_instance.set_associated_crate(value.clone());
                    assert_eq!(new_instance.associated_crate(), Some(Rc::from(value)));
                }"#}
        );
    }

    #[test]
    fn test_test_inheritance_fragment_body() {
        assert_eq!(
            test_inheritance_fragment(&concept_attr_config()).body(80),
            indoc! {"
                #[test]
                #[allow(clippy::clone_double_ref)]
                fn test_associated_crate_inheritance() {
                    initialize_kb();
                    let new_type = Form::archetype().individuate_as_archetype();
                    let new_instance = Form::from(new_type.individuate_as_form().id());
                    assert_eq!(new_instance.associated_crate(), None);

                    let value = Crate::new();
                    #[allow(clippy::clone_on_copy)]
                    Form::from(new_type.id()).set_associated_crate(&value);
                    assert_eq!(new_instance.associated_crate(), Some(value));
                }"}
        );
    }

    #[test]
    fn test_primitive_test_inheritance_fragment_body() {
        assert_eq!(
            test_inheritance_fragment(&primitive_attr_config()).body(80),
            indoc! {r#"
                #[test]
                #[allow(clippy::clone_double_ref)]
                fn test_associated_crate_inheritance() {
                    initialize_kb();
                    let new_type = Form::archetype().individuate_as_archetype();
                    let new_instance = Form::from(new_type.individuate_as_form().id());
                    assert_eq!(new_instance.associated_crate(), None);

                    let value = "";
                    #[allow(clippy::clone_on_copy)]
                    Form::from(new_type.id()).set_associated_crate(value.clone());
                    assert_eq!(new_instance.associated_crate(), Some(Rc::from(value)));
                }"#}
        );
    }

    #[test]
    fn test_primitive_test_set_multiple_fragment_body() {
        assert_eq!(
            test_multi_set_fragment(&primitive_attr_config())
                .unwrap()
                .body(80),
            indoc! {r#"
                #[test]
                #[allow(clippy::clone_double_ref)]
                fn test_set_associated_crate_multiple_times() {
                    initialize_kb();
                    let mut new_instance = Form::new();
                    let default = "";
                    #[allow(clippy::clone_on_copy)]
                    new_instance.set_associated_crate(default.clone());
                    #[allow(clippy::clone_on_copy)]
                    assert_eq!(new_instance.associated_crate(), Some(Rc::from(default)));

                    let new_value = "a";
                    #[allow(clippy::clone_on_copy)]
                    new_instance.set_associated_crate(new_value.clone());
                    assert_eq!(new_instance.associated_crate(), Some(Rc::from(new_value)));
                }"#}
        );
    }
}