github.com/vugu/vugu@v0.3.5/vugufmt/formatter_test.go (about) 1 package vugufmt 2 3 import ( 4 "bytes" 5 "io/ioutil" 6 "os" 7 "path/filepath" 8 "strings" 9 "testing" 10 11 "github.com/stretchr/testify/assert" 12 ) 13 14 func TestOptsCustom(t *testing.T) { 15 jsFormat := func(f *Formatter) { 16 f.ScriptFormatters["js"] = func(input []byte) ([]byte, *FmtError) { 17 return nil, nil 18 } 19 } 20 formatter := NewFormatter(jsFormat) 21 assert.NotNil(t, formatter.ScriptFormatters["js"]) 22 } 23 24 func TestOptsGoFmt(t *testing.T) { 25 gofmt := UseGoFmt(false) 26 formatter := NewFormatter(gofmt) 27 assert.NotNil(t, formatter.ScriptFormatters["application/x-go"]) 28 } 29 30 func TestOptsGoFmtSimple(t *testing.T) { 31 gofmt := UseGoFmt(true) 32 formatter := NewFormatter(gofmt) 33 assert.NotNil(t, formatter.ScriptFormatters["application/x-go"]) 34 } 35 36 func TestOptsGoImports(t *testing.T) { 37 goimports := UseGoImports 38 formatter := NewFormatter(goimports) 39 assert.NotNil(t, goimports, formatter.ScriptFormatters["application/x-go"]) 40 } 41 42 func TestVuguFmtNoError(t *testing.T) { 43 formatter := NewFormatter(UseGoFmt(false)) 44 fmtr := func(f string) { 45 // Need to un-relativize the paths 46 absPath, err := filepath.Abs(f) 47 48 if filepath.Ext(absPath) != ".vugu" { 49 return 50 } 51 52 assert.NoError(t, err, f) 53 // get a handle on the file 54 testFile, err := ioutil.ReadFile(absPath) 55 testFileString := string(testFile) 56 assert.NoError(t, err, f) 57 // run gofmt on it 58 var buf bytes.Buffer 59 err = formatter.FormatHTML(absPath, strings.NewReader(testFileString), &buf) 60 assert.Nil(t, err, f) 61 prettyVersion := buf.String() 62 63 // make sure nothing changed! 64 assert.NotNil(t, buf.String(), f) 65 assert.Equal(t, testFileString, prettyVersion, f) 66 67 //ioutil.WriteFile(absPath+".html", []byte(prettyVersion), 0644) 68 } 69 70 err := filepath.Walk("./testdata/ok/", func(path string, info os.FileInfo, err error) error { 71 fmtr(path) 72 return nil 73 }) 74 75 assert.NoError(t, err) 76 } 77 78 func TestUncompilableGo(t *testing.T) { 79 formatter := NewFormatter(UseGoFmt(false)) 80 fmtr := func(f string) { 81 // Need to un-relativize the paths 82 absPath, err := filepath.Abs(f) 83 84 if filepath.Ext(absPath) != ".vugu" { 85 return 86 } 87 88 assert.NoError(t, err, f) 89 // get a handle on the file 90 testFile, err := ioutil.ReadFile(absPath) 91 assert.NoError(t, err, f) 92 testFileString := string(testFile) 93 // run gofmt on it 94 var buf bytes.Buffer 95 err = formatter.FormatHTML("oknow", strings.NewReader(testFileString), &buf) 96 ferr, ok := err.(*FmtError) 97 assert.True(t, ok) 98 assert.NotNil(t, ferr, f) 99 // confirm the offset is correct! 100 assert.Equal(t, 46, ferr.Line, f) 101 assert.Equal(t, 22, ferr.Column, f) 102 } 103 104 fmtr("./testdata/bad/badgo.vugu") 105 106 } 107 108 func TestEscaping(t *testing.T) { 109 // I'd like the > to not be escaped into > 110 testCode := "<div vg-if='len(data.bpi.BPI) > 0'></div>" 111 formatter := NewFormatter(UseGoFmt(false)) 112 // run gofmt on it 113 var buf bytes.Buffer 114 assert.Nil(t, formatter.FormatHTML("", strings.NewReader(testCode), &buf), testCode) 115 prettyVersion := buf.String() 116 assert.Equal(t, testCode, prettyVersion) 117 } 118 119 func TestBadHTML(t *testing.T) { 120 testCode := "<html><head></head><body><oh no></body></html>" 121 formatter := NewFormatter(UseGoFmt(false)) 122 // run gofmt on it 123 var buf bytes.Buffer 124 err := formatter.FormatHTML("", strings.NewReader(testCode), &buf) 125 assert.Error(t, err, testCode) 126 prettyVersion := buf.String() 127 assert.NotEqual(t, testCode, prettyVersion) 128 }