-
Notifications
You must be signed in to change notification settings - Fork 0
/
official_test.go
162 lines (135 loc) · 3.85 KB
/
official_test.go
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
package fhirpath
import (
_ "embed"
"encoding/xml"
"fmt"
"github.com/halprin/fhirpath/context"
"os"
"reflect"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
//go:embed official_tests/r4/tests-fhir-r4.xml
var officialTestXmlSpec []byte
type OfficialTests struct {
XMLName xml.Name `xml:"tests"`
Groups []OfficialTestGroup `xml:"group"`
}
type OfficialTestGroup struct {
Name string `xml:"name,attr"`
Tests []OfficialTest `xml:"test"`
}
type OfficialTest struct {
Name string `xml:"name,attr"`
// Expression string `xml:"expression"`
Expression OfficialExpression `xml:"expression"`
InputFile string `xml:"inputfile,attr"`
Outputs []string `xml:"output"`
}
type OfficialExpression struct {
Expression string `xml:",chardata"`
Invalid string `xml:"invalid,attr"`
}
func TestOfficial(t *testing.T) {
//parse official_tests/r4/test-fhir-r4.xml
var officialTests OfficialTests
err := xml.Unmarshal(officialTestXmlSpec, &officialTests)
assert.NoError(t, err)
failTests := false
totalTests := 0
passedTests := 0
for _, group := range officialTests.Groups {
for _, test := range group.Tests {
testName := fmt.Sprintf("%s/%s/%s/%s", group.Name, test.Name, test.InputFile, test.Expression.Expression)
totalTests++
fhir, err := readFhirTestFile(convertXmlFileNameToJsonFileName(test.InputFile))
assert.NoError(t, err)
passed := t.Run(testName, officialTestTemplate(test.Expression, fhir, test.Outputs, failTests))
if passed {
passedTests++
}
}
}
t.Logf("%d/%d tests pass", passedTests, totalTests)
}
func convertXmlFileNameToJsonFileName(fileName string) string {
return strings.Replace(fileName, ".xml", ".json", 1)
}
func readFhirTestFile(fileName string) (string, error) {
content, err := os.ReadFile(fmt.Sprintf("official_tests/r4/input/%s", fileName))
if err != nil {
return "", err
}
return string(content), nil
}
func officialTestTemplate(expression OfficialExpression, fhir string, expectedResult []string, fail bool) func(*testing.T) {
return func(t *testing.T) {
//report on any possible panics
defer func() {
r := recover()
if r != nil {
t.Logf("Evaluate failed with a panic: %v", r)
if fail {
t.Fail()
}
}
}()
results, err := EvaluateWithContext[any](fhir, expression.Expression, context.R4())
if err != nil {
if expression.Invalid != "" {
//this was an expected error
t.Log("Successfully failed")
return
}
t.Logf("Evaluate failed with an error: %s", err.Error())
if fail {
t.Fail()
}
return
}
stringifiedResults := stringifySlice(results)
if len(expectedResult) != len(stringifiedResults) {
t.Log("Expected results are not equal to actual results")
t.Logf("Expected=%v", expectedResult)
t.Logf("Actual=%v", stringifiedResults)
if fail {
t.Fail()
}
return
}
expectedCount := make(map[string]int)
for _, currentExpectedResult := range expectedResult {
count, ok := expectedCount[currentExpectedResult]
if !ok {
expectedCount[currentExpectedResult] = 1
continue
}
expectedCount[currentExpectedResult] = count + 1
}
actualCount := make(map[string]int)
for _, currentStringifiedResults := range stringifiedResults {
count, ok := actualCount[currentStringifiedResults]
if !ok {
actualCount[currentStringifiedResults] = 1
continue
}
actualCount[currentStringifiedResults] = count + 1
}
if !reflect.DeepEqual(expectedCount, actualCount) {
t.Log("Expected results are not equal to actual results")
t.Logf("Expected=%v", expectedResult)
t.Logf("Actual=%v", stringifiedResults)
if fail {
t.Fail()
}
}
}
}
func stringifySlice[T any](results []T) []string {
stringValues := make([]string, len(results))
for index, result := range results {
stringValues[index] = fmt.Sprintf("%v", result)
}
return stringValues
}