Commit 2b2fbc82 authored by mohamad.alturky's avatar mohamad.alturky

adding tests

parent 3ff6fd3a
package tokenization_test
import (
// Settings "donut/settings"
Tokenizer "donut/tokenization"
"testing"
)
func TestConvertNestingToConcatenation_ShouldReturnTheExpectedResult_WhenTheTemplateContainsNewLinesBetweenDirectives(t *testing.T) {
template :=
`
@template {{ some code.... @exec {{ nothing }} }}
@exec {{ some go code.... }}
@imports {{ "tesing_package" }}
@template {{ some code.... }}
@exec {{ some go code.... }}
`
expectedTemplate :=
`
@template {{ some code.... }}@exec {{ nothing }}@template{{ }}
@exec {{ some go code.... }}
@imports {{ "tesing_package" }}
@template {{ some code.... }}
@exec {{ some go code.... }}
`
new := ""
startingIdx := 0
for {
if startingIdx == len(template) {
break
}
res := Tokenizer.ConvertNestingToConcatenation(startingIdx, template, "exec", "template")
startingIdx = res.TerminatedAt
new = new + res.Template
}
// t.Log(new)
if new != expectedTemplate {
t.Error("error in getting template")
}
}
func TestConvertNestingToConcatenation_ShouldReturnTheExpectedResult_WhenTheTemplateStacksDirectivesWithNoSpaces(t *testing.T) {
template :=
`
@template {{ some code.... @exec{{nothing}}555@exec{{nothing}} }}@exec {{ some go code.... }}
@template {{ "tesing_package"@exec{{nothing}} }}
@template {{ some code.... }}
@exec {{ some go code.... }}
`
expectedTemplate :=
`
@template {{ some code.... }}@exec{{nothing}}@template{{555}}@exec{{nothing}}@template{{ }}@exec {{ some go code.... }}
@template {{ "tesing_package"}}@exec{{nothing}}@template{{ }}
@template {{ some code.... }}
@exec {{ some go code.... }}
`
new := ""
startingIdx := 0
for {
if startingIdx == len(template) {
break
}
res := Tokenizer.ConvertNestingToConcatenation(startingIdx, template, "exec", "template")
startingIdx = res.TerminatedAt
new = new + res.Template
}
t.Log(new)
if new != expectedTemplate {
t.Error("error in getting template")
}
}
package tokenization_test
import (
Settings "donut/settings"
Tokenizer "donut/tokenization"
"testing"
)
func TestExtractDirectiveContent_ShouldReturnTheExpectedResult_WhenTheTemplateContainsNewLinesBetweenDirectives(t *testing.T) {
template :=
`
@template {{ some code.... }}
@exec {{ some go code.... }}
@imports {{ "tesing_package" }}
@template {{ some code.... }}
@exec {{ some go code.... }}
`
expectedImports := ` "tesing_package" `
expectedTemplate :=
`
@template {{ some code.... }}
@exec {{ some go code.... }}
@template {{ some code.... }}
@exec {{ some go code.... }}
`
result := Tokenizer.ExtractDirectiveContent(template, Settings.IMPORT_KEY_WORD)
if result[0] != expectedImports {
t.Error("error in getting imports")
}
if result[1] != expectedTemplate {
t.Error("error in getting template")
}
}
func TestExtractDirectiveContent_ShouldReturnTheExpectedResult_WhenTheTemplateContainsSpacesBetweenDirectives(t *testing.T) {
template :=
`
@template {{ some code.... }} @exec {{ some go code.... }} @imports {{"tesing_package" }} @template {{ some code.... }} @exec {{ some go code.... }}
`
expectedImports := `"tesing_package" `
expectedTemplate :=
`
@template {{ some code.... }} @exec {{ some go code.... }} @template {{ some code.... }} @exec {{ some go code.... }}
`
result := Tokenizer.ExtractDirectiveContent(template, Settings.IMPORT_KEY_WORD)
if result[0] != expectedImports {
t.Error("error in getting imports")
}
if result[1] != expectedTemplate {
t.Error("error in getting template")
}
}
func TestExtractDirectiveContent_ShouldReturnTheExpectedResult_WhenTheTemplateStacksDirectivesWithNoSpaces(t *testing.T) {
template :=
`
@template {{ some code.... }}@exec {{ some go code.... }}54646@imports{{ 8 "tesing_package"}}@template{{ some code.... }}@exec{{ some go code.... }}
`
expectedImports := ` 8 "tesing_package"`
expectedTemplate :=
`
@template {{ some code.... }}@exec {{ some go code.... }}54646@template{{ some code.... }}@exec{{ some go code.... }}
`
result := Tokenizer.ExtractDirectiveContent(template, Settings.IMPORT_KEY_WORD)
if result[0] != expectedImports {
t.Error("error in getting imports")
}
if result[1] != expectedTemplate {
t.Error("error in getting template")
t.Log(result[1])
}
}
package tokenization_test
import (
Tokenizer "donut/tokenization"
"testing"
)
func TestReplaceDirectiveContentWithTemplateWithNewLinesBetweenDirectives(t *testing.T) {
template :=
`----
@template {{ some code.... }}
@exec {{
for i := 0; i < 7; i++ {
@inject {{ string(i) }}
}
}}
@template {{ some code.... }}
@exec {{ some go code.... }}
------
`
expectedTemplate :=
`----
@template {{ some code.... }}
@exec {{
for i := 0; i < 7; i++ {
code = code + string(i)
}
}}
@template {{ some code.... }}
@exec {{ some go code.... }}
------
`
result := Tokenizer.ReplaceDirectiveContent(template, "@inject", Identity)
t.Log(result)
if result != expectedTemplate {
t.Error("error in getting template")
}
}
func TestReplaceDirectiveContentWithTemplateWithSpacesBetweenDirectives(t *testing.T) {
template :=
`----
@template {{ some code.... }}
@exec {{
for i := 0; i < 7; i++ { @inject {{ string(i) }} }
}}
@template {{ some code.... }}
@exec {{ some go code.... }}
------
`
expectedTemplate :=
`----
@template {{ some code.... }}
@exec {{
for i := 0; i < 7; i++ { code = code + string(i) }
}}
@template {{ some code.... }}
@exec {{ some go code.... }}
------
`
result := Tokenizer.ReplaceDirectiveContent(template, "@inject", Identity)
t.Log(result)
if result != expectedTemplate {
t.Error("error in getting template")
}
}
func Identity(text string) string {
return "code = " + " code + " + text
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment