ifc-properties 0.8.1

Property sets, quantities, and unit resolution. No geometry.
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
//! Exact enumerated, list, bounded, table and reference values (#150).
//!
//! Each kind resolves on an occurrence and on a type, bound to the release
//! `FILE_SCHEMA` declares. The attribute layouts differ between releases
//! (`references/ifc-spec/*/*.exp`):
//!
//! - IFC2X3 TC1 requires `EnumerationValues`, `ListValues`, both table
//!   columns and `PropertyReference`; IFC4 ADD2 TC1 and IFC4X3 ADD2 make
//!   them optional.
//! - `IfcPropertyBoundedValue.SetPointValue` and
//!   `IfcPropertyTableValue.CurveInterpolation` are IFC4 additions.
//! - `IfcObjectReferenceSelect` admits `IfcTable` from IFC4 on.
//!
//! Models are parsed from STEP text through the strict codec.

use ifc_model::{Codec, EntityId, Model};
use ifc_properties::{
    exact_properties, exact_property, exact_unit, ExactEntityRef, ExactProperty,
    ExactPropertyError, ExactResolution, ExactSource, ExactTypedValue, ExactValue,
};
use ifc_step::StepCodec;

const WALL: EntityId = EntityId(1);
const WALL_TYPE: EntityId = EntityId(2);

/// A wall typed by #2, with occurrence set `Pset_Occurrence` (#20) and type
/// set `Pset_Type` (#40) holding the given `(id, record)` members.
fn model(schema: &str, occurrence: &[(u64, &str)], typed: &[(u64, &str)], extra: &[&str]) -> Model {
    let wall_slots = if schema == "IFC2X3" {
        "$,$,$,$,$"
    } else {
        "$,$,$,$,$,$"
    };
    let refs = |members: &[(u64, &str)]| {
        members
            .iter()
            .map(|(id, _)| format!("#{id}"))
            .collect::<Vec<_>>()
            .join(",")
    };
    let mut records = vec![
        format!("#1=IFCWALL('0000000000000000000001',$,'Wall',{wall_slots});"),
        "#2=IFCWALLTYPE('0000000000000000000002',$,'WT',$,$,(#40),$,$,$,.STANDARD.);".into(),
        "#3=IFCRELDEFINESBYTYPE('0000000000000000000003',$,$,$,(#1),#2);".into(),
        format!(
            "#20=IFCPROPERTYSET('0000000000000000000020',$,'Pset_Occurrence',$,({}));",
            refs(occurrence)
        ),
        "#21=IFCRELDEFINESBYPROPERTIES('0000000000000000000021',$,$,$,(#1),#20);".into(),
        format!(
            "#40=IFCPROPERTYSET('0000000000000000000040',$,'Pset_Type',$,({}));",
            refs(typed)
        ),
        "#90=IFCSIUNIT(*,.LENGTHUNIT.,.MILLI.,.METRE.);".into(),
    ];
    for (id, record) in occurrence.iter().chain(typed) {
        records.push(format!("#{id}={record};"));
    }
    records.extend(extra.iter().map(|record| (*record).to_owned()));
    let text = format!(
        "ISO-10303-21;\nHEADER;\nFILE_DESCRIPTION((''),'2;1');\n\
         FILE_NAME('','',(''),(''),'','','');\nFILE_SCHEMA(('{schema}'));\nENDSEC;\n\
         DATA;\n{}\nENDSEC;\nEND-ISO-10303-21;\n",
        records.join("\n")
    );
    let model = StepCodec
        .read_bytes(text.as_bytes())
        .unwrap_or_else(|e| panic!("fixture must parse: {e:?}"));
    assert!(model.diagnostics().is_empty(), "{:?}", model.diagnostics());
    model
}

/// A model with one property on the occurrence and a filler on the type.
fn one(schema: &str, record: &str, extra: &[&str]) -> Model {
    let filler = "IFCPROPERTYSINGLEVALUE('Filler',$,IFCLABEL('x'),$)";
    model(schema, &[(10, record)], &[(41, filler)], extra)
}

fn get(model: &Model, name: &str) -> Result<ExactProperty, ExactPropertyError> {
    match exact_property(model, WALL, None, name)? {
        ExactResolution::Present(property) => Ok(property),
        other => panic!("{name}: expected a present property, got {other:?}"),
    }
}

fn typed(value_type: &str, value: ExactValue) -> ExactTypedValue {
    ExactTypedValue {
        value_type: value_type.into(),
        value,
    }
}

fn label(text: &str) -> ExactTypedValue {
    typed("IFCLABEL", ExactValue::Text(text.into()))
}

fn length(value: f64) -> ExactTypedValue {
    typed("IFCLENGTHMEASURE", ExactValue::Real(value))
}

fn inconsistent(entity: u64, rule: &'static str) -> Result<ExactProperty, ExactPropertyError> {
    Err(ExactPropertyError::InconsistentValues {
        entity: EntityId(entity),
        rule,
    })
}

const ENUMERATION: &str =
    "#80=IFCPROPERTYENUMERATION('PEnum_Status',(IFCLABEL('NEW'),IFCLABEL('OLD')),$);";

#[test]
fn an_enumerated_value_resolves_on_occurrence_and_type() {
    for schema in ["IFC2X3", "IFC4", "IFC4X3_ADD2"] {
        let m = model(
            schema,
            &[(
                10,
                "IFCPROPERTYENUMERATEDVALUE('Status',$,(IFCLABEL('NEW')),#80)",
            )],
            &[(
                41,
                "IFCPROPERTYENUMERATEDVALUE('Grade',$,(IFCLABEL('A'),IFCINTEGER(2)),$)",
            )],
            &[ENUMERATION],
        );
        let status = get(&m, "Status").expect(schema);
        assert_eq!(status.source, ExactSource::Occurrence);
        assert_eq!(
            (status.property_id, status.value_type),
            (EntityId(10), None)
        );
        assert_eq!(status.unit_id, None);
        let ExactValue::Enumerated(status) = status.value else {
            panic!("{schema}: {:?}", status.value)
        };
        assert_eq!(status.values, [label("NEW")]);
        let enumeration = status.enumeration.expect("referenced");
        assert_eq!(enumeration.id, EntityId(80));
        assert_eq!(enumeration.name.as_ref(), "PEnum_Status");
        assert_eq!(enumeration.values, [label("NEW"), label("OLD")]);

        let grade = get(&m, "Grade").expect(schema);
        assert_eq!(grade.source, ExactSource::Type(WALL_TYPE));
        let ExactValue::Enumerated(grade) = grade.value else {
            panic!("{schema}: {:?}", grade.value)
        };
        // Without a reference, selected values of several types are legal.
        assert_eq!(
            grade.values,
            [label("A"), typed("IFCINTEGER", ExactValue::Integer(2))]
        );
        assert_eq!(grade.enumeration, None);
    }
}

#[test]
fn an_empty_enumeration_is_a_value_in_ifc4_and_malformed_in_ifc2x3() {
    let record = "IFCPROPERTYENUMERATEDVALUE('Status',$,$,#80)";
    for schema in ["IFC4", "IFC4X3_ADD2"] {
        let m = one(schema, record, &[ENUMERATION]);
        let ExactValue::Enumerated(status) = get(&m, "Status").expect(schema).value else {
            panic!("{schema}: not enumerated")
        };
        assert!(status.values.is_empty(), "{schema}: nothing is selected");
        assert_eq!(status.enumeration.map(|e| e.values.len()), Some(2));
    }
    // IFC2X3 declares `EnumerationValues : LIST [1:?]` without OPTIONAL.
    let m = one("IFC2X3", record, &[ENUMERATION]);
    let malformed = Err(ExactPropertyError::MalformedAggregate {
        entity: EntityId(10),
        attribute: "EnumerationValues",
    });
    assert_eq!(get(&m, "Status"), malformed);
    // A present list is never empty in any release.
    for schema in ["IFC2X3", "IFC4", "IFC4X3_ADD2"] {
        let m = one(schema, "IFCPROPERTYENUMERATEDVALUE('Status',$,(),$)", &[]);
        assert_eq!(get(&m, "Status"), malformed, "{schema}");
    }
}

#[test]
fn a_malformed_enumeration_is_refused() {
    // A selected value the reference does not permit: IFC2X3 WR1, IFC4 WR21.
    let record = "IFCPROPERTYENUMERATEDVALUE('Status',$,(IFCLABEL('GONE')),#80)";
    assert_eq!(
        get(&one("IFC2X3", record, &[ENUMERATION]), "Status"),
        inconsistent(10, "WR1")
    );
    assert_eq!(
        get(&one("IFC4", record, &[ENUMERATION]), "Status"),
        inconsistent(10, "WR21")
    );
    // Same text, other type: not the permitted value.
    let record = "IFCPROPERTYENUMERATEDVALUE('Status',$,(IFCTEXT('NEW')),#80)";
    assert_eq!(
        get(&one("IFC4", record, &[ENUMERATION]), "Status"),
        inconsistent(10, "WR21")
    );

    let record = "IFCPROPERTYENUMERATEDVALUE('Status',$,(IFCLABEL('NEW')),#80)";
    let repeated = "#80=IFCPROPERTYENUMERATION('E',(IFCLABEL('NEW'),IFCLABEL('NEW')),$);";
    assert_eq!(
        get(&one("IFC4", record, &[repeated]), "Status"),
        inconsistent(80, "EnumerationValues UNIQUE")
    );
    let mixed = "#80=IFCPROPERTYENUMERATION('E',(IFCLABEL('NEW'),IFCINTEGER(1)),$);";
    assert_eq!(
        get(&one("IFC4", record, &[mixed]), "Status"),
        inconsistent(80, "WR01")
    );
    let empty = "#80=IFCPROPERTYENUMERATION('E',$,$);";
    assert_eq!(
        get(&one("IFC4", record, &[empty]), "Status"),
        Err(ExactPropertyError::MalformedAggregate {
            entity: EntityId(80),
            attribute: "EnumerationValues",
        })
    );
    // The reference must be an `IfcPropertyEnumeration`.
    let wrong = "IFCPROPERTYENUMERATEDVALUE('Status',$,(IFCLABEL('NEW')),#90)";
    assert_eq!(
        get(&one("IFC4", wrong, &[]), "Status"),
        Err(ExactPropertyError::UnsupportedValue {
            property: EntityId(10)
        })
    );
    let dangling = "IFCPROPERTYENUMERATEDVALUE('Status',$,(IFCLABEL('NEW')),#99)";
    assert_eq!(
        get(&one("IFC4", dangling, &[]), "Status"),
        Err(ExactPropertyError::MissingReference {
            from: EntityId(10),
            to: EntityId(99),
        })
    );
    // An untyped member is no `IfcValue`.
    let untyped = "IFCPROPERTYENUMERATEDVALUE('Status',$,('NEW'),$)";
    assert_eq!(
        get(&one("IFC4", untyped, &[]), "Status"),
        Err(ExactPropertyError::UnsupportedValue {
            property: EntityId(10)
        })
    );
    // The enumeration's unit resolves like a single value's.
    let with_unit =
        "#80=IFCPROPERTYENUMERATION('E',(IFCLENGTHMEASURE(1.),IFCLENGTHMEASURE(2.)),#90);";
    let record = "IFCPROPERTYENUMERATEDVALUE('Width',$,(IFCLENGTHMEASURE(2.)),#80)";
    let width = get(&one("IFC4", record, &[with_unit]), "Width").expect("resolves");
    assert_eq!(width.unit_id, Some(EntityId(90)));
}

