ebiten/internal/shader/shader_test.go

121 lines
2.5 KiB
Go
Raw Normal View History

2020-05-08 17:46:01 +02:00
// 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 (
2020-06-06 17:52:43 +02:00
"go/parser"
"go/token"
2020-06-08 04:18:27 +02:00
"io/ioutil"
"path/filepath"
"runtime"
2020-06-08 04:18:27 +02:00
"strings"
2020-05-08 17:46:01 +02:00
"testing"
. "github.com/hajimehoshi/ebiten/internal/shader"
)
2020-06-08 04:18:27 +02:00
func TestCompile(t *testing.T) {
if runtime.GOOS == "js" {
t.Skip("file open might not be implemented in this environment")
}
2020-06-08 04:18:27 +02:00
files, err := ioutil.ReadDir("testdata")
if err != nil {
t.Fatal(err)
}
2020-05-31 16:57:03 +02:00
2020-06-08 04:18:27 +02:00
type testcase struct {
Name string
Src []byte
VS []byte
FS []byte
2020-05-31 16:57:03 +02:00
}
2020-06-08 04:18:27 +02:00
fnames := map[string]struct{}{}
for _, f := range files {
if f.IsDir() {
continue
2020-05-31 20:24:43 +02:00
}
2020-06-08 04:18:27 +02:00
fnames[f.Name()] = struct{}{}
2020-05-31 16:57:03 +02:00
}
2020-06-08 04:18:27 +02:00
tests := []testcase{}
for n := range fnames {
if !strings.HasSuffix(n, ".go") {
continue
}
2020-06-08 04:18:27 +02:00
src, err := ioutil.ReadFile(filepath.Join("testdata", n))
if err != nil {
t.Fatal(err)
}
2020-06-03 16:56:08 +02:00
2020-06-08 04:18:27 +02:00
name := n[:len(n)-len(".go")]
tc := testcase{
Name: name,
Src: src,
}
2020-06-03 16:56:08 +02:00
2020-06-08 04:18:27 +02:00
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
}
2020-06-03 16:56:08 +02:00
2020-06-08 04:18:27 +02:00
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
}
2020-06-03 16:56:08 +02:00
2020-06-08 04:18:27 +02:00
if tc.VS == nil && tc.FS == nil {
t.Fatalf("no expected file for %s", name)
}
2020-06-03 16:56:08 +02:00
2020-06-08 04:18:27 +02:00
tests = append(tests, tc)
2020-05-08 17:46:01 +02:00
}
2020-06-08 04:18:27 +02:00
2020-05-08 17:46:01 +02:00
for _, tc := range tests {
2020-05-31 19:23:27 +02:00
t.Run(tc.Name, func(t *testing.T) {
2020-06-06 17:52:43 +02:00
fset := token.NewFileSet()
2020-06-08 04:18:27 +02:00
f, err := parser.ParseFile(fset, "", tc.Src, parser.AllErrors)
2020-06-06 17:52:43 +02:00
if err != nil {
t.Fatal(err)
}
s, err := Compile(fset, f, "Vertex", "Fragment")
2020-05-31 19:23:27 +02:00
if err != nil {
t.Error(err)
return
2020-05-31 16:57:03 +02:00
}
2020-05-31 19:23:27 +02:00
vs, fs := s.Glsl()
2020-06-08 04:18:27 +02:00
if got, want := vs, string(tc.VS); got != want {
2020-05-31 19:23:27 +02:00
t.Errorf("got: %v, want: %v", got, want)
}
2020-06-08 04:18:27 +02:00
if tc.FS != nil {
if got, want := fs, string(tc.FS); got != want {
2020-05-31 19:23:27 +02:00
t.Errorf("got: %v, want: %v", got, want)
}
}
})
2020-05-08 17:46:01 +02:00
}
}