Skip to content

Commit

Permalink
Change template syntax to $(VARIABLE)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanlloyd committed Jan 27, 2018
1 parent 48fcc5c commit d8285b9
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 27 deletions.
32 changes: 20 additions & 12 deletions internal/pkg/templating/templating.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,17 @@ func charState(l *lexer) stateFn {
l.tokens = append(l.tokens, l.input[l.start:l.pos])
return nil
}

char := l.input[l.pos]
switch char {
case '{':
l.tokens = append(l.tokens, l.input[l.start:l.pos])
var prevChar byte
if l.pos != 0 {
prevChar = l.input[l.pos-1]
}

if prevChar == '$' && char == '(' {
l.tokens = append(l.tokens, l.input[l.start:l.pos-1])
l.start, l.pos = l.pos+1, l.pos+1
return varState
case '}':
l.errMsg = fmt.Sprintf("Unexpected '}' at character %d", l.pos+1)
return nil
default:
continue
}
}
}
Expand All @@ -56,12 +56,20 @@ func varState(l *lexer) stateFn {
l.errMsg = fmt.Sprintf("Template string terminates in a variable")
return nil
}

char := l.input[l.pos]
var prevChar byte
if l.pos != 0 {
prevChar = l.input[l.pos-1]
}

switch char {
case '{':
l.errMsg = fmt.Sprintf("Unexpected '{' at character %d", l.pos+1)
return nil
case '}':
case '(':
if prevChar == '$' {
l.errMsg = fmt.Sprintf("Illegal nested variable at character %d", l.pos)
return nil
}
case ')':
l.tokens = append(l.tokens, l.input[l.start:l.pos])
l.start, l.pos = l.pos+1, l.pos+1
return charState
Expand Down
22 changes: 10 additions & 12 deletions internal/pkg/templating/templating_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@
package templating_test

import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/turingincomplete/rif/internal/pkg/templating"
"testing"
)

func TestParse(t *testing.T) {
renderFunc, err := templating.Parse("The {THING}'s name was {NAME}")
renderFunc, err := templating.Parse("The $(THING)'s name was $(NAME)")
assert.Nil(t, err)

output, err := renderFunc(map[string]string{
Expand All @@ -46,12 +47,10 @@ func TestStringWithNoVariablesUnaltered(t *testing.T) {
assert.Equal(t, "No variables here", output)
}

func TestBracesShouldBeClosed(t *testing.T) {
func TestVariablesShouldBeClosed(t *testing.T) {
testCases := []string{
" a} {b}",
"{a {b}",
"{a} b}",
"{a} {b ",
"$(a $(b)",
"$(a) $(b ",
}

for _, testCase := range testCases {
Expand All @@ -60,11 +59,10 @@ func TestBracesShouldBeClosed(t *testing.T) {
}
}

func TestBracesShouldNotBeNested(t *testing.T) {
func TestVariablesShouldNotBeNested(t *testing.T) {
testCases := []string{
"{ { }",
"{ } }",
"{ {} }",
"$($(a))",
"$($(a)",
}

for _, testCase := range testCases {
Expand All @@ -74,7 +72,7 @@ func TestBracesShouldNotBeNested(t *testing.T) {
}

func TestAllTemplateVariablesRequired(t *testing.T) {
renderFunc, err := templating.Parse("The {THING}'s name was {NAME}")
renderFunc, err := templating.Parse("The $(THING)'s name was $(NAME)")
assert.Nil(t, err)

_, err = renderFunc(map[string]string{
Expand Down
2 changes: 1 addition & 1 deletion tests/acceptance-tests/test-data/body-params.rif
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
rif_version: 0
url: http://localhost:8080/body-params
method: POST
body: "Value: {BODY_VALUE}"
body: "Value: $(BODY_VALUE)"
variables:
BODY_VALUE:
type: string
2 changes: 1 addition & 1 deletion tests/acceptance-tests/test-data/header-params.rif
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ rif_version: 0
url: http://localhost:8080/header-params
method: GET
headers:
x-custom-header: "{HEADER_VALUE}"
x-custom-header: "$(HEADER_VALUE)"
variables:
HEADER_VALUE:
type: string
2 changes: 1 addition & 1 deletion tests/acceptance-tests/test-data/url-params.rif
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
rif_version: 0
url: http://localhost:8080/url-params?count={NUM_THINGS}
url: http://localhost:8080/url-params?count=$(NUM_THINGS)
method: GET
variables:
NUM_THINGS:
Expand Down

0 comments on commit d8285b9

Please sign in to comment.