#[test]
fn a_list_value_resolves_with_its_unit_on_occurrence_and_type() {
    for schema in ["IFC2X3", "IFC4", "IFC4X3_ADD2"] {
        let m = model(
            schema,
            &[(10, "IFCPROPERTYLISTVALUE('Widths',$,(IFCLENGTHMEASURE(900.),IFCLENGTHMEASURE(1200.)),#90)")],
            &[(41, "IFCPROPERTYLISTVALUE('Tags',$,(IFCLABEL('a')),$)")],
            &[],
        );
        let widths = get(&m, "Widths").expect(schema);
        assert_eq!(widths.source, ExactSource::Occurrence);
        assert_eq!(widths.unit_id, Some(EntityId(90)));
        assert_eq!(
            widths.value,
            ExactValue::List(vec![length(900.0), length(1200.0)])
        );
        let tags = get(&m, "Tags").expect(schema);
        assert_eq!(tags.source, ExactSource::Type(WALL_TYPE));
        assert_eq!(tags.value, ExactValue::List(vec![label("a")]));
    }
    // `ListValues` is optional from IFC4 on, required in IFC2X3.
    let record = "IFCPROPERTYLISTVALUE('Widths',$,$,$)";
    assert_eq!(
        get(&one("IFC4", record, &[]), "Widths").unwrap().value,
        ExactValue::List(vec![])
    );
    assert_eq!(
        get(&one("IFC2X3", record, &[]), "Widths"),
        Err(ExactPropertyError::MalformedAggregate {
            entity: EntityId(10),
            attribute: "ListValues",
        })
    );
    // WR31: one type per list, in every release.
    let mixed = "IFCPROPERTYLISTVALUE('Widths',$,(IFCLENGTHMEASURE(1.),IFCREAL(2.)),$)";
    for schema in ["IFC2X3", "IFC4", "IFC4X3_ADD2"] {
        assert_eq!(
            get(&one(schema, mixed, &[]), "Widths"),
            inconsistent(10, "WR31")
        );
    }
    // A unit that is no `IfcUnit`.
    let bad_unit = "IFCPROPERTYLISTVALUE('Widths',$,(IFCLENGTHMEASURE(1.)),#80)";
    assert_eq!(
        get(&one("IFC4", bad_unit, &[ENUMERATION]), "Widths"),
        Err(ExactPropertyError::UnsupportedUnit {
            property: EntityId(10)
        })
    );
}

#[test]
fn a_bounded_value_resolves_with_its_unit_and_ifc4_set_point() {
    let ifc4 = "IFCPROPERTYBOUNDEDVALUE('Width',$,IFCLENGTHMEASURE(1200.),IFCLENGTHMEASURE(800.),#90,IFCLENGTHMEASURE(900.))";
    let ifc2x3 =
        "IFCPROPERTYBOUNDEDVALUE('Width',$,IFCLENGTHMEASURE(1200.),IFCLENGTHMEASURE(800.),#90)";
    for (schema, record, set_point) in [
        ("IFC2X3", ifc2x3, None),
        ("IFC4", ifc4, Some(length(900.0))),
        ("IFC4X3_ADD2", ifc4, Some(length(900.0))),
    ] {
        let type_record = record.replace("'Width'", "'TypeWidth'");
        let m = model(schema, &[(10, record)], &[(41, &type_record)], &[]);
        let width = get(&m, "Width").expect(schema);
        assert_eq!(width.unit_id, Some(EntityId(90)));
        let ExactValue::Bounded(bounds) = &width.value else {
            panic!("{schema}: {:?}", width.value)
        };
        assert_eq!(bounds.lower, Some(length(800.0)));
        assert_eq!(bounds.upper, Some(length(1200.0)));
        assert_eq!(bounds.set_point, set_point);
        // The stated unit scales the bounds exactly.
        let unit = exact_unit(&m, "IFCLENGTHMEASURE", width.unit_id).expect(schema);
        assert_eq!(unit.scale, 0.001);
        assert!(!unit.from_project);
        assert_eq!(
            get(&m, "TypeWidth").expect(schema).source,
            ExactSource::Type(WALL_TYPE)
        );
    }
    // IFC2X3 declares no SetPointValue: a sixth slot is a malformed record.
    assert!(matches!(
        get(&one("IFC2X3", ifc4, &[]), "Width"),
        Err(ExactPropertyError::MalformedEntitySlots {
            entity: EntityId(10),
            expected: 5,
            actual: 6,
            ..
        })
    ));
}

