-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathconformance.go
More file actions
133 lines (123 loc) · 3.98 KB
/
Copy pathconformance.go
File metadata and controls
133 lines (123 loc) · 3.98 KB
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
package testplan
import (
"fmt"
"strings"
"github.com/project-chip/alchemy/matter"
"github.com/project-chip/alchemy/matter/conformance"
"github.com/project-chip/alchemy/matter/spec"
"github.com/project-chip/alchemy/matter/types"
)
type conformanceEntityFormatter func(entity types.Entity) string
func renderPicsConformance(b *strings.Builder, doc *spec.Doc, cluster *matter.Cluster, cs conformance.Set) {
if len(cs) == 0 {
return
}
renderConformance(cs, b, doc, cluster, entityPICSConformance)
}
func renderFeatureConformance(b *strings.Builder, doc *spec.Doc, cluster *matter.Cluster, cs conformance.Set) {
if len(cs) == 0 {
return
}
renderConformance(cs, b, doc, cluster, entityVariable)
}
func renderChoice(c *conformance.Optional, b *strings.Builder) {
// PICS tool does not support + style conformances, so unless this is a "pick one" choice,
//render as fully optional, we'll check the choice conformance properly in the tests.
o := conformance.ChoiceExactLimit{Limit: 1}
if c.Choice != nil && o.Equal(c.Choice.Limit) {
b.WriteRune('.')
b.WriteString(c.Choice.ASCIIDocString())
}
}
func renderConformance(cs conformance.Set, b *strings.Builder, doc *spec.Doc, cluster *matter.Cluster, formatter conformanceEntityFormatter) {
// PICS tool can't handle otherwise conformances, so render anything with an otherwise conformance as optional for the purposes of the
// test plan PICS. This can be fully evaluated in the tests.
// The only exception is if it is provisional, which should be rendered as X.
if len(cs) != 1 {
switch cs[0].(type) {
case *conformance.Provisional:
b.WriteRune('X')
default:
b.WriteString("{PICS_S}: O")
}
return
}
switch c := cs[0].(type) {
case *conformance.Mandatory:
if c.Expression == nil {
b.WriteString("{PICS_S}: M")
return
}
renderExpression(b, doc, cluster, c.Expression, formatter)
case *conformance.Optional:
if c.Expression == nil {
b.WriteString("{PICS_S}: O")
renderChoice(c, b)
return
}
renderExpression(b, doc, cluster, c.Expression, formatter)
b.WriteString(": O")
renderChoice(c, b)
case *conformance.Provisional:
b.WriteRune('X')
case *conformance.Disallowed:
b.WriteRune('X')
case *conformance.Deprecated:
b.WriteRune('X')
case *conformance.Described:
b.WriteString("{PICS_S}: O")
default:
b.WriteString(fmt.Sprintf("unknown conformance: %T", c))
}
}
func renderExpression(b *strings.Builder, doc *spec.Doc, cluster *matter.Cluster, exp conformance.Expression, formatter conformanceEntityFormatter) {
switch exp := exp.(type) {
case *conformance.EqualityExpression:
b.WriteRune('(')
renderExpression(b, doc, cluster, exp.Left, formatter)
b.WriteString(" == ")
renderExpression(b, doc, cluster, exp.Right, formatter)
b.WriteRune(')')
case *conformance.FeatureExpression:
b.WriteString(renderIdentifier(cluster.Features, exp.Feature, formatter))
case *conformance.IdentifierExpression:
b.WriteString(renderIdentifier(cluster, exp.ID, formatter))
case *conformance.ReferenceExpression:
b.WriteString(renderReference(doc, exp.Reference, formatter))
case *conformance.LogicalExpression:
if exp.Not {
b.WriteString("NOT")
}
b.WriteRune('(')
renderExpression(b, doc, cluster, exp.Left, formatter)
for _, e := range exp.Right {
b.WriteString(" ")
b.WriteString(exp.Operand)
b.WriteString(" ")
renderExpression(b, doc, cluster, e, formatter)
}
b.WriteRune(')')
default:
b.WriteString(fmt.Sprintf("ERROR: unknown expression type: %T", exp))
}
}
func renderIdentifier(store conformance.IdentifierStore, id string, formatter conformanceEntityFormatter) string {
if store == nil {
return ""
}
entity, ok := store.Identifier(id)
if !ok {
return fmt.Sprintf("UNKNOWN_ID_%s", id)
}
return formatter(entity)
}
func renderReference(store conformance.ReferenceStore, id string, formatter conformanceEntityFormatter) string {
if store == nil {
return ""
}
entity, ok := store.Reference(id)
if !ok {
return fmt.Sprintf("UNKNOWN_ID_%s", id)
}
return formatter(entity)
}