parser_test.go 1.9 KB
Newer Older
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
package parser

import (
	"strings"
	"testing"

	"github.com/stretchr/testify/assert"
)

func Test_Parser(t *testing.T) {

	input := `
FROM model1
ADAPTER adapter1
LICENSE MIT
PARAMETER param1 value1
PARAMETER param2 value2
TEMPLATE template1
`

	reader := strings.NewReader(input)

	commands, err := Parse(reader)
	assert.Nil(t, err)

	expectedCommands := []Command{
		{Name: "model", Args: "model1"},
		{Name: "adapter", Args: "adapter1"},
		{Name: "license", Args: "MIT"},
		{Name: "param1", Args: "value1"},
		{Name: "param2", Args: "value2"},
		{Name: "template", Args: "template1"},
	}

	assert.Equal(t, expectedCommands, commands)
}

func Test_Parser_NoFromLine(t *testing.T) {

	input := `
PARAMETER param1 value1
PARAMETER param2 value2
`

	reader := strings.NewReader(input)

	_, err := Parse(reader)
	assert.ErrorContains(t, err, "no FROM line")
}

func Test_Parser_MissingValue(t *testing.T) {

	input := `
FROM foo
PARAMETER param1
`

	reader := strings.NewReader(input)

	_, err := Parse(reader)
	assert.ErrorContains(t, err, "missing value for [param1]")

}
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

func Test_Parser_Messages(t *testing.T) {

	input := `
FROM foo
MESSAGE system You are a Parser. Always Parse things.
MESSAGE user Hey there!
MESSAGE assistant Hello, I want to parse all the things!
`

	reader := strings.NewReader(input)
	commands, err := Parse(reader)
	assert.Nil(t, err)

	expectedCommands := []Command{
		{Name: "model", Args: "foo"},
		{Name: "message", Args: "system: You are a Parser. Always Parse things."},
		{Name: "message", Args: "user: Hey there!"},
		{Name: "message", Args: "assistant: Hello, I want to parse all the things!"},
	}

	assert.Equal(t, expectedCommands, commands)
}

func Test_Parser_Messages_BadRole(t *testing.T) {

	input := `
FROM foo
MESSAGE badguy I'm a bad guy!
`

	reader := strings.NewReader(input)
	_, err := Parse(reader)
	assert.ErrorContains(t, err, "role must be one of \"system\", \"user\", or \"assistant\"")
}