#[test]
fn a_malformed_bounded_value_is_refused() {
    let bounded = |upper: &str, lower: &str, set_point: &str| {
        format!("IFCPROPERTYBOUNDEDVALUE('Width',$,{upper},{lower},$,{set_point})")
    };
    let (metre, real) = ("IFCLENGTHMEASURE(2.)", "IFCREAL(1.)");
    for (record, rule) in [
        (bounded(metre, real, "$"), "SameUnitUpperLower"),
        (bounded(metre, "$", real), "SameUnitUpperSet"),
        (bounded("$", metre, real), "SameUnitLowerSet"),
    ] {
        assert_eq!(
            get(&one("IFC4", &record, &[]), "Width"),
            inconsistent(10, rule)
        );
        assert_eq!(
            get(&one("IFC4X3_ADD2", &record, &[]), "Width"),
            inconsistent(10, rule)
        );
    }
    let ifc2x3 =
        |upper: &str, lower: &str| format!("IFCPROPERTYBOUNDEDVALUE('Width',$,{upper},{lower},$)");
    assert_eq!(
        get(&one("IFC2X3", &ifc2x3(metre, real), &[]), "Width"),
        inconsistent(10, "WR21")
    );
    // IFC2X3 WR22 requires a bound; IFC4 dropped the rule.
    assert_eq!(
        get(&one("IFC2X3", &ifc2x3("$", "$"), &[]), "Width"),
        inconsistent(10, "WR22")
    );
    let none = get(&one("IFC4", &bounded("$", "$", "$"), &[]), "Width").expect("legal in IFC4");
    let ExactValue::Bounded(bounds) = none.value else {
        panic!("{:?}", none.value)
    };
    assert_eq!(
        (bounds.lower, bounds.upper, bounds.set_point),
        (None, None, None)
    );
    // A bare number is no `IfcValue`.
    assert_eq!(
        get(&one("IFC4", &bounded("2.", "$", "$"), &[]), "Width"),
        Err(ExactPropertyError::UnsupportedValue {
            property: EntityId(10)
        })
    );
}

#[test]
fn a_table_value_resolves_rows_units_expression_and_interpolation() {
    let ifc4 = "IFCPROPERTYTABLEVALUE('Curve',$,(IFCREAL(0.),IFCREAL(1.)),(IFCLENGTHMEASURE(10.),IFCLENGTHMEASURE(20.)),'y = 10x + 10',$,#90,.LINEAR.)";
    let ifc2x3 = "IFCPROPERTYTABLEVALUE('Curve',$,(IFCREAL(0.),IFCREAL(1.)),(IFCLENGTHMEASURE(10.),IFCLENGTHMEASURE(20.)),'y = 10x + 10',$,#90)";
    for (schema, record, interpolation) in [
        ("IFC2X3", ifc2x3, None),
        ("IFC4", ifc4, Some("LINEAR")),
        ("IFC4X3_ADD2", ifc4, Some("LINEAR")),
    ] {
        let type_record = record.replace("'Curve'", "'TypeCurve'");
        let m = model(schema, &[(10, record)], &[(41, &type_record)], &[]);
        let curve = get(&m, "Curve").expect(schema);
        assert_eq!((curve.value_type.clone(), curve.unit_id), (None, None));
        let ExactValue::Table(table) = &curve.value else {
            panic!("{schema}: {:?}", curve.value)
        };
        let rows: Vec<_> = table
            .rows
            .iter()
            .map(|r| (r.defining.clone(), r.defined.clone()))
            .collect();
        let real = |v| typed("IFCREAL", ExactValue::Real(v));
        assert_eq!(rows, [(real(0.0), length(10.0)), (real(1.0), length(20.0))]);
        assert_eq!(table.expression.as_deref(), Some("y = 10x + 10"));
        assert_eq!(
            (table.defining_unit, table.defined_unit),
            (None, Some(EntityId(90)))
        );
        assert_eq!(table.interpolation.as_deref(), interpolation);
        assert_eq!(
            get(&m, "TypeCurve").expect(schema).source,
            ExactSource::Type(WALL_TYPE)
        );
    }
    // IFC2X3 declares no CurveInterpolation.
    assert!(matches!(
        get(&one("IFC2X3", ifc4, &[]), "Curve"),
        Err(ExactPropertyError::MalformedEntitySlots {
            expected: 7,
            actual: 8,
            ..
        })
    ));
}

