-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathtesting.go
79 lines (60 loc) · 1.25 KB
/
testing.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
package internal
import (
"bytes"
"encoding/json"
"io"
"log"
"net/http"
"reflect"
)
type (
Test struct {
Rule io.Reader
Data io.Reader
Expected io.Reader
}
Tests []Test
)
func convertInterfaceToReader(i any) io.Reader {
var result bytes.Buffer
encoder := json.NewEncoder(&result)
err := encoder.Encode(i)
if err != nil {
panic(err)
}
return &result
}
func GetScenariosFromOfficialTestSuite() Tests {
var tests Tests
response, err := http.Get("http://jsonlogic.com/tests.json")
if err != nil {
log.Fatal(err)
return tests
}
buffer, _ := io.ReadAll(response.Body)
response.Body.Close()
var scenarios []any
err = json.Unmarshal(buffer, &scenarios)
if err != nil {
log.Fatal(err)
return tests
}
// add missing but relevant scenarios
var rule []any
scenarios = append(scenarios,
append(rule,
make(map[string]any),
make(map[string]any),
make(map[string]any)))
for _, scenario := range scenarios {
if reflect.ValueOf(scenario).Kind() == reflect.String {
continue
}
tests = append(tests, Test{
Rule: convertInterfaceToReader(scenario.([]any)[0]),
Data: convertInterfaceToReader(scenario.([]any)[1]),
Expected: convertInterfaceToReader(scenario.([]any)[2]),
})
}
return tests
}