ebiten/internal/shader/shader_test.go
2020-06-08 11:18:27 +09:00

116 lines
2.4 KiB
Go

// Copyright 2020 The Ebiten Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package shader_test
import (
"go/parser"
"go/token"
"io/ioutil"
"path/filepath"
"strings"
"testing"
. "github.com/hajimehoshi/ebiten/internal/shader"
)
func TestCompile(t *testing.T) {
files, err := ioutil.ReadDir("testdata")
if err != nil {
t.Fatal(err)
}
type testcase struct {
Name string
Src []byte
VS []byte
FS []byte
}
fnames := map[string]struct{}{}
for _, f := range files {
if f.IsDir() {
continue
}
fnames[f.Name()] = struct{}{}
}
tests := []testcase{}
for n := range fnames {
if !strings.HasSuffix(n, ".go") {
continue
}
src, err := ioutil.ReadFile(filepath.Join("testdata", n))
if err != nil {
t.Fatal(err)
}
name := n[:len(n)-len(".go")]
tc := testcase{
Name: name,
Src: src,
}
vsn := name + ".expected.vs"
if _, ok := fnames[vsn]; ok {
vs, err := ioutil.ReadFile(filepath.Join("testdata", vsn))
if err != nil {
t.Fatal(err)
}
tc.VS = vs
}
fsn := name + ".expected.fs"
if _, ok := fnames[fsn]; ok {
fs, err := ioutil.ReadFile(filepath.Join("testdata", fsn))
if err != nil {
t.Fatal(err)
}
tc.FS = fs
}
if tc.VS == nil && tc.FS == nil {
t.Fatalf("no expected file for %s", name)
}
tests = append(tests, tc)
}
for _, tc := range tests {
t.Run(tc.Name, func(t *testing.T) {
fset := token.NewFileSet()
f, err := parser.ParseFile(fset, "", tc.Src, parser.AllErrors)
if err != nil {
t.Fatal(err)
}
s, err := Compile(fset, f, "Vertex", "Fragment")
if err != nil {
t.Error(err)
return
}
vs, fs := s.Glsl()
if got, want := vs, string(tc.VS); got != want {
t.Errorf("got: %v, want: %v", got, want)
}
if tc.FS != nil {
if got, want := fs, string(tc.FS); got != want {
t.Errorf("got: %v, want: %v", got, want)
}
}
})
}
}