#[test]
fn a_malformed_table_value_is_refused() {
    let table = |defining: &str, defined: &str, interpolation: &str| {
        format!("IFCPROPERTYTABLEVALUE('Curve',$,{defining},{defined},$,$,$,{interpolation})")
    };
    let ifc2x3 = |defining: &str, defined: &str| {
        format!("IFCPROPERTYTABLEVALUE('Curve',$,{defining},{defined},$,$,$)")
    };
    let (one_row, two_rows) = ("(IFCREAL(0.))", "(IFCREAL(0.),IFCREAL(1.))");
    assert_eq!(
        get(&one("IFC4", &table(one_row, two_rows, "$"), &[]), "Curve"),
        inconsistent(10, "WR21")
    );
    assert_eq!(
        get(&one("IFC2X3", &ifc2x3(one_row, two_rows), &[]), "Curve"),
        inconsistent(10, "WR1")
    );
    // Only one column stated breaks IFC4 WR21 too.
    assert_eq!(
        get(&one("IFC4", &table(one_row, "$", "$"), &[]), "Curve"),
        inconsistent(10, "WR21")
    );
    let both_absent =
        get(&one("IFC4", &table("$", "$", "$"), &[]), "Curve").expect("legal in IFC4");
    assert!(matches!(both_absent.value, ExactValue::Table(t) if t.rows.is_empty()));
    assert_eq!(
        get(&one("IFC2X3", &ifc2x3("$", "$"), &[]), "Curve"),
        Err(ExactPropertyError::MalformedAggregate {
            entity: EntityId(10),
            attribute: "DefiningValues",
        })
    );
    let mixed = "(IFCREAL(0.),IFCINTEGER(1))";
    assert_eq!(
        get(&one("IFC4", &table(mixed, two_rows, "$"), &[]), "Curve"),
        inconsistent(10, "WR22")
    );
    assert_eq!(
        get(&one("IFC4", &table(two_rows, mixed, "$"), &[]), "Curve"),
        inconsistent(10, "WR23")
    );
    assert_eq!(
        get(&one("IFC2X3", &ifc2x3(mixed, two_rows), &[]), "Curve"),
        inconsistent(10, "WR2")
    );
    assert_eq!(
        get(&one("IFC2X3", &ifc2x3(two_rows, mixed), &[]), "Curve"),
        inconsistent(10, "WR3")
    );
    let repeated = "(IFCREAL(0.),IFCREAL(0.))";
    assert_eq!(
        get(&one("IFC4", &table(repeated, two_rows, "$"), &[]), "Curve"),
        inconsistent(10, "DefiningValues UNIQUE")
    );
    // Only `IfcCurveInterpolationEnum` constants of the release.
    assert_eq!(
        get(
            &one("IFC4", &table(one_row, one_row, ".CUBIC."), &[]),
            "Curve"
        ),
        Err(ExactPropertyError::UnsupportedValue {
            property: EntityId(10)
        })
    );
}

#[test]
fn a_reference_value_resolves_its_target_on_occurrence_and_type() {
    let person = "#81=IFCPERSON($,'Doe','Jane',$,$,$,$,$);";
    for schema in ["IFC2X3", "IFC4", "IFC4X3_ADD2"] {
        let m = model(
            schema,
            &[(10, "IFCPROPERTYREFERENCEVALUE('Owner',$,'Responsible',#81)")],
            &[(41, "IFCPROPERTYREFERENCEVALUE('TypeOwner',$,$,#81)")],
            &[person],
        );
        let owner = get(&m, "Owner").expect(schema);
        let ExactValue::Reference(reference) = &owner.value else {
            panic!("{schema}: {:?}", owner.value)
        };
        assert_eq!(reference.usage_name.as_deref(), Some("Responsible"));
        let target = ExactEntityRef {
            id: EntityId(81),
            type_name: "IFCPERSON".into(),
        };
        assert_eq!(reference.target, Some(target));
        let type_owner = get(&m, "TypeOwner").expect(schema);
        assert_eq!(type_owner.source, ExactSource::Type(WALL_TYPE));
    }
    // `PropertyReference` is optional from IFC4 on, required in IFC2X3.
    let record = "IFCPROPERTYREFERENCEVALUE('Owner',$,$,$)";
    let absent = get(&one("IFC4", record, &[]), "Owner").expect("legal in IFC4");
    assert!(matches!(absent.value, ExactValue::Reference(r) if r.target.is_none()));
    assert_eq!(
        get(&one("IFC2X3", record, &[]), "Owner"),
        Err(ExactPropertyError::MissingValueSlot {
            property: EntityId(10)
        })
    );
}

