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