#[test]
fn a_reference_target_is_checked_against_the_release_select() {
    // `IfcTable` joined `IfcObjectReferenceSelect` in IFC4.
    let record = "IFCPROPERTYREFERENCEVALUE('Doc',$,$,#82)";
    assert!(get(&one("IFC4", record, &["#82=IFCTABLE('T',$,$);"]), "Doc").is_ok());
    assert_eq!(
        get(&one("IFC2X3", record, &["#82=IFCTABLE('T',$);"]), "Doc"),
        Err(ExactPropertyError::UnsupportedValue {
            property: EntityId(10)
        })
    );
    // A wall is no reference target anywhere; a dangling or mis-sized one
    // is refused as such.
    let wall = "IFCPROPERTYREFERENCEVALUE('Doc',$,$,#1)";
    assert_eq!(
        get(&one("IFC4", wall, &[]), "Doc"),
        Err(ExactPropertyError::UnsupportedValue {
            property: EntityId(10)
        })
    );
    let dangling = "IFCPROPERTYREFERENCEVALUE('Doc',$,$,#99)";
    assert_eq!(
        get(&one("IFC4", dangling, &[]), "Doc"),
        Err(ExactPropertyError::MissingReference {
            from: EntityId(10),
            to: EntityId(99),
        })
    );
    assert!(matches!(
        get(
            &one(
                "IFC4",
                "IFCPROPERTYREFERENCEVALUE('Doc',$,$,#82)",
                &["#82=IFCTABLE('T',$);"]
            ),
            "Doc"
        ),
        Err(ExactPropertyError::MalformedEntitySlots {
            entity: EntityId(82),
            ..
        })
    ));
}

#[test]
fn a_value_type_foreign_to_the_release_is_refused() {
    // `IfcPositiveInteger` is an IFC4 type; IFC2X3 does not declare it.
    assert!(ifc_schema::ifc2x3()
        .type_def("IFCPOSITIVEINTEGER")
        .is_none());
    let record = "IFCPROPERTYLISTVALUE('Counts',$,(IFCPOSITIVEINTEGER(3)),$)";
    assert!(get(&one("IFC4", record, &[]), "Counts").is_ok());
    assert!(matches!(
        get(&one("IFC2X3", record, &[]), "Counts"),
        Err(ExactPropertyError::NotInSchema {
            entity: EntityId(10),
            ..
        })
    ));
}

#[test]
fn enumeration_lists_every_composite_kind() {
    let m = model(
        "IFC4",
        &[
            (
                10,
                "IFCPROPERTYENUMERATEDVALUE('Status',$,(IFCLABEL('NEW')),#80)",
            ),
            (
                11,
                "IFCPROPERTYLISTVALUE('Widths',$,(IFCLENGTHMEASURE(1.)),$)",
            ),
            (
                12,
                "IFCPROPERTYBOUNDEDVALUE('Range',$,IFCREAL(2.),IFCREAL(1.),$,$)",
            ),
            (
                13,
                "IFCPROPERTYTABLEVALUE('Curve',$,(IFCREAL(0.)),(IFCREAL(1.)),$,$,$,$)",
            ),
            (14, "IFCPROPERTYREFERENCEVALUE('Link',$,$,$)"),
        ],
        &[(41, "IFCPROPERTYLISTVALUE('Tags',$,(IFCLABEL('a')),$)")],
        &[ENUMERATION],
    );
    let entries = exact_properties(&m, WALL).expect("every kind resolves");
    let names: Vec<_> = entries.iter().map(|entry| entry.name.as_ref()).collect();
    assert_eq!(
        names,
        ["Status", "Widths", "Range", "Curve", "Link", "Tags"]
    );
    for entry in &entries {
        assert_eq!(
            Ok(ExactResolution::Present(entry.property.clone())),
            exact_property(&m, WALL, Some(&entry.property.property_set), &entry.name)
        );
    }
}